Garment Classifier Below there is a net given, that start to use convolutions to consider the advantages of the spatial structure of images. Test the gradient descent method on this net and compare it. def get_GarmentClassifier(): return nn.Sequential( nn.Conv2d(1, 6, kernel_size=5), nn.ReLU(), nn.MaxPool2d(2,2), nn.Conv2d(6, 16, kernel_size=5), nn.ReLU(), nn.MaxPool2d(2,2), nn.Flatten(), nn.Linear(16 * 4 * 4, 120), nn.ReLU(), nn.Linear(120, 84), nn.ReLU(), nn.Linear(84, 10) ).cuda() 帮我根据上面的要求补充完整下面的代码 net = ... epochs = 10 optimizer = GD(net.parameters(), 0.02) losses, states = ... fig = plot_losses(losses) conf_pltly() init_notebook_mode(connected=False) iplot(fig)
时间: 2024-01-27 09:05:04 浏览: 116
Learning to rank using gradient descent.
下面是根据上述要求补充完整的代码:
```python
import torch.nn as nn
from torch.optim import SGD as GD
from plotly.offline import iplot, init_notebook_mode
from utils import plot_losses
def get_GarmentClassifier():
return nn.Sequential(
nn.Conv2d(1, 6, kernel_size=5),
nn.ReLU(),
nn.MaxPool2d(2,2),
nn.Conv2d(6, 16, kernel_size=5),
nn.ReLU(),
nn.MaxPool2d(2,2),
nn.Flatten(),
nn.Linear(16 * 4 * 4, 120),
nn.ReLU(),
nn.Linear(120, 84),
nn.ReLU(),
nn.Linear(84, 10)
).cuda()
net = get_GarmentClassifier()
epochs = 10
optimizer = GD(net.parameters(), 0.02)
losses, states = train(net, optimizer, epochs)
fig = plot_losses(losses)
init_notebook_mode(connected=False)
iplot(fig)
```
其中 `train` 函数是用来训练模型的,需要根据具体的数据集和训练方式来实现。`plot_losses` 函数是用来绘制训练过程中损失函数的变化情况。请注意,上面的代码中省略了这两个函数的具体实现。
阅读全文