我试图得到我的hidden_layer2的权重矩阵并打印出来。似乎我可以得到重量矩阵,但我不能打印它。
当使用tf.Print(w, [w])时,它不会输出任何内容。在使用print(tf.Print(w,[w])时,它至少会打印关于张量的信息:
Tensor("hidden_layer2_2/Print:0", shape=(3, 2), dtype=float32)我还尝试在with-Statement,之外使用tf.Print(),结果也是一样。
完整的代码在这里,我只是在一个前馈NN:https://pastebin.com/KiQUBqK4中处理随机数据。
“我的守则”的一部分是:
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])发布于 2020-01-06 08:00:50
更新为TENSORFLOW 2.X
从TensorFlow 2.0 (>= 2.0)开始,由于Session对象已被删除,推荐的高级后端是Keras,因此获取权重的方法如下:
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])发布于 2018-03-16 08:30:22
试着做这个,
w = tf.get_variable("kernel")
print(w.eval())发布于 2019-10-29 14:00:05
我采取了不同的方法。首先,我列出所有可培训的变量,并使用所需变量的索引,并使用当前会话运行它。守则附后如下:
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 requiredhttps://stackoverflow.com/questions/49315432
复制相似问题