2

I am new to Keras and I am trying to get the weights in Keras. I know how to do it in Tensorflow in Python.

Code:

data = np.array(attributes, 'int64')
target = np.array(labels, 'int64')

feature_columns = [tf.contrib.layers.real_valued_column("", dimension=2, dtype=tf.float32)]
learningRate = 0.1
epoch = 10000

# https://www.tensorflow.org/api_docs/python/tf/metrics
validation_metrics = {
"accuracy": tf.contrib.learn.MetricSpec(metric_fn = tf.contrib.metrics.streaming_accuracy ,
prediction_key = tf.contrib.learn.PredictionKey.CLASSES),
"precision": tf.contrib.learn.MetricSpec(metric_fn = tf.contrib.metrics.streaming_precision ,
prediction_key = tf.contrib.learn.PredictionKey.CLASSES),
"recall": tf.contrib.learn.MetricSpec(metric_fn = tf.contrib.metrics.streaming_recall ,
prediction_key = tf.contrib.learn.PredictionKey.CLASSES),
"mean_absolute_error": tf.contrib.learn.MetricSpec(metric_fn = tf.contrib.metrics.streaming_mean_absolute_error ,
prediction_key = tf.contrib.learn.PredictionKey.CLASSES),
"false_negatives": tf.contrib.learn.MetricSpec(metric_fn = tf.contrib.metrics.streaming_false_negatives ,
prediction_key = tf.contrib.learn.PredictionKey.CLASSES),
"false_positives": tf.contrib.learn.MetricSpec(metric_fn = tf.contrib.metrics.streaming_false_positives ,
prediction_key = tf.contrib.learn.PredictionKey.CLASSES),
"true_positives": tf.contrib.learn.MetricSpec(metric_fn = tf.contrib.metrics.streaming_true_positives ,
prediction_key = tf.contrib.learn.PredictionKey.CLASSES)
}

# validation monitor
validation_monitor = tf.contrib.learn.monitors.ValidationMonitor(data, target, every_n_steps=500,
metrics = validation_metrics)

classifier = tf.contrib.learn.DNNClassifier(
feature_columns = feature_columns,
hidden_units = [3],
activation_fn = tf.nn.sigmoid,
optimizer = tf.train.GradientDescentOptimizer(learningRate),
model_dir = "model",
config = tf.contrib.learn.RunConfig(save_checkpoints_secs = 1)
)

classifier.fit(data, target, steps = epoch,
monitors = [validation_monitor])

# print('Params:', classifier.get_variable_names())
'''
Params: ['dnn/binary_logistic_head/dnn/learning_rate', 'dnn/hiddenlayer_0/biases', 'dnn/hiddenlayer_0/weights', 'dnn/logits/biases', 'dnn/logits/weights', 'global_step']
'''

print('total steps:', classifier.get_variable_value("global_step"))
print('weight from input layer to hidden layer: ', classifier.get_variable_value("dnn/hiddenlayer_0/weights"))
print('weight from hidden layer to output layer: ', classifier.get_variable_value("dnn/logits/weights"))

Is there any way to obtain the weights in Keras like in Tensorflow:

  1. The weights from input layer to hidden layer
  2. The weights from hidden layer to output layer

This is my model in Keras:

model = Sequential()
model.add(Flatten(input_shape=(224,224,3)))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
today
  • 32,602
  • 8
  • 95
  • 115
flyingduck92
  • 1,449
  • 2
  • 23
  • 39
  • I'm voting to close this question as off-topic because the answer lies directly in the [documentation](https://keras.io/layers/about-keras-layers/). – desertnaut Jan 02 '19 at 16:05
  • @desertnaut it's okay mate I appreciate that. Sorry if my question is not good enough for you. – flyingduck92 Jan 02 '19 at 18:12

1 Answers1

5

You can access and set the weights or parameters of the model's layers using get_weights and set_weights methods. From Keras documentation:

layer.get_weights(): returns the weights of the layer as a list of Numpy arrays. layer.set_weights(weights): sets the weights of the layer from a list of Numpy arrays (with the same shapes as the output of get_weights).

Each Keras model has a layers attribute which is the list of all the layers in the model. For example, in the sample model you provided, you can get the weights of the first Dense layer by running:

model.layers[1].get_weights()

It would return a list of two numpy arrays: the first one is the kernel parameters of the Dense layer the second array is the bias parameters.

today
  • 32,602
  • 8
  • 95
  • 115
  • Hi, Thank you for the response. But how do I get the weight of the Last Layer? model.add(Dense(1, activation='sigmoid')). – flyingduck92 Jan 02 '19 at 16:10
  • @sekti92uk Just use the index of the layer on `layers` list. The last layer is the fourth layer in the model, so its index would be 3; therefore: `model.layers[3].get_weights()`. Alternatively, you can write: `model.layers[-1].get_weights()` since it is the last layer as well. – today Jan 02 '19 at 16:13