My input is a tensor of X_train.shape=(4291, 1, 278, 29, 1). My output is a tensor of Y_train.shape=(4291, 1, 9). When it conducted fit(X_train,Y_train), it showed me an error of
"ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (4291, 1, 9)"
So how do I deal with the shape of output?
model = Sequential()
model.add(ConvLSTM2D(filters=8, kernel_size=5, strides=2,
input_shape=(1, 278, 29, 1),activation='relu',
padding='same',return_sequences=False))
model.add(Flatten())
model.add(Dense(9))
model.compile(loss="mse", optimizer="Adam", metrics=['mse'])
model.fit(X_train, Y_train,batch_size=batch_size, epochs=epochs, verbose=2, shuffle=False)
The output of your model has a shape of (None, 9). Therefore, the target array (i.e. Y_train) must have the same shape, i.e. (num_samples, 9). Try to reshape it:
Y_train = Y_train.reshape(-1, 9) # -1 indicates that the dimension of that axis should be automatically inferred.
Related
I want to apply LSTM.
I have 12 features and 74 rows
my data shape after dropping the targeted variable and reshape it for 3d arrays:(1, 74, 12)
and my targeted shape: (74,)
when I split the data using this code:
x_train, x_test, y_train, y_test = train_test_split(data_1, target, test_size = 0.2,random_state =25)
I got this error:
ValueError: Found input variables with inconsistent numbers of samples: [1, 74]
I defined the model well but when I fit the model also I have another error
defining the model:
model = Sequential()
model.add(LSTM(1, batch_input_shape=(1, 74, 12), return_sequences = True))
model.add(Dense(units = 1, activation = 'sigmoid'))
model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['accurecy'])
model.summary()
fitting the model:
history = model.fit(x_train, y_train, epochs = 100, validation_data= (x_test, y_test))
here I have also this error:
ValueError: Input 0 of layer sequential_14 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 12)
How can I resolve this error?
tf.keras.layers.LSTM expects inputs: A 3D tensor with shape [batch, timesteps, feature].
import tensorflow as tf
inputs = tf.random.normal([32, 10, 8])
lstm = tf.keras.layers.LSTM(4, return_sequences=True, return_state=True)
whole_seq_output, final_memory_state, final_carry_state = lstm(inputs)
print(whole_seq_output.shape)
Output
(1, 74, 4)
If your input shape is of 2D, use tf.expand_dims(input, axis=0) to add extra dimension.
I am trying to test a simple convolutional layer where the input images (with 1 band= grayscale) are numpy arrays stored in a list and targets are stored in a pandas dataframe. The size of input images is 16x16. The output for the model.fit is an error of "Layer sequential expects 1 input(s), but it received 239 input tensors". I also checked this link but still I couldn't find the answer. Can anyone help me to resolve this error?
(trainY, testY, trainX, testX) = train_test_split(df, images, test_size=0.20, random_state=42)
print (np.shape(trainY),np.shape(testY),np.shape(trainX),np.shape(testX))
result: (239, 1) (60, 1) (239, 16, 16) (60, 16, 16)
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(16, 16, 1),name='Layer1'))
model.add(layers.Flatten())
model.add(layers.Dense(16, activation='relu',name='layer2'))
model.add(layers.Dense(1,activation='linear',name='Layer3'))
model.summary()
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['mae'])
history = model.fit(trainX, trainY, epochs=10,
validation_split=.2, batch_size=4)
The input expected by the model as defined in you model architecture is
input_shape=(16, 16, 1)
So while training you can send a batch of 16X16 single channel (X1) images. However your data shape is (239, 16, 16). i.e you have a batch of 239 images of 16X16. All you have to do is reshape 16X16 to 16X16X1. Since your data is in numpy array you can do this using expand_dim.
trainX = np.expand_dims(trainX, -1)
Fixed code
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(16, 16, 1),name='Layer1'))
model.add(layers.Flatten())
model.add(layers.Dense(16, activation='relu',name='layer2'))
model.add(layers.Dense(1,activation='linear',name='Layer3'))
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['mae'])
trainY = np.random.randint(0,10, (239, 1))
trainX = np.random.randn(239, 16, 16)
trainX = np.expand_dims(trainX, -1)
history = model.fit(trainX, trainY, epochs=10, validation_split=.2, batch_size=4)
I am building an LSTM network.
My data looks as following:
X_train.shape = (134, 300000, 4)
X_train contains 134 sequences, with 300000 timesteps and 4 features.
Y_train.shape = (134, 2)
Y_train contains 134 labels, [1, 0] for True and [0, 1] for False.
Below is my model in Keras.
model = Sequential()
model.add(LSTM(4, input_shape=(300000, 4), return_sequences=True))
model.compile(loss='categorical_crossentropy', optimizer='adam')
Whenever I run the model, I get the following error:
Error when checking target: expected lstm_52 to have 3 dimensions, but got array with shape (113, 2)
It seems to be related to my Y_train data -- as its shape is (113, 2).
Thank you!
The output shape of your LSTM layer is (batch_size, 300000, 4) (because of return_sequences=True). Therefore your model expects the target y_train to have 3 dimensions but you are passing an array with only 2 dimensions (batch_size, 2).
You probably want to use return_sequences=False instead. In this case the output shape of the LSTM layer will be (batch_size, 4). Moreover, you should add a final softmax layer to your model in order to have the desired output shape of (batch_size, 2):
model = Sequential()
model.add(LSTM(4, input_shape=(300000, 4), return_sequences=False))
model.add(Dense(2, activation='softmax')) # 2 neurons because you have 2 classes
model.compile(loss='categorical_crossentropy', optimizer='adam')
I'm new with Keras and I'm trying to implement a Sequence to Sequence LSTM.
Particularly, I have a dataset with 9 features and I want to predict 5 continuous values.
I split the training and the test set and their shape are respectively:
X TRAIN (59010, 9)
X TEST (25291, 9)
Y TRAIN (59010, 5)
Y TEST (25291, 5)
The LSTM is extremely simple at the moment:
model = Sequential()
model.add(LSTM(100, input_shape=(9,), return_sequences=True))
model.compile(loss="mean_absolute_error", optimizer="adam", metrics= ['accuracy'])
history = model.fit(X_train,y_train,epochs=100, validation_data=(X_test,y_test))
But I have the following error:
ValueError: Input 0 is incompatible with layer lstm_1: expected
ndim=3, found ndim=2
Can anyone help me?
LSTM layer expects inputs to have shape of (batch_size, timesteps, input_dim). In keras you need to pass (timesteps, input_dim) for input_shape argument. But you are setting input_shape (9,). This shape does not include timesteps dimension. The problem can be solved by adding extra dimension to input_shape for time dimension. E.g adding extra dimension with value 1 could be simple solution. For this you have to reshape input dataset( X Train) and Y shape. But this might be problematic because the time resolution is 1 and you are feeding length one sequence. With length one sequence as input, using LSTM does not seem the right option.
x_train = x_train.reshape(-1, 1, 9)
x_test = x_test.reshape(-1, 1, 9)
y_train = y_train.reshape(-1, 1, 5)
y_test = y_test.reshape(-1, 1, 5)
model = Sequential()
model.add(LSTM(100, input_shape=(1, 9), return_sequences=True))
model.add(LSTM(5, input_shape=(1, 9), return_sequences=True))
model.compile(loss="mean_absolute_error", optimizer="adam", metrics= ['accuracy'])
history = model.fit(X_train,y_train,epochs=100, validation_data=(X_test,y_test))
Could someone help me to understand what this error is all about?
model = Sequential()
model.add(Embedding(82, 100, weights=[embedding_matrix], input_length=1000))
model.add(LSTM(100))
model.add(Dense(100, activation = 'sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(x_train, y_train, epochs = 5, batch_size=64)
When i run this LSTM model, I am getting an error as
ValueError: Error when checking model target: expected dense_16 to have shape (None, 100) but got array with shape (16, 2)
I am not sure how much the below information would be useful:
x_train.shape
Out[959]: (16, 1000)
y_train.shape
Out[962]: (16, 2)
If you need any other information, I am ready to provide
you have defined dense layer input shape is 100.
model.add(Dense(100, activation = 'sigmoid'))
so you need to make sure your input should always same shape.
here in your case make x_train and and y_train same shape.
try with :
model = Sequential()
# here the batch dimension is None,
# which means any batch size will be accepted by the model.
model.add(Dense(32, batch_input_shape=(None, 500)))
model.add(Dense(32))
Your last layer has an output shape of None,100
model.add(Dense(100, activation = 'sigmoid'))
But your data (y_train) has the shape (16,2). It should be
model.add(Dense(2, activation = 'sigmoid'))