Keras - How to convert LSTM code into CNN - python

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.

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)

How can I make the output of a convnet an image with keras?

I'm using 3d images as my input and my output...
model = Sequential()
#add model layers
model.add(Convolution3D(64, kernel_size=3, activation="relu", input_shape=(240, 240, 155, 1)))
model.add(Convolution3D(32, kernel_size=3, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
But instead of making the final layer a Dense with softmax, I want the output to be a 3D image of the same dimensions as the input.
What would I have to do to upsample?
There are many ways of upsampling - you can look in github for simple implementation of seg-net or U-net (or seg-u-net).

Saving the specific layer from within a sequential Keras model

I am building an auto-encoder and training the model so the targeted output is the same as the input.
I am using a sequential Keras model. When I use model.predict I would like it to export the array from a specific layer (Dense256) not the output.
This is my current model:
model = Sequential()
model.add(Dense(4096, input_dim = x.shape[1], activation = 'relu'))
model.add(Dense(2048, activation='relu'))
model.add(Dense(1024, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dense(1024, activation='relu'))
model.add(Dense(2048, activation='relu'))
model.add(Dense(4096, activation='relu'))
model.add(Dense(x.shape[1], activation ='sigmoid'))
model.compile(loss = 'mean_squared_error', optimizer = 'adam')
history = model.fit(data_train,data_train,
verbose=1,
epochs=10,
batch_size=256,
shuffle=True,
validation_data=(data_test, data_test))
After training, create a new model (model2) from your trained model (model) ending in your desired layer.
You can do so either with layer name:
(In model.summary(), your dense's layer 'name' with 256 neurons is dense_5)
from keras.models import Model
model2= Model(model.input,model.get_layer('dense_5').output)
Or with layer order:
(your dense layer with 256 neurons is fifth in model.summary())
from keras.models import Model
model2= Model(model.input,model.layers[4].output)
Then you can use predict
preds=model2.predict(x)
layer.get_weights() returns the weights of a layer as a numpy array which can then be saved, for example with np.save.
To set the weights from a numpy array, layer.set_weights(weights) can be used.
You can access your layer either by name (model.get_layer(LAYER_NAME) or by its number (model.layers[LAYER_INDEX]).

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.

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

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.

Categories

Resources