如何理解 "torch.randn()" size* 参数参数?
How to understand "torch.randn()" size* parameter arguments?
据我了解,torch.randn(layers/depth, rows, columns)
,执行时可以看到:torch.randn(2, 3, 3)
==> 2层(3x3)矩阵:
tensor([[[ 1.4838, 1.2926, 1.6147],
[ 0.7923, 0.6414, -0.2676],
[-0.1949, 0.3859, -0.6940]],
[[ 0.2454, -1.9215, -0.3078],
[ 0.8544, 0.9726, 0.0330],
[ 0.3579, 0.8247, 2.1288]]])
但是在 size* 参数中添加一个额外的项意味着什么?如:torch.randn(2, 1, 3, 3)
tensor([[[[ 0.6206, -1.3697, -0.2267],
[ 1.0511, 2.3375, -0.9598],
[-0.8148, -0.0911, -2.1211]]],
[[[ 0.0659, 1.0764, 0.6150],
[-1.7226, 0.5038, -0.9544],
[-0.6447, -0.3325, 0.2048]]]])
“1”添加到创建的张量中是什么?
简而言之,您只是添加了一个包含单个元素的维度。注意额外的大括号。
也许重写输出会帮助您理解发生了什么:
torch.randn(2, 3, 3)
> tensor(
tensor(
[ # 1st dim - 1st element
[ # 2nd dim - 1st element
[ 1.4838, # 3rd dim - 1st element
1.2926, # 3rd dim - 2nd element
1.6147], # 3rd dim - 3rd element
[ 0.7923, 0.6414, -0.2676], # 2nd dim - 2nd element ( 3rd dim ... )
[-0.1949, 0.3859, -0.6940] # 2nd dim - 3rd element ( 3rd dim ... )
], #1st dim - 2nd element
[ ## .. same as above
[ 0.2454, -1.9215, -0.3078],
[ 0.8544, 0.9726, 0.0330],
[ 0.3579, 0.8247, 2.1288]
]
])
torch.randn(2, 1, 3, 3)
>
tensor(
[ # 1st dim - 1st element
[ # 2nd dim - 1st element
[ # 3rd dim - 1st element
[ 0.6206, # 4th dim - 1st element
-1.3697, # 4th dim - 2nd element
-0.2267],# 4th dim - 3rd element
[ 1.0511, 2.3375, -0.9598], # 3rd dim - 2nd element
[-0.8148, -0.0911, -2.1211] # 3rd dim - 3rd element
]
# 2nd dim - nothing more ...
], # 1st dim - 2nd element
[ # .. same as above
[
[ 0.0659, 1.0764, 0.6150],
[-1.7226, 0.5038, -0.9544],
[-0.6447, -0.3325, 0.2048]
]
]
])
你引入的每个数字都是指矩阵的一个维度。人类很难想象超过 3 个维度,但计算机可以。
在这种特殊情况下,您可以将额外维度视为类似于批量大小的东西。
据我了解,torch.randn(layers/depth, rows, columns)
,执行时可以看到:torch.randn(2, 3, 3)
==> 2层(3x3)矩阵:
tensor([[[ 1.4838, 1.2926, 1.6147],
[ 0.7923, 0.6414, -0.2676],
[-0.1949, 0.3859, -0.6940]],
[[ 0.2454, -1.9215, -0.3078],
[ 0.8544, 0.9726, 0.0330],
[ 0.3579, 0.8247, 2.1288]]])
但是在 size* 参数中添加一个额外的项意味着什么?如:torch.randn(2, 1, 3, 3)
tensor([[[[ 0.6206, -1.3697, -0.2267],
[ 1.0511, 2.3375, -0.9598],
[-0.8148, -0.0911, -2.1211]]],
[[[ 0.0659, 1.0764, 0.6150],
[-1.7226, 0.5038, -0.9544],
[-0.6447, -0.3325, 0.2048]]]])
“1”添加到创建的张量中是什么?
简而言之,您只是添加了一个包含单个元素的维度。注意额外的大括号。
也许重写输出会帮助您理解发生了什么:
torch.randn(2, 3, 3)
> tensor(
tensor(
[ # 1st dim - 1st element
[ # 2nd dim - 1st element
[ 1.4838, # 3rd dim - 1st element
1.2926, # 3rd dim - 2nd element
1.6147], # 3rd dim - 3rd element
[ 0.7923, 0.6414, -0.2676], # 2nd dim - 2nd element ( 3rd dim ... )
[-0.1949, 0.3859, -0.6940] # 2nd dim - 3rd element ( 3rd dim ... )
], #1st dim - 2nd element
[ ## .. same as above
[ 0.2454, -1.9215, -0.3078],
[ 0.8544, 0.9726, 0.0330],
[ 0.3579, 0.8247, 2.1288]
]
])
torch.randn(2, 1, 3, 3)
>
tensor(
[ # 1st dim - 1st element
[ # 2nd dim - 1st element
[ # 3rd dim - 1st element
[ 0.6206, # 4th dim - 1st element
-1.3697, # 4th dim - 2nd element
-0.2267],# 4th dim - 3rd element
[ 1.0511, 2.3375, -0.9598], # 3rd dim - 2nd element
[-0.8148, -0.0911, -2.1211] # 3rd dim - 3rd element
]
# 2nd dim - nothing more ...
], # 1st dim - 2nd element
[ # .. same as above
[
[ 0.0659, 1.0764, 0.6150],
[-1.7226, 0.5038, -0.9544],
[-0.6447, -0.3325, 0.2048]
]
]
])
你引入的每个数字都是指矩阵的一个维度。人类很难想象超过 3 个维度,但计算机可以。
在这种特殊情况下,您可以将额外维度视为类似于批量大小的东西。