softmax实现
公式:
- Softmax函数常用于多分类问题中,将输入向量转换为概率分布。
- 在代码中,使用numpy和torch分别实现了Softmax函数。
- numpy实现中,首先计算输入向量的指数值,然后除以所有指数值的总和以得到概率分布。
- torch实现中,利用torch的softmax函数对输入向量进行处理,dim=0表示按照列计算Softmax。
import numpy as np
import torch
matrix = [i for i in range(4)]
res = np.exp(matrix)/np.sum(np.exp(matrix), axis=-1, keepdims=True)
print(res)
# [0.0320586 0.08714432 0.23688282 0.64391426]
matrix = [i for i in range(4)]
res = torch.FloatTensor(matrix)
print(torch.softmax(res, dim=0))
# tensor([0.0321, 0.0871, 0.2369, 0.6439])
relu实现
公式:

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



