pytorch一维cnn代码
时间: 2023-10-22 18:10:48 浏览: 185
以下是一个简单的一维CNN的PyTorch代码示例:
```python
import torch
import torch.nn as nn
class OneDCNN(nn.Module):
def __init__(self, input_size, num_classes):
super(OneDCNN, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv1d(input_size, 16, kernel_size=5, stride=1, padding=2),
nn.BatchNorm1d(16),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2, stride=2))
self.layer2 = nn.Sequential(
nn.Conv1d(16, 32, kernel_size=5, stride=1, padding=2),
nn.BatchNorm1d(32),
nn.ReLU(),
nn.MaxPool1d(kernel_size=2, stride=2))
self.fc = nn.Linear(32*int(input_size/4), num_classes)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = out.reshape(out.size(0), -1)
out = self.fc(out)
return out
```
阅读全文