python - Keras CNN 错误 : expected Sequence to have 3 dimensions, 但获得形状为 (500, 400) 的数组

标签 python machine-learning keras conv-neural-network

我收到此错误:

ValueError: Error when checking input: expected Sequence to have 3 dimensions, but got array with shape (500, 400)

这些是我正在使用的以下代码。

print(X1_Train.shape)
print(X2_Train.shape)
print(y_train.shape)

输出(这里每行有 500 行):

(500, 400)
(500, 1500)
(500,)

400 => timesteps (below)
1500 => n (below)

代码:

timesteps = 50 * 8
n = 50 * 30

def createClassifier():
    sequence = Input(shape=(timesteps, 1), name='Sequence')
    features = Input(shape=(n,), name='Features')

    conv = Sequential()
    conv.add(Conv1D(10, 5, activation='relu', input_shape=(timesteps, 1)))
    conv.add(Conv1D(10, 5, activation='relu'))
    conv.add(MaxPool1D(2))
    conv.add(Dropout(0.5))

    conv.add(Conv1D(5, 6, activation='relu'))
    conv.add(Conv1D(5, 6, activation='relu'))
    conv.add(MaxPool1D(2))
    conv.add(Dropout(0.5))
    conv.add(Flatten())
    part1 = conv(sequence)

    merged = concatenate([part1, features])

    final = Dense(512, activation='relu')(merged)
    final = Dropout(0.5)(final)
    final = Dense(num_class, activation='softmax')(final)

    model = Model(inputs=[sequence, features], outputs=[final])
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

model = createClassifier()
# print(model.summary())
history = model.fit([X1_Train, X2_Train], y_train, epochs =5)

有什么见解吗?

最佳答案

有两件事 -

Conv1D 层期望输入的形状为 (batch_size,x,filters),在您的情况下为 (500,400,1)
您需要 reshape 输入层的形状,添加另一个大小为 1 的轴。(这不会改变数据中的任何内容)。

您正在尝试使用多个输入,顺序 API 不是最佳选择。我建议使用Functional API

编辑: 关于您的评论,不确定您做错了什么,但这是代码的工作版本(带有虚假数据),并进行了 reshape :

import keras

import numpy as np



X1_Train = np.ones((500,400))
X2_Train = np.ones((500,1500))
y_train = np.ones((500))
print(X1_Train.shape)
print(X2_Train.shape)
print(y_train.shape)

num_class = 1


timesteps = 50 * 8
n = 50 * 30

def createClassifier():
    sequence = keras.layers.Input(shape=(timesteps, 1), name='Sequence')
    features = keras.layers.Input(shape=(n,), name='Features')

    conv = keras.Sequential()
    conv.add(keras.layers.Conv1D(10, 5, activation='relu', input_shape=(timesteps, 1)))
    conv.add(keras.layers.Conv1D(10, 5, activation='relu'))
    conv.add(keras.layers.MaxPool1D(2))
    conv.add(keras.layers.Dropout(0.5))

    conv.add(keras.layers.Conv1D(5, 6, activation='relu'))
    conv.add(keras.layers.Conv1D(5, 6, activation='relu'))
    conv.add(keras.layers.MaxPool1D(2))
    conv.add(keras.layers.Dropout(0.5))
    conv.add(keras.layers.Flatten())
    part1 = conv(sequence)

    merged = keras.layers.concatenate([part1, features])

    final = keras.layers.Dense(512, activation='relu')(merged)
    final = keras.layers.Dropout(0.5)(final)
    final = keras.layers.Dense(num_class, activation='softmax')(final)

    model = keras.Model(inputs=[sequence, features], outputs=[final])
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

model = createClassifier()
# print(model.summary())
X1_Train = X1_Train.reshape((500,400,1))
history = model.fit([X1_Train, X2_Train], y_train, epochs =5)

输出:

Using TensorFlow backend.
(500, 400)
(500, 1500)
(500,)
Epoch 1/5
500/500 [==============================] - 1s 3ms/step - loss: 1.1921e-07 - acc: 1.0000
Epoch 2/5
500/500 [==============================] - 0s 160us/step - loss: 1.1921e-07 - acc: 1.0000
Epoch 3/5
500/500 [==============================] - 0s 166us/step - loss: 1.1921e-07 - acc: 1.0000
Epoch 4/5
500/500 [==============================] - 0s 154us/step - loss: 1.1921e-07 - acc: 1.0000
Epoch 5/5
500/500 [==============================] - 0s 157us/step - loss: 1.1921e-07 - acc: 1.0000

关于python - Keras CNN 错误 : expected Sequence to have 3 dimensions, 但获得形状为 (500, 400) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53465066/

相关文章:

python - 如何使用 AutoKeras 获得可重复的结果

python - 使用用户的个人数据计算相似度?

python-2.7 - 父树 : Expected a node value and child list or a single string or 'ParentedTree' object has no attribute 'label'

tensorflow - 带有验证数据的 keras model.fit - 哪个 batch_size 用于评估验证数据?

python - 如何在自定义 Keras 模型函数中共享层权重

machine-learning - 在 iPython 控制台中保存脚本运行之间的参数

python - 如何使用 .apply() 将一列字典合并为一个字典?

Python - 动态调用具有动态数量参数的方法。反射?

python - numpy 数组中随机选择的索引

python - 决策树显示没有达到我想要的效果