如何在keras模型中正确定义input_shape

发布于 2025-01-19 11:39:30 字数 2484 浏览 1 评论 0原文

张量流新手。

以下是我正在处理的数据集:

abalone_train = pd.read_csv(
    "https://storage.googleapis.com/download.tensorflow.org/data/abalone_train.csv",
    names=["Length", "Diameter", "Height", "Whole weight", "Shucked weight",
           "Viscera weight", "Shell weight", "Age"])

abalone_train.head()

abalone_cols = abalone_train.columns
y_train = abalone_train[abalone_cols[-1]]
x_train = abalone_train[abalone_cols[:-1]]

我尝试了模型的 2 次迭代:

第一次迭代:

 model = tf.keras.models.Sequential([
    tf.keras.layers.InputLayer(input_shape = (None,7)),
    tf.keras.layers.Dense(20, activation='relu'),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(2, activation = 'relu'),
    tf.keras.layers.Dense(1, activation = 'relu'),
    ]
)

model.compile(optimizer = 'sgd', loss = 'mean_squared_error')


x_train_np = np.array(x_train)
y_train_np = np.array(y_train)

modelcheck = model.fit(x_train_np, y_train_np, epochs = 5)

第二次迭代:

与第一次迭代类似,但我只更改了 input_shape:


model = tf.keras.models.Sequential([
    tf.keras.layers.InputLayer(input_shape = (7,)),
    tf.keras.layers.Dense(20, activation='relu'),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(2, activation = 'relu'),
    tf.keras.layers.Dense(1, activation = 'relu'),
    ]
)


model.compile(optimizer = 'sgd', loss = 'mean_squared_error')


x_train_np = np.array(x_train)
y_train_np = np.array(y_train)

modelcheck = model.fit(x_train_np, y_train_np, epochs = 5)


看起来在第一次迭代中,我得到了 108.0 的恒定损失迭代和纪元:

104/104 [================================] - 1s 4ms/步 - 损失: 108.2235 纪元 2/5 104/104 [==============================] - 0s 4ms/步 - 损耗:108.2235 纪元 3/5 104/104 [==============================] - 0s 4ms/步 - 损耗:108.2235 纪元 4/5 104/104 [==============================] - 0s 4ms/步 - 损耗:108.2235 纪元 5/5 104/104 [==============================] - 0s 4ms/步 - 损耗:108.2235

在第二个中,代码工作正常,但我的损失如下:

Epoch 1/5 104/104 [==============================] - 1s 5ms/步 - 损耗:13.9729 纪元 2/5 104/104 [==============================] - 0s 4ms/步 - 损耗:8.0497 纪元 3/5 104/104 [==============================] - 0s 4ms/步 - 损耗:7.4067 纪元 4/5 104/104 [==============================] - 0s 4ms/步 - 损耗:6.9215 纪元 5/5 104/104 [================================] - 0s 5ms/步 - 损失:6.5436

我似乎没有了解 keras 如何以不同的方式处理这两个迭代。根据我的阅读,即使我在开头加上“无”,也没关系,因为它是“batch_size”。

我在这里错过了什么吗?任何指导都会非常有帮助!

New to tensorflow.

Following is the datasets I am working on:

abalone_train = pd.read_csv(
    "https://storage.googleapis.com/download.tensorflow.org/data/abalone_train.csv",
    names=["Length", "Diameter", "Height", "Whole weight", "Shucked weight",
           "Viscera weight", "Shell weight", "Age"])

abalone_train.head()

abalone_cols = abalone_train.columns
y_train = abalone_train[abalone_cols[-1]]
x_train = abalone_train[abalone_cols[:-1]]

I tried 2 iterations of model:

1st iteration:

 model = tf.keras.models.Sequential([
    tf.keras.layers.InputLayer(input_shape = (None,7)),
    tf.keras.layers.Dense(20, activation='relu'),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(2, activation = 'relu'),
    tf.keras.layers.Dense(1, activation = 'relu'),
    ]
)

model.compile(optimizer = 'sgd', loss = 'mean_squared_error')


x_train_np = np.array(x_train)
y_train_np = np.array(y_train)

modelcheck = model.fit(x_train_np, y_train_np, epochs = 5)

2nd iteration:

Similar to 1st one, but I only changed the input_shape:


model = tf.keras.models.Sequential([
    tf.keras.layers.InputLayer(input_shape = (7,)),
    tf.keras.layers.Dense(20, activation='relu'),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(2, activation = 'relu'),
    tf.keras.layers.Dense(1, activation = 'relu'),
    ]
)


model.compile(optimizer = 'sgd', loss = 'mean_squared_error')


x_train_np = np.array(x_train)
y_train_np = np.array(y_train)

modelcheck = model.fit(x_train_np, y_train_np, epochs = 5)


It looks like that in the first iteration, I get constant loss of 108.0 across iterations and epochs:

104/104 [==============================] - 1s 4ms/step - loss: 108.2235
Epoch 2/5
104/104 [==============================] - 0s 4ms/step - loss: 108.2235
Epoch 3/5
104/104 [==============================] - 0s 4ms/step - loss: 108.2235
Epoch 4/5
104/104 [==============================] - 0s 4ms/step - loss: 108.2235
Epoch 5/5
104/104 [==============================] - 0s 4ms/step - loss: 108.2235

In the 2nd one, the code is working fine and I am getting a loss as follows:

Epoch 1/5
104/104 [==============================] - 1s 5ms/step - loss: 13.9729
Epoch 2/5
104/104 [==============================] - 0s 4ms/step - loss: 8.0497
Epoch 3/5
104/104 [==============================] - 0s 4ms/step - loss: 7.4067
Epoch 4/5
104/104 [==============================] - 0s 4ms/step - loss: 6.9215
Epoch 5/5
104/104 [==============================] - 0s 5ms/step - loss: 6.5436

I don't seem to understand how keras is treating these two iterations differently. From what I have read, even if I put 'None' at the beginning, it should not matter as it is the 'batch_size'.

Am I missing something here?! Any guidance would be really helpful!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2025-01-26 11:39:30

在输入层中,您没有定义批量大小。您只需定义输入的形状,不包括批量大小。 Keras 会自动在每层形状的前面添加 None 值,该值稍后会替换为批量大小。

因此,在第一次迭代中,您的输入形状不正确。您希望将 7 个输入放在形状 (7, 1) 的向量中,因为您的数据由 7 个元素的行组成。因此,正确的输入形状是(7,)。

In the input layer you don't define the batch size. You just define the shape of the input, excluding the batch size. Keras automatically adds the None value in the front of the shape of each layer, which is later replaced by the batch size.

So in the 1st iteration, you have an incorrect input shape. You want to have the 7 inputs in a vector of shape (7, 1) because your data is made up of rows of 7 elements. Thus, the correct input shape is (7,).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文