【代码bug消除】TCN模型PyTorch版本代码时间步无法修改的小bug已解决

TCN模型PyTorch版本代码时间步无法修改的小bug已解决

关于涡扇发动(CMPASS数据集)和滚动轴承RUL预测的pytorch版本代码的小问题——timestep(时间步)默认36,一旦修改,模型无法打印,后续也无法训练的问题,已经解决。

因为代码时我2020年的,当时一直在研究涡扇发动机的RUL预测,探究出的最佳时间步是36,所以我在TemporalBlock自定类中,默认写进去了,导致后续操作采用不同的序列长度也即时间步sequence_length无法修改

###时间步
sequence_length = 36
mask_value = 0
feats =features_colums
# feats = ['s' + str(i) for i in range(1, 1281)]##需要的特征数据维度,可以自己选择
with torch.no_grad():
      fake_input = torch.randn(1,n_inputs,36)#根据自己设置的(miniBatch,features,timstep)修改
class TemporalBlock(nn.Module):
  def __init__(self, n_inputs, n_outputs, kernel_size, stride, dilation, padding, dropout=0.2):
    super(TemporalBlock, self).__init__()
    self.conv1 =nn.Conv1d(n_inputs, n_outputs, kernel_size,
                                       stride=stride, padding=padding, dilation=dilation)
    self.chomp1 = Chomp1d(padding)
    # self.relu1 = nn.ReLU()
    self.leaky_relu1 = nn.LeakyReLU(negative_slope=0.1)

    self.dropout1 = nn.Dropout2d(dropout)

    self.conv2 = nn.Conv1d(n_outputs, n_outputs, kernel_size,
                                       stride=stride, padding=padding, dilation=dilation)
    self.chomp2 = Chomp1d(padding)
    # self.relu2 = nn.ReLU()
    self.leaky_relu2 = nn.LeakyReLU(negative_slope=0.1)
    self.dropout2 = nn.Dropout2d(dropout)
    # compute shape by doing a forward pass
    with torch.no_grad():
      fake_input = torch.randn(1,n_inputs,36)#根据自己设置的(miniBatch,features,timstep)修改
      out        = self.conv1(fake_input)
      # out        = self.chomp1(out)
      bn1_size   = out.size()[1:]
      out        = self.chomp1(out)
      out        = self.conv2(out)
      # out        = self.chomp2(out)
      bn2_size   = out.size()[1:]
    self.layer_norm1 = LayerNorm(bn1_size)
    self.layer_norm2 = LayerNorm(bn2_size)

    self.net = nn.Sequential(self.conv1,self.layer_norm1,self.chomp1,self.leaky_relu1, self.dropout1,
                             self.conv2,self.layer_norm2,self.chomp2,self.leaky_relu2, self.dropout2)
    self.downsample = nn.Conv1d(n_inputs, n_outputs, 1) if n_inputs != n_outputs else None
    # self.relu = nn.ReLU()
    self.leaky_relu = nn.LeakyReLU(negative_slope=0.1)
    self.init_weights()

修改版

TCN.py文件处的修改

(1)tcn.py文件下的第 22行

class TemporalBlock(nn.Module):
  def __init__(self, n_inputs,timestep, n_outputs, kernel_size, stride, dilation, padding, dropout=0.2):
    super(TemporalBlock, self).__init__()

(2)tcn.py文件下的第 41行

  with torch.no_grad():
      fake_input = torch.randn(1,n_inputs,timestep)#根据自己设置的(miniBatch,features,timstep)修改

(3)tcn.py文件下的第 78行

class TemporalConvNet(nn.Module):
  def __init__(self, num_inputs,timestep, num_channels, kernel_size=2, dropout=0.2, max_length=200, attention=False):
    super(TemporalConvNet, self).__init__()
    layers = []

(4)tcn.py文件下的第 86行

  out_channels = num_channels[i]
      layers += [TemporalBlock(in_channels, timestep, out_channels, kernel_size, stride=1, dilation=dilation_size,
                               padding=(kernel_size-1) * dilation_size, dropout=dropout)]
      if attention == True:

TCN_torch的jupyter notebook主程序文件处的修改

(1)TCN模型构建处的修改

"""构建model"""
class TCN(nn.Module):
    def __init__(self, input_size, timestep, output_size, num_channels, kernel_size, dropout):
        super(TCN, self).__init__()
        self.tcn = TemporalConvNet(input_size, timestep, num_channels, kernel_size=kernel_size, dropout=dropout)
        self.linear = nn.Linear(num_channels[-1], output_size)

    def forward(self, inputs):
        """Inputs have to have dimension (N, C_in, L_in)"""
        y1 = self.tcn(inputs)  # input should have dimension (N, C, L)
        o = self.linear(y1[:, :, -1])
        return F.relu(o)

(2)TCN模型初始化+打印模型处的修改

###设置随机数种子
np.random.seed(1234)
torch.manual_seed(1234)
batch_size=128
input_channels=x_train.shape[-1]
n_classes=1
"""此处就是时间步,需要修改一下"""
seq_length =sequence_length
epochs = 20
steps = 0

###TCN模型的每一层的卷积单元数目,卷积核大小设置
channel_sizes = [64, 32, 16,16, 8, 8]
kernel_size=3

model = TCN(input_channels, seq_length, n_classes, channel_sizes, kernel_size=kernel_size, dropout=0.25).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)##初始学习率可以自己自行设置

epoch_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer,mode='min',factor=0.1,patience=50,
                                                             min_lr=0.0001)###设置学习率下降策略,连续50次loss没有下降,学习率为原来的0.1倍,最小值为0.0001
Losses = nn.MSELoss()
"""这一步是打印你的TCN模型,input_channels为特征维度,sequence_length为时间步,
轴承数据看你的特征列数"""
summary(model,(input_channels, sequence_length))

##下面diamante是当你已经保存好了训练的模型以及权重以后,离线读取模型权重
# model.load_state_dict(torch.load(os.path.join(save_dir, "model_onTestBest.pth")))

新的打印模型如下时间步取30:
大功告成,你们来去试试吧,目前还没有人提出这个问题,我先发出来了

----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv1d-1               [-1, 16, 32]          61,504
            Conv1d-2               [-1, 16, 32]          61,504
         LayerNorm-3               [-1, 16, 32]           1,024
         LayerNorm-4               [-1, 16, 32]           1,024
           Chomp1d-5               [-1, 16, 30]               0
           Chomp1d-6               [-1, 16, 30]               0
         LeakyReLU-7               [-1, 16, 30]               0
         LeakyReLU-8               [-1, 16, 30]               0
         Dropout2d-9               [-1, 16, 30]               0
        Dropout2d-10               [-1, 16, 30]               0
           Conv1d-11               [-1, 16, 32]             784
           Conv1d-12               [-1, 16, 32]             784
        LayerNorm-13               [-1, 16, 32]           1,024
        LayerNorm-14               [-1, 16, 32]           1,024
          Chomp1d-15               [-1, 16, 30]               0
          Chomp1d-16               [-1, 16, 30]               0
        LeakyReLU-17               [-1, 16, 30]               0
        LeakyReLU-18               [-1, 16, 30]               0
        Dropout2d-19               [-1, 16, 30]               0
        Dropout2d-20               [-1, 16, 30]               0
           Conv1d-21               [-1, 16, 30]          20,512
        LeakyReLU-22               [-1, 16, 30]               0
    TemporalBlock-23               [-1, 16, 30]               0
           Conv1d-24               [-1, 16, 34]             784
           Conv1d-25               [-1, 16, 34]             784
        LayerNorm-26               [-1, 16, 34]           1,088
        LayerNorm-27               [-1, 16, 34]           1,088
          Chomp1d-28               [-1, 16, 30]               0
          Chomp1d-29               [-1, 16, 30]               0
        LeakyReLU-30               [-1, 16, 30]               0
        LeakyReLU-31               [-1, 16, 30]               0
        Dropout2d-32               [-1, 16, 30]               0
        Dropout2d-33               [-1, 16, 30]               0
           Conv1d-34               [-1, 16, 34]             784
           Conv1d-35               [-1, 16, 34]             784
        LayerNorm-36               [-1, 16, 34]           1,088
        LayerNorm-37               [-1, 16, 34]           1,088
          Chomp1d-38               [-1, 16, 30]               0
          Chomp1d-39               [-1, 16, 30]               0
        LeakyReLU-40               [-1, 16, 30]               0
        LeakyReLU-41               [-1, 16, 30]               0
        Dropout2d-42               [-1, 16, 30]               0
        Dropout2d-43               [-1, 16, 30]               0
        LeakyReLU-44               [-1, 16, 30]               0
    TemporalBlock-45               [-1, 16, 30]               0
           Conv1d-46                [-1, 8, 38]             392
           Conv1d-47                [-1, 8, 38]             392
        LayerNorm-48                [-1, 8, 38]             608
        LayerNorm-49                [-1, 8, 38]             608
          Chomp1d-50                [-1, 8, 30]               0
          Chomp1d-51                [-1, 8, 30]               0
        LeakyReLU-52                [-1, 8, 30]               0
        LeakyReLU-53                [-1, 8, 30]               0
        Dropout2d-54                [-1, 8, 30]               0
        Dropout2d-55                [-1, 8, 30]               0
           Conv1d-56                [-1, 8, 38]             200
           Conv1d-57                [-1, 8, 38]             200
        LayerNorm-58                [-1, 8, 38]             608
        LayerNorm-59                [-1, 8, 38]             608
          Chomp1d-60                [-1, 8, 30]               0
          Chomp1d-61                [-1, 8, 30]               0
        LeakyReLU-62                [-1, 8, 30]               0
        LeakyReLU-63                [-1, 8, 30]               0
        Dropout2d-64                [-1, 8, 30]               0
        Dropout2d-65                [-1, 8, 30]               0
           Conv1d-66                [-1, 8, 30]             136
        LeakyReLU-67                [-1, 8, 30]               0
    TemporalBlock-68                [-1, 8, 30]               0
           Conv1d-69                [-1, 8, 46]             200
           Conv1d-70                [-1, 8, 46]             200
        LayerNorm-71                [-1, 8, 46]             736
        LayerNorm-72                [-1, 8, 46]             736
          Chomp1d-73                [-1, 8, 30]               0
          Chomp1d-74                [-1, 8, 30]               0
        LeakyReLU-75                [-1, 8, 30]               0
        LeakyReLU-76                [-1, 8, 30]               0
        Dropout2d-77                [-1, 8, 30]               0
        Dropout2d-78                [-1, 8, 30]               0
           Conv1d-79                [-1, 8, 46]             200
           Conv1d-80                [-1, 8, 46]             200
        LayerNorm-81                [-1, 8, 46]             736
        LayerNorm-82                [-1, 8, 46]             736
          Chomp1d-83                [-1, 8, 30]               0
          Chomp1d-84                [-1, 8, 30]               0
        LeakyReLU-85                [-1, 8, 30]               0
        LeakyReLU-86                [-1, 8, 30]               0
        Dropout2d-87                [-1, 8, 30]               0
        Dropout2d-88                [-1, 8, 30]               0
        LeakyReLU-89                [-1, 8, 30]               0
    TemporalBlock-90                [-1, 8, 30]               0
  TemporalConvNet-91                [-1, 8, 30]               0
           Linear-92                    [-1, 1]               9
================================================================
Total params: 164,177
Trainable params: 164,177
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.15
Forward/backward pass size (MB): 0.27
Params size (MB): 0.63
Estimated Total Size (MB): 1.04
----------------------------------------------------------------

猜你喜欢

转载自blog.csdn.net/weixin_45279187/article/details/128684103