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)
#输出weight和bias
print(m.weight)
print(m.bias)
#在 input 上应用该正则化并输出
output=m(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应用该范数并输出
output = pdist(input,target)
print(output)
#/********** End *********/