How can I extract Flatten Layer Output for each epoch? - python

model = Sequential()
model.add(Conv2D(50, (5,5), activation='relu', input_shape =(5,5,1), kernel_initializer='he_normal'))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
model.summary()
# compile the model
model.compile(loss='binary_crossentropy', optimizer= 'adam', metrics=['accuracy'])
model_checkpoint=ModelCheckpoint(r'C:\Users\globo\Desktop\Test_CNN\Results\Kernel5x5\Weights'+'\\'+test+'\model_test{epoch:02d}.h5',save_freq=1,save_weights_only=True)
# fit the model
history = model.fit(X_train, Y_train, epochs=10, batch_size=32, verbose=1, callbacks=[model_checkpoint], shuffle=True, validation_split=0.5)
I'm already extracting weights for each epoch with "ModelCheckpoint", but how can I extract flatten layer output for each epoch and save them?

doing this with sequential models is not feasible at all.
you should use functional API
inp = Input((5,5,1))
x = Conv2D(50, (5,5), activation='relu', kernel_initializer='he_normal')(inp)
xflatten = Flatten()(x)
out = Dense(1, activation='sigmoid')(xflatten)
main_model = Model(inp, out) # this works same as your model
flatten_model = Model(inp, xflatten) # and this only outputs the flatten layer and is not necessary to compile it because we won't train it, it just shows the output of a layer
main_model.compile(loss='binary_crossentropy', optimizer= 'adam', metrics=['accuracy'])
history = main_model.fit(X_train, Y_train, epochs=10, batch_size=32, verbose=1, callbacks=[model_checkpoint], shuffle=True, validation_split=0.5)
to see the flatten layers's output:
flatten_model.predict(X)

Related

Evaluate a deep learning model

I want to evaluate the following deep learning model.
lst=[]
for i in range(10):
model = Sequential()
model.add(Dense(64, kernel_initializer='he_normal', input_dim=X_train.shape[1], activation='relu'))
model.add(Dropout(0.7))
model.add(Dense(64, kernel_initializer='he_normal', activation='relu'))
model.add(Dropout(0.7))
model.add(Dense(1, activation='sigmoid'))
model.summary()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['Recall'])
early_stopping = EarlyStopping(monitor='val_loss', patience=10)
history = model.fit(X_train, y_train, epochs=150,
batch_size=128, validation_data=(X_val, y_val), callbacks=[early_stopping])
# Generating predictions on the test set
y_pred = model.predict(X_test)
y_pred = (y_pred > 0.5)
clsf=classification_report(y_test, y_pred)
lst.append (clsf)
print (clsf)
By this, I am running the model for 10 times and after that take the average of the recall metric.
I am wondering if I am doing right the procedure or I can do this with some other way
Any suggestions? Thanks

I'd like to change the Keras to a pytorch, but I don't know how to build a neural network

The HAR dataset should be analyzed using LSTM and 1D CNN.
I need to check the graph of the change in loss and check the confusion matrix.
I don't know how to make init and forward functions in pytorch....
# define model
model = Sequential()
model.add(ConvLSTM2D(filters=64, kernel_size=(1,3), activation='relu', input_shape=(n_steps, 1, n_length, n_features)))
model.add(Dropout(0.1))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
hist = model.fit(X_train, Y_train, epochs=epochs, validation_data=(X_test, Y_test), batch_size=batch_size, verbose=verbose)
# evaluate model
(loss, accuracy) = model.evaluate(X_test, Y_test, batch_size=batch_size, verbose=verbose)
print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss, accuracy * 100))
The above is an LSTM model implemented by keras.
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,n_features)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', padding = 'same'))
model.add(Dropout(0.3))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
history = model.fit(X_train, Y_train, validation_data=(X_test, Y_test),
epochs=epochs, batch_size=batch_size, callbacks = [checkpoint], verbose=verbose)
# evaluate model
(loss, accuracy) = model.evaluate(X_test, Y_test, batch_size=batch_size, verbose=verbose)
print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss, accuracy * 100))
The above is a 1D CNN model implemented by keras.
I started deep learning a few months ago, so I don't know. Help me.

why val_loss and val_accuracy not showing in epochs

I'm trying to classify images whether they're cats,dogs or pandas. the data contains all of images (cats + dogs + pandas) and the labels contains the labels of them but somehow when i fit the data to the model, the val_loss and val_accuracy does not show up, the only metrics shown in each epochs are loss and accuracy. I have no clue why it's not showing up but i have feeling that it's because i don't pass validation_data so i passed X_test.all() into validation_data but the val_loss and val_accuracy still does not show up, what should i do?
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2)
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (2,2), activation = 'relu', input_shape= (height, width, n_channels)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64,(2,2), activation= 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(128,(2,2), activation= 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(256,(2,2), activation= 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation= 'relu'),
tf.keras.layers.Dense(3, activation= 'softmax')
])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
y_train = np_utils.to_categorical(y_train, 3)
model.fit(X_train, y_train, batch_size=32, epochs=25, verbose=1)
You forgot to input validation test in your model fit.
model.fit(X_train, y_train, batch_size=32, epochs=25, verbose=1, validation_data=(X_test,y_test))
you forget to convert y_test variable to categorical type. Add this line,
y_test = np_utils.to_categorical(y_test , 3)

get a list of predictions of a neural network

I created a neural network to classify messages. Now I want to collect the predictions into a list in python. How do I do this?
So here is the model:
model = Sequential()
model.add(layers.Dense(500, activation = "relu", input_shape=(7600,)))
# Hidden - Layers
model.add(layers.Dropout(0.4, noise_shape=None, seed=None))
model.add(layers.Dense(300, activation = "relu"))
model.add(layers.Dropout(0.4, noise_shape=None, seed=None))
model.add(layers.Dense(100, activation = "relu"))
model.add(layers.Dropout(0.4, noise_shape=None, seed=None))
model.add(layers.Dense(20, activation = "softmax"))
model.summary()
model.compile(loss="categorical_crossentropy",
optimizer="adam",
metrics=['accuracy'])
model.fit( np.array(vectorized_training), np.array(y_train_neralnet),
batch_size=2000,
epochs=3,
verbose=1,
validation_data=(np.array(vectorized_validation), np.array(y_validation_neralnet)))
Here I tried to print the shape of validation_data that is inside of the model.fit() method but it gives an error.
NameError: name 'validation_data' is not defined
This is what you are looking for:
preds = model.predict(X_test)

A `Concatenate` layer should be called on a list of at least 2 inputs

I am trying to create two sequential models in Keras for CNN deep learning and merge(concatenate) these two models before adding it into the dense layer. But I am getting the error:
A Concatenate layer should be called on a list of at least 2 inputs
model_1 = models.Sequential()
model_1.add(layers.Conv1D(num_filters, 7, activation='relu',
input_shape=(TEXT_MAX_LENGTH, LENGTH_ALPHABET)))
model_1.add(layers.MaxPooling1D(pool_size=3))
model_1.add(layers.Flatten())
model_2 = models.Sequential()
model_2.add(layers.Conv1D(num_filters, 7, activation='relu',
input_shape=(TEXT_MAX_LENGTH, LENGTH_ALPHABET)))
model_2.add(layers.Conv1D(num_filters, 7, activation='relu'))
model_2.add(layers.MaxPooling1D(3))
model_2.add(layers.Flatten())
concat = Concatenate([model_1, model_2])
merged_model = models.Sequential()
model.add(concat)
model.add(layers.Dense(width_hidden, activation='relu'))
model.add(layers.Dropout(rate=dropout))
model.add(layers.Dense(width_output, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
callbacks=callbacks_list,
validation_data=(x_test, y_test)
)

Categories

Resources