ValueError:检查输入时出错:预期conv3d_1_input有5个维度,但得到的数组有形状(7,9,384,1)

时间:2018-03-15 17:14:45

标签: python tensorflow keras conv-neural-network

我有张量流作为后端。我在keras中使用3D卷积层。我的最终x训练数据是形状(7,9,384,1),通道等于1,y训练数据是形状(7,1,384,1)。运行model.fit()时,我不断收到此错误。

我检查了大多数在线发布的相关问题,但他们都关注的是它是作为后端的theaon还是tensorflow。其中一些建议扩展尺寸,但仍然不起作用,并出现一些其他问题。

根据keras文档,3D卷积应该具有5D输入形状,并且我缺少第一维样本。我只有这一个3D数据输入(形状7,9,384),我尝试在第一层中的input_shape参数的开头添加1,这将导致另一个问题,即我在输入形状中超出了一个维度

任何人都可以看看并告诉我什么是错的?非常感谢你!

import numpy as np
from keras.models import Sequential
from keras.layers import *

# Read the array from disk
data = np.loadtxt('D:/8_CNN_pycharm/data.txt', delimiter=',')
x = data[:,1:10]
y = data[:,0]

rownum = np.size(x,0)
# boundIndex = 384
boundIndex = int(rownum/7/3*2)  
x_train = x[0:7*boundIndex,:]
y_train = y[0:7*boundIndex]
x_test = x[7*boundIndex:,:]
y_test = y[7*boundIndex:]
print(rownum)
print(x_train.shape[0])
print(x_test.shape)

# Going to 3D array using desired shape of the array
new_x_train = x_train.reshape(7,9,boundIndex,1)
# new_x_train = new_x_train.reshape(new_x_train.shape[0],7,9,boundIndex,1)
# new_x_train = np.expand_dims(new_x_train,axis=0)

new_y_train = y_train.reshape(7,1,boundIndex,1)
# new_y_train = np.expand_dims(new_y_train,axis=0)
print(new_x_train.shape[:])
# x_input = Input(shape=(7,9,boundIndex, 1))

new_x_test = x_test.reshape((7,9,int(rownum/7-boundIndex)))
new_y_test = y_test.reshape((7,1,int(rownum/7-boundIndex)))

model = Sequential()
# model.add(Conv2D(64, (4,3), padding='same', activation='relu', input_shape=(7,9,boundIndex)))
model.add(Conv3D(64, (4,3,2), padding='same', activation='relu', input_shape=(7,9,boundIndex,1)))
model.add(Conv3D(32, (4,3,2), activation='relu'))
model.add(MaxPooling3D(pool_size=(2,2,2), padding='valid'))
model.add(Conv3D(16, (2,2,2), activation='relu'))
model.add(GlobalAveragePooling3D())
model.add(Dense(7, activation='relu'))

model.compile(loss='mean_squared_error', optimizer='sgd', metrics=['accuracy'])
model.fit(new_x_train, new_y_train, batch_size=16, epochs=10)
score = model.evaluate(new_x_test, new_y_test, batch_size=16)

错误讯息:

Traceback (most recent call last):   File "E:\Program Files\JetBrains\PyCharm Community Edition
2017.2.3\helpers\pydev\pydevd.py", line 1599, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)   File "E:\Program Files\JetBrains\PyCharm Community Edition
2017.2.3\helpers\pydev\pydevd.py", line 1026, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script   File "E:\Program Files\JetBrains\PyCharm Community Edition
2017.2.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)   File "E:/CNN/myCNN/myFirstCNN.py", line 67, in <module>
    model.fit(new_x_train, new_y_train, batch_size=16, epochs=10)   File "C:\Users\dell\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\models.py", line 963, in fit
    validation_steps=validation_steps)   File "C:\Users\dell\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1630, in fit
    batch_size=batch_size)   File "C:\Users\dell\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1476, in _standardize_user_data
    exception_prefix='input')   File "C:\Users\dell\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 113, in _standardize_input_data
    'with shape ' + str(data_shape)) ValueError: Error when checking input: expected conv3d_1_input to have 5 dimensions, but got array with shape (7, 9, 384, 1)

1 个答案:

答案 0 :(得分:2)

keras层所需的input_shape是每个样本。所以x的形状应该比input_shape大一维。我不知道数据中7的含义,但是如果这是样本数而不是你不应该在input_shape中包含它,那么input_shape就会变成:

(9,boundIndex,1)。

如果您只训练1个样本(由于某种原因),您可以将x重塑为:

(1,7,9,boundIndex,1)

希望这有帮助!