2017-04-13 110 views
14

我試圖建立一層CNN,但我遇到了一些問題。 事實上,compilator說我conv1D中形狀的尺寸

ValueError: Error when checking model input: expected conv1d_1_input to have 3 dimensions, but got array with shape (569, 30)

這是代碼

import numpy 
from keras.models import Sequential 
from keras.layers.convolutional import Conv1D 
numpy.random.seed(7) 
datasetTraining = numpy.loadtxt("CancerAdapter.csv",delimiter=",") 
X = datasetTraining[:,1:31] 
Y = datasetTraining[:,0] 
datasetTesting = numpy.loadtxt("CancereEvaluation.csv",delimiter=",") 
X_test = datasetTraining[:,1:31] 
Y_test = datasetTraining[:,0] 
model = Sequential() 
model.add(Conv1D(2,2,activation='relu',input_shape=X.shape)) 
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 
model.fit(X, Y, epochs=150, batch_size=5) 
scores = model.evaluate(X_test, Y_test) 
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)) 

回答

35

TD; LR你需要重塑你的數據有一個空間尺寸爲Conv1d有道理:

X = np.expand_dims(X, axis=2) # reshape (569, 30) to (569, 30, 1) 
# now input can be set as 
model.add(Conv1D(2,2,activation='relu',input_shape=(30, 1)) 

本質上重塑一個數據集,看起來像這樣:

features  
.8, .1, .3 
.2, .4, .6 
.7, .2, .1 

要:

[[.8 
.1 
.3], 

[.2, 
.4, 
.6 
], 

[.3, 
.6 
.1]] 

說明與示例

正常情況下,卷積在空間維度上起作用。內核在產生張量的維度上「卷積」。在Conv1D的情況下,內核通過每個示例的「步驟」維度傳遞。

您會看到Conv1D用於NLP,其中steps是句子中的單詞數(填充到某個固定的最大長度)。這些話可能會被編碼爲長度爲4

這裏的載體是一個例句:

jack .1 .3 -.52 | 
is  .05 .8, -.7 |<--- kernel is `convolving` along this dimension. 
a  .5 .31 -.2 | 
boy .5 .8 -.4 \|/ 

,我們將設定在這種情況下,輸入到卷積方式:

maxlen = 4 
input_dim = 3 
model.add(Conv1D(2,2,activation='relu',input_shape=(maxlen, input_dim)) 

在您的情況下,您會將這些功能視爲空間尺寸,每個功能的長度爲1.(請參閱下文)

這裏將是您的數據集中的一個示例

att1 .04 | 
att2 .05 | < -- kernel convolving along this dimension 
att3 .1  |  notice the features have length 1. each 
att4 .5 \|/  example have these 4 featues. 

,我們將設置Conv1D例子爲:

maxlen = num_features = 4 # this would be 30 in your case 
input_dim = 1 # since this is the length of _each_ feature (as shown above) 

model.add(Conv1D(2,2,activation='relu',input_shape=(maxlen, input_dim)) 

當你看到你的數據集在被重塑到(569,30,1) 使用:

X = np.expand_dims(X, axis=2) # reshape (569, 30, 1) 
# now input can be set as 
model.add(Conv1D(2,2,activation='relu',input_shape=(30, 1)) 

這裏有一個完整的例子,你可以運行(我將使用Functional API

from keras.models import Model 
from keras.layers import Conv1D, Dense, MaxPool1D, Flatten, Input 
import numpy as np 

inp = Input(shape=(5, 1)) 
conv = Conv1D(filters=2, kernel_size=2)(inp) 
pool = MaxPool1D(pool_size=2)(conv) 
flat = Flatten()(pool) 
dense = Dense(1)(flat) 
model = Model(inp, dense) 
model.compile(loss='mse', optimizer='adam') 

print(model.summary()) 

# get some data 
X = np.expand_dims(np.random.randn(10, 5), axis=2) 
y = np.random.randn(10, 1) 

# fit model 
model.fit(X, y) 
+0

如果我有尺寸爲1x690的數據, Conv1D圖層有40個內核大小爲3的濾鏡,當我查找該圖層的權重時,它說我有40 * 690 * 3的權重。我不確定我是否理解這是爲什麼,我認爲我只有40 * 3的重量?它如何輸出1x40形狀? – jerpint

1

沒有能夠看到更詳細的數據是不是在預處理後的正確的形狀。
整形X爲具有3個維度:

np.reshape(X, (1, X.shape[0], X.shape[1])) 
+0

我的數據集由30個屬性,2個類和569個值組成。 我不明白我在哪裏重塑我的X – protti

+0

所以你的數組'0'和'1'的值是什麼? –

+0

在X數組中我有屬性的值,在YI中只有0和1. X的形狀是(569,30),而Y是(569,) – protti