numpy.repeat() 与 torch tensor.repeat() 对比

  • numpy.repeat()官方文档链接
numpy.repeat(a, repeats, axis=None)
	Repeat elements of an array.

Parameters: a: array_like
				Input array.

			repeats: int or array of ints
			The number of repetitions for each element. repeats is broadcasted to fit the 														
			shape of the given axis.

			axis: int, optional
			The axis along which to repeat values. By default, use the flattened input 
			array, and return a flat output array.

Returns:	repeated_array: ndarray
			Output array which has the same shape as a, except along the given axis.

example

>>>import numpy as np
# axis 默认值为 None, 相当与先将x进行flatten(展平)然后每个元素单独进行重复
>>>x = np.array([3])
>>>np.repeat(x, 4)
array([3, 3, 3, 3])
>>>x = np.array([[1, 2], [3, 4]])
>>>np.repeat(x, 2)
array([1, 1, 2, 2, 3, 3, 4, 4])

# axis 指定数值时,表示以第几个维的元素为单位进行repeat
>>>x = np.array([[1, 2], [3, 4]])
>>>np.repeat(x, 2, 0) # 第 0 维的元素是[1, 2], [3, 4]
array([[1, 2],
       [1, 2],
       [3, 4],
       [3, 4]])
>>>np.repeat(x, 2, 1) # 第 1 维的元素是 1, 2, 3, 4
array([[1, 1, 2, 2],
       [3, 3, 4, 4]])

# 参数repeats也可以是数组,对应下标的数值对应按照第axis维的元素的重复次数
>>>np.repeat(x, [3, 2], 0) # [1, 2] 重复 3 次, [2, 4] 重复 2 次
array([[1, 2],
       [1, 2],
       [1, 2],
       [3, 4],
       [3, 4]])
>>>np.repeat(x, [3, 2], 1) # 1, 3 重复 3 次, 2, 4 重复 2 次
array([[1, 1, 1, 2, 2],
       [3, 3, 3, 4, 4]])
  • tensor.repeat()官方文档[链接](https://pytorch.org/docs/stable/generated/torch.Tensor.repeat.html?highlight=repeat#torch.Tensor.repeat
Tensor.repeat(*sizes) → Tensor
	Repeats this tensor along the specified dimensions.
	Unlike expand(), this function copies the tensor’s data.

example

>>>import torch
# *size 从后往前执行,最后一个元素对应在第-1维度重复的次数,倒数第二个维度对应tensor的第-2维度重复的次数,以此类推
>>>x = torch.tensor([1, 2, 3])
>>>x.repeat(4, 2) 
'''
先重复第-1维度2次变为[1, 2, 3, 1, 2, 3]
没有第-2维就在最前面插入新的维度变为[[1, 2, 3, 1, 2 ,3]] 
再重复第-2维度4次变为
'''
tensor([[1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3]])

>>>x.repeat(4, 2, 1)
'''
先重复第-1维度1次变为[1, 2, 3]
没有第1维就在最前面插入新的维度变为[[1, 2, 3]] 
再重复第-2维度2次变为 
[[1, 2, 3],
 [1, 2, 3]]
没有第-3维就在最前面插入新的维度变为
[[[1, 2, 3],
[1, 2, 3]]]
再重复第-3维度4次
'''
tensor([[[1, 2, 3],
         [1, 2, 3]],

        [[1, 2, 3],
         [1, 2, 3]],

        [[1, 2, 3],
         [1, 2, 3]],

        [[1, 2, 3],
         [1, 2, 3]]])

>>>x = torch.tensor([[0], [1]])
>>>x.repeat(3, 2, 4)
'''
先重复第-1维度4次,变为
[[0, 0, 0, 0], 
 [1, 1, 1, 1]]
在重复第-2维度2次,变为
[[0, 0, 0, 0], 
 [1, 1, 1, 1],
 [0, 0, 0, 0], 
 [1, 1, 1, 1]]
没有第-3维度,新增-3维度并重复3次,变为
[[[0, 0, 0, 0], 
 [1, 1, 1, 1],
 [0, 0, 0, 0], 
 [1, 1, 1, 1]],
 [[0, 0, 0, 0], 
 [1, 1, 1, 1],
 [0, 0, 0, 0], 
 [1, 1, 1, 1]],
 [[0, 0, 0, 0], 
 [1, 1, 1, 1],
 [0, 0, 0, 0], 
 [1, 1, 1, 1]]]
'''
### PyTorch `torch.matmul` `@` 操作符的区别 #### 行为差异 在 PyTorch 中,`torch.matmul` 和 `@` 操作符均用于实现矩阵乘法功能。然而,在处理多维张量时,两者的具体行为存在细微差别。 对于二维张量(即矩阵),二者的行为完全一致[^1]。例如: ```python import torch A = torch.tensor([[1, 2], [3, 4]]) B = torch.tensor([[5, 6], [7, 8]]) C_matmul = torch.matmul(A, B) C_at = A @ B print(C_matmul) # 输出 [[19, 22], [43, 50]] print(C_at) # 输出 [[19, 22], [43, 50]] ``` 但对于三维及以上维度的张量,`torch.matmul` 的广播机制更为灵活。它会沿着最后一个两个维度进行矩阵乘法运算,并支持更复杂的形状匹配逻辑。而 `@` 操作符则严格遵循 NumPy 风格的广播规则,可能无法满足一些复杂场景的需求[^3]。 #### 使用场景对比 当涉及简单矩阵相乘或者低阶张量计算时,可以任意选用这两种方式之一完成任务;但在面对高阶张量操作需求时,则需优先考虑采用 `torch.matmul` 方法来获得更好的兼容性和灵活性。 另外值得注意的是,“@”符号作为算子形式更加简洁直观,在代码可读性方面具有一定优势。 #### 性能比较 就性能而言,官方文档并未明确指出两者之间存在显著差距。通常来说,由于底层实现相同,因此它们应该具有相似的速度表现。不过实际应用过程中可能会因为硬件环境等因素影响而导致微小波动,但这部分差异一般不会成为决定因素。 ```python # 测试性能示例 import timeit setup_code = """ import torch a = torch.rand((100, 100)) b = torch.rand((100, 100)) """ matmul_time = min(timeit.repeat('torch.matmul(a, b)', setup=setup_code, number=1000)) at_operator_time = min(timeit.repeat('a @ b', setup=setup_code, number=1000)) print(f"MatMul Time: {matmul_time}") print(f"@ Operator Time: {at_operator_time}") ``` 上述脚本可用于粗略评估两种方法的时间消耗情况。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值