Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Class OOP新手对声明变量感到困惑_Class_Oop_Pytorch - Fatal编程技术网

Class OOP新手对声明变量感到困惑

Class OOP新手对声明变量感到困惑,class,oop,pytorch,Class,Oop,Pytorch,在pytorch上建立一个神经网络,尝试让完全连接的层接受我之前称之为平坦层的维度 到目前为止,我正在做以下工作: from torch import nn from torch.nn import functional as F class my_model(nn.Module): def __init__(self, channel=16, out_class=1, flat_dim=24): super(my_model, self).__init__()

在pytorch上建立一个神经网络,尝试让完全连接的层接受我之前称之为平坦层的维度

到目前为止,我正在做以下工作:

from torch import nn
from torch.nn import functional as F

class my_model(nn.Module):
    def __init__(self, channel=16, out_class=1, flat_dim=24):
        super(my_model, self).__init__()

        self.channel = channel
        n_class = out_class

        self.conv1 = nn.Conv3d(1, channel // 8, kernel_size=5, stride=2, padding=1)
        self.pool1 = nn.AvgPool3d(2)
        self.bn1 = nn.BatchNorm3d(channel //8)
        self.conv2 = nn.Conv3d(channel // 8, channel // 4, kernel_size=4, stride=2, padding=1)
        self.bn2 = nn.BatchNorm3d(channel // 4)
        self.pool2 = nn.AvgPool3d(2)
        self.fc1 = nn.Linear(flat_dim,500)
        self.fc2 = nn.Linear(500,100)

    def forward(self, x):
        batch_size, D, W, H, T = x.shape
        print('input to model', x.shape)
        x = x.permute(0,4,1,2,3)
        x = x.reshape(batch_size * T, 1, D, W, H)
        print('after reshape', x.shape)
        h1 = F.relu(self.bn1(self.pool1(self.conv1(x))))
        print('after first layer sequence', h1.shape)
        h2 = F.relu(self.bn2(self.pool2(self.conv2(h1))))
        print('output to model', h2.shape)
        h2, flat_dim = self.flatten_cube(h2)
        h3 = self.fc1(h2)
        h4 = self.fc2(h3)
        print(h4.shape)

        return h4

    def flatten_cube(self, x):
        super_batch, C, D, W, H = x.shape
        flat_dim = C * D * W * H
        y = x.view(super_batch, flat_dim)
        return y, flat_dim

这是正确的做法吗