深度学习与PyTorch笔记18
交叉熵(cross entropy loss)
Entropy
熵,不确定性,entropy,uncertainty,measure of surprise,higher entropy=less info,定义:
熵越大越稳定,越小越不稳定。
Cross Entropy
(KL,divergence,散度,真正衡量两个分布的距离的关系,越相似越接近于0)
if
if (0,1)entropy
运算推导
若,需要最大化。若,需要最大化,最小化p。
总结
why not use MSE
- sigmoid+MSE
- gradient vanish。sigmoid饱和,梯度离散。
- converge slower
- But,sometimes好用
- e.g.meta-learing
使用F.cross_entropy()
时必须传入logits
,如果要自己计算就先用F.softmax()
再用torch.log()
最后用F.nll.loss()
,此时传入的参数为pred_log
,不建议自己计算,建议直接使用F.cross_entropy()
。
使用交叉熵优化多分类问题
十分类。
tensor的定义和网络的forward的过程:
#新建三个线性层
#输入维度为784,输出维度为200,(ch-out,ch-in)
w1, b1 = torch.randn(200, 784, requires_grad=True),\
torch.zeros(200, requires_grad=True)
#第二个隐藏层
w2, b2 = torch.randn(200, 200, requires_grad=True),\
torch.zeros(200, requires_grad=True)
#输出
w3, b3 = torch.randn(10, 200, requires_grad=True),\
torch.zeros(10, requires_grad=True)
#将forward过程写在一个函数里面
def forward(x):
x = [email protected]() + b1
x = F.relu(x)
x = [email protected]() + b2
x = F.relu(x)
x = [email protected]() + b3
x = F.relu(x)#logits
return x
定义一个优化器优化目标为:
optimizer = optim.SGD([w1, b1, w2, b2, w3, b3], lr=learning_rate)#定义一个优化器
criteon = nn.CrossEntropyLoss()#有softmax操作
for epoch in range(epochs):
for batch_idx, (data, target) in enumerate(train_loader):
data = data.view(-1, 28*28)
logits = forward(data)#不能再加softmax了
loss = criteon(logits, target)
optimizer.zero_grad()
loss.backward()
# print(w1.grad.norm(), w2.grad.norm())
optimizer.step()
一定要初始化,不然会出现梯度离散。b已经初始化为0。
torch.nn.init.kaiming_normal_(w1)
torch.nn.init.kaiming_normal_(w2)
torch.nn.init.kaiming_normal_(w3)
(何凯明)