![](/img/trans.png)
[英]Found unexpected instance while processing input tensors for keras functional model - Keras, Tensorflow
[英]What is the advantage of using an InputLayer (or an Input) in a Keras model with Tensorflow tensors?
甲Keras模型可以用作一个张量一个Tensorflow功能,通过所述功能API,如所描述这里。
所以我们可以这样做:
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
情况下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_history
输入,并带有额外的元数据(例如源代码中所述的_keras_history
)。 但这不是我们不能用第二种形式做的事情,那么, InputLayer
(和Input
a InputLayer
是否有特殊用法(多个输入除外)?
此外, InputLayer
很棘手,因为它使用input_shape
与其他 keras 层不同:我们指定批次大小(此处为None
),通常情况并非如此...
InputLayer
似乎有一些用途:
首先,它允许您按原样提供纯张量流张量,而无需指定其形状。 例如你可以写
model.add(InputLayer(input_tensor=a))
这很好,有几个明显的原因,其中包括减少重复。
其次,它们允许您使用单个输入编写非序列网络,例如
input / \\ / \\ / \\ conv1 conv2 | |
如果没有InputLayer
您将需要明确地为conv1
和conv2
相同的张量,或者在模型之上创建任意标识层。 两者都不是很令人满意。
最后,他们消除了“也是输入的层”和“正常层”之间的任意区别。 如果您使用InputLayer
您可以编写代码,其中明确区分哪个层是输入和哪个层执行某些操作。 这提高了代码的可读性并使重构更加容易。 例如,替换第一层就像替换任何其他层一样简单,您无需考虑input_shape
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.