aditya royal matturi
aditya royal matturi

Reputation: 111

I need to print out hyper parameters and parameters from a machine learning model in python

I need to print out Tensorflow models parameters and hyper-parameters.Below is the code I am using and when I try to print out the model I am only getting the model storage location. I also tried sklearn wrapper for Tensorflow with GridSearchCv but I am getting some errors. I can't try Hyperparams as it is deprecated in Tensorflow2.0.0 and I am using Tensorflow 2.0 and python 3.7

model = Sequential()
model.add(Dense(32,input_shape=(3,)))
model.add(Dense(1))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x, y, batch_size=5, epochs=10)
print(model)

Error
<tensorflow.python.keras.engine.sequential.Sequential object at 0x000001823B68BDD8>

Upvotes: 2

Views: 4139

Answers (2)

Space4VV
Space4VV

Reputation: 135

one option is model.get_config() that gives you a JSON list

Upvotes: 4

user11530462
user11530462

Reputation:

Elaborating the suggestions mentioned in the comment and providing the solution below.

From the question and comments, I understand that you have built a Model, trained it and you want to access Parameters/Metrics/Loss like loss, epochs, batch_size, metrics, etc..

For getting those values, as mentioned in the comments, assign the results of model.fit to a variable, say history, as shown below

history = model.fit(x = Train_Generator, shuffle=True, epochs = 3, steps_per_epoch = steps_per_epoch,
          validation_data = Val_Generator, validation_steps = validation_steps
                   )

All the values which we need will be present in history.history:

print(history.history)

Output of the above command is :

{'loss': [0.7191072106361389, 0.6968516707420349, 0.7295272946357727],
 'acc': [0.5074999928474426, 0.531499981880188, 0.5584999918937683],
 'val_loss': [4.8877854347229, 5.5809173583984375, 58.02076721191406],
 'val_acc': [0.5, 0.49900001287460327, 0.5090000033378601]}

As seen above, since the Training was done for 3 Epochs, there are 3 Values corresponding to Training Loss, Training Accuracy, Validation Loss and Validation Accuracy.

We shall now print the values which we want:

print('Number of Epochs are ', len(history.history['loss']))
print("Training Loss is ", history.history['loss'])
print("Validation Loss is ", history.history['val_loss'])
print("Training Accuracy is ", history.history['acc'])
print("Validation Accuracy is ", history.history['val_acc'])

Output of the above print statements are shown below:

Number of Epochs are  3
Training Loss is  [0.7191072106361389, 0.6968516707420349, 0.7295272946357727]
Validation Loss is  [4.8877854347229, 5.5809173583984375, 58.02076721191406]
Training Accuracy is  [0.5074999928474426, 0.531499981880188, 0.5584999918937683]
Validation Accuracy is  [0.5, 0.49900001287460327, 0.5090000033378601]

We can also plot the beautiful plots of the above Metrics/Values using the Code below:

training_acc = history.history['acc']
val_acc = history.history['val_acc']
training_loss = history.history['loss']
val_loss = history.history['val_loss']

Number_Of_Epochs = range(1, len(history.history['acc']) + 1)

plt.plot(Number_Of_Epochs, training_acc, color = 'green', marker = '*', label = 'Training Accuracy')
plt.plot(Number_Of_Epochs, val_acc, color = 'blue', marker = 'o', label = 'Validation Accuracy')
plt.title('Training Accuracy and Validation Accuracy Vs Epochs')
plt.legend()
plt.figure()

plt.plot(Number_Of_Epochs, training_loss, color = 'green', marker = '*', label = 'Training Loss')
plt.plot(Number_Of_Epochs, val_loss, color = 'blue', marker = 'o', label = 'Validation Loss')
plt.title('Training Loss and Validation Loss Vs Epochs')
plt.legend()
plt.figure()

Plots are shown in the screenshot below:

Plot of Accuracy and Loss

Hope this helps. Happy Learning!

Upvotes: 3

Related Questions