Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

python - 如何在PyTorch中为数字数据正确实现一维CNN?(How to properly implement 1D CNN for numerical data in PyTorch?)

I have a 500x2000 matrix, where each row represents an individual and each column is a measurement of some particular quality about that individual.

(我有一个500x2000的矩阵,其中每一行代表一个个体,每一列代表对该个体的某些特定质量的度量。)

I'm using a batch size of 64, so the input for each cycle of the network is actually a 64x2000 matrix.

(我使用的批处理大小为64,因此网络每个周期的输入实际上是64x2000矩阵。)

I'm trying to build a CNN in PyTorch to classify individuals given a set of these measurements.

(我正在尝试在PyTorch中构建CNN,以便根据一组这些度量对个人进行分类。)

However, I've stumbled on the parameters for the convolutional layer.

(但是,我偶然发现了卷积层的参数。)

Below is my current definition for a simple convolutional neural network.

(以下是我对简单卷积神经网络的当前定义。)

class CNNnet(nn.Module)
    def __init__(self):
        self.conv1 = nn.Conv1d(2000, 200, (1,2), stride=10)
        self.pool = nn.MaxPool1d(kernel_size = (1, 2), stride = 2)

        self.fc1 = nn.Linear(64, 30)
        self.fc2 = nn.Linear(30, 7)

    def forward(self, x):
        x = x.view(64, 2000, 1)
        x = F.relu(self.conv1(x))
        x = self.pool(x)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

Attempting to train this model produces the following error:

(尝试训练此模型会产生以下错误:)

"RuntimeError: Expected 4-dimensional input for 4-dimensional weight 200 2000 1 2, but got 3-dimensional input of size [64, 2000, 1] instead".

(“ RuntimeError:4维权重为200 2000 1 2的预期4维输入,但尺寸为[64,2000,1]的3维输入”。)

I'm confused on why it's expecting a 4D 200x2000x1x2 matrix (shouldn't the number of output channels be irrelevant to the input? And why is there a 2 at the end?).

(我对为什么期望使用4D 200x2000x1x2矩阵感到困惑(输出通道的数量与输入无关吗?为什么末尾有2?)。)

My question is what would be the proper syntax or approach for writing a CNN (specifically the convolutional layer) when dealing with 1D data.

(我的问题是在处理一维数据时编写CNN(特别是卷积层)的正确语法或方法是什么?)

Any help is greatly appreciated.

(任何帮助是极大的赞赏。)

  ask by AldehydeDeva translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So the kernel size in the 1 dimensional case is simply a vector.

(因此,在一维情况下的内核大小仅仅是一个向量。)

So if you'll want a kernel of size '1X2' you need to specify the '2' In the 2 dimensional case 2 will mean a '2X2' kernel size.

(因此,如果您想要的内核大小为“ 1X2”,则需要指定“ 2”。在二维情况下2表示内核大小为“ 2X2”。)

You gave a tuple of 2 values so you use 2 kernel types each will create its own channel

(您给了一个包含2个值的元组,因此您使用了2种内核类型,每种类型都会创建自己的通道)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...