0

I am working by first time with LSTMs in Keras and Tensorflow in Python, and I want to create a neural network with some layers and which gives 10 output values. I generated multiple layers in a neural network, and I created an output DenseLayer of 10 elements. I have the next code:

from pandas import DataFrame
from pandas import Series
from pandas import concat
from pandas import read_csv
from pandas import datetime
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from math import sqrt
from matplotlib import pyplot
import numpy
from numpy import array
import math


# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back):
    dataX, dataY = [], []
    for i in range(len(dataset)-look_back-1):
        a = dataset[i:(i+look_back), 0]
        dataX.append(a)
        dataY.append(dataset[i + look_back, 0])
    return numpy.array(dataX), numpy.array(dataY)

look_back = 10
epochs = 1000
batch_size = 50

data = data.astype('float32')

scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(data)


# 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),:]

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(100, activation = 'tanh', inner_activation = 'hard_sigmoid', return_sequences=True))#, input_shape=(1, look_back)))
model.add(LSTM(50, activation = 'tanh', inner_activation = 'hard_sigmoid', return_sequences=True))
model.add(LSTM(25, activation = 'tanh', inner_activation = 'hard_sigmoid'))
# I want 10 outputs    
model.add(Dense(10))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=epochs, batch_size=batch_size, verbose=2)

But when I execute the code I get the next error message:

ValueError: Error when checking target: expected dense_1 to have shape (10,) but got array with shape (1,)

What can I do to solve the problem? I want to give me predictions for the next 10 elements, that is the reason why I put a final layer of 10 elements.

jartymcfly
  • 1,945
  • 9
  • 30
  • 51

1 Answers1

1

From what you have said above the error ValueError: Error when checking target: expected dense_1 to have shape (10,) but got array with shape (1,) is due to a problem in your target:

  • you have a list of values that are the targets.
  • you try to predict ten values while having only one to compare to.

you need to rework the trainY matrx to include every value you wish to predict. for example if you wish to predict the 5 values in the closest futur, you'll need a target line (ie each element) of size 5 including all values.

as such you'll train the network to predict the 5 futur values. i'll try to get you the code howerver it's just a reshaping with a roll to get futur values.

to be more precise, for 1 X (one input) you'll need a y=[v1,v2,v3,v4,v5] so if you have train = [X1,X2,..] then Y = [[v1,v2,v3,v4,v5],[v2,v3,v4,v5,v6]

Frayal
  • 2,117
  • 11
  • 17