【问题标题】:Fitting 3D data as Input into Keras Sequential Model Layer将 3D 数据作为输入拟合到 Keras 序列模型层
【发布时间】:2018-02-06 10:50:18
【问题描述】:

我是机器学习和 Keras 的新手。实际上,我使用过 scikit-learn,但 Keras 似乎有点复杂。我的问题是我有一些 3D 数据并希望将其放入密集层(我也尝试过使用 Conv2D 和 Conv1D 层)。 我所做的如下:

arr1 = np.random.random((30,2))
arr2 = np.random.random((30,2))
arr3 = np.random.random((30,2))
arr4 = np.random.random((30,2))
arr5 = np.random.random((30,2))
arr6 = np.random.random((30,2))

x_matrix = np.dstack(
    (arr1
    ,arr2
    ,arr3
    ,arr4
    ,arr5
    ,arr6)
).swapaxes(1,2)
print(x_matrix.shape)

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x_matrix, y_matrix, test_size=0.33, random_state=42)

from keras.models import Sequential
model = Sequential()

from keras.layers import Dense, Conv2D, Conv1D, Flatten

model = Sequential()

model.add(Dense(6, activation='sigmoid', input_shape=(6,2)))

model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

model.fit(np.array(X_train), np.array(y_train), epochs=20, batch_size=1)#
score = model.evaluate(X_test, y_test)

print(score)

而且我在 fit 步骤中遇到了错误。错误如下:

ValueError: Error when checking target: expected dense_1 to have 3 dimensions, but got array with shape (20, 2)

对于 Conv1D 层,我尝试了这个:

model.add(Conv1D(6, (2),  activation='sigmoid', input_shape=(6 ,2)))

并想出了错误:

ValueError: Error when checking target: expected conv1d_1 to have 3 dimensions, but got array with shape (20, 2)

Conv2D 看起来更复杂,我可能不需要它作为我的输入层,但使用下面的调用我仍然有同样的错误。

model.add(Conv2D(6, (2,2),  activation='sigmoid', input_shape=(20,6 ,2)))

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (20, 6, 2)

我要问的是:如何使用 Keras 将这样的数据拟合到神经网络中?

【问题讨论】:

  • 输入训练数据的实际形状是什么?如果它的 (20, 2) 它似乎不是 3D。
  • 现在是 (30, 6, 2)

标签: python neural-network keras conv-neural-network keras-layer


【解决方案1】:

首先,您必须了解您的数据是什么以及您想用它做什么。

然后您决定如何塑造数据以及使用哪些层。

不过有一些重要的约定:

  • 数据中的第一个维度是“样本/示例”的数量。由于您创建了一个形状 (30,6,2),因此您决定有 30 个样本,每个样本的形状为 (6,2)——这就是为什么了解您的数据和您想要做什么很重要。
  • X 和 Y 必须具有相同数量的样本。因此,如果 X 中有 30 个样本,那么 Y 中肯定也应该有 30 个样本,但您的数据似乎认为它有 20 个样本。查看消息中target 的形状:(20,2)
  • 其他维度是免费的,但是:
    • 密集层仅适用于最后一个维度,其他维度保持不变:输出形状为 (30,6,units)
    • Conv1D 层将 3D 输入解释为:(samples, length, input_channels),输出形状为 (samples, modified_length, filters)
    • Conv2D 层需要 4D 输入:(samples, width, heigth, input_channels),并将输出(samples, modified_width, modified_height, filters)
  • 模型的输出形状必须与 Y 的形状匹配。在这里,您必须再次了解 Y 是什么,并确保准备好模型以匹配它。
  • 如果在模型中的某个点需要将 3D 数据变为 2D,则需要使用 FlattenReshapeGlobalMaxPooling1DGlobalAveragePooling1D 层。

提示:使用model.summary() 查看每一层的输出形状以及最终输出形状。

提示2:首先明确定义您的数据和目标,然后是 X 和 Y 的形状,然后是模型的形状。

【讨论】:

  • 感谢所有的解释。实际上,我的样本量似乎为 20 的原因是因为我在训练中拆分数据并以 2/1 的比例进行测试。我无法弄清楚为什么我在 Conv2D 中出现形状 (20,2) 错误,但我想我正在寻找的图层是 Conv1D。我想要实现的是我的输入(X)的形状为(x,6,2),我的输出(Y)的形状为(n,2)。我想预测那些测试 Y 值。
  • 我已将数据简化为 2D 并使用密集层。我想它暂时解决了我的问题。
猜你喜欢
  • 2018-03-16
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-21
  • 1970-01-01
  • 2021-05-24
相关资源
最近更新 更多