conv1D 中的形状尺寸 [英] Dimension of shape in conv1D

查看:32
本文介绍了conv1D 中的形状尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试用一层构建 CNN,但我遇到了一些问题.确实,编译器告诉我

I have tried to build a CNN with one layer, but I have some problem with it. Indeed, the compilator says me that

ValueError:检查模型输入时出错:预期的 conv1d_1_input有 3 个维度,但得到了形状为 (569, 30) 的数组

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("
%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

推荐答案

td;lr 你需要重塑你的数据,让 Conv1d 有一个 空间 维度才有意义:

td; lr you need to reshape you data to have a spatial dimension for Conv1d to make sense:

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

本质上重塑如下所示的数据集:

Essentially reshaping a dataset that looks like this:

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

致:

[[.8
.1
.3],

[.2,
 .4,
 .6
 ],

[.7,
 .2,
 .1]]
 

说明和示例

通常卷积适用于空间维度.内核是卷积的"在产生张量的维度上.在 Conv1D 的情况下,内核通过每个示例的步骤"维度.

Normally convolution works over spatial dimensions. The kernel is "convolved" over the dimension producing a tensor. In the case of Conv1D, the kernel is passed over the 'steps' dimension of every example.

您将看到 NLP 中使用的 Conv1D,其中 steps 是句子中的多个单词(填充到某个固定的最大长度).这些词将被编码为长度为 4 的向量.

You will see Conv1D used in NLP where steps is a number of words in the sentence (padded to some fixed maximum length). The words would be encoded as vectors of length 4.

这是一个例句:

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

在这种情况下,我们将输入设置为 conv 的方式:

And the way we would set the input to the conv in this case:

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

在您的情况下,您会将特征视为每个特征长度为 1 的空间维度.(见下文)

In your case, you will treat the features as the spatial dimensions with each feature having length 1. (see below)

这是您数据集中的一个示例

Here would be an example from your dataset

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 示例设置为:

And we would set the Conv1D example as:

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)使用:

As you see your dataset has to be reshaped in to (569, 30, 1) use:

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

这是一个可以运行的完整示例(我将使用 功能API)

Here is a full-fledged example that you can run (I'll use the 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)

这篇关于conv1D 中的形状尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆