【Pytorch】Pytorch中张量的创建和维度的操作

32 篇文章 1 订阅
21 篇文章 2 订阅

书P59开始

1 - Python列表和Numpy数组转换为Pytorch张量

首先导入包

import numpy as np
import torch

1.1 - 转换Python列表为Pytorch张量

torch.tensor([1, 2, 3, 4])  # 转换python列表为Pytorch张量

输出结果为

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

查看张量数据类型

torch.tensor([1, 2, 3, 4]).dtype  # 查看张量数据类型 

输出

torch.int64

1.2 - 指定张量数据类型

torch.tensor([1, 2, 3, 4], dtype=torch.float32)

输出

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

查看张量数据类型

torch.tensor([1, 2, 3, 4], dtype=torch.float32).dtype

输出

torch.float32

1.3 - 转换迭代器(range)为张量

torch.tensor(range(10)) # 转换迭代器为张量

输出

tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

1.4 - 转换numpy数组为张量

查看numpy数组类型

np.array([1,2,3,4]).dtype  # 查看numpy数组类型

输出

dtype('int32')

转换numpy数组为pytorch张量

torch.tensor(np.array([1,2,3,4]))  # 转换numpy数组为pytorch张量

输出

tensor([1, 2, 3, 4], dtype=torch.int32)

转换后pytorch张量的类型

torch.tensor(np.array([1,2,3,4])).dtype  # 转换后pytorch张量的类型

输出

torch.int32

1.5 - pytorch和numpy的默认浮点类型

pytorch的默认浮点类型为32位单精度

torch.tensor([1.0, 2.0, 3.0, 4.0]).dtype

输出

torch.float32

numpy的默认浮点类型为64位双精度

torch.tensor(np.array([1.0, 2.0, 3.0, 4.0])).dtype

输出

torch.float64

1.6 - 列表嵌套创建张量

torch.tensor([[1, 2, 3], [4, 5, 6]])  # 注意外面还有一对中括号!

输出

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

1.7 - torch.float与torch.int的互换

从torch.float转换到torch.int

torch.randn(3, 3).to(torch.int)

输出

tensor([[ 0,  0, -1],
        [ 0, -1,  0],
        [ 0,  0,  0]], dtype=torch.int32)

从torch.float转换到torch.int

torch.randint(0, 5, (3, 3)).to(torch.float)

输出

tensor([[1., 0., 0.],
        [0., 4., 0.],
        [0., 2., 2.]])


2 - 张量的创建方式

2.1 通过torch.tensor函数创建张量

如果预先有数据(包括列表和numpy数组),可以用这个方法进行转换,具体代码见第一节。

2.2 通过pytorch内置的函数创建张量

通过指定张量的形状,返回给定形状的张量。

torch.rand() 服从[0,1)上的均匀分布

torch.rand(3, 3)  # 生成3×3的矩阵,服从[0,1)上的均匀分布

输出

tensor([[0.8495, 0.6578, 0.6414],
        [0.4056, 0.3283, 0.8018],
        [0.3692, 0.6454, 0.9068]])

torch.randn() 服从标准正态分布

torch.randn(2, 3, 4)  # 生成2×3×4的张量,服从标准正态分布

输出

tensor([[[-1.4592,  0.8455,  0.2028,  1.4529],
         [-0.6733,  0.1475, -0.6303, -1.0275],
         [ 1.0311, -0.0114,  0.0187,  1.1736]],
        [[-0.6119,  0.9253, -0.6474, -1.1126],
         [-0.2808,  1.8824, -1.3947,  0.6264],
         [-0.4868, -0.0196,  1.3352,  0.9196]]])

torch.zeros()

torch.zeros(2, 2, 2)

输出

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

torch.ones()

torch.ones(1, 2, 3)

输出

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

torch.eye() 生成单位矩阵

torch.eye(3)

输出

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

torch.randint(0, 10, (3, 3)) 生成[0,10)之间均匀分布整数的3×3矩阵

torch.randint(0, 10, (3, 3))

输出

tensor([[8, 2, 1],
        [9, 7, 5],
        [5, 4, 4]])

2.3 - 通过已知张量创建形状相同的张量

torch.zeros_like(t)

torch.ones_like(t)

torch.rand_like(t)

torch.randn_like(t)


2.4 - 通过已知张量创建形状不同但数据类型相同的张量 (一般很少用到)



3 - 和张量维度相关的方法

t.ndimension() 获取维度的数目

t.nelement() 获取该张量的总元素数目

t.size() 获取该张量每个维度的大小,调用方法

t.shape 获取该张量每个维度的大小,访问属性

t.view(3, 4) 向量改变形状为3×4的矩阵

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值