代码
import torch
from scipy import special
# set torch print options preference
torch.set_printoptions(linewidth=1024)
# input
tensor1d = torch.tensor([1, 2, 3, 4, 5], dtype=torch.int32)
tensor2d = torch.tensor([[1, 2], [3, 4], [5, 6]], dtype=torch.int32)
print(tensor1d)
print(tensor2d)
# calculate
f1d = special.factorial(tensor1d).int()
f2d = special.factorial(tensor2d).int()
# output
print(f1d)
print(f2d)
输出
tensor([1, 2, 3, 4, 5], dtype=torch.int32)
tensor([[1, 2], [3, 4], [5, 6]], dtype=torch.int32)
tensor([ 1, 2, 6, 24, 120], dtype=torch.int32)
tensor([[ 1, 2], [ 6, 24], [120, 720]], dtype=torch.int32)