簡體   English   中英

ValueError:檢查輸入時出錯:預期conv3d_1_input具有5個維,但數組的形狀為(7,9,384,1)

[英]ValueError: Error when checking input: expected conv3d_1_input to have 5 dimensions, but got array with shape (7, 9, 384, 1)

我有張量流作為后端。 我在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)

keras層期望的input_shape是每個樣本。 因此,x的形狀應比input_shape大一維。 我不知道7在您的數據中的含義,但是如果這是樣本數,那么您不應該將其包含在input_shape中,因此input_shape變為:

(9,boundIndex,1)。

如果您僅訓練1個樣本(由於某種原因),則可以將x重塑為:

(1,7,9,boundIndex,1)

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM