python keras neural network: IndexError: indices are out-of-bounds - python

I was trying to fit a neural network model for multiclass classification but i had the
IndexError: indices are out-of-bounds
error.
My training data's dimension is (26728, 450), with 450 features. The output size is 5 (5 classes). I used to_categorical(train_Y) to transform it to a matrix of 5 columns.
The code is
model = Sequential()
model.add(Dense(64, input_dim=train_X.shape[1], init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(5, init='uniform'))
model.add(Activation('softmax'))
model.compile(
loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy']
)
train_Y_keras = to_categorical(train_Y)
model.fit(train_X, train_Y_keras, nb_epoch=10)
I don't fully understand the addition of layers and I copied and modified the code given here http://keras.io/getting-started/functional-api-guide/#getting-started-with-the-keras-functional-api, can anyone point out my error? Thanks.

I made it work by transforming the pandas dataframe to numpy array.

Related

CONV1D NEURAL NETWORK SHAPE

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)

expected dense_8 to have shape (2,) but got array with shape (1,)

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

Keras - How to convert LSTM code into CNN

I am working on binary classification sentiment analysis either positive or negative my lstm code is working fine but i am converting my lstm code into cnn having Value error of "input_length" is 30, but received input has shape (None, 1)
my input shape is (30,1) my batch size is 24 in lstm
model.add(Embedding(30,30,input_length=30))
model.add(Conv1D(padding='valid',activation='relu',strides=1))
model.add(Dropout(0.2))
model.add(Dense(30))
model.add(Dropout(0.2))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(inputBatch, ponlabel,batch_size=24,epochs=20,validation_data=(inputBatch, ponlabel))
for the sake of reference I have put my LSTM Code.
model.add(LSTM(100, input_shape=(30, 1)))
model.add(Dense(30, activation="relu"))
model.add(Dense(1, activation="sigmoid"))
model.compile(loss='mean_absolute_error', optimizer='adam',metrics=["accuracy"])
model.fit(inputBatch, ponlabel,
batch_size=24, epochs=20, verbose=1)
You are using Convolution2D but your data over timesteps is 1 dimensional. So you need to use Convolutional1D to convolve over the tokens in your sentence. There is a CNN text classification in the Keras examples folder imdb_cnn.py.

Creating an RNN with Keras Python

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)

How to make RNN with same input as CNN?

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.

Categories

Resources