公式
其中,loss、x、y的维数相同,可以是向量或矩阵,I是下标。
许多loss函数都有名为size_average和reduce的布尔参数。
因为典型的丢失函数直接计算batch的数据,所以返回的所有loss结果都是维(batch_size,)的向量。
a )如果reduce=False,则禁用size_average参数,并按原样返回向量格式lossb;如果reduce=True,则loss返回标量a ) reduction='mean b ) reduction='mean ',返回sum; 默认情况下,reduce=True,size_average=True
importtorchimportnumpyasnploss_fn=torch.nn.MSEloss (reduction=' mean ' ) loss _ fn=torch.nn.MSE loss ) reduch 5 ) ) print(a(a ) print ) torch.from _ numpy (a ) ) input=torch.Autograd.Variable ) torch.from _ nu numpy (a ) loss=loss_fn(input.float (,target.float ) )、print (loss ) userwarninguserwarning 3360 size _ averageandrer prer
criterion=torch.nn.BCEloss (size _ average=true )是criterion=torch.nn.BCE loss ) reduction='mean ' (创建者