Reputation: 2250
I made a model using Keras with Tensorflow. I use Inputlayer
with these lines of code:
img1 = tf.placeholder(tf.float32, shape=(None, img_width, img_heigh, img_ch))
first_input = InputLayer(input_tensor=img1, input_shape=(img_width, img_heigh, img_ch))
first_dense = Conv2D(16, 3, 3, activation='relu', border_mode='same', name='1st_conv1')(first_input)
But I get this error:
ValueError: Layer 1st_conv1 was called with an input that isn't a symbolic tensor. Received type: <class 'keras.engine.topology.InputLayer'>. Full input: [<keras.engine.topology.InputLayer object at 0x00000000112170F0>]. All inputs to the layer should be tensors.
When I use Input
like this, it works fine:
first_input = Input(tensor=img1, shape=(224, 224, 3), name='1st_input')
first_dense = Conv2D(16, 3, 3, activation='relu', border_mode='same', name='1st_conv1')(first_input)
What is the difference between Inputlayer
and Input
?
Upvotes: 25
Views: 11216
Reputation: 557
Just complementing on what has already been said, I tested and they don't seem equivalent (a similar layout doesn't generate the exact same results). So might be better to just stay with input_shape as recommended.
Upvotes: 0
Reputation: 3
To define it in simple words:
keras.layers.Input
is used to instantiate a Keras Tensor. In this case, your data is probably not a tf tensor, maybe an np
array.
On the other hand, keras.layers.InputLayer
is a layer where your data is already defined as one of the tf tensor types, i.e., can be a ragged tensor or constant or other types.
I hope this helps!
Upvotes: 0
Reputation: 166
Input: Used for creating a functional model
inp=tf.keras.Input(shape=[?,?,?])
x=layers.Conv2D(.....)(inp)
Input Layer: used for creating a sequential model
x=tf.keras.Sequential()
x.add(tf.keras.layers.InputLayer(shape=[?,?,?]))
And the other difference is that
When using InputLayer with the Keras Sequential model, it can be skipped by moving the input_shape parameter to the first layer after the InputLayer.
That is in sequential model you can skip the InputLayer and specify the shape directly in the first layer. i.e From this
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(4,)),
tf.keras.layers.Dense(8)])
To this
model = tf.keras.Sequential([
tf.keras.layers.Dense(8, input_shape=(4,))])
Upvotes: 8
Reputation: 285
According to tensorflow website, "It is generally recommend to use the functional layer API via Input, (which creates an InputLayer) without directly using InputLayer." Know more at this page here
Upvotes: 10
Reputation: 86600
InputLayer
is a layer. Input
is a tensor. You can only call layers passing tensors to them.
The idea is:
outputTensor = SomeLayer(inputTensor)
So, only Input
can be passed because it's a tensor.
Honestly, I have no idea about the reason for the existence of InputLayer
. Maybe it's supposed to be used internally. I never used it, and it seems I'll never need it.
Upvotes: 28