My train set has 10 columns including a target column that I'm trying to predict, while my test set (dataframe_test) has 9 columns. When I run the code I receive this error:
Input 0 of layer "Hidden1" is incompatible with the layer: expected axis -1 of input shape to have value 10, but received input with shape (None, 9)
Call arguments received:
• inputs=tf.Tensor(shape=(None, 9), dtype=float64)
• training=False
• mask=None**
My model looks like this:
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(units=10,
activation='relu',
kernel_regularizer=tf.keras.regularizers.l2(l=0.01),
name='Hidden1'))
model.add(tf.keras.layers.Dense(units=6,
activation='relu',
kernel_regularizer=tf.keras.regularizers.l2(l=0.01),
name='Hidden2'))
model.add(tf.keras.layers.Dense(units=1,
name='Output'))
my_learning_rate = 0.3
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=my_learning_rate),
loss="categorical_crossentropy",
metrics='accuracy')
epochs = 10
batch_size = 32
history = model.fit(train, y_train, epochs = epochs, batch_size = batch_size)
epochs = history.epoch
print(epochs)
score = model.predict(dataframe_test)
You must to split your train set in a 9 columns input matrix x_train = train[:, :10] and a single column training target matrix y_train = train[:, 10].reshape((-1, 1)).
Try using sigmoid
input_size=len(X.columns)
model.add(Dense(10,activation='sigmoid', input_shape=(input_size,)))
model.add(Dense(10,activation='relu'))
model.add(Dense(10,activation='relu'))
model.add(Dense(1))
Related
I'm developing a neural network architecture that takes a vector as the input and outputs another different vector.
The error message I´m getting is the following:
ValueError: Dimensions must be equal, but are 384 and 96 for '{{node mean_squared_error/SquaredDifference}} = SquaredDifference[T=DT_FLOAT](sequential/cu_dnnlstm_1/transpose_1, IteratorGetNext:1)' with input shapes: [8,384,4], [8,96,4].
With regards to the code, I have declared the following training and validation vectors which shapes are:
x_train.shape = (96, 384, 21)
y_train.shape = (96, 96, 4)
x_val.shape = (25, 384, 21)
y_val.shape = (25, 96, 4)
Being "x" the input vector, and "y" the output vector, or the label of "x".
The architecture of the neural network is the following:
model = Sequential()
model.add(CuDNNLSTM(96,input_shape=(x_train.shape[1],x_train.shape[2]),return_sequences=True))
model.add(CuDNNLSTM(4,input_shape=(y_train.shape[1],y_train.shape[2]),return_sequences=True))
model.compile(optimizer=optimizer, loss='mse', metrics='mse')
Finally the fit of the neural network is done in the following lines:
# Fit the model
epochs = 10
batch_size = 8
train_steps = x_train.shape[0] // batch_size
valid_steps = x_val.shape[0] // batch_size
with tf.device("GPU:1"):
history = model.fit(x_train, y_train,
validation_data=(x_val, y_val),
epochs = epochs,
batch_size = batch_size,
steps_per_epoch = train_steps,
validation_steps = valid_steps,
callbacks=callbacks)
I dont understand where is the error, since in this dummy architecture both shapes match both the input and the output, I have seen that people usually make the output of the neural network a dense layer, but I would like it to be just the whole vector of "y" and not just a point.
Thanks in advance.
I have tried modifying the shapes that the LSTM layers use, doesnt work.
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 cannot find a hands on tutorial on how to structure the data for use with keras LSTM.
Data
x_train = 7300 rows where each vector is length 64.
y_train = array of 7300 items either 0's or 1's (the class).
Model
model = Sequential()
model.add(LSTM(200, dropout=0.2, recurrent_dropout=0.2, input_shape = (1, 64)))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train,
epochs = 5,
batch_size = 32,
validation_split = 0.1,
callbacks=[EarlyStopping(monitor='val_loss', patience=3, min_delta=0.0001)])
My question is simply, why doesn't this work? Why isn't is as simple as giving an 2d array of vectors and similar length y values to fit.
Keras LSTM expects input of shape [batch_size, timesteps, features]. Your data is of shape [batch_size, features].
To add the timestep dimension (where number of timesteps is 1), do the following:
x_train = np.expand_dims(x_train, axis=1)
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))
I have received multiple diffrent ValueErrors when trying to solve following by changing many parameters.
It is a time series problem, I have data from 60 shops, 215 items, 1034 days. I have splitted 973 days for train and 61 for test.:
train_x = train_x.reshape((60, 973, 215))
test_x = test_x.reshape((60, 61, 215))
train_y = train_y.reshape((60, 973, 215))
test_y = test_y.reshape((60, 61, 215))
My model:
model = Sequential()
model.add(LSTM(100, input_shape=(train_x.shape[1], train_x.shape[2]),
return_sequences='true'))
model.add(Dense(215))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=
['accuracy'])
history = model.fit(train_x, train_y, epochs=10,
validation_data=(test_x, test_y), verbose=2, shuffle=False)
ValueError: Error when checking input: expected lstm_1_input to have
shape (973, 215) but got array with shape (61, 215)
You've split your data with respect to timesteps as opposed to the samples. You need to decide on what are your samples in the first instance. For the sake of the answer I will assume these are along the first axis (assuming the data has been framed as a supervised time-series problem).
The input_size in LSTM expects the shape of (timesteps, data_dim) as explained here, and these dimensions must remain the same for each batch. In your example, samples from training and testing have different dimensions. The batch size can differ (unless specified with batch_size parameter).
Your data should be split between training and testing along the first axis. Here is an analogous example from Keras tutorials:
from keras.models import Sequential
from keras.layers import LSTM, Dense
import numpy as np
data_dim = 16
timesteps = 8
num_classes = 10
# expected input data shape: (batch_size, timesteps, data_dim)
model = Sequential()
model.add(LSTM(32, return_sequences=True,
input_shape=(timesteps, data_dim))) # returns a sequence of vectors of dimension 32
model.add(LSTM(32, return_sequences=True)) # returns a sequence of vectors of dimension 32
model.add(LSTM(32)) # return a single vector of dimension 32
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
# Generate dummy training data
x_train = np.random.random((1000, timesteps, data_dim))
y_train = np.random.random((1000, num_classes))
# Generate dummy validation data
x_val = np.random.random((100, timesteps, data_dim))
y_val = np.random.random((100, num_classes))
model.fit(x_train, y_train,
batch_size=64, epochs=5,
validation_data=(x_val, y_val))
You will notice that timesteps is the same for training and testing data and x_train.shape[1] == x_val.shape[1]. It is the number of samples that differs along the first axis x_train.shape[0] is 1000 and x_val.shape[0] is 100.