【问题标题】:Difference Between Keras Input Layer and Tensorflow PlaceholdersKeras 输入层和 TensorFlow 占位符之间的区别
【发布时间】:2023-03-08 20:13:02
【问题描述】:

我希望有人能解释 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})

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

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

以下是我观察到的支持输入层和 tf 占位符不一样的说法:

1) 我看到人们使用 tf.Placeholder 代替输入层返回的张量。比如:

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

【问题讨论】:

    标签: python tensorflow keras keras-layer tensor


    【解决方案1】:

    Input() 返回已创建占位符的句柄,并且不创建其他 tf 运算符;张量代表操作的输出和占位符,所以没有矛盾。

    要分析 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"
    

    查看结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-31
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 2018-08-24
      • 2017-01-02
      相关资源
      最近更新 更多