How to extract weights of hidden layers with Keras API - python

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]

Related

How many layers should I stack in a sequential model?

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?

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)

Validation data performing worse than training data in keras

I am training a CNN on some text data. The sentences are padded and embedded and fed to a CNN. The model architecture is:
model = Sequential()
model.add(Embedding(max_features, embedding_dims, input_length=maxlen))
model.add(Conv1D(128, 5, activation='relu'))
model.add(GlobalMaxPooling1D())
model.add(Dense(50, activation = 'relu'))
model.add(BatchNormalization())
model.add(Dense(50, activation = 'relu'))
model.add(BatchNormalization())
model.add(Dense(25, activation = 'relu'))
#model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
Any help would be appreciated.
You model is over-fitting so the best practice is:
add layers and preferably that goes in the power of 2
instead of
model.add(Dense(50, activation = 'relu'))
use
model.add(Dense(64, activation = 'relu'))
and go with 512 128 64 32 16
add some dropout layers preferably after two layers.
train on bigger data.
You can try removing BatchNormalization and adding more Convolutional and Pooling Layer that may increase your accuracy.
You can also check out this -:
https://forums.fast.ai/t/batch-normalization-with-a-large-batch-size-breaks-validation-accuracy/7940

Counting CNN layers

i created a CNN for a project i am involved with and i need to present it. The issue is, I am not sure about how to count the layers.
Here is my model:
model = Sequential()
model.add(Conv2D(64,(3,3), input_shape = (40,40,2)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64,(3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(1600))
model.add(Reshape((40,40)))
model.add(Activation('sigmoid'))
model.compile(loss='MSE',
optimizer='SGD',
metrics=['MAE'])
len(model.layers) returned 12 :
So i used 1 input 10 hidden 1 output layers,
or
i need to count them as a group and say 1 input 2 hidden 1 output?
When calculating the depth of a CNN network, we only consider the layers that have tunable/trainable weights/parameters. In CNN only Convolutional Layers and Fully Connected Layers will have trainable parameters. If you want to label layers consider only Convolutional, Full Connected and Output layers (Conv2D and Dense).
Max Pooling Layers are generally take together with Convolutional Layers as one layer.

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]).

Categories

Resources