您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

conv1D中的形状尺寸

conv1D中的形状尺寸

你需要重塑你的数据有一个 空间 维度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,在每个示例的“步骤”维度上传递内核。

您将看到NLP中使用的Conv1D,其中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)
其他 2022/1/1 18:36:39 有208人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶