TypeError: expected Tensor as element 0 in argument 0, but got int

ParticularProgramme: <p>I was using <a href="https://pytorch.org/tutorials/intermediate/reinforcement_q_learning.html#training-loop" rel="noopener nofollow ugc">this</a> example code in a deep q network but it keeps throwing an error “TypeError: expected Tensor as element 0 in argument 0, but got int” and the line in question is “action_batch = torch.cat(batch.action)” I have tried to use cpu and cuda but neither work any help would be apreciated.</p>

ptrblck: <p>I guess this error might be raised, if the batch size was set to 1 and <code>batch.action</code> might then be a single scalar value / integer? If so, you could most likely use <code>torch.tensor(batch.action)</code> instead of <code>torch.cat</code>.</p>

banikr: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a> ,<br>
I was using the following code and got the similar error:</p>
<pre><code class="lang-auto">all_loss[phase] = torch.cat((all_loss[phase], loss.detach().view(1, -1)))
</code></pre>
<pre><code class="lang-auto">all_loss[phase] = torch.cat((all_loss[phase], loss.detach().view(1, -1)))
TypeError: expected Tensor as element 0 in argument 0, but got numpy.float32
</code></pre>
<p>I tried to print the output:</p>
<pre><code class="lang-auto">print("&gt;&gt;&gt;&gt;&gt;", loss.detach().view(1, -1), type(loss.detach().view(1, -1)), loss.detach().view(1, -1).dtype)
</code></pre>
<p>and got:</p>
<pre><code class="lang-auto">&gt;&gt;&gt;&gt;&gt; tensor([[0.4254]], device='cuda:0') &lt;class 'torch.Tensor'&gt; torch.float32
&gt;&gt;&gt;&gt;&gt; tensor([[0.5419]], device='cuda:0') &lt;class 'torch.Tensor'&gt; torch.float32
Traceback (most recent call last):
  File "/home/banikr2/PycharmProjects/Blockface/Codes/Main.py", line 131, in &lt;module&gt;
    all_loss[phase] = torch.cat((all_loss[phase], loss.detach().view(1, -1)))
TypeError: expected Tensor as element 0 in argument 0, but got numpy.float32
</code></pre>
<p>Could you help to debug it?<br>
Btw <code>phase</code> is just either <code>train</code> or <code>val</code>.</p>

ptrblck: <p>Could you check the <code>type</code> of <code>all_loss[phase]</code> and in case it’s containing more than one object, the <code>type</code> of the first object?<br>
Based on the error message it seems that <code>all_loss[phase]</code> is already containing a numpy array, which will raise this error.</p>

banikr: <p>I am actually trying to implement the codes <a href="https://github.com/choosehappy/PytorchDigitalPathology/blob/master/segmentation_epistroma_unet/train_unet.ipynb" rel="noopener nofollow ugc">here</a>:</p>
<pre><code class="lang-auto">print("&gt;", all_loss)
&gt; {'train': tensor([0.], device='cuda:0'), 'valid': tensor([0.], device='cuda:0')}
</code></pre>
<pre><code class="lang-auto">print("&gt;&gt;", all_loss[phase], type(all_loss[phase]))
&gt;&gt; tensor([0.], device='cuda:0') &lt;class 'torch.Tensor'&gt;
</code></pre>
<pre><code class="lang-auto">print("&gt;&gt;&gt;", loss.detach().view(1, -1), type(loss.detach().view(1, -1)), loss.detach().view(1, -1).dtype)
&gt;&gt;&gt; tensor([[0.5484]], device='cuda:0') &lt;class 'torch.Tensor'&gt; torch.float32
</code></pre>

ptrblck: <p>These objects seem to work after fixing the shape mismatch:</p>
<pre><code class="lang-python">all_loss = {'train': torch.tensor([0.], device='cuda:0'),
            'valid': torch.tensor([0.], device='cuda:0')}
print(all_loss)
&gt; {'train': tensor([0.], device='cuda:0'), 'valid': tensor([0.], device='cuda:0')}

loss = torch.tensor([[0.5484]], device='cuda:0')
print(loss)
&gt; tensor([[0.5484]], device='cuda:0')


phase = 'train'
all_loss[phase] = torch.cat((all_loss[phase], loss.detach().view(1, -1)))
&gt; RuntimeError: Tensors must have same number of dimensions: got 2 and 1

all_loss[phase] = torch.cat((all_loss[phase], loss.detach().view(-1)))
print(all_loss)
&gt; {'train': tensor([0.0000, 0.5484], device='cuda:0'), 'valid': tensor([0.], device='cuda:0')}
</code></pre>

banikr: <p>Made the two changes you’ve mentioned but getting the same error:</p>
<pre><code class="lang-auto">    all_loss[phase] = torch.cat((all_loss[phase], loss.detach().view(-1)))
TypeError: expected Tensor as element 0 in argument 0, but got numpy.float32
</code></pre>
<p>Change/diff:</p>
<pre><code class="lang-auto"># all_loss = {key: torch.zeros(1).to(device) for key in phases}
    all_loss = {'train': torch.tensor([0.], device='cuda:0'),
                'valid': torch.tensor([0.], device='cuda:0')}
</code></pre>

ptrblck: <p>Are you getting this error also using my code snippet or only yours?<br>
In the latter case, it seems you are still adding a numpy array to <code>all_loss</code>, so would still have to narrow down the offending line of code.</p>

Maryam_Osama: <p>I tried to implement 3 Bayesian NN simultaneously each outputs a tuple, however when I combined their results using torch cat I got the same error: ValueError: only one element tensors can be converted to Python scalars.<br>
my code is as following:</p>
<blockquote>
<p>self.bcnn1=nn.Sequential(bcnn1d(in_channels=self.in_channels, out_channels=8,<br>
kernel_size=5, padding=2, dilation=1),<br>
BN(num_features=8))<br>
self.bcnn2=nn.Sequential(bcnn1d(in_channels=self.in_channels, out_channels=8,<br>
kernel_size=5, padding=6, dilation=3),<br>
BN(num_features=8))<br>
self.bcnn3=nn.Sequential(bcnn1d(in_channels=self.in_channels, out_channels=8,<br>
kernel_size=5,padding=12, dilation=6),<br>
BN(num_features=8))<br>
bcnn_out = self.bcnn(torch.cat(bcnn1, bcnn2, bcnn3),dim=1)</p>
</blockquote>
<p>however, I tried co convert each one onf the bcnn to a tensor but got <code>ValueError: only one element tensors can be converted to Python scalars</code>:</p>
<blockquote>
<p>bcnn_out = self.bcnn(torch.cat(torch.Tensor(bcnn_out1), torch.Tensor(bcnn_out2), torch.Tensor(bcnn_out3))</p>
</blockquote>

ptrblck: <p>Could you post the entire stacktrace and a minimal, executable code snippet to reproduce the issue, please?</p>

Maryam_Osama: <p>OK<br>
I tried to implement 3 Bayesian NN simultaneously each outputs a tuple, however when I combined their results using <code>torch.cat</code> I got the same error: ValueError: only one element tensors can be converted to Python scalars.<br>
my code is as following:<br>
however, I tried co convert each one one of the bcnn to a tensor but got <code>ValueError: only one element tensors can be converted to Python scalars</code>:</p>
<blockquote>
<p>bcnn_out = self.bcnn(torch.cat(torch.Tensor(bcnn_out1), torch.Tensor(bcnn_out2), torch.Tensor(bcnn_out3))</p>
</blockquote>

areeb_adnan: <p>Hello there I am facing the similar error like the one mentioned above. I am getting<br>
error like “expected Tensor as element 4 in argument 0, but got int”. Here element 4 keeps changing like element 5, 6 or 9. I don’t know why is this happening but I do know that problem arises when I am concatenating two different datasets on from the fall_detection dataset and other from the UP_FALL_dataset using torch.utils.data.ConcatDataset<br>
I am sharing my colab link here: <a href="https://colab.research.google.com/drive/12vCXui3Ulkk9olACYZiixwZfIW2UGWBl?usp=sharing" class="inline-onebox" rel="noopener nofollow ugc">Google Colab</a></p>
<p>can anyone please suggest why is this problem occuring</p>

Tushara_Reddy: <p>TypeError: only integer tensors of a single element can be converted to an index</p>
<h1>
<a name="start-training-1" class="anchor" href="#start-training-1"></a>start training</h1>
<p>Trainer(model=model, args=training_args, train_dataset=train_dataset, eval_dataset=test_dataset,<br>
data_collator=lambda data: {‘input_ids’: torch.tensor([f[0] for f in data]),<br>
‘attention_mask’: torch.tensor([f[1] for f in data]),<br>
‘sentiment’: torch.tensor([f[0] for f in data])}).train()</p>
<p>Hi Im trying to run this code to implement GPT for NLP data, can you please help me resolve it?</p>

Waleed_Eid: <p>You can replace the torch.cat or torch.stack with this problem " TypeError: expected Tensor as element 0 in argument 0, but got float " or "expected Tensor as element 0 in argument 0, but got numpy.float32 "  or " TyprError: expected tensor as element 0 in argument 0 , but got int " to torch.tensor  and it will work .<br>
worked for me .</p>

Vishnu_G: <p>Hey, I am facing the same issue as you do. Can i know how did u overcome the error?</p>

dipakmeher: <p>Hi,<br>
I am getting the similar kind of error as given below. Any help would be appreciated. Thanks.</p>
<p>31 Dec 10:09    INFO  epoch 0 training [time: 39.53s, train loss: 119.6776]<br>
^MEvaluate   :   0%|                                                            | 0/2 [00:00&lt;?, ?it/s]^MEvaluate   :   0%|                                                            | 0/2 [00:06&lt;?, ?it/s]<br>
Traceback (most recent call last):<br>
File “/home/dmeher/Recbole-CDR-LLMasRec/run_recbole_cdr.py”, line 18, in <br>
run_recbole_cdr(model=args.model, config_file_list=config_file_list)<br>
File “/home/dmeher/Recbole-CDR-LLMasRec/recbole_cdr/quick_start/quick_start.py”, line 53, in run_recbole_cdr<br>
best_valid_score, best_valid_result = trainer.fit(<br>
File “/home/dmeher/Recbole-CDR-LLMasRec/recbole_cdr/trainer/trainer.py”, line 71, in fit<br>
super().fit(train_data, target_valid_data, verbose, saved, show_progress, callback_fn)<br>
File “/home/dmeher/llm_env/lib/python3.9/site-packages/recbole/trainer/trainer.py”, line 352, in fit<br>
valid_score, valid_result = self._valid_epoch(valid_data, show_progress=show_progress)<br>
File “/home/dmeher/llm_env/lib/python3.9/site-packages/recbole/trainer/trainer.py”, line 209, in _valid_epoch<br>
valid_result = self.evaluate(valid_data, load_best_model=False, show_progress=show_progress)<br>
File “/home/dmeher/llm_env/lib/python3.9/site-packages/torch/utils/_contextlib.py”, line 115, in decorate_context<br>
return func(*args, **kwargs)<br>
File “/home/dmeher/llm_env/lib/python3.9/site-packages/recbole/trainer/trainer.py”, line 474, in evaluate<br>
for batch_idx, batched_data in enumerate(iter_data):<br>
File “/home/dmeher/llm_env/lib/python3.9/site-packages/tqdm/std.py”, line 1182, in <strong>iter</strong><br>
for obj in iterable:<br>
File “/home/dmeher/llm_env/lib/python3.9/site-packages/recbole/data/dataloader/abstract_dataloader.py”, line 79, in <strong>next</strong><br>
return self._next_batch_data()<br>
File “/home/dmeher/llm_env/lib/python3.9/site-packages/recbole/data/dataloader/general_dataloader.py”, line 148, in _next_batch_data<br>
cur_data = cat_interactions(data_list)<br>
File “/home/dmeher/llm_env/lib/python3.9/site-packages/recbole/data/interaction.py”, line 367, in cat_interactions<br>
new_inter = {col: torch.cat([inter[col] for inter in interactions]) for col in columns_set}<br>
File “/home/dmeher/llm_env/lib/python3.9/site-packages/recbole/data/interaction.py”, line 367, in <br>
new_inter = {col: torch.cat([inter[col] for inter in interactions]) for col in columns_set}<br>
TypeError: expected Tensor as element 0 in argument 0, but got Interaction</p>

ptrblck: <p>I’m not familiar with your use case and which libraries you are using but based on the error message you are trying to concatenate objects of the <code>Interaction</code> class (I don’t know where it comes from) while tensors are expected.</p>

dipakmeher: <p>The error is due to the below function which is a part of Recbole github repository. (<a href="https://github.com/RUCAIBox/RecBole/blob/master/recbole/data/interaction.py" rel="noopener nofollow ugc">https://github.com/RUCAIBox/RecBole/blob/master/recbole/data/interaction.py</a>):</p>
<p>def cat_interactions(interactions):<br>
“”"Concatenate list of interactions to single interaction.</p>
<pre><code>Args:
    interactions (list of :class:`Interaction`): List of interactions to be concatenated.

Returns:
    :class:`Interaction`: Concatenated interaction.
"""
if not isinstance(interactions, (list, tuple)):
    raise TypeError(f"Interactions [{interactions}] should be list or tuple.")
if len(interactions) == 0:
    raise ValueError(
        f"Interactions [{interactions}] should have some interactions."
    )

columns_set = set(interactions[0].columns)
for inter in interactions:
    if columns_set != set(inter.columns):
        raise ValueError(
            f"Interactions [{interactions}] should have some interactions."
        )

new_inter = {
    col: torch.cat([inter[col] for inter in interactions]) for col in columns_set
}
return Interaction(new_inter)
</code></pre>
<p>I am not able to resolve this issue. Any suggestions you have?</p>