【PyTorch框架学习】-创建tensor
2019-06-04 16:37
666 查看
4000
导入库
import torch import numpy as np
1. 先定义数组,再转换为torch向量
a=np.array([2,3.3]) a=torch.from_numpy(a) print(a) #Out:tensor([2.0000, 3.3000], dtype=torch.float64) b=np.ones([2,3])#2行3列值1数组 b=torch.from_numpy(b) print(b) #Out: ''' tensor([[0., 0., 0.], [0., 0., 0.]], dtype=torch.float64) '''
2. 直接定义torch向量
a=torch.tensor([2.,3.2]) print(a) #Out:tensor([2.0000, 3.2000]) b=torch.FloatTensor([2.,3.2])#float数据类型 print(b) #Out:tensor([2.0000, 3.2000]) c=torch.tensor([[2.,3.2],[1.,22.3]]) print(c) #Out: ''' tensor([[ 2.0000, 3.2000], [ 1.0000, 22.3000]]) '''
3. torch中自带的创建向量方法
3.1 Torch.empty() 创建随机数的torch向量
a=torch.empty(1) print(a) #Out:tensor([2.3201e+21])
3.2 torch.Tensor(d1,d2),d1,d2表示维度,没有[ ]
b=torch.Tensor(2,3)#注意没有[].(这里改成小写的tensor会报错。使用大写Tensor才可以表示维度) print(b) #Out: ''' tensor([[-2.3677e+19, 4.5689e-41, 6.6585e-37], [ 0.0000e+00, 4.4842e-44, 0.0000e+00]]) ''' #b=torch.tensor(2,3) #print(b) #Out:TypeError: tensor() takes 1 positional argument but 2 were given
3.3 Torch.IntTensor(d1,d2,d3) 产生随机的整型数据
a=torch.IntTensor(2,3)#2行3列产生随机的整型数据 print(a) #Out: ''' tensor([[24836984, 32678, 40727424], [ 0, 32, 0]], dtype=torch.int32) ''' b=torch.FloatTensor(2,3)#2行3列产生随机的浮点型数据 print(b) #Out: ''' tensor([[0.0000e+00, 0.0000e+00, 1.4013e-45], [0.0000e+00, 4.4842e-44, 0.0000e+00]]) '''
4. set default type 设置默认数据类型
print(torch.tensor([1.2,3]).type()) #Out:torch.FloatTensor torch.set_default_tensor_type(torch.DoubleTensor)#更改默认数据类型 print(torch.tensor([1.2,3]).type()) #Out:torch.DoubleTensor
5. torch自带的随机生成数函数(rand/rand_like,randint)
- rand
- rand_like
- randint(min,max,[N,M])
- randn
- full()
- arange/range
- ones/zeros/eye
- randperm(N)
5.1 rand
生成随机数
a=torch.rand(3,3)#3行3列随机数 print(a) #Out: ''' tensor([[0.1318, 0.8855, 0.1509], [0.4633, 0.1706, 0.9565], [0.8536, 0.9419, 0.3204]]) '''
5.2 rand_like
生成维度相同的随机数
b=torch.rand_like(a) print(b) #Out: ''' tensor([[0.8785, 0.3964, 0.9163], [0.0378, 0.6336, 0.9027], [0.1980, 0.3264, 0.3515]]) '''
5.3 randint(min,max,[N,M])
取值范围是[min,max),N行M列的随机数
a=torch.randint(1,10,[3,3]) print(a) #Out: ''' tensor([[5, 7, 7], [5, 7, 9], [7, 1, 3]]) '''
5.4 randn
生成的是均值为0方差为1的高斯分布
a=torch.randn(3,3) print(a) #Out: ''' tensor([[ 1.8433, 0.5534, -0.5419], [ 0.2904, 0.2480, -0.0665], [ 0.7695, 0.1245, 0.2835]]) '''
5.5 full()
a=torch.full([2,3],7)#2行3列值都为7的数组 print(a) #Out: ''' tensor([[7., 7., 7.], [7., 7., 7.]]) ''' b=torch.full([],7) print(b) #Out:tensor(7.)
5.6 arange/range
a=torch.arange(0,10)#取值[0,10) print(a) #Out:tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) b=torch.arange(0,10,2)#取值[0,10),步长为2 print(b) #Out:tensor([0, 2, 4, 6, 8])
5.7 linspace/logspace#等差数列/等比数列
a=torch.linspace(0,10,steps=4)#0为起始值,10为终值,steps为生成个数 print(a) #Out:tensor([ 0.0000, 3.3333, 6.6667, 10.0000]) b=torch.linspace(0,10,steps=10) print(b) #Out:tensor([ 0.0000, 1.1111, 2.2222, 3.3333, 4.4444, 5.5556, 6.6667, 7.7778, 8.8889, 10.0000]) c=torch.linspace(0,10,steps=11) print(c) #Out:tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]) a=torch.logspace(0,-1,steps=11) print(a) #Out: ''' tensor([1.0000, 0.7943, 0.6310, 0.5012, 0.3981, 0.3162, 0.2512, 0.1995, 0.1585, 0.1259, 0.1000]) ''' b=torch.logspace(0.1,-1,steps=11)#返回一个1维张量,包含在区间 \(10^{start}\) 和 \( 10^{end} \),steps为生成个数 print(b) #Out: ''' tensor([1.2589, 0.9772, 0.7586, 0.5888, 0.4571, 0.3548, 0.2754, 0.2138, 0.1660, 0.1288, 0.1000]) '''
5.8 ones/zeros/eye
a=torch.ones(3,3) print(a) #Out: ''' tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]) ''' b=torch.zeros(3,3) print(b) #Out: ''' tensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]) ''' c=torch.eye(3,3)#单位矩阵 print(c) #Out: ''' tensor([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) '''
5.9 randperm(N)
生成[0,N)的N个随机数
a=torch.randperm(10)#数值0-9的数 print(a) #Out:tensor([1, 4, 6, 3, 9, 0, 8, 2, 5, 7])
相关文章推荐
- windous10下+Anaconda+深度学习框架(TensorFlow cpu/gpu 、Keras、Pytorch)+Cuda+Cudnn+pycharm安装教程及避坑手册
- Pytorch深度学习基础之Tensor 3ff0
- 【异周话题 第 18 期】TensorFlow与PyTorch,深度学习框架你选哪一个?
- 一图看懂深度学习框架对比----Caffe Torch Theano TensorFlow
- 深度学习框架caffe/CNTK/Tensorflow/Theano/Torch的对比
- 深度学习框架之Pytorch学习(一)
- 如何选择深度学习框架 TensorFlow/Torch/Mxnet/Theano
- Pytorch-学习记录 卷积操作——Tensor.size()
- PyTorch深度学习计算机视觉框架
- 如何选择深度学习框架 TensorFlow/Torch/Mxnet/Theano
- 深度学习框架Caffe/CNTK/Tensorflow/Theano/Torch的比较
- 如何在anaconda中创建不同版本(3.5)进行TensorFlow/pytorch安装
- 阿里开源大规模分布式图学习框架:专为Graph嵌入,无缝对接TF/PyTorch
- Pytorch-0.4.0 深度学习之Tensor
- 学习 SSM (Spring + Spring MVC + MyBatis) 框架 (一) IDEA Web 项目创建及配置(未完,正在学习中, 不定时更新)
- PyTorch 深度学习【一】Ubuntu16.04 下安装 PyTorch
- python网络编程学习笔记(10):webpy框架
- 深度学习之PyTorch实战(3)——实战手写数字识别
- PyTorch学习之Window10环境搭建
- pytorch 学习资料