注意变量名不能乱改,要按照左边文字的要求命名,一般来说是一样的。

第1关:正则化
 

import torch
import torch.nn as nn
from torch.autograd import Variable

input = Variable(torch.Tensor([[1,2,3,4],[5,6,7,8]]))

#/********** Begin *********/
# 创建一个4维的 带有学习参数的正则化量 m
m = nn.BatchNorm1d(4)
output=m(input)
#输出weight和bias

print(m.weight)
print(m.bias)

#在 input 上应用该正则化并输出
print(output)

#/********** End *********/

第2关:损失函数

import torch
import torch.nn as nn
from torch.autograd import Variable

input = Variable(torch.Tensor([1.1,2.2,2.93,3.8]))
target = Variable(torch.Tensor([1,2,3,4]))

#/********** Begin *********/

#创建名为 loss 的 L1Loss损失函数
loss = nn.L1Loss()
#对 input 和 target应用 loss 并输出
output = loss(input, target)
print(output.data)

#/********** End *********/

第3关:距离函数

import torch
import torch.nn as nn
from torch.autograd import Variable

input = Variable(torch.Tensor([[1.1,2.2],[2.93,3.8]]))
target = Variable(torch.Tensor([[1,2],[3,4]]))

#/********** Begin *********/
#创建一范数的变量pdist
pdist=nn.PairwiseDistance(p=1)
#对 input 、 target应用该范数并输出
print(pdist(input,target))

#/********** End *********/
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐