user3921232
user3921232

Reputation: 617

How to print weights in Tensorflow?

I am trying to get the weight matrix of my hidden_layer2 and print it. It seems like I am able to get the weight matrix, but I am not able to print it.

When using tf.Print(w, [w]) it prints nothing. When using print(tf.Print(w,[w]) it prints at least the info about the tensor:

Tensor("hidden_layer2_2/Print:0", shape=(3, 2), dtype=float32)

I also tried to use tf.Print() outside of the with-Statement, same result.

Full code is here, I am just processing random data in a feed-forward NN: https://pastebin.com/KiQUBqK4

A part of my Code:

hidden_layer2 = tf.layers.dense(
        inputs=hidden_layer1,
        units=2,
        activation=tf.nn.relu,
        name="hidden_layer2")


with tf.variable_scope("hidden_layer2", reuse=True):
        w = tf.get_variable("kernel")
        tf.Print(w, [w])
        # Also tried tf.Print(hidden_layer2, [w])

Upvotes: 8

Views: 26326

Answers (6)

Timbus Calin
Timbus Calin

Reputation: 15063

UPDATED FOR TENSORFLOW 2.X

Starting from TensorFlow 2.0 (>= 2.0), since the Session object has been removed and the recommended high-level backend is Keras, the way to do get the weights is:

from tensorflow.keras.applications import MobileNetV2

model = MobileNetV2(input_shape=[128, 128, 3], include_top=False) #or whatever model
print(model.layers[0].get_weights()[0])

Upvotes: 8

jwpol
jwpol

Reputation: 1485

As an update to Timbus Calin answer in Tensorflow 2, biases can be accessed also using get_weights(), specifically get_weights()[1].

To access and print weights and biases for example in feedforward network:

for layer in self.model.layers:
      print(layer.get_weights()[0]) # weights
      print(layer.get_weights()[1]) # biases

Upvotes: 1

Maksim Shamihulau
Maksim Shamihulau

Reputation: 1748

An example on how to print weights per layer in tensorflow.js:

// 
const model = tf.sequential();
...

// kernel:
model.layers[0].getWeights()[0].print()

// bias:
model.layers[0].getWeights()[1].print()

Upvotes: 0

learner
learner

Reputation: 3472

I took a different approach. First I list all the trainable variables and use the index of the required variable and run it using the current session. The code is attached below:

variables = tf.trainable_variables()

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

print("Weight matrix: {0}".format(sess.run(variables[0])))  # For me the variable at the 0th index was the one I required

Upvotes: 0

Bhargavi
Bhargavi

Reputation: 21

I believe there are multiple issues to be tackled here.

  1. Running eval() should be accompanied by a session. As suggested in In TensorFlow, what is the difference between Session.run() and Tensor.eval()?, .eval() expects a default session to be running which was not likely in your case earlier. So, the best option here was to precede the code with a session. From your comment, we can see that this is done.

  2. The variables in the hidden layers (i.e. weights/kernels) need to be initialized once the graph is complete. Hence, you might want to use something similar to this:

    
    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)
    with tf.variable_scope("hidden_layer2", reuse=True):
        w = tf.get_variable("kernel")
        print(w.eval(session=sess))
    
    

Upvotes: 2

Darshit Mulani
Darshit Mulani

Reputation: 41

Try to do this,

w = tf.get_variable("kernel")
print(w.eval())

Upvotes: 0

Related Questions