神经网络构建
构建数据集:
网络搭建:
class nn(nn.Module): def __init__(self): super(nn, self).__init__() self.nets = nn.Sequential( # 网络细节
# 针对单元(图像···)
)
def forward(self, x):
# 针对批数据batch
# 如Conv2d的输入尺度为 [batch_size, channels, height, weight] x = self.nets(x)return x
训练:
loss = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
train_pred = model(data[0].cuda())
train_loss = loss(train_label, data[1].cuda())
train_loss.backward()
optimizer.step()

浙公网安备 33010602011771号