In the below code:

  1. I get an error when I do not specify the input shape in the $1^{st}$ layer. I thought that TensorFlow can infer the shape for the dense layer. For some code I have seen, TensorFlow works without the input shape specified.

  2. What is the input shape I should specify? X is (9, ). By default, am I supposed to omit the sample size for the input shape?

import numpy as np
from numpy import array

import pandas as pd

from tensorflow.keras.layers import Activation, Dense, SimpleRNN, GRU, LSTM, Conv1D, Conv2D, MaxPool2D, RepeatVector, TimeDistributed

X = [1, 2,  3,  4,  5,  6,  8,  9,  10]
Y = [10, 15,20, 25, 30, 35, 45, 50, 55]
X = np.array(X).reshape(9, )
Y = np.array(Y).reshape(9, )
model = Sequential()

model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X, Y, epochs=500,  batch_size=sample_size, verbose=1)

  • $\begingroup$ The first layer in every Sequential model requires an input_shape argument, so that it can infer the shapes of the trainable parameters like weights and biases. $\endgroup$ Jan 22, 2022 at 7:40

1 Answer 1

  1. My TensorFlow 2.6 can infer the input_shape in .fit().
  2. Yes, omit the sample size, (1, ) is your input_shape.

P.S. if you do not specify your input_shape, you cannot call .summary() before .fit()


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.