Recently, i was in an interview and i was asked to build a neural netowrk using tensorflow
which meets the following requirements:
- The input layer of the model must have an input shape of (32, 10, 1)
- The model must have an output shape of (32, 10, 1)
and in response, i provided the following solution:
import tensorflow as tf
from tensorflow.keras.layers import Dense, Conv1D
model = tf.keras.models.Sequential([
Conv1D(filters=32, kernel_size=1, activation='relu', input_shape=(32, 10, 1)),
Dense(30, activation='relu'),
Dense(10, activation='relu'),
tf.keras.layers.Dense(1)
])
and in order to prove that my model can pass the requeirment, i printed the input-shape and out-put shape of each model using the below code:
for layer in model.layers:
print('input shape: ',layer.input_shape, 'output shape: ',layer.output_shape)
and here is what i got in the output:
input shape: (None, 32, 10, 1) output shape: (None, 32, 10, 32)
input shape: (None, 32, 10, 32) output shape: (None, 32, 10, 30)
input shape: (None, 32, 10, 30) output shape: (None, 32, 10, 10)
input shape: (None, 32, 10, 10) output shape: (None, 32, 10, 1)
Sadly and apparently my answer to this question was not correct and i don't know how to build such model ?
As you can see, my model has 4 dimentions and the input and output layer start by None
.
Is it the problem ?
CodePudding user response:
I am not 100% sure but for me really seems like you did not explicitly declared the input layer, I really think at the shape's command response we should not see a 'None' on it.
Two possible solutions I found at this source, which the best one seems to be the following (not tested):
inputs = Input(shape=(32, 10, 1))
x = Conv1D(filters=32, kernel_size=1)(inputs)
x = Dense(30, "relu")(x)
outputs = Dense(10, "relu")(x)
model = Model(inputs=inputs, outputs=outputs, name="my_model_name")
Let's see if that makes any sense.
CodePudding user response:
Thanks to @Pedro Silva and @AloneTogether i came out with a possible solution as below. So, in the Input
or Conv1D
layer the input_shape
does not include the Batch_size
of the input data. The input_shape
only specifies the shape of each Data point or (entry of data) and if we need to specify the Batch_size
then we cn use the batch_size
parameter in the layer. So, if we develop the mode as :
import tensorflow as tf
from tensorflow.keras.layers import Dense, Conv1D,Input
from tensorflow.keras.models import Model
model = tf.keras.models.Sequential([
Conv1D(filters=32, kernel_size=1, activation='relu', input_shape=(10, 1),batch_size=32),
Dense(30, activation='relu'),
Dense(10, activation='relu'),
tf.keras.layers.Dense(1)
])
for layer in model.layers:
print('input shape: ',layer.input_shape, 'output shape: ',layer.output_shape)
or this:
inputs = Input(shape=(10, 1),batch_size=32)
x = Conv1D(filters=32, kernel_size=1)(inputs)
x = Dense(30, "relu")(x)
outputs = Dense(10, "relu")(x)
model = Model(inputs=inputs, outputs=outputs, name="my_model_name")
for layer in model.layers:
print('input shape: ',layer.input_shape, 'output shape: ',layer.output_shape)
Then in both cases, the model has the following shape of input and output:
input shape: (32, 10, 1) output shape: (32, 10, 1)
input shape: (32, 10, 1) output shape: (32, 10, 32)
input shape: (32, 10, 32) output shape: (32, 10, 30)
input shape: (32, 10, 30) output shape: (32, 10, 10)