ValueError: only one element tensors can be converted to Python scalars

Hey, guys! I’m using Google Colab and I’m facing this error and don’t know how to fix it. Can you help to solve this error: “ValueError: only one element tensors can be converted to Python scalars”

   6         model.hidden = (torch.zeros(1, 1, model.hidden_layer_size),
    7                         torch.zeros(1, 1, model.hidden_layer_size))

----> 8 test_inputs.append(model(seq).item())

ValueError: only one element tensors can be converted to Python scalars

You try and convert the output of your model to a python scalar with .item() but it complains that it has more than one element, so it cannot do that.
What is the size of your model’s output? What do you try to achieve with this .item() call?

3 Likes

I want to predict a product’s price throughout 24 hours during one day, my dataset has 1488 records, I’m using 1000 values to training and 488 to test my model.
Initially the test_inputs item will contain 24 items. Inside a for loop these 24 items will be used to make predictions about the first item from the test set, for instance, the item number 1001. The predict value will then be appended to the test_inputs list. During the second iteration, again the last 24 items will be used as input and a new prediction will be made which will then be appended to the test_inputs list again.

model.eval()

for i in range(fut_pred):
seq = torch.FloatTensor(test_inputs[-train_window:])
with torch.no_grad():
model.hidden = (torch.zeros(1, 1, model.hidden_layer_size),
torch.zeros(1, 1, model.hidden_layer_size))
test_inputs.append(model(seq).item())

You might want to add a print statement to check the object your model(seq) returns and make it contains what you expect.

2 Likes

Now I got it! I did what you said, (seq) was returning 25 values. I changed it and now the error has gone!
Thanks for being so supportive!

3 Likes

Hi,
i have the same error , but i found that the error will be generated when it reaches the last epoch of training,
this is the line when the error is generated :
“”" dae.fit(trloader, valoader, lr=lr, batch_size=batch_size, num_epochs=num_epochs, corrupt=corrupt, loss_type=“mse”) “”"
I need some help please,

This just calls the fit function, this is not very informative to discern the error, can you show the whole error and possibly code ?

“”" the full error “”""
stackedDAE.py", line 89, in pretrain
dae.fit(trloader, valoader, lr=lr, batch_size=batch_size, num_epochs=num_epochs, corrupt=corrupt, loss_type=“mse”)denoisingAutoencoder.py", line 95, in fit
inputs = np.reshape(inputs, [-1, 512])
File “<array_function internals>”, line 6, in reshape
PycharmProjects/test/venv/lib/python3.6/site-packages/numpy/core/_asarray.py", line 85, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: only one element tensors can be converted to Python scalars

“”" the code when the error is generated :
def fit(self, trainloader, validloader, lr=0.001, batch_size=128, num_epochs=10, corrupt=0.3,
loss_type=“mse”):
“”"
data_x: FloatTensor
valid_x: FloatTensor
“”"
use_cuda = torch.cuda.is_available()
if use_cuda:
self.cuda()
print("=====Denoising Autoencoding layer=======")
optimizer = optim.Adam(filter(lambda p: p.requires_grad, self.parameters()), lr=lr)
if loss_type==“mse”:
criterion = MSELoss()
elif loss_type==“cross-entropy”:
criterion = BCELoss()

    # validate
    total_loss = 0.0
    total_num = 0
    for batch_idx, inputs in enumerate(validloader):
        inputs = np.reshape(inputs, [-1, 512])
        #inputs = inputs.view(inputs.size(0), -1).float()
        inputs= torch.Tensor(inputs)
        if use_cuda:
            inputs = inputs.cuda()

        inputs = Variable(inputs)
        hidden = self.encode(inputs)
        if loss_type=="cross-entropy":
            outputs = self.decode(hidden, binary=True)
        else:
            outputs = self.decode(hidden)

thanks a lot if you can help me

Can you print out the ‘inputs’ before it is reshaped ? I feel it is not a tenosr (or ndarray).

this is the type of inputs also values:
<class ‘torch.Tensor’>
tensor([[ 5.4912, 15.0690],
[ 16.0690, 16.9260],
[ 20.8600, 7.9206],
[ 14.1370, -2.4545],
[ 4.6316, 0.1090],
[ 2.5703, 6.2384],
[ 5.4974, -4.6409],
[ 5.9872, -16.3710],
[ 7.2405, 1.5709],
[ 11.6820, 23.7860],
[ 17.0000, 18.5840],
[ 15.9120, 5.3584],
[ 14.4450, 13.5100],
[ 18.0920, 29.0380],
[ 14.0630, 14.6210],
[ 3.5360, -15.3180],
[ 9.0134, -8.3628],
[ 20.2160, 22.8300],
[ 12.6020, 25.1670],
[ 5.1467, 7.7010],
[ 17.3860, 9.5863],
[ 24.0870, 21.2980],
[ 14.7980, 17.0010],
[ 10.2700, -0.5617],
[ 18.4030, 2.8889],
[ 25.7380, 23.3100],
[ 26.1480, 23.8580],
[ 25.7910, 16.7410],
[ 18.9220, 11.6950],
[ 8.1207, -1.2489],
[ 14.0910, 6.2904],
[ 27.4450, 31.9340],
[ 25.0390, 26.9570],
[ 18.9390, 10.1220],
[ 24.2290, 24.1300],
[ 25.2840, 31.5700],
[ 21.3540, 13.3130],
[ 23.9520, 0.1076],
[ 24.1140, -2.5747],
[ 19.9330, 6.1196],
[ 19.3440, 19.7600],
[ 18.1190, 17.2560],
[ 16.6440, 12.2080],
[ 15.6850, 13.0550],
[ 12.4310, -1.5546],
[ 12.7660, -9.0830],
[ 14.6310, 2.9567],
[ 13.4250, 3.2005],
[ 16.3100, 2.2940],
[ 21.7370, 21.1850],
[ 21.5250, 27.4870],
[ 25.1440, 18.0580],
[ 33.7300, 24.3720],
[ 21.7210, 16.1040],
[ 5.6736, -7.0732],
[ 15.7400, 1.5107],
[ 22.0630, 13.2540],
[ 12.0160, 3.6242],
[ 16.5670, 2.8435],
[ 23.7810, 6.0249],
[ 15.8980, 3.3200],
[ 14.6870, 15.4390],
[ 19.0990, 23.5510],
[ 20.3020, 15.5420],
[ 26.3520, 21.4830],
[ 27.3990, 28.1680],
[ 16.8200, 10.5620],
[ 11.0070, -7.3579],
[ 12.4970, 1.2271],
[ 8.8723, 10.9280],
[ 9.2209, 5.7773],
[ 21.5720, 11.5010],
[ 26.4690, 23.5880],
[ 22.4060, 27.2000],
[ 18.6150, 19.6860],
[ 11.7740, 6.0727],
[ 8.4078, -1.9962],
[ 13.9940, 0.9488],
[ 23.6450, 17.5470],
[ 17.9330, 20.4150],
[ 2.2834, 1.0522],
[ 2.5298, -2.5277],
[ 11.5870, 11.9130],
[ 6.1784, 5.3533],
[ -2.0187, -14.0790],
[ 10.3760, -1.8917],
[ 22.8510, 25.8270],
[ 18.0370, 28.1530],
[ 13.8800, 17.3160],
[ 13.0590, 10.7350],
[ 4.3241, -5.9080],
[ 1.3700, -5.3742],
[ 10.5600, 21.8480],
[ 4.0053, 16.0160],
[-15.6770, -17.5050],
[-17.5310, -18.7140],
[ -8.0127, -5.3597],
[ -8.7475, -8.6592],
[-14.4600, -6.1989],
[-10.3170, -3.7481],
[ 0.0672, -9.7358],
[ 5.0474, -2.5462],
[ 2.3458, 2.0610],
[ -0.9833, -3.4382],
[ -0.5967, 0.5338],
[ 3.2368, 13.8130],
[ 3.2241, 11.7160],
[ -3.6843, -2.8880],
[ -5.5857, -1.0893],
[ 3.7589, 13.4740],
[ 5.4042, 10.6110],
[ -2.5345, -0.1572],
[ -2.3967, 4.9518],
[ 8.5950, 13.8710],
[ 13.4140, 12.5400],
[ 1.4853, 3.4241],
[ -3.8843, 1.7507],
[ 4.5598, 13.7480],
[ 7.3512, 19.1860],
[ 12.6860, 20.1320],
[ 18.7660, 18.3930],
[ 5.6671, 1.2094],
[ -0.4848, -6.3297],
[ 13.4370, 7.6145],
[ 9.5535, 9.6281],
[ -5.2978, 5.5039],
[ -5.2771, 7.6536],
[ -3.4686, -1.3158],
[ -3.4551, -5.3972],
[ 5.2426, 11.6090],
[ 11.2240, 16.1600],
[ 3.2625, -1.7814],
[ -4.5400, -7.1376],
[ -1.7130, 1.9572],
[ 0.4649, -0.4769],
[ -2.6021, -7.8821],
[ 0.9839, -4.4029],
[ 7.8759, 4.9366],
[ 5.4191, 8.1099],

I am not sure what your dataset returns, but usually it returns a tuple of tensors (x and y). Can you replace

for batch_idx, inputs in enumerate(validloader):

with

for batch_idx, (inputs,outputs) in enumerate(validloader):

the dataset is a np array with shape (5850,256,2)
if i applie for batch_idx, (inputs,outputs) in enumerate(validloader): an other error will generated, i tested this

Hmmm, it should be able to reshape that numpy array to (-1, 512). Weird. Can you please post the entire code if possible ?

this is the entire code for denoising Autoencoder:
import torch
import torch.nn as nn
from torch.nn import Parameter
import torch.nn.functional as F
import torch.optim as optim
import torchvision
from torchvision import datasets, transforms
from torch.autograd import Variable
import tensorflow as tf
import numpy as np
import math
from udlp.utils import Dataset, masking_noise
from udlp.ops import MSELoss, BCELoss

class DenoisingAutoencoder(nn.Module):
def init(self, in_features, out_features, activation=“relu”,
dropout=0.2, tied=False):
super(self.class, self).init()
self.weight = Parameter(torch.Tensor(out_features, in_features))
if tied:
self.deweight = self.weight.t()
else:
self.deweight = Parameter(torch.Tensor(in_features, out_features))
self.bias = Parameter(torch.Tensor(out_features))
self.vbias = Parameter(torch.Tensor(in_features))

    if activation=="relu":
        self.enc_act_func = nn.ReLU()
    elif activation=="sigmoid":
        self.enc_act_func = nn.Sigmoid()
    self.dropout = nn.Dropout(p=dropout)

    self.reset_parameters()

def reset_parameters(self):
    stdv = 1. / math.sqrt(self.weight.size(1))
    self.weight.data.uniform_(-stdv, stdv)
    self.bias.data.uniform_(-stdv, stdv)
    stdv = 1. / math.sqrt(self.deweight.size(1))
    self.deweight.data.uniform_(-stdv, stdv)
    self.vbias.data.uniform_(-stdv, stdv)

def forward(self, x):
    return self.dropout(self.enc_act_func(F.linear(x, self.weight, self.bias)))

def encode(self, x, train=True):
    if train:
        self.dropout.train()
    else:
        self.dropout.eval()
    return self.dropout(self.enc_act_func(F.linear(x, self.weight, self.bias)))

def encodeBatch(self, dataloader):
    use_cuda = torch.cuda.is_available()
    encoded = []
    for batch_idx, inputs in enumerate(dataloader):
        inputs = np.reshape(inputs, [-1, 512])
        inputs = torch.Tensor(inputs)
        #inputs = inputs.view(inputs.size(0), -1).float()
        if use_cuda:
            inputs = inputs.cuda()
        inputs = Variable(inputs)
        hidden = self.encode(inputs, train=False)
        encoded.append(hidden.data.cpu())

    encoded = torch.cat(encoded, dim=0)
    return encoded

def decode(self, x, binary=False):
    if not binary:
        return F.linear(x, self.deweight, self.vbias)
    else:
        return F.sigmoid(F.linear(x, self.deweight, self.vbias))

def fit(self, trainloader, validloader, lr=0.001, batch_size=128, num_epochs=10, corrupt=0.3,
    loss_type="mse"):
    """
    data_x: FloatTensor
    valid_x: FloatTensor
    """
    use_cuda = torch.cuda.is_available()
    if use_cuda:
        self.cuda()
    print("=====Denoising Autoencoding layer=======")
    optimizer = optim.Adam(filter(lambda p: p.requires_grad, self.parameters()), lr=lr)
    if loss_type=="mse":
        criterion = MSELoss()
    elif loss_type=="cross-entropy":
        criterion = BCELoss()

    # validate
    total_loss = 0.0
    total_num = 0
    for batch_idx, inputs in enumerate(validloader):
        print(type(inputs))
        print(inputs)
        inputs = np.reshape(inputs, [-1, 512])
        #inputs = inputs.view(inputs.size(0), -1).float()
        #inputs= torch.Tensor(inputs)
        if use_cuda:
            inputs = inputs.cuda()

        inputs = Variable(inputs)
        hidden = self.encode(inputs)
        if loss_type=="cross-entropy":
            outputs = self.decode(hidden, binary=True)
        else:
            outputs = self.decode(hidden)

        valid_recon_loss = criterion(outputs, inputs)
        total_loss += valid_recon_loss.data*len(inputs)
        total_num += inputs.size()[0]
        print("finish_batch",batch_idx)
    valid_loss = total_loss / total_num
    print("#Epoch 0: Valid Reconstruct Loss: %.3f" % (valid_recon_loss))

    for epoch in range(num_epochs):
        # train 1 epoch
        train_loss = 0.0
        for batch_idx, inputs in enumerate(trainloader):
            #inputs = inputs.view(inputs.size(0), -1).float()
            inputs= np.reshape(inputs,[-1,512])
            inputs=torch.Tensor(inputs)
            inputs_corr = masking_noise(inputs, corrupt)
            if use_cuda:
                inputs = inputs.cuda()
                inputs_corr = inputs_corr.cuda()
            optimizer.zero_grad()
            inputs = Variable(inputs)
            inputs_corr = Variable(inputs_corr)

            hidden = self.encode(inputs_corr)
            if loss_type=="cross-entropy":
                outputs = self.decode(hidden, binary=True)
            else:
                outputs = self.decode(hidden)
            recon_loss = criterion(outputs, inputs)
            train_loss += recon_loss.data*len(inputs)
            recon_loss.backward()
            optimizer.step()

        # validate
        valid_loss = 0.0
        for batch_idx,inputs in enumerate(validloader):
            inputs = np.reshape(inputs, [-1, 512])
            inputs = torch.Tensor(inputs)
            #inputs = inputs.view(inputs.size(0), -1).float()
            if use_cuda:
                inputs = inputs.cuda()
            inputs = Variable(inputs)
            hidden = self.encode(inputs, train=False)
            if loss_type=="cross-entropy":
                outputs = self.decode(hidden, binary=True)
            else:
                outputs = self.decode(hidden)

            valid_recon_loss = criterion(outputs, inputs)
            valid_loss += valid_recon_loss.data * len(inputs)

        print("#Epoch %3d: Reconstruct Loss: %.3f, Valid Reconstruct Loss: %.3f" % (
            epoch+1, recon_loss , valid_recon_loss))

I am able to run the code (without that error) using a dummy dataset

class Dataset(torch.utils.data.Dataset):
    def __init__(self):
        super(Dataset, self).__init__()

    def __getitem__(self, i):
        return np.random.randn(5850, 256, 2).astype('float32')

    def __len__(self):
        return 100


trainloader = torch.utils.data.DataLoader(Dataset(), batch_size=8)
validloader = torch.utils.data.DataLoader(Dataset(), batch_size=8)

Can you show the dataset class as well as dataloader ?

this is my datasets class:
“”“”“”"
def load_data(root_folder):
# These are delcarations of variables that have been used inside the for loop
final_list = list()
labels = list()
# It is iterating through both the categories Fatigue and Non Fatigue
k=0
for cat in category:
# It is taking and processing each file in the folder
for filename in os.listdir(root_folder + cat):

        # Read each file for each category and drop the unnecessary columns
        path = root_folder + cat + '/' + filename
        df = pd.read_csv(path)  # Read the CSV using inbuilt Pandas Function
        # df.drop(index=0, axis=0, inplace=True) # Drop the first row, which contains
        # the units of measurement
        df.columns = ["time", "EEGO1", "EEGO2"]  # Rename the columns for convinience and easy access of
        # the columns
        df.drop(['time'], axis=1, inplace=True)  # Drop the time column, as we are not using it as a
        # time series.
        df.EEGO1 = pd.to_numeric(
            df.EEGO1)  # The data by default is in the form of an object, Convert each row into
        # numeric or floating point
        df.EEGO2 = pd.to_numeric(df.EEGO2)

        print(filename, len(df))

        # Split each file into 6 parts and then make each of them a new row by transposing
        df_split = np.array_split(df, 450)  # Split the dataset into 450 (115200/256=450) different sets.
        for splitted_array in df_split:
            final_list.append(np.array(
                splitted_array))  # After splitting, we are appending all the splitted arrays into 1 single
            # large array of 3 dimentions
            # The following if-else block is used to create labels. We have taken '1' for Fatigue EEG and '0'
            # for Non Fatigue EEG
            # This is not the ideal way to create labels, but this is the most simplest way for this situation
            if cat == 'Fatigue':
                labels.append(0)
            if cat == 'Non Fatigue':
                labels.append(1)
#print (tf.concat(labels),[0,2995200])
#print(type(labels))
# Before returning, convert the lists to arrays and increase the dimentions for being able to feed into the
# Neural Network

return np.array(final_list), np.expand_dims(np.array(labels), axis=1)

if name == “main”:
parser = argparse.ArgumentParser(description=‘VAE MNIST Example’)
parser.add_argument(‘–lr’, type=float, default=0.002, metavar=‘N’,
help=‘learning rate for training (default: 0.001)’)
parser.add_argument(‘–batch-size’, type=int, default=128, metavar=‘N’,
help=‘input batch size for training (default: 128)’)
parser.add_argument(‘–pretrainepochs’, type=int, default=10, metavar=‘N’,
help=‘number of epochs to train (default: 10)’)
parser.add_argument(‘–epochs’, type=int, default=10, metavar=‘N’,
help=‘number of epochs to train (default: 10)’)
args = parser.parse_args()

X_train, y_train = load_data(root_folder_train)
X_test, y_test = load_data(root_folder_test)
in_features = 512
out_features = 256

X_train= torch.Tensor(X_train)
X_test=torch.Tensor(X_test)
#X_test = np.reshape(X_test, [-1, 512])
sdae = StackedDAE(input_dim=512, z_dim=10, binary=True,
    encodeLayer=[256,100,10], decodeLayer=[10,100,256], activation="relu",
    dropout=0)
sdae.pretrain(X_train,X_test, lr=args.lr, batch_size=args.batch_size,
              num_epochs=args.pretrainepochs, corrupt=0.3, loss_type="cross-entropy")
sdae.save_model("model/sdae.pt")
sdae.fit(X_train,X_test, lr=args.lr, num_epochs=args.epochs, corrupt=0.3, loss_type="cross-entropy")

“”“”“”

As far as I can see, there is something wrong in X_train and X_test. can you print the dimensions of X_train and X_test please. Sorry, it is not as straightforward as I thought.

X_train (5850, 256, 2)
X_test (1800, 256, 2)

Do you get the same error when you try X_train and X_test with

X_train = np.random.randn(5850, 256, 2)
X_test = np.random.randn(1800, 256, 2)

yes i have the same error