python - 将 3D 数据作为 Keras 序列模型层的输入进行拟合

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

我是机器学习和 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)

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

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 的神经网络中?

最佳答案

首先,您必须了解您的数据是什么以及您希望使用这些数据做什么。

然后您决定如何调整数据以及使用哪些图层。

不过,有一些重要的约定:

  • 数据中的第一个维度是“样本/例子”的数量。因为你创建了一个形状 (30,6,2),你决定你有 30 个样本,每个样本都有形状 (6,2) -- 这就是为什么了解你的数据和你想做什么很重要。
  • X 和 Y 必须具有相同数量的样本。因此,如果 X 中有 30 个样本,那么 Y 中肯定也应该有 30 个样本,但您的数据似乎认为它有 20 个样本。查看消息中 target 的形状:(20,2) <- 这是 Y 的形状。
  • 其他维度是免费的,但是:
    • 密集层仅在最后一个维度上起作用,其他维度保持不变:输出形状为 (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,您将需要使用 FlattenReshape GlobalMaxPooling1DGlobalAveragePooling1D 层。

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

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

关于python - 将 3D 数据作为 Keras 序列模型层的输入进行拟合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48641189/

相关文章:

python - 尽管 Shell 脚本已在 Mac OS X 登录项中注册,但它不会自动运行

python - 为 django 模型中的每个字段添加置信度值

artificial-intelligence - 在尽可能少的 "parameters"中表示 double 的二维映射

image-processing - 什么是inception和vgg16的输出维度

python - 在 Keras 中,为什么必须根据神经网络的输出来计算损失函数?

python - Tkinter get() 函数不将值保存为 float 或 int 变量

python - Gevent.monkey.patch_all 破坏依赖于 socket.shutdown() 的代码

python - Theano 逻辑回归维度不匹配

java - 使用 Neuroph 神经网络进行图像识别?

python - 将 tensorflow 梯度应用于特定输入