- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Pytorch中Softmax和LogSoftmax的使用详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
(1)dim=0:对每一列的所有元素进行softmax运算,并使得每一列所有元素和为1.
(2)dim=1:对每一行的所有元素进行softmax运算,并使得每一行所有元素和为1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
class
Softmax(Module):
r
"""Applies the Softmax function to an n-dimensional input Tensor
rescaling them so that the elements of the n-dimensional output Tensor
lie in the range [0,1] and sum to 1.
Softmax is defined as:
.. math::
\text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}
Shape:
- Input: :math:`(*)` where `*` means, any number of additional
dimensions
- Output: :math:`(*)`, same shape as the input
Returns:
a Tensor of the same dimension and shape as the input with
values in the range [0, 1]
Arguments:
dim (int): A dimension along which Softmax will be computed (so every slice
along dim will sum to 1).
.. note::
This module doesn't work directly with NLLLoss,
which expects the Log to be computed between the Softmax and itself.
Use `LogSoftmax` instead (it's faster and has better numerical properties).
Examples::
>>> m = nn.Softmax(dim=1)
>>> input = torch.randn(2, 3)
>>> output = m(input)
"""
__constants__
=
[
'dim'
]
def
__init__(
self
, dim
=
None
):
super
(Softmax,
self
).__init__()
self
.dim
=
dim
def
__setstate__(
self
, state):
self
.__dict__.update(state)
if
not
hasattr
(
self
,
'dim'
):
self
.dim
=
None
def
forward(
self
,
input
):
return
F.softmax(
input
,
self
.dim, _stacklevel
=
5
)
def
extra_repr(
self
):
return
'dim={dim}'
.
format
(dim
=
self
.dim)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
class
LogSoftmax(Module):
r
"""Applies the :math:`\log(\text{Softmax}(x))` function to an n-dimensional
input Tensor. The LogSoftmax formulation can be simplified as:
.. math::
\text{LogSoftmax}(x_{i}) = \log\left(\frac{\exp(x_i) }{ \sum_j \exp(x_j)} \right)
Shape:
- Input: :math:`(*)` where `*` means, any number of additional
dimensions
- Output: :math:`(*)`, same shape as the input
Arguments:
dim (int): A dimension along which LogSoftmax will be computed.
Returns:
a Tensor of the same dimension and shape as the input with
values in the range [-inf, 0)
Examples::
>>> m = nn.LogSoftmax()
>>> input = torch.randn(2, 3)
>>> output = m(input)
"""
__constants__
=
[
'dim'
]
def
__init__(
self
, dim
=
None
):
super
(LogSoftmax,
self
).__init__()
self
.dim
=
dim
def
__setstate__(
self
, state):
self
.__dict__.update(state)
if
not
hasattr
(
self
,
'dim'
):
self
.dim
=
None
def
forward(
self
,
input
):
return
F.log_softmax(
input
,
self
.dim, _stacklevel
=
5
)
|
输入代码 。
1
2
3
4
5
6
7
8
9
10
11
12
|
import
torch
import
torch.nn as nn
import
numpy as np
batch_size
=
4
class_num
=
6
inputs
=
torch.randn(batch_size, class_num)
for
i
in
range
(batch_size):
for
j
in
range
(class_num):
inputs[i][j]
=
(i
+
1
)
*
(j
+
1
)
print
(
"inputs:"
, inputs)
|
得到大小batch_size为4,类别数为6的向量(可以理解为经过最后一层得到) 。
tensor([[ 1., 2., 3., 4., 5., 6.], [ 2., 4., 6., 8., 10., 12.], [ 3., 6., 9., 12., 15., 18.], [ 4., 8., 12., 16., 20., 24.]]) 。
接着我们对该向量每一行进行Softmax 。
1
2
3
|
Softmax
=
nn.Softmax(dim
=
1
)
probs
=
Softmax(inputs)
print
(
"probs:\n"
, probs)
|
得到 。
tensor([[4.2698e-03, 1.1606e-02, 3.1550e-02, 8.5761e-02, 2.3312e-01, 6.3369e-01], [3.9256e-05, 2.9006e-04, 2.1433e-03, 1.5837e-02, 1.1702e-01, 8.6467e-01], [2.9067e-07, 5.8383e-06, 1.1727e-04, 2.3553e-03, 4.7308e-02, 9.5021e-01], [2.0234e-09, 1.1047e-07, 6.0317e-06, 3.2932e-04, 1.7980e-02, 9.8168e-01]]) 。
此外,我们对该向量每一行进行LogSoftmax 。
1
2
3
|
LogSoftmax
=
nn.LogSoftmax(dim
=
1
)
log_probs
=
LogSoftmax(inputs)
print
(
"log_probs:\n"
, log_probs)
|
得到 。
tensor([[-5.4562e+00, -4.4562e+00, -3.4562e+00, -2.4562e+00, -1.4562e+00, -4.5619e-01], [-1.0145e+01, -8.1454e+00, -6.1454e+00, -4.1454e+00, -2.1454e+00, -1.4541e-01], [-1.5051e+01, -1.2051e+01, -9.0511e+00, -6.0511e+00, -3.0511e+00, -5.1069e-02], [-2.0018e+01, -1.6018e+01, -1.2018e+01, -8.0185e+00, -4.0185e+00, -1.8485e-02]]) 。
验证每一行元素和是否为1 。
1
2
3
4
5
6
7
|
# probs_sum in dim=1
probs_sum
=
[
0
for
i
in
range
(batch_size)]
for
i
in
range
(batch_size):
for
j
in
range
(class_num):
probs_sum[i]
+
=
probs[i][j]
print
(i,
"row probs sum:"
, probs_sum[i])
|
得到每一行的和,看到确实为1 。
0 row probs sum: tensor(1.) 1 row probs sum: tensor(1.0000) 2 row probs sum: tensor(1.) 3 row probs sum: tensor(1.) 。
验证LogSoftmax是对Softmax的结果进行Log 。
1
2
3
4
5
6
7
|
# to numpy
np_probs
=
probs.data.numpy()
print
(
"numpy probs:\n"
, np_probs)
# np.log()
log_np_probs
=
np.log(np_probs)
print
(
"log numpy probs:\n"
, log_np_probs)
|
得到 。
numpy probs: [[4.26977826e-03 1.16064614e-02 3.15496325e-02 8.57607946e-02 2.33122006e-01 6.33691311e-01] [3.92559559e-05 2.90064461e-04 2.14330270e-03 1.58369839e-02 1.17020354e-01 8.64669979e-01] [2.90672347e-07 5.83831024e-06 1.17265590e-04 2.35534250e-03 4.73083146e-02 9.50212955e-01] [2.02340233e-09 1.10474026e-07 6.03167746e-06 3.29318427e-04 1.79801770e-02 9.81684387e-01]] log numpy probs: [[-5.4561934e+00 -4.4561934e+00 -3.4561934e+00 -2.4561932e+00 -1.4561933e+00 -4.5619333e-01] [-1.0145408e+01 -8.1454077e+00 -6.1454072e+00 -4.1454072e+00 -2.1454074e+00 -1.4540738e-01] [-1.5051069e+01 -1.2051069e+01 -9.0510693e+00 -6.0510693e+00 -3.0510693e+00 -5.1069155e-02] [-2.0018486e+01 -1.6018486e+01 -1.2018485e+01 -8.0184851e+00 -4.0184855e+00 -1.8485421e-02]] 。
验证完毕 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import
torch
import
torch.nn as nn
import
numpy as np
batch_size
=
4
class_num
=
6
inputs
=
torch.randn(batch_size, class_num)
for
i
in
range
(batch_size):
for
j
in
range
(class_num):
inputs[i][j]
=
(i
+
1
)
*
(j
+
1
)
print
(
"inputs:"
, inputs)
Softmax
=
nn.Softmax(dim
=
1
)
probs
=
Softmax(inputs)
print
(
"probs:\n"
, probs)
LogSoftmax
=
nn.LogSoftmax(dim
=
1
)
log_probs
=
LogSoftmax(inputs)
print
(
"log_probs:\n"
, log_probs)
# probs_sum in dim=1
probs_sum
=
[
0
for
i
in
range
(batch_size)]
for
i
in
range
(batch_size):
for
j
in
range
(class_num):
probs_sum[i]
+
=
probs[i][j]
print
(i,
"row probs sum:"
, probs_sum[i])
# to numpy
np_probs
=
probs.data.numpy()
print
(
"numpy probs:\n"
, np_probs)
# np.log()
log_np_probs
=
np.log(np_probs)
print
(
"log numpy probs:\n"
, log_np_probs)
|
1
2
3
4
5
6
7
|
import
torch
import
numpy as np
input
=
torch.autograd.Variable(torch.rand(
1
,
3
))
print
(
input
)
print
(
'softmax={}'
.
format
(torch.nn.functional.softmax(
input
, dim
=
1
)))
print
(
'logsoftmax={}'
.
format
(np.log(torch.nn.functional.softmax(
input
, dim
=
1
))))
|
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/qq_36556893/article/details/105889978 。
最后此篇关于Pytorch中Softmax和LogSoftmax的使用详解的文章就讲到这里了,如果你想了解更多关于Pytorch中Softmax和LogSoftmax的使用详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 已关闭 3 年前。 此帖子于去年编辑
据我所知,在使用 GPU 训练和验证模型时,GPU 内存主要用于加载数据,向前和向后。据我所知,我认为 GPU 内存使用应该相同 1) 训练前,2) 训练后,3) 验证前,4) 验证后。但在我的例子中
我正在尝试在 PyTorch 中将两个复数矩阵相乘,看起来 the torch.matmul functions is not added yet to PyTorch library for com
我正在尝试定义二分类问题的损失函数。但是,目标标签不是硬标签0,1,而是0~1之间的一个 float 。 Pytorch 中的 torch.nn.CrossEntropy 不支持软标签,所以我想自己写
我正在尝试让 PyTorch 与 DataLoader 一起工作,据说这是处理小批量的最简单方法,在某些情况下这是获得最佳性能所必需的。 DataLoader 需要一个数据集作为输入。 大多数关于 D
Pytorch Dataloader 的迭代顺序是否保证相同(在温和条件下)? 例如: dataloader = DataLoader(my_dataset, batch_size=4,
PyTorch 的负对数似然损失,nn.NLLLoss定义为: 因此,如果以单批处理的标准重量计算损失,则损失的公式始终为: -1 * (prediction of model for correct
在PyTorch中,new_ones()与ones()有什么区别。例如, x2.new_ones(3,2, dtype=torch.double) 与 torch.ones(3,2, dtype=to
假设我有一个矩阵 src带形状(5, 3)和一个 bool 矩阵 adj带形状(5, 5)如下, src = tensor([[ 0, 1, 2], [ 3, 4,
我想知道如果不在第 4 行中使用“for”循环,下面的代码是否有更有效的替代方案? import torch n, d = 37700, 7842 k = 4 sample = torch.cat([
我有三个简单的问题。 如果我的自定义损失函数不可微会发生什么? pytorch 会通过错误还是做其他事情? 如果我在我的自定义函数中声明了一个损失变量来表示模型的最终损失,我应该放 requires_
我想知道 PyTorch Parameter 和 Tensor 的区别? 现有answer适用于使用变量的旧 PyTorch? 最佳答案 这就是 Parameter 的全部想法。类(附加)在单个图像中
给定以下张量(这是网络的结果 [注意 grad_fn]): tensor([121., 241., 125., 1., 108., 238., 125., 121., 13., 117., 12
什么是__constants__在 pytorch class Linear(Module):定义于 https://pytorch.org/docs/stable/_modules/torch/nn
我在哪里可以找到pytorch函数conv2d的源代码? 它应该在 torch.nn.functional 中,但我只找到了 _add_docstr 行, 如果我搜索conv2d。我在这里看了: ht
如 documentation 中所述在 PyTorch 中,Conv2d 层使用默认膨胀为 1。这是否意味着如果我想创建一个简单的 conv2d 层,我必须编写 nn.conv2d(in_chann
我阅读了 Pytorch 的源代码,发现它没有实现 convolution_backward 很奇怪。函数,唯一的 convolution_backward_overrideable 函数是直接引发错
我对编码真的很陌生,现在我正在尝试将我的标签变成一种热门编码。我已经完成将 np.array 传输到张量,如下所示 tensor([4., 4., 4., 4., 4., 4., 4., 4., 4.
我正在尝试实现 text classification model使用CNN。据我所知,对于文本数据,我们应该使用一维卷积。我在 pytorch 中看到了一个使用 Conv2d 的示例,但我想知道如何
我有一个多标签分类问题,我正试图用 Pytorch 中的 CNN 解决这个问题。我有 80,000 个训练示例和 7900 个类;每个示例可以同时属于多个类,每个示例的平均类数为 130。 问题是我的
我是一名优秀的程序员,十分优秀!