I'm working on a regression probem using Tensorflow, and have created two models with slight differences in their first Dense layer.
The Models
# Create some regression data
X_regression = tf.range(0, 1000, 5)
y_regression = tf.range(100, 1100, 5) # Y = X+ 100
# Split regression data into training and test sets
X_reg_train = X_regression[:150]
X_reg_test = X_regression[150:]
y_reg_train = y_regression[:150]
y_reg_test = y_regression[150:]
Model 1
# Setup random seed
tf.random.set_seed(42)
model_1_reg = tf.keras.Sequential([
tf.keras.layers.Dense(100),
tf.keras.layers.Dense(10),
tf.keras.layers.Dense(1)
])
model_1_reg.compile(loss=tf.keras.losses.mae,
optimizer=tf.keras.optimizers.Adam(),
metrics=['mae'])
model_1_reg.fit(tf.expand_dims(X_reg_train, axis=-1), y_reg_train, epochs=100)
Model 2
# Setup random seed
tf.random.set_seed(42)
model_2_reg = tf.keras.Sequential([
tf.keras.layers.Dense(100, input_shape=(None, 1)),
tf.keras.layers.Dense(10),
tf.keras.layers.Dense(1)
])
model_2_reg.compile(loss=tf.keras.losses.mae,
optimizer=tf.keras.optimizers.Adam(),
metrics=['mae'])
model_2_reg.fit(tf.expand_dims(X_reg_train, axis=-1), y_reg_train, epochs=100)
I'm confused about whether I should add the input_shape
or not. Model 1's input shape becomes (None, 1)
and Model 2's input becomes (None, None, 1)
.
Both of them run, but perform differently.
Model 2 makes sense since we're inputting an array, but if I think about it, does that mean I only have a single node in the input layer? Since I'm giving it a whole ndarray instead of the instances it self. Model 1 makes sense too since I want to give each number into it.
So, which one makes sense more? Or what case should I use each model for? Also, for model 2's fit why does doing
tf.expand_dims(X_reg_train, axis=-1)
for the X of
model_2_reg.fit(tf.expand_dims(X_reg_train, axis=-1), y_reg_train, epochs=100
work? I thought we're suppose to put it in as a batch or like an array of the data so it should be inside an ndarray
?
Dense
layers is equivalent to a singleDense
layer; you should include non-linearities (e.g. ReLU) in the intermediate layers. Check this answer for details on this issue. $\endgroup$(none, 1)
or should it be(1,)
? If I set(none, 1)
it means that in the fit I'm only fitting in 1 instance of the data If i give an ndarray. However, if I set it as(1,)
, that means that the ndarray of integers are treated as a batch, so I'm giving it multiple instances. $\endgroup$