I'm a little lost with this code. I'm trying to create a seq2seq model and I think I have things all setup but I can't seem to figure out why I am getting the following error in the train method
ValueError: Input 0 of layer "functional_1" is incompatible with the layer: expected shape=(None, 24, 9), found shape=(None, 9)
My code is the following:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import sys
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Input, LSTM, Dense
from tensorflow.keras.models import Model
import matplotlib.pyplot as plt
class Seq2SeqLSTM:
def __init__(self,Train):
self.TrainClass = Train
def StartModel(self):
XTrain, YTrain, XTest, YTest = self.GetTrainingData()
self.DefineModel(XTrain,YTrain)
self.CompileModel()
# Train the model
self.Train(XTrain, YTrain, YTrain)
Predictions = self.Predict(XTest[1], YTest[0])
def DefineModel(self,XTrain,YTrain):
self.InputSeqLength = len(XTrain)
self.InputDim = len(XTrain[0])
self.HiddenUnits = 64
self.EncoderInputs = Input(shape = (self.InputSeqLength, self.InputDim))
self.DecoderInputs = Input(shape = (self.InputSeqLength, self.InputDim))
self.Model = self.BuildModel()
def BuildModel(self):
EncoderLSTM = LSTM(self.HiddenUnits, return_state = True)
EncoderOutputs, StateH, StateC = EncoderLSTM(self.EncoderInputs)
EncoderStates = [StateH, StateC]
DecoderLSTM = LSTM(self.HiddenUnits, return_sequences = True, return_state = True)
DecoderOutputs, _, _ = DecoderLSTM(self.DecoderInputs, initial_state = EncoderStates)
DecoderDense = Dense(1, activation = 'linear')
DecoderOutputs = DecoderDense(DecoderOutputs)
PModel = Model([self.EncoderInputs, self.DecoderInputs], DecoderOutputs)
return PModel
def CompileModel(self):
self.Model.compile(optimizer = 'adam', loss = 'mse')
def Train(self, XEncoder, XDecoder, Y, BatchSize = 32, Epochs = 10):
self.History = self.Model.fit([XEncoder, XDecoder], Y, batch_size = BatchSize, epochs = Epochs, verbose = 1)
def Predict(self, XEncoder, XDecoder):
return self.Model.predict([XEncoder, XDecoder])
def PlotLoss(self):
plt.plot(self.History.history['loss'])
plt.legend(["This is my legend"], fontsize="x-large")
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.savefig('loss.png')
def SplitTrainData(self,X, Y, TestSize = 0.2):
SplitIndex = int(len(X) * (1 - TestSize))
XTrain, XTest = X[:SplitIndex], X[SplitIndex:]
YTrain, YTest = Y[:SplitIndex], Y[SplitIndex:]
return XTrain, YTrain, XTest, YTest
def GetTrainingData(self):
X = []
Y = []
Data = self.TrainClass.ProcessData()
for I in Data:
X1 = []
Y1 = []
for J in I[0]:
X1.append([J])
for K in I[1]:
Y1.append([K])
X.append(np.array(X1))
Y.append(np.array(Y1))
X = np.array(X)
Y = np.array(Y)
self.X = X
self.Y = Y
return self.SplitTrainData(X,Y)
My training(XTrain, YTrain) data has a shape of (24,9,1). Y has a shape of (24, 12, 1) which is correct. I don't understand why my shape is changing to (None, 9). Do I need to reshape the data or is it some other issue I am not realizing?