一. p4 pytorch基本操作
- x = torch.empty(5, 3);
5行3列的一个张量(矩阵),特需格式是tensor
import torch
x = torch.empty(5, 3)
x
输出:
tensor([[-4.7960e+35, 3.0794e-41, 0.0000e+00],
[ 0.0000e+00, -4.8290e+35, 3.0794e-41],
[-4.8290e+35, 3.0794e-41, -4.8290e+35],
[ 3.0794e-41, 0.0000e+00, 0.0000e+00],
[-2.7661e+13, 4.5565e-41, -2.6833e+13]])
- x = torch.rand(5, 3)
5行3列的随机数
#随机值,tensors是他的一个特需格式,所有计算都是在tensor的基础上运算
x = torch.rand(5, 3)
x
输出:
tensor([[0.5898, 0.6413, 0.3710],
[0.8552, 0.1343, 0.6362],
[0.0649, 0.6644, 0.0517],
[0.4938, 0.4127, 0.6358],
[0.1633, 0.9749, 0.7178]])
- x = torch.zeros(5, 3, dtype=torch.long)
5行3列的零矩阵
x = torch.zeros(5, 3, dtype=torch.long)
x
输出:
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
- x = torch.tensor([5.5, 3])
写入一个矩阵(传入数据) - x = torch.randn_like(x, dtype=torch.float)
构建的大小一样的矩阵
x = torch.tensor([5.5, 3])
x
输出:
tensor([5.5000, 3.0000])
# 构建的矩阵大小一样
x= x.new_ones(5, 3, dtype=torch.double)
x = torch.randn_like(x, dtype=torch.float)
x
输出:
tensor([[ 0.5455, 0.3819, -0.4844],
[-2.3630, 1.4743, 0.2780],
[ 0.5868, 0.3403, -0.2346],
[ 1.5565, 1.4095, -0.7017],
[ 0.2720, 1.6439, -1.3772]])