2017-04-10 93 views
0

我有以下錯誤的語句...ValueError異常:錯誤檢查時模型的目標:預計activation_2有形狀(無,761,1),但得到陣列形狀(1,779,1)

ValueError: Error when checking model target: expected activation_2 to have shape (None, 761, 1) but got array with shape (1, 779, 1) 

出現錯誤時,我不知道761是什麼意思,我的data1的形狀是779 * 80,我的data3的形狀是779 * 1。感謝您的幫助!

from __future__ import print_function 
from keras.preprocessing.image import ImageDataGenerator 
from keras.models import Sequential 

from keras.layers import Dense, \ 
         Dropout, \ 
         Activation, \ 
         Flatten 

from keras.layers import Convolution1D, \ 
         MaxPooling2D, \ 
         Convolution2D 

from keras.utils import np_utils 

import scipy.io as sio 
import numpy as np 

matfn = 'LIVE_data.mat' 

data = sio.loadmat(matfn) 
data0 = data['data'] 
data1 = np.ones((1, 779, 80)) 
data1[0, :, :] = data0 
data00 = data['label'] 
data2 = np.ones((1,779,1)) 
data2[0, :, :] = data00 
data000 = data['ref_ind_live'] 
data3 = np.ones((1, 779, 1)) 
data3[0, :, :] = data000 
batch_size = 64 
nb_classes = 30 
nb_epoch = 50 

X_train = data1 
y_train = data3 
X_test = data1[0, :] 
y_test = data3[0, :] 

X_train = X_train.astype('double') 
X_test = X_test.astype('double') 
X_train /= 255 
X_test /= 255 

# Convert class vectors to binary class matrices. 
Y_train = np_utils.to_categorical(y_train, nb_classes) 
Y_test = np_utils.to_categorical(y_test, nb_classes) 

model = Sequential() 

model.add(Convolution1D(32, \ 
         10, \ 
         border_mode = 'same', \ 
         input_shape = (779, \ 
             80))) 
model.add(Activation('relu')) 
model.add(Convolution1D(64, \ 
         10, \ 
         activation='relu')) 
model.add(Dropout(0.25)) 
model.add(Convolution1D(128, \ 
         10, \ 
         activation='relu')) 
model.add(Dropout(0.5)) 
model.add(Dense(1)) 
model.add(Activation('softmax')) 

# Let's train the model using RMSprop 
model.compile(loss = 'categorical_crossentropy', \ 
       optimizer = 'rmsprop', \ 
       metrics=['accuracy']) 

print("start train") 

model.fit(X_train, \ 
      Y_train, \ 
      batch_size = batch_size, \ 
      nb_epoch = nb_epoch, \ 
      shuffle = True) 

print("end") 

score = model.evaluate(X_test, \ 
         Y_test, \ 
         batch_size = 32) 

print('Test score:', \ 
     score[0]) 
print('Test accuracy:', \ 
     score[1]) 
+0

我已經修改了你的佈局,使其更容易調試。你的舊佈局是好的,但不一致的應用。另外,你的價值觀,經營者等常常被混淆在一起。這並不是最佳選擇,因爲不分離的元素可能會讓某些人閱讀您的代碼,特別是如果他們不熟悉您的佈局風格或某些使用的技術,有時甚至是他們時。要做到這一點,你應該像我用'data1 = np.ones((1,779,80))'和下面的25行一樣分開它們,或者如我已經完成的其餘代碼一樣。 – toonice

+0

有關如何在Python中執行間距的更多信息,請參閱http://stackoverflow.com/questions/9714161/spaces-in-python-coding-style和http://stackoverflow.com/questions/4172448/is-it - 可能致斷-A-長的線路到多線式-蟒。 – toonice

回答

0

你的模型輸出形狀是(779, 1)和相同的形狀在最終層預期,但由於2卷積運算的是減少到761.因此,通過在其它2卷積層加入border_mode = 'same'會解決這個問題。

可以在模型彙總檢查:


層(類型)輸出形狀帕拉姆#

conv1d_1(Conv1D)(無,779,32)25632


activation_1 (激活)(無,779,32)0


conv1d_2(Conv1D)(無,770,64)20544


dropout_1(降)(無,770,64)0


conv1d_3(Conv1D)(無,761,128 )82048


dropout_2(降)(無,761,128)0


dense_13(密集)(無,761,1)129


activation_2(激活)(無,761,1)0

相關問題