2017-04-13 4 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 단계;

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 당신은 공간 차원이 당신에게 데이터를 바꿀 필요

설명 및 예

일반적으로 컨볼 루션은 공간 차원에서 작동합니다. 커널은 텐서를 생성하는 차원에서 "컨볼 루션 (convolved)"합니다. Conv1D의 경우, 커널은 모든 예제의 'steps'차원으로 전달됩니다.

steps은 문장의 단어 수 (고정 된 최대 길이로 채워짐) 인 NLP에서 Conv1D가 사용 된 것을 볼 수 있습니다.

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)) 
을 단어가 여기에

길이 4의 벡터로 인코딩 할 수있는 것이라고하는 것은 예를 들어 문장이다

귀하의 경우에는 길이가 1 인 각 피처의 공간 치수로 피처를 처리합니다. (아래 참조)

여기에 데이터 세트

01의 예제가 있습니다. 23,516,
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 인 데이터는 커널 크기가 3 인 40 개의 필터가있는 Conv1D 레이어를 보면 레이어의 가중치를 보면 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