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

I was using this 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.

1 Like

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

Hi @ptrblck ,
I was using the following code and got the similar error:

all_loss[phase] = torch.cat((all_loss[phase], loss.detach().view(1, -1)))
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

I tried to print the output:

print(">>>>>", loss.detach().view(1, -1), type(loss.detach().view(1, -1)), loss.detach().view(1, -1).dtype)

and got:

>>>>> tensor([[0.4254]], device='cuda:0') <class 'torch.Tensor'> torch.float32
>>>>> tensor([[0.5419]], device='cuda:0') <class 'torch.Tensor'> torch.float32
Traceback (most recent call last):
  File "/home/banikr2/PycharmProjects/Blockface/Codes/Main.py", line 131, in <module>
    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

Could you help to debug it?
Btw phase is just either train or val.

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

I am actually trying to implement the codes here:

print(">", all_loss)
> {'train': tensor([0.], device='cuda:0'), 'valid': tensor([0.], device='cuda:0')}
print(">>", all_loss[phase], type(all_loss[phase]))
>> tensor([0.], device='cuda:0') <class 'torch.Tensor'>
print(">>>", loss.detach().view(1, -1), type(loss.detach().view(1, -1)), loss.detach().view(1, -1).dtype)
>>> tensor([[0.5484]], device='cuda:0') <class 'torch.Tensor'> torch.float32

These objects seem to work after fixing the shape mismatch:

all_loss = {'train': torch.tensor([0.], device='cuda:0'),
            'valid': torch.tensor([0.], device='cuda:0')}
print(all_loss)
> {'train': tensor([0.], device='cuda:0'), 'valid': tensor([0.], device='cuda:0')}

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


phase = 'train'
all_loss[phase] = torch.cat((all_loss[phase], loss.detach().view(1, -1)))
> 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)
> {'train': tensor([0.0000, 0.5484], device='cuda:0'), 'valid': tensor([0.], device='cuda:0')}

Made the two changes you’ve mentioned but getting the same error:

    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

Change/diff:

# 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')}

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

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.
my code is as following:

self.bcnn1=nn.Sequential(bcnn1d(in_channels=self.in_channels, out_channels=8,
kernel_size=5, padding=2, dilation=1),
BN(num_features=8))
self.bcnn2=nn.Sequential(bcnn1d(in_channels=self.in_channels, out_channels=8,
kernel_size=5, padding=6, dilation=3),
BN(num_features=8))
self.bcnn3=nn.Sequential(bcnn1d(in_channels=self.in_channels, out_channels=8,
kernel_size=5,padding=12, dilation=6),
BN(num_features=8))
bcnn_out = self.bcnn(torch.cat(bcnn1, bcnn2, bcnn3),dim=1)

however, I tried co convert each one onf the bcnn to a tensor but got ValueError: only one element tensors can be converted to Python scalars:

bcnn_out = self.bcnn(torch.cat(torch.Tensor(bcnn_out1), torch.Tensor(bcnn_out2), torch.Tensor(bcnn_out3))

Could you post the entire stacktrace and a minimal, executable code snippet to reproduce the issue, please?

OK
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.
my code is as following:
however, I tried co convert each one one of the bcnn to a tensor but got ValueError: only one element tensors can be converted to Python scalars:

bcnn_out = self.bcnn(torch.cat(torch.Tensor(bcnn_out1), torch.Tensor(bcnn_out2), torch.Tensor(bcnn_out3))

Hello there I am facing the similar error like the one mentioned above. I am getting
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
I am sharing my colab link here: Google Colab

can anyone please suggest why is this problem occuring

TypeError: only integer tensors of a single element can be converted to an index

start training

Trainer(model=model, args=training_args, train_dataset=train_dataset, eval_dataset=test_dataset,
data_collator=lambda data: {‘input_ids’: torch.tensor([f[0] for f in data]),
‘attention_mask’: torch.tensor([f[1] for f in data]),
‘sentiment’: torch.tensor([f[0] for f in data])}).train()

Hi Im trying to run this code to implement GPT for NLP data, can you please help me resolve it?

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 .
worked for me .

Hey, I am facing the same issue as you do. Can i know how did u overcome the error?

Hi,
I am getting the similar kind of error as given below. Any help would be appreciated. Thanks.

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

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 Interaction class (I don’t know where it comes from) while tensors are expected.

The error is due to the below function which is a part of Recbole github repository. (https://github.com/RUCAIBox/RecBole/blob/master/recbole/data/interaction.py):

def cat_interactions(interactions):
“”"Concatenate list of interactions to single interaction.

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)

I am not able to resolve this issue. Any suggestions you have?