Home > Enterprise >  ValueError: Can not squeeze dim[1], expected a dimension of 1, got 60 for '{{node Squeeze}} = S
ValueError: Can not squeeze dim[1], expected a dimension of 1, got 60 for '{{node Squeeze}} = S

Time:02-17

I am currently building a LSTM model to predict the close price of next 60 days. and I am getting the following error when I try to fit the model : ValueError: Can not squeeze dim[1], expected a dimension of 1, got 60 for '{{node Squeeze}} = SqueezeT=DT_FLOAT, squeeze_dims=[-1]' with input shapes: [?,60].

below is my code

model = Sequential()
model.add(LSTM(64, activation='tanh', recurrent_activation='sigmoid', input_shape=(x_train.shape[1], x_train.shape[2]),
               return_sequences=True))
model.add(LSTM(256, activation='tanh', recurrent_activation='sigmoid', return_sequences=False))
model.add(Dense(128))
model.add(Dropout(0.2))
model.add(Dense(y_train.shape[1]))
model.compile(optimizer=Adam(learning_rate=0.0001), loss='mse', metrics=['accuracy'])
model.summary()

history = model.fit(x_train, y_train, epochs=20, batch_size=16, validation_split=0.2, verbose=1)

x_train shape = (2066,300,2) y_train shape = (2066, 60, 1) So I use 300 days of data (2 features) to predict the next 60 days closing price. I am unsure why I am getting this error and want to seek help.

CodePudding user response:

You are missing the last dimension of y_train. Just reshape your output to match the dimensions of y_train:

import tensorflow as tf

x_train = tf.random.normal((2066, 300, 2))
y_train = tf.random.normal((2066, 60, 1))
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(64, activation='tanh', recurrent_activation='sigmoid', input_shape=(x_train.shape[1], x_train.shape[2]),
               return_sequences=True))
model.add(tf.keras.layers.LSTM(256, activation='tanh', recurrent_activation='sigmoid', return_sequences=False))
model.add(tf.keras.layers.Dense(128))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(y_train.shape[1]))
model.add(tf.keras.layers.Reshape((y_train.shape[1], y_train.shape[2])))
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001), loss='mse', metrics=['accuracy'])
model.summary()

history = model.fit(x_train, y_train, epochs=1, batch_size=16, validation_split=0.2, verbose=1)
  • Related