把张量变成int_Python|张量创建操作[4]

本文介绍了PyTorch中将张量转换为int以及多种张量创建操作,如torch.logspace、torch.eye、torch.empty等,详细解析了各个函数的参数和使用示例,帮助理解张量的创建和操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建操作续2

torch.logspace(start, end, steps=100, base=10.0, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

返回一个一维的张量,使用对数,从start到end。比如在start处,值是10,base是2,那么start对应的值输出为2的10次方。参数start(float):点集的开始值

end:点集的结束值

steps(int):默认100,在start和end之间点集的数量

base(float):对数函数的底值,默认10.0

out(Tensor,可选参数):输出张量

dtype

layout

device

requires_grad

例子>>> torch.logspace(start=-10, end=10, steps=5)tensor([ 1.0000e-10,  1.0000e-05,  1.0000e+00,  1.0000e+05,  1.0000e+10])>>> torch.logspace(start=0.1, end=1.0, steps=5)tensor([  1.2589,   2.1135,   3.5481,   5.9566,  10.0000])>>> torch.logspace(start=0.1, end=1.0, steps=1)tensor([1.2589])>>> torch.logspace(start=2, end=2, steps=1, base=2)tensor([4.0])

torch.eye(n, m=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

返回一个2维的张量,对角是1,其余位置是0。参数n(int):行数

m(int,可选参数):列数,默认等于n

out(Tensor,可选参数)

dtype

layout

device

requires_grad

例子>>> torch.eye(3)tensor([[ 1.,  0.,  0.],        [ 0.,  1.,  0.],        [ 0.,  0.,  1.]])

torch.empty(*size, out=None, dtype=None, layout=torch.strided, device=None, requiresgrad=False, pinmemory=False) → Tensor

返回一个未初始化的张量,形状由size指定 参数:size(int...):整数序列,可以是集合,list,tuple等等

out

dtype

layout

device

requires_grad

pin_memory(bool,可选参数):只对cpu张量有效,默认False,用来确定是否分配在一个固定的内存

memory_format

例子>>> torch.empty(2, 3)tensor(1.00000e-08 *       [[ 6.3984,  0.0000,  0.0000],        [ 0.0000,  0.0000,  0.0000]])

torch.emptylike(input, dtype=None, layout=None, device=None, requiresgrad=False, memoryformat=torch.preserveformat) → Tensor

返回一个未初始化的张量,和oneslike()和zeroslike()方法类同。

例子>>> torch.empty((2,3), dtype=torch.int64)tensor([[ 9.4064e+13,  2.8000e+01,  9.3493e+13],        [ 7.5751e+18,  7.1428e+18,  7.5955e+18]])

torch.full(size, fillvalue, out=None, dtype=None, layout=torch.strided, device=None, requiresgrad=False) → Tensor

返回一个指定值,且全部填充了的张量 参数size(int...)

fill_value:输出张量需要填充的值

out

dtype

layout

device

requires_grad

例子>>> torch.full((2, 3), 3.141592)tensor([[ 3.1416,  3.1416,  3.1416],        [ 3.1416,  3.1416,  3.1416]])

torch.fulllike(input, fillvalue, out=None, dtype=None, layout=torch.strided, device=None, requiresgrad=False, memoryformat=torch.preserve_format) -> Tensor

参考函数名称后缀带like的使用

torch.quantizepertensor(input, scale, zero_point, dtype) → Tensor

根据给出的scale和零点,把浮点类型的张量量化成的新张量 参数input(Tensor):需要转化的张量

scale(float):

zero_point(int):原点的位移

dtype

例子>>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8)tensor([-1.,  0.,  1.,  2.], size=(4,), dtype=torch.quint8,       quantization_scheme=torch.per_tensor_affine, scale=0.1, zero_point=10)>>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8).int_repr()tensor([ 0, 10, 20, 30], dtype=torch.uint8)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值