PyTorch深度学习框架 的基础知识

目录

1.pyTorch检查是否安装成功

 2.PyTorch的张量tensor 基础创建方式(三种)

 2.2用列表创建tensor

2.2使用元组创建 tensor

 2.3使用ndarray创建创建 tensor

2.4 快速创建tensor的常用方法

3.pyTorch中的张量tensor的常用属性 

4. tensor中的基础数据类型 

5.张量tensor的基础运算 

 6.改变tensor的形状

7.tensor聚合操作 

8.tensor索引 

9.矩阵乘法 

10.tensor张量的自动微分(重点) 

11.复合函数求导 


1.pyTorch检查是否安装成功

import torch


torch.cuda_version  #查看cuda版本
'12.1'
torch.cuda.is_available()   #检测cuda是否能使用

True

#打印cuda设备,即显卡名字
torch.cuda.get_device_name()  #检测可以使用的gpu

'NVIDIA GeForce GTX 1060'

torch.cuda.get_device_name(0)
'NVIDIA GeForce GTX 1060'

 2.PyTorch的张量tensor 基础创建方式(三种)

PyTorch与TensorFlow中的张量一样,都叫做tensor,
tensor和numpy里的ndarray是一个意思,不同点:tensor可以在GPU上加速计算

创建的tensor返回值都是一个一维数组

import torch
import numpy as np

 2.2用列表创建tensor

torch.tensor([6,2])
tensor([6, 2])
torch.tensor([6,2], dtype = torch.int32)  #指定数据类型

 tensor([6, 2], dtype=torch.int32)

2.2使用元组创建 tensor

torch.tensor((6,2))

 tensor([6, 2])

 2.3使用ndarray创建创建 tensor

torch.tensor(np.array([6,2]))

 tensor([6, 2], dtype=torch.int32)

np.array([6, 2])

 array([6, 2])

2.4 快速创建tensor的常用方法

快速创建tensor的常用方法, 和numpy中的routines的方法一样

例如:ones, zeros, full, eye, random.randn, random.normal, arange....

#创建一个0到1之间的随机数组成的tensor
torch.rand((2, 3))
tensor([[0.6626, 0.1513, 0.6840],
        [0.1509, 0.0196, 0.4638]])
torch.rand(2, 3)
tensor([[0.6335, 0.3162, 0.9308],
        [0.5522, 0.2508, 0.6437]])
#创建一个两行三列的标准正态分布,均值为0
torch.randn(2,3)

 tensor([[ 0.0580, 1.5962, -0.6440],

             [ 0.9074, 2.2482, -0.3065]])

torch.randn((2,3))

 tensor([[ 0.3305, 0.7139, -0.0988],

             [-0.1087, 0.1576, -0.9505]])

torch.ones(2,3)

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

             [1., 1., 1.]])

torch.zeros(2,3)

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

             [0., 0., 0.]])

3.pyTo

猜你喜欢

转载自blog.csdn.net/Hiweir/article/details/146989210
今日推荐