【发布时间】:2017-12-26 08:14:30
【问题描述】:
Keras 模型可以通过函数 API 用作 Tensor 上的 Tensorflow 函数,如 here 所述。
所以我们可以这样做:
from keras.layers import InputLayer
a = tf.placeholder(dtype=tf.float32, shape=(None, 784))
model = Sequential()
model.add(InputLayer(input_tensor=a, input_shape=(None, 784)))
model.add(Dense(32, activation='relu'))
model.add(Dense(10, activation='softmax'))
output = model.output
张量是什么:
<tf.Tensor 'dense_24/Softmax:0' shape=(?, 10) dtype=float32>
但是,这也可以在没有任何 InputLayer 的情况下工作:
a = tf.placeholder(dtype=tf.float32, shape=(None, 784))
model = Sequential()
model.add(Dense(32, activation='relu', input_shape=(784,)))
model.add(Dense(10, activation='softmax'))
output = model(a)
有效,output 的形状和以前一样:
<tf.Tensor 'sequential_9/dense_22/Softmax:0' shape=(?, 10) dtype=float32>
我假设第一种形式允许:
- 明确附加
inputs和outputs作为模型的属性(同名),以便我们可以在其他地方重用它们。例如与其他 TF 操作。 - 将作为输入给出的张量转换为 Keras 输入,并带有额外的元数据(例如
_keras_history,如 the source code 中所述)。
但这不是我们不能用第二种形式做的事情,所以,InputLayer(和Input更是如此)有什么特殊用法吗(除了多个输入)?
此外,InputLayer 很棘手,因为它使用 input_shape 与其他 keras 层不同:我们指定批量大小(此处为None),但通常情况并非如此......
【问题讨论】:
标签: python tensorflow deep-learning keras