热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

YDOOKAI:Pytorch:pytorch常用的张量生成与张量定义函数tensor生成与定义函数方法

print('t11_3.type';print('t9_2.type';print('t11_3';t11_3.typetorch

YDOOK AI : Pytorch : pytorch常用的张量生成与张量定义函数 tensor 生成与定义函数方法


1. 源代码展示:

import torch
a = [[1, 2, 3], [4, 5, 6]]
print('a = ', a)
# YDOOK JYLin : 普通的数组不能使用 tensor.dytpe 获取数据类型,需要使用 type(valuable) 获取
print('type(a) = ', type(a))
print()
t1 = torch.tensor(a)
print('t1 = ', t1)
print('t1.type = ', t1.dtype)
print()
t2_1 = torch.zeros(2, 3)
print('t2_1 = ', t2_1)
print('t2_1.type = ', t2_1.dtype)
print()
t2_2 = torch.zeros(3, 2)
print('t2_2 = ', t2_2)
print('t2_2.type = ', t2_2.dtype)
print()
t3 = torch.zeros_like(t1)
print('t3 = ', t3)
print('t3.type = ', t3.dtype)
print()
t4_1 = torch.ones(2, 3)
print('t4_1 = ', t4_1)
print('t4_1.type = ', t4_1.dtype)
print()
t4_2 = torch.ones(2, 3)
print('t4_2 = ', t4_2)
print('t4_2.type = ', t4_2.dtype)
print()
t5 = torch.ones_like(t1)
print('t5 = ', t5)
print('t5.type = ', t5.dtype)
print()
t6 = torch.full(t1.size(), 3.14)
print('t6 = ', t6)
print('t6.type = ', t6.dtype)
print()
t6_2 = torch.full(t1.size(), 3.14, dtype=torch.float64)
print('t6_2 = ', t6_2)
print('t6_2.type = ', t6_2.dtype)
print()
t7 = torch.full_like(t1, 6.28)
print('t7 = ', t7)
print('t7.type = ', t7.dtype)
print()
t7_2 = torch.full_like(t1, 6.28, dtype=torch.float64)
print('t7_2 = ', t7_2)
print('t7_2.type = ', t7_2.dtype)
print()
t8 = torch.empty(t1.size())
print('t8 = ', t8)
print('t8.type = ', t8.dtype)
print()
t8_2 = torch.empty(t1.size(), out=t1)
print('t8_2 = ', t8_2)
print('t8_2.type = ', t8_2.dtype)
print()
t8_3 = torch.empty_like(t1)
print('t8_3 = ', t8_3)
print('t8_3.type = ', t8_3.dtype)
print()
t9 = torch.eye(2, 3)
print('t9 = ', t9)
print('t9.type = ', t9.dtype)
print()
t9_2 = torch.eye(3, 3)
print('t9_2 = ', t9_2)
print('t9_2.type = ', t9_2.dtype)
print()
t10 = torch.arange(1, 10)
print('t10 = ', t10)
print('t10.type = ', t10.dtype)
print()
# YDOOK JYLin : 很有用的 torch.linspace(start, end, steps)
t11 = torch.linspace(1, 10, steps=10)
print('t11 = ', t11)
print('t11.type = ', t11.dtype)
print()
t11_2 = torch.linspace(1, 10, steps=9)
print('t11_2 = ', t11_2)
print('t11_2.type = ', t11_2.dtype)
print()
t11_3 = torch.linspace(1, 10, steps=8)
print('t11_3 = ', t11_3)
print('t11_3.type = ', t11_3.dtype)
print()

2. 输出:

a = [[1, 2, 3], [4, 5, 6]]
type(a) =
t1 = tensor([[1, 2, 3],
[4, 5, 6]])
t1.type = torch.int64
t2_1 = tensor([[0., 0., 0.],
[0., 0., 0.]])
t2_1.type = torch.float32
t2_2 = tensor([[0., 0.],
[0., 0.],
[0., 0.]])
t2_2.type = torch.float32
t3 = tensor([[0, 0, 0],
[0, 0, 0]])
t3.type = torch.int64
t4_1 = tensor([[1., 1., 1.],
[1., 1., 1.]])
t4_1.type = torch.float32
t4_2 = tensor([[1., 1., 1.],
[1., 1., 1.]])
t4_2.type = torch.float32
t5 = tensor([[1, 1, 1],
[1, 1, 1]])
t5.type = torch.int64
t6 = tensor([[3.1400, 3.1400, 3.1400],
[3.1400, 3.1400, 3.1400]])
t6.type = torch.float32
t6_2 = tensor([[3.1400, 3.1400, 3.1400],
[3.1400, 3.1400, 3.1400]], dtype=torch.float64)
t6_2.type = torch.float64
t7 = tensor([[6, 6, 6],
[6, 6, 6]])
t7.type = torch.int64
t7_2 = tensor([[6.2800, 6.2800, 6.2800],
[6.2800, 6.2800, 6.2800]], dtype=torch.float64)
t7_2.type = torch.float64
t8 = tensor([[3.1400, 3.1400, 3.1400],
[3.1400, 3.1400, 3.1400]])
t8.type = torch.float32
t8_2 = tensor([[1, 2, 3],
[4, 5, 6]])
t8_2.type = torch.int64
t8_3 = tensor([[4614253070214989087, 4614253070214989087, 4614253070214989087],
[4614253070214989087, 4614253070214989087, 4614253070214989087]])
t8_3.type = torch.int64
t9 = tensor([[1., 0., 0.],
[0., 1., 0.]])
t9.type = torch.float32
t9_2 = tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
t9_2.type = torch.float32
t10 = tensor([1, 2, 3, 4, 5, 6, 7, 8, 9])
t10.type = torch.int64
t11 = tensor([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
t11.type = torch.float32
t11_2 = tensor([ 1.0000, 2.1250, 3.2500, 4.3750, 5.5000, 6.6250, 7.7500, 8.8750,
10.0000])
t11_2.type = torch.float32
t11_3 = tensor([ 1.0000, 2.2857, 3.5714, 4.8571, 6.1429, 7.4286, 8.7143, 10.0000])
t11_3.type = torch.float32

推荐阅读
  • Day2列表、字典、集合操作详解
    本文详细介绍了列表、字典、集合的操作方法,包括定义列表、访问列表元素、字符串操作、字典操作、集合操作、文件操作、字符编码与转码等内容。内容详实,适合初学者参考。 ... [详细]
  • 本文介绍了贝叶斯垃圾邮件分类的机器学习代码,代码来源于https://www.cnblogs.com/huangyc/p/10327209.html,并对代码进行了简介。朴素贝叶斯分类器训练函数包括求p(Ci)和基于词汇表的p(w|Ci)。 ... [详细]
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • Python瓦片图下载、合并、绘图、标记的代码示例
    本文提供了Python瓦片图下载、合并、绘图、标记的代码示例,包括下载代码、多线程下载、图像处理等功能。通过参考geoserver,使用PIL、cv2、numpy、gdal、osr等库实现了瓦片图的下载、合并、绘图和标记功能。代码示例详细介绍了各个功能的实现方法,供读者参考使用。 ... [详细]
  • EPPlus绘制刻度线的方法及示例代码
    本文介绍了使用EPPlus绘制刻度线的方法,并提供了示例代码。通过ExcelPackage类和List对象,可以实现在Excel中绘制刻度线的功能。具体的方法和示例代码在文章中进行了详细的介绍和演示。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • Python使用Pillow包生成验证码图片的方法
    本文介绍了使用Python中的Pillow包生成验证码图片的方法。通过随机生成数字和符号,并添加干扰象素,生成一幅验证码图片。需要配置好Python环境,并安装Pillow库。代码实现包括导入Pillow包和随机模块,定义随机生成字母、数字和字体颜色的函数。 ... [详细]
  • 关于如何快速定义自己的数据集,可以参考我的前一篇文章PyTorch中快速加载自定义数据(入门)_晨曦473的博客-CSDN博客刚开始学习P ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
author-avatar
昔日重来r_510
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有