创建模型
如,创建线性模型
class LinearModel(nn.Module):
def __init__(self, ndim):
super(LinearModel, self).__init__()
self.ndim = ndim
self.weight = nn.Parameter(torch.randn(ndim, 1))
self.bias = nn.Parameter(torch.randn(1))
def forward(self, x):
return x.mm(self.weight) + self.bias
模型初始化、参数
lm = LinearModel(5)
x = torch.randn(4, 5)
lm(x)
'''
tensor([[-3.0970],
[-2.9674],
[ 3.3265],
[ 4.1923]], grad_fn=<AddBackward0>)
'''
x
'''
tensor([[-0.3725, -1.7013, -2.6523, -0.8103, -0.1179],
[-1.1700, 0.0091, -0.0386, -1.3510, 0.9027],
[ 1.5329, 0.9760, -0.4165, 0.2783, -0.6180],
[ 1.0752, 0.0267, 0.9067, 2.2452, 0.6527]])
'''
lm.named_parameters()
'''
<generator object Module.named_parameters at 0x7ffa27d46c50>
list(lm.named_parameters() )
[('weight', Parameter containing:
tensor([[2.2394],
[0.2185],
[0.5514],
[0.4709],
[0.3480]], requires_grad=True)), ('bias', Parameter containing:
tensor([-0.0059], requires_grad=True))]
'''
lm.parameters()
'''
<generator object Module.parameters at 0x7ffa29907d50>
list(lm.parameters() )
[Parameter containing:
tensor([[2.2394],
[0.2185],
[0.5514],
[0.4709],
[0.3480]], requires_grad=True), Parameter containing:
tensor([-0.0059], requires_grad=True)]
'''
lm.half()
'''
LinearModel()
list(lm.parameters())
[Parameter containing:
tensor([[2.2402],
[0.2185],
[0.5513],
[0.4709],
[0.3479]], dtype=torch.float16, requires_grad=True),
Parameter containing:
tensor([-0.0059], dtype=torch.float16, requires_grad=True)]
lm.parameters
<bound method Module.parameters of LinearModel()>
'''
from sklearn.datasets import load_boston
boston = load_boston()
lm = LinearModel(13)
criterion = nn.MSELoss()
optim = torch.optim.SGD(lm.parameters(), lr=1e-6)
optim
'''
SGD (
Parameter Group 0
dampening: 0
lr: 1e-06
momentum: 0
nesterov: False
weight_decay: 0
)
'''
data = torch.tensor(boston['data'], requires_grad=True, dtype=torch.float32)
data
'''
tensor([[6.3200e-03, 1.8000e+01, 2.3100e+00, ..., 1.5300e+01, 3.9690e+02,
4.9800e+00],
...
[4.7410e-02, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9690e+02,
7.8800e+00]], requires_grad=True)
'''
target = torch.tensor(boston['target'], dtype=torch.float32)
for step in range(10000):
predict = lm(data)
loss = criterion(predict, target)
if step and step%1000 == 0:
print('-- loss : {:.3f}'.format(loss.item()) )
optim.zero_grad()
loss.backward()
optim.step()
'''
/Users/xx/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/loss.py:446: UserWarning: Using a target size (torch.Size([506])) that is different to the input size (torch.Size([506, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
return F.mse_loss(input, target, reduction=self.reduction)
-- loss : 224.251
-- loss : 150.535
-- loss : 143.163
-- loss : 138.828
-- loss : 135.080
-- loss : 131.752
-- loss : 128.779
-- loss : 126.110
-- loss : 123.706
'''
optim.state_dict()
'''
{'state': {},
'param_groups': [{'lr': 1e-06,
'momentum': 0,
'dampening': 0,
'weight_decay': 0,
'nesterov': False,
'params': [0, 1]}]}
'''
保存/加载模型
save_info = {
'iter_num': 10000,
'optimizer': optim.state_dict(),
'model': lm.state_dict(),
}
save_info
'''
{'iter_num': 10000,
'optimizer': {'state': {},
'param_groups': [{'lr': 1e-06,
'momentum': 0,
'dampening': 0,
'weight_decay': 0,
'nesterov': False,
'params': [0, 1]}]},
'model': OrderedDict([('weight', tensor([[-0.0506],
[ 0.1244],
[ 0.9757],
[-1.9508],
[-0.1465],
[-1.9823],
[ 0.0850],
[ 0.4799],
[-0.3672],
[ 0.0141],
[ 0.4012],
[ 0.0298],
[-0.5437]])), ('bias', tensor([1.5198]))])}
'''
save_path = 'model1.txt'
torch.save(save_info, save_path)
save_info1 = torch.load(save_path)
save_info1
'''
{'iter_num': 10000,
'optimizer': {'state': {},
'param_groups': [{'lr': 1e-06,
'momentum': 0,
'dampening': 0,
'weight_decay': 0,
'nesterov': False,
'params': [0, 1]}]},
'model': OrderedDict([('weight', tensor([[-0.0506],
[ 0.1244],
[ 0.9757],
[-1.9508],
[-0.1465],
[-1.9823],
[ 0.0850],
[ 0.4799],
[-0.3672],
[ 0.0141],
[ 0.4012],
[ 0.0298],
[-0.5437]])), ('bias', tensor([1.5198]))])}
'''
optim.load_state_dict(save_info1['optimizer'])
lm.load_state_dict(save_info1['model'])
<All keys matched successfully>
自动求导
import torch
t1 = torch.randn(3, 3, requires_grad=True)
'''
tensor([[-0.4336, -0.1928, 0.3398],
[-0.5616, 0.1290, 0.8002],
[-1.1966, 1.4117, -0.3643]], requires_grad=True)
'''
t2 = t1.pow(2)
'''
tensor([[0.1880, 0.0372, 0.1154],
[0.3154, 0.0166, 0.6403],
[1.4319, 1.9929, 0.1327]], grad_fn=<PowBackward0>)
'''
t2 = t2.sum()
t2.backward()
t1.grad
'''
tensor([[-0.8671, -0.3855, 0.6795],
[-1.1232, 0.2580, 1.6004],
[-2.3932, 2.8234, -0.7287]])
'''
t1.grad.zero_()
'''
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
'''
梯度函数
t1 = torch.randn(3, 3, requires_grad=True)
'''
tensor([[ 1.3203, -0.6757, 1.4479],
[ 0.6133, -0.8377, -0.9381],
[ 0.3214, 0.9020, 0.1285]], requires_grad=True)
'''
t2 = t1.sum()
with torch.no_grad():
t3 = t1.sum()
t3
t1.sum()
t1.sum().detach()
损失函数
mse
import torch.nn as nn
import torch
mse = nn.MSELoss()
t1 = torch.randn(5, requires_grad=True)
t2 = torch.randn(5, requires_grad=True)
mse(t1, t2)
二分类 bce
t1 = torch.randn(5, requires_grad=True)
t1s = torch.sigmoid(t1)
t2 = torch.randint(0, 2, (5, )).float()
bce = nn.BCELoss()
bce(t1s, t2)
bce_logits = nn.BCEWithLogitsLoss()
bce_logits(t1, t2)
多分类
N = 10
t1 = torch.randn(5, N, requires_grad=True)
t2 = torch.randint(0, N, (5, ))
t1s = nn.functional.log_softmax(t1, -1)
n11 = nn.NLLLoss()
n11(t1s, t2)
ce = nn.CrossEntropyLoss()
ce(t1, t2)
模块的组合
顺序模块构建
model = nn.Sequential(
nn.Conv2d(1, 20, 5),
nn.ReLU(),
nn.Conv2d(20, 64, 5),
nn.ReLU()
)
model = nn.Sequential(
OrderedDict([
('conv1', nn.Conv2d(1, 20, 5)),
('relu1', nn.ReLU()),
('conv2', nn.Conv2d(20, 64, 5)),
('relu2', nn.ReLU()),
])
)
|