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

Daniel_Alves: <p>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”</p>
<pre><code>   6         model.hidden = (torch.zeros(1, 1, model.hidden_layer_size),
    7                         torch.zeros(1, 1, model.hidden_layer_size))
</code></pre>
<p>----&gt; 8         test_inputs.append(model(seq).item())</p>
<p>ValueError: only one element tensors can be converted to Python scalars</p>

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

Daniel_Alves: <p>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.<br>
Initially the  <code>test_inputs</code>  item will contain 24 items. Inside a  <code>for</code>  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  <code>test_inputs</code>  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  <code>test_inputs</code>  list again.</p>
<p>model.eval()</p>
<p>for i in range(fut_pred):<br>
seq = torch.FloatTensor(test_inputs[-train_window:])<br>
with torch.no_grad():<br>
model.hidden = (torch.zeros(1, 1, model.hidden_layer_size),<br>
torch.zeros(1, 1, model.hidden_layer_size))<br>
test_inputs.append(model(seq).item())</p>

albanD: <p>You might want to add a print statement to check the object your <code>model(seq)</code> returns and make it contains what you expect.</p>

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

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

user_123454321: <p>This just calls the fit function, this is not very informative to discern the error, can you show the whole error and possibly code ?</p>

Islem_Maatar: <h2>“”" the full error “”""<br>
stackedDAE.py", line 89, in pretrain<br>
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<br>
inputs = np.reshape(inputs, [-1, 512])<br>
File “&lt;<strong>array_function</strong> internals&gt;”, line 6, in reshape<br>
PycharmProjects/test/venv/lib/python3.6/site-packages/numpy/core/_asarray.py", line 85, in asarray<br>
return array(a, dtype, copy=False, order=order)<br>
ValueError: only one element tensors can be converted to Python scalars</h2>
<p>“”" the code when the error is generated :<br>
def fit(self, trainloader, validloader, lr=0.001, batch_size=128, num_epochs=10, corrupt=0.3,<br>
loss_type=“mse”):<br>
“”"<br>
data_x: FloatTensor<br>
valid_x: FloatTensor<br>
“”"<br>
use_cuda = torch.cuda.is_available()<br>
if use_cuda:<br>
self.cuda()<br>
print("=====Denoising Autoencoding layer=======")<br>
optimizer = optim.Adam(filter(lambda p: p.requires_grad, self.parameters()), lr=lr)<br>
if loss_type==“mse”:<br>
criterion = MSELoss()<br>
elif loss_type==“cross-entropy”:<br>
criterion = BCELoss()</p>
<pre><code>    # 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)
</code></pre>
<p>thanks a lot if you can help me</p>

user_123454321: <p>Can you print out the ‘inputs’ before it is reshaped ? I feel it is not a tenosr (or ndarray).</p>

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

user_123454321: <p>I am not sure what your dataset returns, but usually it returns a tuple of tensors (x and y). Can you replace</p>
<pre><code class="lang-auto">for batch_idx, inputs in enumerate(validloader):
</code></pre>
<p>with</p>
<pre><code class="lang-auto">for batch_idx, (inputs,outputs) in enumerate(validloader):
</code></pre>

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

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

Islem_Maatar: <p>this is the entire code for denoising Autoencoder:<br>
import torch<br>
import torch.nn as nn<br>
from torch.nn import Parameter<br>
import torch.nn.functional as F<br>
import torch.optim as optim<br>
import torchvision<br>
from torchvision import datasets, transforms<br>
from torch.autograd import Variable<br>
import tensorflow as tf<br>
import numpy as np<br>
import math<br>
from udlp.utils import Dataset, masking_noise<br>
from udlp.ops import MSELoss, BCELoss</p>
<p>class DenoisingAutoencoder(nn.Module):<br>
def <strong>init</strong>(self, in_features, out_features, activation=“relu”,<br>
dropout=0.2, tied=False):<br>
super(self.<strong>class</strong>, self).<strong>init</strong>()<br>
self.weight = Parameter(torch.Tensor(out_features, in_features))<br>
if tied:<br>
self.deweight = self.weight.t()<br>
else:<br>
self.deweight = Parameter(torch.Tensor(in_features, out_features))<br>
self.bias = Parameter(torch.Tensor(out_features))<br>
self.vbias = Parameter(torch.Tensor(in_features))</p>
<pre><code>    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))</code></pre>

user_123454321: <p>I am able to run the code (without that error) using a dummy dataset</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>Can you show the dataset class as well as dataloader ?</p>

Islem_Maatar: <aside class="quote no-group" data-username="user_123454321" data-post="15" data-topic="59800">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/user_123454321/48/27631_2.png" class="avatar"> user_123454321:</div>
<blockquote>
<p>Can you show the dataset class as well as dataloader ?</p>
</blockquote>
</aside>
<p>this is my datasets class:<br>
“”“”“”"<br>
def load_data(root_folder):<br>
# These are delcarations of variables that have been used inside the for loop<br>
final_list = list()<br>
labels = list()<br>
# It is iterating through both the categories Fatigue and Non Fatigue<br>
k=0<br>
for cat in category:<br>
# It is taking and processing each file in the folder<br>
for filename in os.listdir(root_folder + cat):</p>
<pre><code>        # 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)
</code></pre>
<p>if <strong>name</strong> == “<strong>main</strong>”:<br>
parser = argparse.ArgumentParser(description=‘VAE MNIST Example’)<br>
parser.add_argument(‘–lr’, type=float, default=0.002, metavar=‘N’,<br>
help=‘learning rate for training (default: 0.001)’)<br>
parser.add_argument(‘–batch-size’, type=int, default=128, metavar=‘N’,<br>
help=‘input batch size for training (default: 128)’)<br>
parser.add_argument(‘–pretrainepochs’, type=int, default=10, metavar=‘N’,<br>
help=‘number of epochs to train (default: 10)’)<br>
parser.add_argument(‘–epochs’, type=int, default=10, metavar=‘N’,<br>
help=‘number of epochs to train (default: 10)’)<br>
args = parser.parse_args()</p>
<pre><code>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")
</code></pre>
<p>“”“”“”</p>

user_123454321: <p>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.</p>

Islem_Maatar: <p>X_train (5850, 256, 2)<br>
X_test (1800, 256, 2)</p>

user_123454321: <p>Do you get the same error when you try X_train and X_test with</p>
<pre><code class="lang-auto">X_train = np.random.randn(5850, 256, 2)
X_test = np.random.randn(1800, 256, 2)
</code></pre>

Islem_Maatar: <aside class="quote no-group" data-username="user_123454321" data-post="19" data-topic="59800">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/user_123454321/48/27631_2.png" class="avatar"> user_123454321:</div>
<blockquote>
<pre><code class="lang-auto">X_train = np.random.randn(5850, 256, 2)
X_test = np.random.randn(1800, 256, 2)
</code></pre>
</blockquote>
</aside>
<p>yes i have the same error</p>