不同输入区别
来源:2-12 函数API实现wide&deep模型
慕姐8158610
2021-10-19
老师好,在写wide&deep和前面分类/回归问题代码时,输入代码有区别。请问keras.layers.InputLayer()和keras.Input()输入有什么不同?
写回答
1回答
-
InputLayer是layer的概念,而Input则是tensor的概念。Layer是用来跟sequential啥的合作使用构建模型,如下:
# With explicit InputLayer. model = tf.keras.Sequential([ tf.keras.layers.InputLayer(input_shape=(4,)), tf.keras.layers.Dense(8)]) model.compile(tf.optimizers.RMSprop(0.001), loss='mse') model.fit(np.zeros((10, 4)), np.ones((10, 8))) # Without InputLayer and let the first layer to have the input_shape. # Keras will add a input for the model behind the scene. model = tf.keras.Sequential([ tf.keras.layers.Dense(8, input_shape=(4,))]) model.compile(tf.optimizers.RMSprop(0.001), loss='mse') model.fit(np.zeros((10, 4)), np.ones((10, 8)))
tensor则是函数式的建立模型:
# this is a logistic regression in Keras x = Input(shape=(32,)) y = Dense(16, activation='softmax')(x) model = Model(x, y)
更多细节看API: https://www.tensorflow.org/api_docs/python/tf/keras/layers/InputLayer
https://www.tensorflow.org/api_docs/python/tf/keras/Input
012021-11-02
相似问题