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])
There are a couple of ways to get things to print out while writing TensorFlow code. Of course, there's the classic Python built-in, print (Or the function print() , if we're being Python 3 about it). And then there's TensorFlow's print function, tf. Print (notice the capital P ).
Call model. summary() to print a useful summary of the model, which includes: Name and type of all layers in the model. Output shape for each layer.
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])
I believe there are multiple issues to be tackled here.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With