当前位置: 首页 > article >正文

1、PyTorch介绍与张量的创建

1. 大佬视频

1、PyTorch介绍与张量的创建

2. python 代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :torch_learning01.py
# @Time      :2024/11/16 18:37
# @Author    :Jason Zhang
import torch
import numpy as np

torch.set_printoptions(sci_mode=False, precision=3)

if __name__ == "__main__":
    run_code = 0
    data = [[1, 2], [3.0, 4]]
    data_tensor = torch.tensor(data)
    print(f"data={data},type(data)={type(data)}")
    print(f"data_tensor={data_tensor},type(data_tensor)={type(data_tensor)}")
    print(f"data_tensor.dtype={data_tensor.dtype}")
    data_numpy = np.array([[1, 2], [3, 4]])
    data_torch = torch.from_numpy(data_numpy)
    print(f"data_numpy={type(data_numpy)},type(data_torch)={type(data_torch)}")
    b = torch.arange(12, dtype=torch.float32).reshape((3, 4))
    b_ones_like = torch.ones_like(b)
    b_zeros_like = torch.zeros_like(b)
    b_rand_like = torch.rand_like(b)
    print(f"b=\n{b}")
    print(f"b_ones_like=\n{b_ones_like}")
    print(f"b_zeros_like=\n{b_zeros_like}")
    print(f"b_rand_like=\n{b_rand_like}")
    rand_shape23 = torch.rand((2, 3))
    print(f"rand_shape23=\n{rand_shape23}")
    ones_shape23 = torch.ones((2, 3))
    print(f"ones_shape23=\n{ones_shape23}")
    zeros_shape23 = torch.zeros((2, 3))
    print(f"zeros_shape23=\n{zeros_shape23}")
    zeros_shape23_dtype = zeros_shape23.dtype
    print(f"zeros_shape23_dtype={zeros_shape23_dtype}")
    zeros_shape23_shape = zeros_shape23.shape
    print(f"zeros_shape23_shape={zeros_shape23_shape}")
    zeros_shape23_device = zeros_shape23.device
    print(f"zeros_shape23_device={zeros_shape23_device}")
    cuda_is_available = torch.cuda.is_available()
    print(f"cuda_is_available=\n{cuda_is_available}")
    zeros_shape23 = zeros_shape23.to('cuda')
    print(f"zeros_shape23_device={zeros_shape23.device}")
    np_23 = np.random.random((2, 3))
    np_23_is_tensor = torch.is_tensor(np_23)
    print(f"{np_23} is {np_23_is_tensor} torch.tensor")
    np_23_torch = torch.from_numpy(np_23)
    np_23_torch_is_tensor = torch.is_tensor(np_23_torch)
    print(f"{np_23_torch} is {np_23_torch_is_tensor} torch.tensor")
    one_tensor = torch.tensor(1.0)
    one_tensor_is_nonzero = torch.is_nonzero(one_tensor)
    print(f"one_tensor={one_tensor}")
    print(f"one_tensor_is_nonzero={one_tensor_is_nonzero}")
    rand_23 = torch.rand((2, 3))
    rand_23_numel = torch.numel(rand_23)
    print(f"{rand_23} is {rand_23_numel}")
    torch_arange = torch.arange(0, 5)
    print(f"torch_arange=\n{torch_arange}")
    #   torch_range = torch.range(start=0, end=5)
    #   print(f"torch_range=\n{torch_range}")
    torch_arange2 = torch.arange(1, 6) % 5
    print(f"torch_arange2=\n{torch_arange2}")
    for i in torch.arange(7):
        print(f"i={i}")
    torch_li = torch.linspace(1, 10, 5)
    print(f"torch_li={torch_li}")
    torch_eye5 = torch.eye(5)
    print(f"torch_eye5=\n{torch_eye5}")
    torch_eye24 = torch.eye(2, 4)
    print(f"torch_eye24=\n{torch_eye24}")
    torch_full_314 = torch.full([3, 4], 3.14)
    torch_ones_314 = 3.14 * torch.ones((3, 4))
    print(f"torch_full_314=\n{torch_full_314}")
    print(f"torch_ones_314=\n{torch_ones_314}")
    cat1 = torch.rand((2, 3))
    cat2 = torch.rand((2, 3))
    torch_cat12 = torch.cat((cat1, cat2), 0)
    torch_cat12_1 = torch.cat((cat1, cat2), 1)
    print(f"cat1=\n{cat1}")
    print(f"cat2=\n{cat2}")
    print(f"torch_cat12=\n{torch_cat12}")
    print(f"torch_cat12_1=\n{torch_cat12_1}")
  • 结果:
data=[[1, 2], [3.0, 4]],type(data)=<class 'list'>
data_tensor=tensor([[1., 2.],
        [3., 4.]]),type(data_tensor)=<class 'torch.Tensor'>
data_tensor.dtype=torch.float32
data_numpy=<class 'numpy.ndarray'>,type(data_torch)=<class 'torch.Tensor'>
b=
tensor([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.]])
b_ones_like=
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])
b_zeros_like=
tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])
b_rand_like=
tensor([[0.334, 0.780, 0.210, 0.252],
        [0.235, 0.528, 0.647, 0.188],
        [0.991, 0.704, 0.177, 0.809]])
rand_shape23=
tensor([[0.377, 0.035, 0.664],
        [0.471, 0.954, 0.777]])
ones_shape23=
tensor([[1., 1., 1.],
        [1., 1., 1.]])
zeros_shape23=
tensor([[0., 0., 0.],
        [0., 0., 0.]])
zeros_shape23_dtype=torch.float32
zeros_shape23_shape=torch.Size([2, 3])
zeros_shape23_device=cpu
cuda_is_available=
True
zeros_shape23_device=cuda:0
[[0.99230163 0.0692696  0.80117093]
 [0.73090885 0.51565045 0.52366436]] is False torch.tensor
tensor([[0.992, 0.069, 0.801],
        [0.731, 0.516, 0.524]], dtype=torch.float64) is True torch.tensor
one_tensor=1.0
one_tensor_is_nonzero=True
tensor([[0.445, 0.668, 0.124],
        [0.717, 0.891, 0.951]]) is 6
torch_arange=
tensor([0, 1, 2, 3, 4])
torch_arange2=
tensor([1, 2, 3, 4, 0])
i=0
i=1
i=2
i=3
i=4
i=5
i=6
torch_li=tensor([ 1.000,  3.250,  5.500,  7.750, 10.000])
torch_eye5=
tensor([[1., 0., 0., 0., 0.],
        [0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 0.],
        [0., 0., 0., 1., 0.],
        [0., 0., 0., 0., 1.]])
torch_eye24=
tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.]])
torch_full_314=
tensor([[3.140, 3.140, 3.140, 3.140],
        [3.140, 3.140, 3.140, 3.140],
        [3.140, 3.140, 3.140, 3.140]])
torch_ones_314=
tensor([[3.140, 3.140, 3.140, 3.140],
        [3.140, 3.140, 3.140, 3.140],
        [3.140, 3.140, 3.140, 3.140]])
cat1=
tensor([[0.868, 0.323, 0.289],
        [0.773, 0.597, 0.806]])
cat2=
tensor([[0.522, 0.376, 0.304],
        [0.854, 0.298, 0.656]])
torch_cat12=
tensor([[0.868, 0.323, 0.289],
        [0.773, 0.597, 0.806],
        [0.522, 0.376, 0.304],
        [0.854, 0.298, 0.656]])
torch_cat12_1=
tensor([[0.868, 0.323, 0.289, 0.522, 0.376, 0.304],
        [0.773, 0.597, 0.806, 0.854, 0.298, 0.656]])

http://www.kler.cn/a/397405.html

相关文章:

  • 15-1.Java 网络编程之 InetAddress(InetAddress 常用静态方法、InetAddress 常用方法)
  • 重置docker版本的octoprint管理员账号密码
  • FreeSWITCH chat 得到的是 Error! Message Not Sent
  • ADS学习笔记 5. 微带天线设计
  • Spring:bean的配置
  • Electron 沙盒模式与预加载脚本:保障桌面应用安全的关键机制
  • 迅睿CMS如何实现文章自动推送百度的便捷方法?
  • 怎样遵守编程规范,减少和控制C++编程中出现的bug?
  • uniapp适配暗黑模式配置plus.nativeUI.setUIStyle适配DarkMode配置
  • phonemizer 获取英文文本句子单词音素 - python实现
  • 智能工厂的设计软件 为了监管控一体化的全能Supervisor 的监督学习 之 序2 架构for认知系统 :机器学习及其行动门上的机器人
  • Gitcode文件历史记录查看和还原
  • 论文解析:基于区块链的去中心化服务选择,用于QoS感知的云制造(四区)
  • C/C++基础知识复习(19)
  • 【Docker容器】一、一文了解docker
  • shell脚本(2)
  • 【分布式】万字图文解析——深入七大分布式事务解决方案
  • 数据结构C语言描述3(图文结合)--双链表、循环链表、约瑟夫环问题
  • 2024智能机器人与自动控制国际学术会议 (IRAC 2024)
  • docker构建多平台容器
  • 前端无感刷新token
  • Vue的局部使用
  • 腾讯IM uniapp微信小程序版本实现迅飞语音听写(流式版)
  • 【机器学习chp2】贝叶斯最优分类器、概率密度函数的参数估计、朴素贝叶斯分类器、高斯判别分析。万字超详细分析总结与思考
  • Typora右键打开文件夹/设置右键打开方式/Windows右键管理器
  • 源码解析-Spring Eureka(更新ing)