Keras输入层和Tensorflow占位符之间的差异

我希望有人可以解释Keras中的输入层和Tensorflow中的占位符之间的差异(如果有的话)?

我调查的越多,两者看起来越相似,但到目前为止,我不相信100% .

Here is what I have observed in favor of the claim that Input Layers and tf Placeholders are the same:

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})

2)来自Tensorflow Contrib的关于Keras的文档Input Layer在其论点描述中提到了占位符:

“sparse:一个布尔值,指定要创建的占位符是否稀疏”

Here is what I have observed in favor of the claim that Input Layers and tf Placeholders are NOT the same:

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})

回答(1)

2 years ago

Input()返回创建占位符的句柄,不创建其他tf运算符; Tensor代表操作输出和占位符,因此没有矛盾 .

要分析Input()创建的确切内容,请运行以下代码:

with tf.name_scope("INPUT_LAYER"):
input_l = Input(shape = [n_features])

然后:

writer = tf.summary.FileWriter('./my_graph', tf.get_default_graph())
writer.close()

并从您的控制台启动Tensorboard:

tensorboard --logdir="./my_graph"

看看结果:
enter image description here