目录
创建数据
torch.empty()
torch.zeros()
torch.ones()
torch.tensor()
torch.rand()
数学运算
torch.add()
torch.sub()
torch.matmul()
索引操作
创建数据
torch.empty()
创建一个空张量矩阵.

格式:
torch.empty(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format) → Tensor
参数:

size: 生成矩阵的形状, 必选
dtype: 数据类型, 默认为 None
例子:
`# 创建一个形状为[2, 2]的矩阵
a = torch.empty(2, 2)
print(a)

创建一个形状为[3, 3]的矩阵

b = torch.empty(3, 3)
print(b)`
输出结果:

tensor([[0., 0.],
[0., 0.]])
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])

torch.zeros()
创建一个全零矩阵.

格式:
torch.zeros(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
参数:

size: 生成矩阵的形状, 必选
dtype: 数据类型, 默认为 None
例子:
`# 创建一个形状为[2, 2]的全零数组
a = torch.zeros([2, 2], dtype=torch.float32)
print(a)

创建一个形状为[3, 3]的全零数组

b = torch.zeros([3, 3], dtype=torch.float32)
print(b)`
输出结果:

tensor([[0., 0.],
[0., 0.]])
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])

torch.ones()
创建一个全一矩阵.

格式:
torch.ones(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
参数:

size: 生成矩阵的形状, 必选
dtype: 数据类型, 默认为 None
例子:
`# 创建一个形状为[2, 2]的全一数组
a = torch.ones([2, 2], dtype=torch.float32)
print(a)

创建一个形状为[3, 3]的全一数组

b = torch.ones([3, 3], dtype=torch.float32)
print(b)`
输出结果:

tensor([[1., 1.],
[1., 1.]])
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])

torch.tensor()
通过数据创建张量.

格式:
torch.tensor(data, *, dtype=None, device=None, requires_grad=False, pin_memory=False) → Tensor
参数:

data: 数据 (数组, 元组, ndarray, scalar)
dtype: 数据类型, 默认为 None
例子:
`# 通过数据创建张量
array = np.arange(1, 10).reshape(3, 3)
print(array)
print(type(array))

tensor = torch.tensor(array)
print(tensor)
print(type(tensor))`
输出结果:

[[1 2 3]
[4 5 6]
[7 8 9]]

<class 'numpy.ndarray'>
tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=torch.int32)

<class 'torch.Tensor'>

torch.rand()
创建一个 0~1 随机数的张量矩阵.

格式:
torch.rand(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
参数:

size: 生成矩阵的形状, 必选
dtype: 数据类型, 默认为 None
例子:
# 创建形状为[2, 2]的随机数矩阵rand = torch.rand(2, 2)print(rand)
输出结果:

tensor([[0.6209, 0.3424],
[0.3506, 0.7986]])

数学运算
torch.add()
返回相加的张量.

格式:
torch.add(input, other, *, out=None) → Tensor
例子:
`# 张量相加
input1 = torch.tensor([[1, 2], [3, 4]])
print(input1)

input2 = torch.tensor([[4, 3], [2, 1]])
print(input2)

output = torch.add(input1, input2)
print(output)`
输出结果:

tensor([[1, 2],
[3, 4]])
tensor([[4, 3],
[2, 1]])
tensor([[5, 5],
[5, 5]])

注: 相加的张量形状必须一致, 否则会报错.

torch.sub()
返回相减的张量.

例子:
`# 张量相减
input1 = torch.tensor([[1, 2], [3, 4]])
print(input1)

input2 = torch.tensor([[4, 3], [2, 1]])
print(input2)

output = torch.sub(input1, input2)
print(output)`
输出结果:

tensor([[1, 2],
[3, 4]])
tensor([[4, 3],
[2, 1]])
tensor([[-3, -1],
[ 1, 3]])

torch.matmul()
例子:
`# 张量矩阵相乘
input1 = torch.tensor([[1, 1, 1]])
print(input1)

input2 = torch.tensor([[3], [3], [3]])
print(input2)

output = torch.matmul(input1, input2)
print(output)`
输出结果:

tensor([[1, 1, 1]])
tensor([[3],
[3],
[3]])
tensor([[9]])

索引操作
索引 (index) 可以帮助我们快速的找到张量中的特定信息.
例子:
# 简单的索引操作ones = torch.ones([3, 3])print(ones[: 2])print(ones[:, : 2])
调试输出:

tensor([[1., 1., 1.],
[1., 1., 1.]])
tensor([[1., 1.],
[1., 1.],
[1., 1.]])
到此这篇关于PyTorch一小时掌握之基本操作篇的文章就介绍到这了,谢谢大家的阅读

更多相关文章

  1. 读深度学习《深度学习简介》
  2. 卫向军 | 从正交分解架构设计到矩阵式团队管理
  3. (lintcode)第454题 矩阵面积
  4. (lintcode)第336题斐波那契数列
  5. (lintcode)第28题 搜索二维矩阵
  6. Pytorch
  7. Numpy
  8. 基于安全压缩感知的大数据隐私保护
  9. 图 - 邻接矩阵广度优先遍历(C语言)

随机推荐

  1. 实现详解Ajax+php数据交互并且局部刷新页
  2. 实现简单的php购物车代码
  3. 十分钟带你了解PHP实现爬虫的过程
  4. PHP与Web页面交互操作实例解析
  5. php ip2long为什么会出现负数?怎么解决?
  6. 手把手教你用php实现图片上传功能
  7. 绝对值得推荐的10本PHP书籍!
  8. 2021年,为什么你该学PHP?!!
  9. 详解五种常见的PHP设计模式
  10. PHP标准库 (SPL)之Countable用法示例