I am trying to build a univariate encoder-decoder LSTM model. I got this error again and again:
ValueError: Input contains NaN, infinity or a value too large for dtype('float32').
I have already searched and read the other posts who asked about the same error, however, I am sure the data hasn't any nan values.
The nan value resulted because of the LSTM hidden computations What's make me sure about this is that:
I did a loop for each epoch to call model.fit
and print the history.
for j in range(numEpoch):
history = model.fit(trX, targetTRAIN, epochs=1, batch_size=batchSize, verbose=0, shuffle=False, validation_split=valdSplit)
print(history.history)
It works well till about numEpoch
=610 (sorry I forgot the exact number) then it started showing nan
as validation loss.
Here is my model definition:
numEpoch = 2000
batchSize = 1
actFunc = 'relu'
valdSplit=0.1
dropOutRate=0.2
optimizer = SGD(lr=0.01, momentum=0.9)
model = Sequential()
randSeed = randSeed + 1
kernelInitializer = RandomNormal(seed=randSeed)
model.add(LSTM(30, batch_input_shape=(batchSize, timeStep, numFeat), activation=actFunc, kernel_initializer=kernelInitializer, dropout=dropOutRate,stateful=True , return_sequences=True))
model.add(Dropout(dropOutRate))
model.add(LSTM(20, kernel_initializer=kernelInitializer, stateful=False ,activation=actFunc, return_sequences=False))
model.add(Dropout(dropOutRate))
randSeed = randSeed + 1
kernelInitializer = RandomNormal(seed=randSeed)
model.add(Dense(numFeat, kernel_initializer=kernelInitializer, activation='linear'))
model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['accuracy'])
Train_X
shape is (362, 3, 27)
I am happy to give more details if needed.