pytorch 学习笔记(一)张量创建

版本为0.4.0以上才行
本节笔记对应视频

0.pytorch下张量的属性

在这里插入图片描述

1.通过numpy/list创建

在这里插入图片描述

"""张量的创建方法"""


import torch
import numpy as np

flag = True

if flag:
    arr = np.ones((3,3))
    print(arr)
    print("*"*50)
    print(arr.dtype)

    t = torch.tensor(arr, device= "cuda") # 张量创建可以设置为GPU用于存储,GPU下由于数据需要从cpu迁移到GPU速度会变慢
    t2 = torch.from_numpy(arr) # 和上面一样,但是这种写法共享了arr和t2变量的内存,其中一个改变都会改变
    print(t)
    print("*" * 50)
    print(t2)
    print("*" * 50)
    arr[0][0] = 9
    print(arr)
    print("*" * 50)
    print(t)
    print("*" * 50)
    print(t2)

[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
**************************************************
float64
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], device='cuda:0', dtype=torch.float64)
**************************************************
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
**************************************************
[[9. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
**************************************************
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], device='cuda:0', dtype=torch.float64)
**************************************************
tensor([[9., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)

进程已结束,退出代码 0

2.依据数值创建

在这里插入图片描述

if flag == False:
    out_t = torch.tensor([1])
    print(out_t)
    t = torch.zeros((3,3), out=out_t) #
    print(t,"\n",out_t)
    print(id(t)) # 内存地址
    print(id(out_t))
    print(id(t)==id(out_t))
tensor([1])
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]) 
 tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])
1750855205896
1750855205896
True

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

if flag2:
    out_t = torch.tensor([9,15432,154])
    out_t1 = torch.tensor([9])
    print(out_t)
    t = torch.zeros_like(out_t) # tensor([0, 0, 0])
    print(t)
    t2 = torch.ones(out_t1) # tensor([1., 1., 1., 1., 1., 1., 1., 1., 1.])
    print(t2)
    t3 = torch.ones_like(out_t) # tensor([1, 1, 1])
    print(t3)

    # 自定义维度和单一元素
    t4 = torch.full((3,3),1)
    print(t4)
    #等差数列张量创建方式
    t5 = torch.arange(2,10,2) # 一个偶数列,起始值为2结束为10(左闭右开),相差2 ----tensor([2, 4, 6, 8])
    print(t5)
    #均分数列
    t6 = torch.linspace(2,10,6)

在这里插入图片描述
在这里插入图片描述

   mean = torch.arange(1,5,dtype= torch.float)
    std = torch.arange(1,5,dtype=torch.float)
    t_normal = torch.normal(mean,std) #正态分布
    print(mean)
    print("*"*100)
    print(std)
    print("*"*500)
    print(t_normal)


tensor([1., 2., 3., 4.])
****************************************************************************************************
tensor([1., 2., 3., 4.])
********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
tensor([0.6984, 2.3168, 2.8330, 3.2437])

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员进化不脱发!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值