user3218279
user3218279

Reputation: 279

Error in LSTM during testing

My data is of 68871 x 43, where the features are in the column no. 1-42 and label in column no. 43

My keras LSTM code for classification of the data is

import numpy
import matplotlib.pyplot as plt
import pandas
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
    dataX, dataY = [], []
    for i in range(len(dataset)-look_back-1):
        a = dataset[i:(i+look_back), 0]
        #if i==0
        # print len(a)
        dataX.append(a)
        dataY.append(dataset[i + look_back, 43])
    return numpy.array(dataX), numpy.array(dataY)
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset
#dataframe = pandas.read_csv('international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=3)
dataset = numpy.loadtxt("Source.txt", delimiter=" ")
#dataset = dataframe.values
#dataset = dataset.astype('float32')
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
look_back = 1
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(3, input_dim=look_back))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, nb_epoch=1, batch_size=1)
score, acc = model.evaluate(testX, testY)
print('Test score:', score)
print('Test accuracy:', acc)

I am getting this error during testing time enter image description here

Please help resolve this, many thanks in advance

Upvotes: 1

Views: 125

Answers (1)

pltrdy
pltrdy

Reputation: 2109

I think that your problem is that model.evaluate(testX, testY) is only returning one value.

Your error message tells you that numpy.float64 isn't iterable. What it means it that model.evaluate(testX, testY) returns a float64 and thus, you cannot put it return value into two variables score, acc.

It would be like doing:

def single_return():
    return np.float64(10)
a, b = single_return()

(Note that this code will raise the exact same error).

I would then suggest, both to fix it now, but also as a quite good practice for the future to always return into a single variable, then to split. It make error message more clear, as only line having problem will be the affectation, not the evaluation one.

Hope it helps.
pltrdy

Upvotes: 3

Related Questions