1、softmax
函数 Softmax(x) 也是一个 non-linearity, 但它的特殊之处在于它通常是网络中一次操作. 这是因为它接受了一个实数向量并返回一个概率分布.其定义如下. 定义 x 是一个实数的向量(正数或负数都无所谓, 没有限制). 然后, 第i个 Softmax(x) 的组成是
exp(xi)∑jexp(xj)
输出是一个概率分布: 每个元素都是非负的, 并且所有元素的总和都是1.
2、log_softmax
在softmax的结果上再做多一次log运算
While mathematically equivalent to log(softmax(x)), doing these two
operations separately is slower, and numerically unstable. This function
uses an alternative formulation to compute the output and gradient correctly.
虽然在数学上等价于log(softmax(x)),但做这两个
单独操作速度较慢,数值上也不稳定。这个函数
使用另一种公式来正确计算输出和梯度。
测试:
import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
data=autograd.Variable(torch.FloatTensor([1.0,2.0,3.0]))
log_softmax=F.log_softmax(data,dim=0)
print(log_softmax)
softmax=F.softmax(data,dim=0)
print(softmax)
np_softmax=soft