model = Sequential()
model.add(Conv1D(filters=4, kernel_size=(1), activation="relu", input_shape=(4,1)))
model.add(MaxPooling1D(pool_size=(1)))
model.add(Dropout(0.25))
model.add(Conv1D(filters=32, kernel_size=(1), activation='relu'))
model.add(MaxPooling1D(pool_size=(1)))
model.add(Dropout(0.25))
model.add(Conv1D(filters=64, kernel_size=(1), activation="relu"))
model.add(MaxPooling1D(pool_size=(1)))
model.add(Dropout(0.25))
model.add(Conv1D(filters=64, kernel_size=(1), activation='relu'))
model.add(MaxPooling1D(pool_size=(1)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(7, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
Hello, I'm new to building neural networks and decided to try my hand solving a multi-label classification problem. I'm take four feature values as input and giving the resulting classification as one or more of 7 categories. As such, I decided to implement the neural network as seen above.
However, upon fitting the model
model.fit(X_train, y_train, epochs = 10, validation_data = (X_test,y_test), batch_size = 64)
I receive this error:
Error when checking input: expected conv1d_92_input to have 3 dimensions, but got array with shape (415, 4)
I'm confused as to watch to do in order to get the neural network to fit to the data. The shape of feature and label data respectively are :
X_train = (414,4)
y_train = (413,7)
I believe you might find this previous stack stackoverflow post (It seems to be addressing your question) helpful: Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (339732, 29)
Related
I am trying to train a sequential model using the LSTM layer.
The size of sequence data for learning is as follows:
x = np.array(sequences)
y = to_categorical(labels).astype(int)
x.shape => (1800, 34, 48)
y.shape => (1800, 20)
After that, I make a sequential model and try to stack the LSTM layer and the dense layer, but I don't know how much to do that.
First, I did something like this:
model = Sequential()
model.add(LSTM(64, return_sequences=True, activation='relu', input_shape=x_train.shape[1:3]))
model.add(LSTM(128, return_sequences=True, activation='relu'))
model.add(LSTM(64, return_sequences=False, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(actions.shape[0], activation='softmax'))
model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['acc'])
model.summary()
However, this doesn't seem to fit my case as I followed someone else's code.
How many layers should I stack in a sequential model?
I am trying to create a multi-channel 1D CNN for analyzing ECG signals. I have 258 12 lead ECGs with length 300 samples, so my input dimension is (258, 300, 12).
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=10, activation='relu', input_shape=(n_timesteps,n_features), padding='same'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=64, kernel_size=10, activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=64, kernel_size=10, activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=64, kernel_size=10, activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.summary()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=10, batch_size=20, verbose=1, validation_split = 0.2)
I'm running the code above, and getting the following error
ValueError: Error when checking target: expected dense_8 to have shape (2,) but got array with shape (1,)
Thanks for any help!
You are trying to train the model like this,
model.fit(X_train, y_train, epochs=10, batch_size=20, verbose=1, validation_split = 0.2)
The shape of y_train is something like (n, 1), where n is the number of samples used to train.
Now, you are building a model with last layer like this,
model.add(Dense(num_classes, activation='softmax'))
From the error message, it can be deduced that you are setting num_classes=2. So, the last layer will have 2 nodes. Such a model expects y_train to be of shape (n,2). But you are using y_train of shape (n,1).
In order to fix the error, you can change the last layer as,
num_classes = 1
model.add(Dense(num_classes, activation='sigmoid'))
Note that, the activation function should be changed to sigmoid.
So, you are solving a binary classification problem.
The error message indicates our model expects label with shape (2,) and i assume you are using num_classes=2. However, Your label is either 1 or 0 as the provided shape of label is (1,). To solve this error, you have yo change the output dense layer of your model, and the layer should have one neuron with sigmoid activation function.
model.add(Dense(num_classes, activation='sigmoid')) # num_classes=1
I'm new to machine learning and Keras. I made an Neural Network with Keras for regression looking like this:
model = Sequential()
model.add(Dense(57, input_dim=44, kernel_initializer='normal',
activation='relu'))
model.add(Dense(45, activation='relu'))
model.add(Dense(35, activation='relu'))
model.add(Dense(20, activation='relu'))
model.add(Dense(18, activation='relu'))
model.add(Dense(15, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(5, activation='relu'))
model.add(Dense(5, activation='relu'))
model.add(Dense(1, activation='linear'))
My data has 44 dimensions, so could you please give me an example how could i make an RNN. I'm trying like this:
model = Sequential()
model.add(LSTM(44, input_shape=(6900, 44), ))
model.add(Dense(1))
model.compile(loss='mape', optimizer='adam', metrics=['mse', 'mae', 'mape'])
model.fit(X_train, y_train, epochs=100, batch_size=10, verbose=1)
But i get this error:
Error when checking input: expected lstm_13_input to have 3 dimensions, but got array with shape (6900, 44)
As far as I understood you, your data is 44 dimensional and not a time series. An RNN is computing operations on a sequence of data, i.e. a 2D and not a 1D tensor. But you can still use a RNN for 1D vectors, by interpreting them not as one n-dimensional vector but as a time series of n steps, each containing a 1D vector.
model = Sequential()
model.add(Reshape((-1, 1)
model.add(LSTM(44, input_shape=(6900, 44), ))
model.add(Dense(1))
model.compile(loss='mape', optimizer='adam', metrics=['mse', 'mae', 'mape'])
model.fit(X_train, y_train, epochs=100, batch_size=10, verbose=1)
I have a working cnn with text
my X_train's shape is (39971, 10000) , y_train: (39971, 4)
max_words = 10000
model = Sequential()
model.add(Dense(512, input_shape=(max_words,), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(4, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
this works so far.
And this his is how I'm trying to make the RNN:
model = Sequential()
model.add(Embedding(max_words, 128))
model.add(LSTM(64, return_sequences=True, dropout=0.5, recurrent_dropout=0.5))
model.add(Dense(4, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
I can compile the model but when I try to run model.fit with my training data i get an error:
ValueError: Error when checking target: expected dense_42 to have 3 dimensions, but got array with shape (39971, 4)
What does this mean? How could I fix this?
As this suggest I might need to add input_shape but I'm not sure with what values.
The LSTM layer is currently returning the full sequence, so the output of the network has shape (batch_size, nb_timesteps, 4). I presume you want to use the last LSTM output only. If that's the case, set return_sequences=False.
I have a simple sequential neural network which I would like to use to train a classifier. It is made of one input layer, 3 hidden layers and one output layer, as follows:
#sequential type of model
model = Sequential()
#stacking layers with .add
model.add(Dense(len(ytrain), activation='relu', input_dim=100))
model.add(Dropout(0.5))
model.add(Dense(len(ytrain), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(ytrain), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(ytrain), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(5, activation='softmax'))
How can I extract the weights associated with each hidden layer. The ultimate goal is to then use the activation function to compute the probability of each label to be the correct one.
Hope you understand. Any kind of help is appreciated.
weights = [layer.get_weights() for layer in model.layers]