【问题标题】:Why I cannot feed my Keras model in batches?为什么我不能批量喂我的 Keras 模型?
【发布时间】:2021-10-15 15:55:11
【问题描述】:

我正在尝试分批提供 Sequential 模型。为了重现我的示例,假设我的数据是:

X = np.random.rand(432,24,1)
Y = np.random.rand(432,24,1)

我的目标是批量输入模型。一次 24 个点(24 x 1 矢量),432 次。

我将模型构建为:

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=12)

model = keras.Sequential([
    #keras.layers.Flatten(batch_input_shape=(None, 432, 2)),
    keras.layers.Dense(64, activation=tf.nn.relu),
    keras.layers.Dense(2, activation=tf.nn.sigmoid),
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

history = model.fit(X_train, y_train, epochs=200, batch_size=32, validation_split=0.3)
test_loss, test_acc = model.evaluate(X_test, y_test)
print('Model loss:', test_loss, 'Model accuracy: ', test_acc)

但是,我收到此错误:

ValueError:dense_25 层的输入 0 与该层不兼容:输入形状的预期轴 -1 具有值 864,但已接收 带形状的输入(无,432)

【问题讨论】:

  • 数据集中单个数据条目的形状是什么?
  • 这是第一批24点。有 432 个这样的: array([296, 87, 280, 87, 320, 87, 280, 95, 336, 118, 352, 118, 288, 158, 280, 214, 336, 214, 280, 285, 280 , 285, 288, 55])
  • 你为什么要扁平化你的输入?不是已经shape(batch_size, 24)了吗?
  • @Lash 您仍在将数据维度与点数混合在一起。批次和数据点不应处于不同的维度;批次是通过沿第一维切片数据获得的。
  • 你是对的。我不必把它弄平。但是输出不是我想要的。当我预测它给我一个形状数组(24,1)。我想要的是:对于模型获得的每批 24 个点,一个输出(0 或 1)。现在我每批 24 个点得到 24 个输出。

标签: python tensorflow machine-learning keras deep-learning


【解决方案1】:

我不太确定你想做什么,但这里有一个工作示例:

import tensorflow as tf
from sklearn.model_selection import train_test_split

X = np.random.rand(432, 24)
Y = np.random.randint(2, size=(432, 2))

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=12)

model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation=tf.nn.relu),
    tf.keras.layers.Dense(2, activation=tf.nn.sigmoid),
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

history = model.fit(X_train, y_train, epochs=200, batch_size=32, validation_split=0.3)
test_loss, test_acc = model.evaluate(X_test, y_test)
print('Model loss:', test_loss, 'Model accuracy: ', test_acc)

请注意,您的数据X 的形状为(432, 24),而您的标签Y 的形状为(432, 2)。我删除了您的Flatten 图层,因为如果您的数据具有(432, 24) 的形状,这没有多大意义。您可以在训练模型后进行预测,如下所示:

X_new = np.random.rand(1, 24)
Y_new = model.predict(X_new)
print(Y_new)

【讨论】:

  • 为什么 Flatten 层在这里没有多大意义?因为我是密集层,所以输入数据 X 应该具有形状 (Batch_size, number_of_input_features) 而输出数据 Y 应该具有形状 (Batch_size, number_of_output_features)?那是对的吗?您建议在哪些情况下使用 Flatten 层?
  • Flatten 在您想将 2D 张量或 3D 张量转换为 1D 张量时很有用。在这种情况下,Flatten 在这里没有意义,因为它已经是一维的(batch_size, features)。
【解决方案2】:

我认为您的输入数据的维度存在一些混淆。我将假设 432 是点数,每个数据点的维数为 24(即 24 个特征)。在这种情况下,第一个维度应该索引这些点,因为 scikit-learn 和 keras 都希望这样。例如,

X = np.random.rand(432, 24)
Y = np.random.rand(432, 24)

如果您相应地更正输入形状,您的代码应该会运行,

model = keras.Sequential([
    keras.layers.Flatten(batch_input_shape=(None, 24)),
    keras.layers.Dense(64, activation=tf.nn.relu),
    keras.layers.Dense(2, activation=tf.nn.sigmoid),
])

【讨论】:

  • 您好,感谢您的回复。您的解决方案提出:ValueError:dense_61 层的输入 0 与该层不兼容:输入形状的预期轴 -1 具有值 24,但接收到的输入具有形状(无,432)。我的数据由 432 批每批 24 个点组成(24x1 向量)。
  • 我对我写的内容做出了明确的假设,但您列出的问题是针对不同的形状配置,在这种情况下您需要进行明显的更改。
猜你喜欢
  • 1970-01-01
  • 2017-03-27
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 2019-02-07
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多