- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个形状为 [batch_size, channel, depth, height, width]
的张量:
torch.Size([1, 1, 32, 64, 64])
数据:
tensor([[[[[-1.8540, -2.8068, -2.7348, ..., -1.9074, -1.8227, -1.4540],
[-2.7012, -4.2785, -3.7421, ..., -3.1961, -2.7786, -1.8042],
[-2.1924, -4.2202, -4.4361, ..., -3.1203, -2.9282, -2.3800],
...,
[-2.7429, -4.3133, -4.4029, ..., -4.4971, -5.3288, -2.8659],
[-3.0169, -4.0198, -3.6886, ..., -3.7542, -4.5010, -2.4040],
[-1.6174, -2.5340, -2.3974, ..., -1.9249, -2.4107, -1.2664]],
[[-2.7840, -3.2442, -3.6118, ..., -3.1365, -2.8342, -1.9516],
[-3.5764, -4.9253, -5.9196, ..., -4.8373, -4.2233, -3.3809],
[-3.1701, -5.0826, -5.6424, ..., -5.2955, -4.6438, -3.4820],
...,
[-4.0111, -6.1946, -5.6582, ..., -6.7947, -6.5305, -4.2866],
[-4.2103, -6.6177, -6.0420, ..., -5.8076, -6.2128, -3.2093],
[-2.3174, -4.1081, -3.7369, ..., -3.5552, -3.1871, -1.9736]],
[[-2.8441, -4.1575, -3.8233, ..., -3.5065, -3.4313, -2.3030],
[-4.0076, -5.4939, -6.2451, ..., -4.6663, -4.9835, -3.1530],
[-3.4737, -5.6347, -6.0232, ..., -5.6191, -5.2626, -3.6109],
...,
[-3.8026, -5.3676, -6.1460, ..., -7.6695, -6.7640, -4.1681],
[-4.4012, -6.1293, -6.1859, ..., -6.0011, -6.1012, -3.5307],
[-2.7917, -4.2264, -4.1388, ..., -4.2080, -3.5555, -1.6384]],
...,
[[-2.2204, -3.5705, -4.3114, ..., -4.2249, -3.9628, -2.9190],
[-3.6343, -5.3445, -6.1638, ..., -6.3998, -6.7561, -4.8491],
[-3.4870, -5.5835, -5.6436, ..., -6.8527, -7.2536, -4.8143],
...,
[-2.4492, -3.7896, -5.4344, ..., -6.2853, -6.0766, -3.7538],
[-2.4723, -3.8393, -4.8480, ..., -5.6503, -5.0375, -3.5580],
[-1.6161, -2.9843, -3.2865, ..., -3.2627, -3.2887, -2.5750]],
[[-2.1509, -3.8303, -4.2807, ..., -3.7945, -3.7561, -3.0863],
[-3.1012, -5.1321, -6.1387, ..., -6.5191, -6.3268, -4.4283],
[-2.8346, -5.0640, -5.4868, ..., -6.6515, -6.5529, -4.3672],
...,
[-2.7278, -4.2538, -4.9776, ..., -6.4153, -6.0100, -3.9929],
[-2.8002, -4.0473, -4.7455, ..., -5.4203, -4.7286, -3.4111],
[-1.7964, -3.2307, -3.6329, ..., -3.2750, -2.3952, -1.9714]],
[[-1.4447, -2.1572, -2.4487, ..., -2.3859, -2.9540, -1.8451],
[-1.8075, -2.8380, -3.5621, ..., -3.8641, -3.5828, -2.7304],
[-1.7862, -2.9849, -3.8364, ..., -4.3380, -4.4745, -2.8476],
...,
[-1.8043, -2.5662, -2.7296, ..., -4.2772, -3.9882, -2.8654],
[-1.2364, -2.5228, -2.7190, ..., -4.1142, -3.6160, -2.2325],
[-1.0395, -1.7621, -2.5738, ..., -2.0349, -1.5140, -1.1625]]]]]
现在为了从中得到预测,我使用
torch.argmax(data, 1)
这应该给我 channel 维度中最大值的位置,但我得到的张量仅包含零。偶max(torch.argmax())
产生0
.
怎么会这样,张量只是一个维度,一个batch,怎么会返回0呢?
为了消除我应用的负值 torch.nn.Sigmoid()
就可以了,但仍然argmax
未能找到最大值。我不明白,怎么可能没有最大值?
numpy.argmax(output.detach().numpy(), 1)
给出相同的输出,全部 0
.
我没有正确使用argmax吗?
最佳答案
关于this page对于 argmax 来说,一切都令人困惑。他们选择的示例是 4x4,因此您无法发现差异
a = torch.randn(5, 3)
print(a)
print(torch.argmax(a, dim=0))
print(torch.argmax(a, dim=1))
输出
tensor([[-1.0329, 0.2042, 2.5499],
[ 0.9893, 0.3913, 0.5096],
[ 0.4951, 0.2260, -0.3810],
[-1.8953, -0.6823, 0.8349],
[-0.6217, 0.4068, -1.0846]])
tensor([1, 4, 0])
tensor([2, 0, 0, 2, 1])
看看 dim=0
中我们如何有 3 个值。这是列的尺寸。所以它告诉你第一列中索引为 1 的元素是该列中的最大值。另一个 dim=1
是行的维度,因此我们有 5 个值。
对于您的示例,您可以计算 argmax
结果的形状:
for i in range(data.dim()):
print("dim", i)
r =torch.argmax(data,i)
print(r.shape)
dim 0
torch.Size([1, 32, 64, 64])
dim 1
torch.Size([1, 32, 64, 64])
dim 2
torch.Size([1, 1, 64, 64])
dim 3
torch.Size([1, 1, 32, 64])
dim 4
torch.Size([1, 1, 32, 64])
对于 dim=0
和 dim=0
你应该得到所有 0,正弦值是 1(索引 = 0)。
I tried that, but then how do I extract the maximum values from argmax and which dimension should it look against?
data = torch.randn(32, 64, 64)
values, indices = data.max(0)
print(values, indices)
values, indices = values.max(0)
print(values, indices)
values, indices = values.max(0)
print(values, indices
)
tensor([[1.9918, 1.6041, 2.6535, ..., 1.5768, 1.7320, 1.8234],
[1.6700, 2.4574, 1.8548, ..., 1.8770, 1.7674, 1.6194],
[1.8361, 1.6800, 1.8982, ..., 1.7983, 2.7109, 2.2166],
...,
[2.7439, 1.6215, 2.9740, ..., 1.7031, 1.4445, 1.6681],
[1.9437, 1.4507, 1.8551, ..., 2.5853, 1.9753, 2.4046],
[1.4198, 2.5250, 1.8949, ..., 3.2618, 2.8547, 2.0487]]) tensor([[ 4, 7, 21, ..., 27, 28, 17],
[16, 27, 18, ..., 29, 30, 19],
[ 6, 16, 14, ..., 22, 24, 29],
...,
[16, 16, 8, ..., 21, 27, 22],
[15, 0, 0, ..., 9, 12, 3],
[30, 14, 9, ..., 23, 20, 14]])
tensor([3.2089, 4.1386, 3.2650, 3.3497, 4.4210, 3.0439, 3.5144, 3.2356, 3.3058,
3.2702, 2.9981, 3.6997, 3.1719, 3.4962, 3.0889, 3.6220, 3.9256, 4.1314,
3.0804, 3.3636, 3.5517, 3.2052, 3.6548, 3.7064, 3.6531, 4.5144, 3.1287,
4.1465, 3.1906, 3.1493, 3.1996, 3.6754, 3.7610, 3.5968, 3.2109, 3.6037,
3.2799, 3.0069, 3.0386, 3.0240, 3.5372, 3.6539, 3.5571, 3.2047, 3.1218,
4.2479, 3.1230, 3.0372, 3.0258, 3.8679, 3.6409, 3.0938, 3.1246, 2.9426,
4.0824, 3.8124, 3.4226, 3.3459, 4.1600, 3.6566, 3.0351, 3.3969, 3.5842,
3.0997]) tensor([17, 21, 30, 62, 62, 63, 43, 31, 45, 63, 20, 4, 58, 23, 22, 43, 54, 30,
15, 28, 13, 4, 4, 28, 6, 52, 53, 19, 33, 20, 3, 1, 14, 40, 0, 0,
46, 62, 58, 45, 28, 50, 4, 55, 25, 5, 21, 16, 27, 32, 10, 19, 38, 30,
48, 27, 20, 9, 2, 39, 55, 58, 32, 6])
tensor(4.5144) tensor(25)
这是按维度,或者简单
m = values.max()
将为您提供最大值。
a = torch.argmax(values)
idx = np.unravel_index(a, values.shape)
将为您提供索引。
关于python - torch.argmax() 无法在包含数据的张量中找到最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56810163/
对于 Pytorch 张量 A : A = tensor([1,0,0], [0,0,0]) 有什么方法可以检查数字 1 是否是张量的元素 A ? 比如是否有一个返回 True
我知道 tf.where 将返回 True 值的位置,以便我可以使用结果的 shape[0] 来获取 True 的数量。 但是,当我尝试使用它时,维度是未知的(这是有道理的,因为它需要在运行时计算)。
我有一个形状为 (None,1024) 的张量 A,我想将它重复 49 次以生成一个形状为 (None, 7,7,1024)。每个 (,1024) 向量都是张量 A 的副本。 我该怎么做? 我试过了
我有两个张量T1(N * D维度)和T2(M * D维度)(M小于N)。 T2 行保证位于 T1 中。对于 T2 中的每一行,有没有办法找到该行匹配的 T1 索引?我能够使用急切执行来解决问题。 im
我是一名优秀的程序员,十分优秀!