2017-04-13 139 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