为什么 get_tensor_by_name 无法正确获取 tf.keras.layers 定义的层的权重 [英] Why get_tensor_by_name can't get the weights of layers defined by tf.keras.layers properly
问题描述
我尝试通过在 tensorflow
中使用 get_tensor_by_name
来获取由 tf.keras.layers
定义的层的权重.代码呈现如下
I try to get the weights of layers defined by tf.keras.layers
by using get_tensor_by_name
in tensorflow
. The code is presented as follows
# encoding: utf-8
import tensorflow as tf
x = tf.placeholder(tf.float32, (None,3))
h = tf.keras.layers.dense(3)(x)
y = tf.keras.layers.dense(1)(h)
for tn in tf.trainable_variables():
print(tn.name)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
w = tf.get_default_graph().get_tensor_by_name("dense/kernel:0")
print(sess.run(w))
权重的名称是dense/kernel:0
.然而,sess.run(w)
的输出很奇怪
The name of the weight is dense/kernel:0
. However, the output of sess.run(w)
is weird
[( 10,) ( 44,) ( 47,) (106,) (111,) ( 98,) ( 58,) (108,) (111,) ( 99,)
( 97,) (108,) (104,) (111,) (115,) (116,) ( 47,) (114,) (101,)
... ]
这不是浮点数组.事实上,如果我使用 tf.layers.dense
来定义网络,一切都会好起来的.所以我的问题是如何通过正确使用张量名称来获得 tf.keras.layers
定义的层的权重.
which is not an array of floats. In fact, if I use tf.layers.dense
to define the network, everything goes fine. So my question is that how I can get the weights of layers defined by tf.keras.layers
by using tensor name properly.
推荐答案
您可以在图层上使用 get_weights()
来获取特定图层的权重值.以下是您案例的示例代码:
You can use get_weights()
on layers to get the weight values of particular layers. Here is an example code for your case:
import tensorflow as tf
input_x = tf.placeholder(tf.float32, [None, 3], name='x')
dense1 = tf.keras.Dense(3, activation='relu')
l1 = dense1(input_x)
dense2 = tf.keras.Dense(1)
y = dense2(l1)
weights = dense1.get_weights()
可以使用 Keras API 以更简单的方式完成,如下所示:
It can be done in a even simpler way with Keras API as follows:
def mymodel():
i = Input(shape=(3, ))
x = Dense(3, activation='relu')(i)
o = Dense(1)(x)
model = Model(input=i, output=o)
return model
model = mymodel()
names = [weight.name for layer in model.layers for weight in layer.weights]
weights = model.get_weights()
for name, weight in zip(names, weights):
print(name, weight.shape)
此示例获取模型每一层的权重矩阵.
This example gets weight matrices for each layer of your model.
这篇关于为什么 get_tensor_by_name 无法正确获取 tf.keras.layers 定义的层的权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!