2017-04-13 5 views
14

1つのレイヤーで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と、

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

へ:

[[.8 
.1 
.3], 

[.2, 
.4, 
.6 
], 

[.3, 
.6 
.1]] 
基本的にはこのようになりますデータセットを再形成

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)) 

Conv1dが意味をなすためにのためにLRあなたは空間次元を持っているあなたにデータを再構築する必要があります

説明と例

通常、畳み込みは空間次元で機能します。カーネルは、テンソルを生成する次元にわたって「畳み込まれている」。 Conv1Dの場合、カーネルはすべての例の 'steps'ディメンションに渡されます。

stepsが文中の単語の数であるNLPでConv1Dが使用されていることがわかります(一定の最大長に埋められます)。言葉はここでは長さのベクトル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例を設定します:あなたはあなたのデータセットがへに再形成されなければならわかるように(569、30、1)

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)) 

使用:

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のデータがある場合は、カーネルサイズ3の40個のフィルタを持つConv1Dレイヤーでは、そのレイヤーの重みを調べると、40 * 690 * 3の重みがあると言います。私はなぜこれが理解できるか分からない、私は40 * 3体重しか持たないだろうと思った?どのように1x40の形状を出力しますか? – jerpint

1

であることを私に言います。三次元持って
リシェイプX:

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

関連する問題