./src/convolutional_layer.c:153:13: error: 'CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT' undeclared (first use in this function) 153 | CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
时间: 2023-05-17 14:06:40 浏览: 87
这个错误是由于在 convolutional_layer.c 文件的第 153 行中使用了未声明的 CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT 变量导致的。可能是您的代码中缺少了必要的头文件或库文件,或者您的代码中使用了不兼容的版本。建议您检查代码并确保所有必要的头文件和库文件都已正确包含。
相关问题
class DownConv(nn.Module): def __init__(self, seq_len=200, hidden_size=64, m_segments=4,k1=10,channel_reduction=16): super().__init__() """ DownConv is implemented by stacked strided convolution layers and more details can be found below. When the parameters k_1 and k_2 are determined, we can soon get m in Eq.2 of the paper. However, we are more concerned with the size of the parameter m, so we searched for a combination of parameter m and parameter k_1 (parameter k_2 can be easily calculated in this process) to find the optimal segment numbers. Args: input_tensor (torch.Tensor): the input of the attention layer Returns: output_conv (torch.Tensor): the convolutional outputs in Eq.2 of the paper """ self.m =m_segments self.k1 = k1 self.channel_reduction = channel_reduction # avoid over-parameterization middle_segment_length = seq_len/k1 k2=math.ceil(middle_segment_length/m_segments) padding = math.ceil((k2*self.m-middle_segment_length)/2.0) # pad the second convolutional layer appropriately self.conv1a = nn.Conv1d(in_channels=hidden_size, out_channels=hidden_size // self.channel_reduction, kernel_size=self.k1, stride=self.k1) self.relu1a = nn.ReLU(inplace=True) self.conv2a = nn.Conv1d(in_channels=hidden_size // self.channel_reduction, out_channels=hidden_size, kernel_size=k2, stride=k2, padding = padding) def forward(self, input_tensor): input_tensor = input_tensor.permute(0, 2, 1) x1a = self.relu1a(self.conv1a(input_tensor)) x2a = self.conv2a(x1a) if x2a.size(2) != self.m: print('size_erroe, x2a.size_{} do not equals to m_segments_{}'.format(x2a.size(2),self.m)) output_conv = x2a.permute(0, 2, 1) return output_conv
这是一个用于实现降采样卷积(DownConv)的PyTorch模型类。在构造函数中,需要指定一些参数,包括序列长度seq_len,隐藏层大小hidden_size,中间段数m_segments,卷积核大小k1和通道缩减channel_reduction。其中,降采样卷积层的实现使用了两个卷积层,第一个卷积层的卷积核大小为k1,步长为k1,将输入张量进行降采样;第二个卷积层的卷积核大小为k2,步长为k2,将第一个卷积层的输出进行进一步的降采样,并按照论文中的公式计算得到输出张量。为了使得第二个卷积层的输出张量大小与中间段数m_segments相等,需要在卷积层中进行适当的padding。在前向传播时,需要将输入张量进行维度变换,使得其可以被卷积层处理,然后将卷积层的输出张量再次进行维度变换,使得其可以作为下一层的输入。如果第二个卷积层的输出张量大小不等于中间段数m_segments,则会输出一条错误信息。
# New module: utils.pyimport torchfrom torch import nnclass ConvBlock(nn.Module): """A convolutional block consisting of a convolution layer, batch normalization layer, and ReLU activation.""" def __init__(self, in_chans, out_chans, drop_prob): super().__init__() self.conv = nn.Conv2d(in_chans, out_chans, kernel_size=3, padding=1) self.bn = nn.BatchNorm2d(out_chans) self.relu = nn.ReLU(inplace=True) self.dropout = nn.Dropout2d(p=drop_prob) def forward(self, x): x = self.conv(x) x = self.bn(x) x = self.relu(x) x = self.dropout(x) return x# Refactored U-Net modelfrom torch import nnfrom utils import ConvBlockclass UnetModel(nn.Module): """PyTorch implementation of a U-Net model.""" def __init__(self, in_chans, out_chans, chans, num_pool_layers, drop_prob, pu_args=None): super().__init__() PUPS.__init__(self, *pu_args) self.in_chans = in_chans self.out_chans = out_chans self.chans = chans self.num_pool_layers = num_pool_layers self.drop_prob = drop_prob # Calculate input and output channels for each ConvBlock ch_list = [chans] + [chans * 2 ** i for i in range(num_pool_layers - 1)] in_chans_list = [in_chans] + [ch_list[i] for i in range(num_pool_layers - 1)] out_chans_list = ch_list[::-1] # Create down-sampling layers self.down_sample_layers = nn.ModuleList() for i in range(num_pool_layers): self.down_sample_layers.append(ConvBlock(in_chans_list[i], out_chans_list[i], drop_prob)) # Create up-sampling layers self.up_sample_layers = nn.ModuleList() for i in range(num_pool_layers - 1): self.up_sample_layers.append(ConvBlock(out_chans_list[i], out_chans_list[i + 1] // 2, drop_prob)) self.up_sample_layers.append(ConvBlock(out_chans_list[-1], out_chans_list[-1], drop_prob)) # Create final convolution layer self.conv2 = nn.Sequential( nn.Conv2d(out_chans_list[-1], out_chans_list[-1] // 2, kernel_size=1), nn.Conv2d(out_chans_list[-1] // 2, out_chans, kernel_size=1), nn.Conv2d(out_chans, out_chans, kernel_size=1), ) def forward(self, x): # Down-sampling path encoder_outs = [] for layer in self.down_sample_layers: x = layer(x) encoder_outs.append(x) x = nn.MaxPool2d(kernel_size=2)(x) # Bottom layer x = self.conv(x) # Up-sampling path for i, layer in enumerate(self.up_sample_layers): x = nn.functional.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True) x = torch.cat([x, encoder_outs[-(i + 1)]], dim=1) x = layer(x) # Final convolution layer x = self.conv2(x) return x
这里给出了一个 PyTorch 实现的 U-Net 模型,并且对代码进行了重构:
1. 将 ConvBlock 提取到了 utils.py 中,以便在 U-Net 中复用。
2. 在 U-Net 模型的 forward 方法中,使用了 ModuleList 对 down-sampling 和 up-sampling 层进行了封装,使得代码更加简洁和易于扩展。
3. 使用了 nn.functional.interpolate 对 feature map 进行了上采样,避免了使用 nn.ConvTranspose2d 带来的一些问题。
4. 在最后的卷积层中,使用了 nn.Sequential 对多个卷积层进行了封装,使得代码更加简洁。
这些重构都是为了提高代码的可读性、可维护性和可扩展性,使得代码更加清晰,易于理解和修改。
阅读全文