jos*_*shf 8 python keras tensorflow keras-layer tensor
我希望有人可以解释Keras中的输入层和Tensorflow中的占位符之间的差异(如果有的话)?
我调查的越多,两者看起来越相似,但到目前为止,我不相信100%.
以下是我观察到的支持输入图层和tf占位符相同的说法:
1)从keras.Input()返回的张量可以像tf.Session的run方法的feed_dict中的占位符一样使用.下面是使用Keras的简单示例的一部分,它添加了两个张量(a和b)并将结果与第三个张量(c)连接起来:
model = create_graph()
con_cat = model.output[0]
ab_add = model.output[1]
# These values are used equivalently to tf.Placeholder() below
mdl_in_a = model.input[0]
mdl_in_b = model.input[1]
mdl_in_c = model.input[2]
sess = k.backend.get_session()
a_in = rand_array() # 2x2 numpy arrays
b_in = rand_array()
c_in = rand_array()
a_in = np.reshape( a_in, (1,2,2))
b_in = np.reshape( b_in, (1,2,2))
c_in = np.reshape( c_in, (1,2,2))
val_cat, val_add = sess.run([con_cat, ab_add],
feed_dict={ mdl_in_a: a_in, mdl_in_b: b_in, mdl_in_c: c_in})
Run Code Online (Sandbox Code Playgroud)
2)来自Tensorflow Contrib的关于Keras 输入层的文档在其参数描述中提到了占位符:
"sparse:一个布尔值,指定要创建的占位符是否稀疏"
以下是我观察到的支持输入图层和tf占位符不同的说法:
1)我见过人们使用tf.Placeholder而不是输入层返回的Tensor.就像是:
a_holder = tf.placeholder(tf.float32, shape=(None, 2,2))
b_holder = tf.placeholder(tf.float32, shape=(None, 2,2))
c_holder = tf.placeholder(tf.float32, shape=(None, 2,2))
model = create_graph()
con_cat, ab_add = model( [a_holder, b_holder, c_holder])
sess = k.backend.get_session()
a_in = rand_array() # 2x2 numpy arrays
b_in = rand_array()
c_in = rand_array()
a_in = np.reshape( a_in, (1,2,2))
b_in = np.reshape( b_in, (1,2,2))
c_in = np.reshape( c_in, (1,2,2))
val_cat, val_add = sess.run([con_cat, ab_add],
feed_dict={ a_holder: a_in, b_holder: b_in, c_holder: c_in})
Run Code Online (Sandbox Code Playgroud)
Input() 返回创建的占位符的句柄,不创建其他 tf 操作符;Tensor 代表操作输出和占位符,因此没有矛盾。
要分析 Input() 究竟创建了什么,让我们运行以下代码:
with tf.name_scope("INPUT_LAYER"):
input_l = Input(shape = [n_features])
Run Code Online (Sandbox Code Playgroud)
然后:
writer = tf.summary.FileWriter('./my_graph', tf.get_default_graph())
writer.close()
Run Code Online (Sandbox Code Playgroud)
并从您的控制台启动 Tensorboard:
tensorboard --logdir="./my_graph"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1829 次 |
| 最近记录: |