Keras模型可以通過函數API在張量中用作Tensorflow函數,如here所述。在具有Tensorflow張量的Keras模型中使用InputLayer(或輸入)有什麼優勢?
因此,我們可以這樣做:
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輸入,並附加元數據(如the source code中所述的
_keras_history
)。
但是,這不是我們不能用第二種形式做的,所以,有沒有InputLayer
(和Input
更不用說)的特殊用途(除了多個輸入)?
此外,InputLayer
是棘手的,因爲它是用input_shape
不同於其他keras層:我們指定批量大小(None
這裏),這不是通常的情況下...