keras Tensorflow 中 InputLayer 的输入形状是什么? [英] What is the input shape of the InputLayer in keras Tensorflow?
问题描述
我有这个数据
X_regression = tf.range(0, 1000, 5)
y_regression = X + 100
X_reg_train, X_reg_test = X_regression[:150], X_regression[150:]
y_reg_train, y_reg_test = y_regression[:150], y_regression[150:]
我检查数据输入数据
X_reg_train[0], X_reg_train[0].shape, X_reg_train[0].ndim
它返回:
(<tf.Tensor: shape=(), dtype=int32, numpy=0>, TensorShape([]), 0)
我建立了一个模型:
# Set the random seed
tf.random.set_seed(42)
# Create the model
model_reg = tf.keras.models.Sequential()
# Add Input layer
model_reg.add(tf.keras.layers.InputLayer(input_shape=[1]))
# Add Hidden layers
model_reg.add(tf.keras.layers.Dense(units=10, activation=tf.keras.activations.relu))
# Add last layer
model_reg.add(tf.keras.layers.Dense(units=1))
# Compile the model
model_reg.compile(optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.mae,
metrics=[tf.keras.metrics.mae])
# Fit the model
model_reg.fit(X_reg_train, y_reg_train, epochs=10)
模型有效.
但是,我对 input_shape
为什么在这种情况下是[1]
?为什么有时是元组?
Why is it [1]
in this situation? Why is it sometimes a tuple?
希望能在不同情况下解释 input_shape
的不同格式.
Would appreciate an explanation of different formats of input_shape
in different situations.
推荐答案
InputLayer
其实就是在Dense
中指定参数input_shape
代码>层.当你在后台使用 method 2
时,Keras 实际上使用了 InputLayer
.
InputLayer
is actually just the same as specifying the parameter input_shape
in a Dense
layer. Keras actually uses InputLayer
when you use method 2
in the background.
# Method 1
model_reg.add(tf.keras.layers.InputLayer(input_shape=(1,)))
model_reg.add(tf.keras.layers.Dense(units=10, activation=tf.keras.activations.relu))
# Method 2
model_reg.add(tf.keras.layers.Dense(units=10, input_shape=(1,), activation=tf.keras.activations.relu))
参数 input_shape
实际上应该是一个元组,如果您注意到我在您的示例中将 input_shape
设置为 (1,)代码> 这是一个包含单个元素的元组.由于您的数据是一维的,因此您一次传入一个元素,因此输入形状为
(1,)
.
The parameter input_shape
is actually supposed to be a tuple, if you noticed that I set the input_shape
in your example to be (1,)
this is a tuple with a single element in it. As your data is 1D, you pass in a single element at a time therefore the input shape is (1,)
.
如果您的输入数据是 2D 输入,例如在尝试根据多个变量预测房屋价格时,您将拥有多行多列数据.在这种情况下,您传入 X_reg_train
的最后一个维度的输入形状,即输入的数量.如果X_reg_train
是(1000,10)
,那么我们使用(10,)
的input_shape
.
If your input data was a 2D input for example when trying to predict the price of a house based on multiple variables, you would have multiple rows and multiple columns of data. In this case, you pass in the input shape of the last dimension of the X_reg_train
which is the number of inputs. If X_reg_train
was (1000,10)
then we use the input_shape
of (10,)
.
model_reg.add(tf.keras.layers.Dense(units=10, input_shape=(X_reg_train.shape[1],), activation=tf.keras.activations.relu))
暂时忽略batch_size
,我们实际上只是发送一行数据来预测单个房价.batch_size
只是在这里将多行数据分块在一起,这样我们就不必将整个数据集加载到计算成本高的内存中,所以我们发送小块,默认值为 32
.运行训练时,您会注意到,在每个时期下,它表示 5/5
表示您拥有的 5 批
数据,因为训练大小为 150
, 150/32 = 5(四舍五入)
.
Ignoring the batch_size
for a moment, with this we are actually just sending a single row of the data to predict a single house price. The batch_size
is just here to chunk multiple rows of data together so that we do not have to load the entire dataset into memory which is computationally expensive, so we send small chunks, with the default value being 32
. When running the training you would have noticed that under each epoch it says 5/5
which are for the 5 batches
of data you have, since the training size is 150
, 150 / 32 = 5(rounded up)
.
对于带有 Dense
层的 3D input
,它实际上只是扁平化为 2D input
,即来自 (batch_size, sequence_length, 昏暗) ->(batch_size * sequence_length, dim) ->(batch_size, sequence_length, hidden_units)
与使用 Conv1D
层相同,kernel
为 1
.所以在这种情况下我什至不会使用 Dense
层.
For 3D input
with the Dense
layer it actually just gets flattened to a 2D input
, i.e. from (batch_size, sequence_length, dim) -> (batch_size * sequence_length, dim) -> (batch_size, sequence_length, hidden_units)
which is the same as using a Conv1D
layer with a kernel
of 1
. So I wouldn't even use the Dense
layer in this case.
这篇关于keras Tensorflow 中 InputLayer 的输入形状是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!