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

美团网站建设总体需求与目标hao123网址下载到桌面

美团网站建设总体需求与目标,hao123网址下载到桌面,珠宝商城网站模板免费下载,网站建设运营合同模板系列文章目录 Numpy学习——创建数组及常规操作(数组创建、切片、维度变换、索引、筛选、判断、广播) Tensor学习——创建张量及常规操作(创建、切片、索引、转换、维度变换、拼接) 基础学习——numpy与tensor张量的转换 基础学习…

系列文章目录

Numpy学习——创建数组及常规操作(数组创建、切片、维度变换、索引、筛选、判断、广播)
Tensor学习——创建张量及常规操作(创建、切片、索引、转换、维度变换、拼接)
基础学习——numpy与tensor张量的转换
基础学习——关于list、numpy、torch在float和int等数据类型转换方面的总结


文章目录

  • 系列文章目录
  • 前言
    • 1、数据类型介绍
    • 2、numpy与torch的一些数组函数的区别
      • 1、empty()
      • 2、randint()
      • 3、normal()
      • 4、transpose()
    • 3、int和float的转换
      • 1、一个数的转换
      • 2、list类型转换
      • 3、numpy类型转换
      • 4、torch数据类型转换


前言

因为自己最近总是遇到一些list、numpy、torch的数据类型转换错误,特别是不同类型间的float转int或int转float,总是遇到错误,所以在这里总结一下。


1、数据类型介绍

Python中基本数据类型主要可分为以下几种:
1.数字(Number);
2.字符串(String);
3.列表(List);
4.字典(Dictionary);
5.元组(Tuple);

在Python3中,支持的数字类型有:
1.int–整型
2.float–浮点型
3.bool–布尔型
4.fractions–分数
5.complex–复数

2、numpy与torch的一些数组函数的区别

导入包

import torch
import torch.nn as nn
import numpy as np 
import math

1、empty()

空数组:返回给定形状和类型的新数组,而不初始化条目

a = np.empty([3,3]) 
b = torch.empty([3,3])
print(a)
print(b)

结果:

[[6.23042070e-307 4.67296746e-307 1.69121096e-306][3.22647253e-307 2.67018777e-306 1.42413555e-306][1.78019082e-306 1.37959740e-306 2.29178686e-312]]
tensor([[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]])

2、randint()

随机生成整数

a= np.random.randint(40, 100, (3, 4))
b = torch.randint(40, 100, (3, 4))
print(a)
print(b)

结果:

[[46 47 55 88][73 96 44 98][96 41 64 45]]
tensor([[98, 65, 57, 97],[90, 74, 45, 64],[67, 54, 79, 45]])

3、normal()

创建符合正态分布的4行5列数据

a = np.random.normal(0, 1, (4, 5))
b = torch.normal(0, 1, size = (4, 5))   
print(a)
print(b)

结果;

[[-0.97259852  1.51207726  0.54528577  1.1024245   0.47090239][ 0.03231742  0.51741803  0.25911092 -1.14377841 -0.02595822][-0.42955202 -0.25546385  0.74112698 -1.57833126  0.69611583][ 0.08953791  0.32776525  0.74939352 -0.43138969  0.26458097]]
tensor([[ 0.6898,  0.4377,  1.8008, -1.3965, -1.7741],[-0.0722,  0.6072,  0.1556,  0.2961, -0.6501],[-1.6929, -1.0789,  2.0120,  1.0724,  1.6193],[ 1.1412, -0.9807,  0.5462, -0.3795, -1.2053]])

4、transpose()

维度转换函数np和torch的区别

import torch
import numpy as np 
a= np.random.randint(40, 100, (3, 4,5))
b = np.transpose(a,(2,1,0))
print(a.shape)
print(b.shape)# torch.transpose只能由两个维度交换
c = torch.randn(2,3,4)
d = torch.transpose(c,0,1)
print(c.shape)
print(d.shape)

结果:

(3, 4, 5)
(5, 4, 3)
torch.Size([2, 3, 4])
torch.Size([3, 2, 4])

3、int和float的转换

导入包

import torch
import torch.nn as nn
import numpy as np 
import math

1、一个数的转换

a = float(1.0)
b = int(a)
print(a)
print(b)

结果:

1.0
1

2、list类型转换

列表类型转换

a = [0.0567, 9.2345, 8.1986, 4.3333]
c = [1,2,3,4]b = [int(a) for a in a] # 或者用下面这个
# b = list(map(int, a))
# b = [math.ceil(a) for a in a]print(a)
print(b)
d = list(map(float, c))
print(c)
print(d)

结果:

[0.0567, 9.2345, 8.1986, 4.3333]
[0, 9, 8, 4]
[1, 2, 3, 4]
[1.0, 2.0, 3.0, 4.0]

3、numpy类型转换

numpy float 转 int
其他类型转换也是一样的

a = np.array([1, 2], dtype = 'float32')        # dtype参数
print(a.dtype,a)
b = a.astype(np.int8)  
print(b.dtype,b)

结果:

float32 [1. 2.]
int8 [1 2]

astype里的类型还可以填这些:
在这里插入图片描述

4、torch数据类型转换

在Tensor后加.long(), .int(), .float(), .double()

a = torch.tensor([1, 2], dtype =torch.int8)        # dtype参数
print(a.dtype,a)
b = a.float()
print(b.dtype,b)

结果:

torch.int8 tensor([1, 2], dtype=torch.int8)
torch.float32 tensor([1., 2.])

用.to()函数进行转换

a = torch.tensor([1, 2], dtype =torch.uint8)        # dtype参数
print(a.dtype,a)
b = a.to(dtype =torch.float32)
print(b.dtype,b)

结果:

torch.uint8 tensor([1, 2], dtype=torch.uint8)
torch.float32 tensor([1., 2.])

其他类型转换也是一样的。
下面是一些类型的名称。
在这里插入图片描述

http://www.yayakq.cn/news/217724/

相关文章:

  • 广州建设工程信息网站wap网站制作需要多少钱
  • 黄石专业网站建设推广深圳app定制开发红孩儿
  • 视频制作软件免费版进行优化
  • 做外汇网站代理商施工企业筹备建立
  • 太原网站建设技术外包用h5做网站是什么意思
  • 伊犁州住房城乡建设局网站商城网站框架
  • 哈尔滨网站优化页面徐州专业网站建设公司
  • 网站开发的技术难点wordpress 编辑器 高亮 引用
  • flash网站引导页网站访问权限
  • 搞笑幽默网站源码最新小众电商平台
  • 班组安全建设 网站广州市网站建设价格
  • 自己做套现要建网站吗用幽默的语言来形容网站开发
  • 做电子相册的网站如何快速建设推广网站
  • 如何搭建一个论坛网站信息平台网站建设
  • 在家里怎样做网站新手做电商需要投资多少
  • 成都旅游网站建站公司那家好
  • 网站开发的税率是多少钱淮南市网站建设
  • 阳江房产网站凡科网邮箱登陆
  • 厦门网站建设求职简历网站认证打款怎么做分录
  • 车陂手机网站建设电话企业主页的特点包括
  • 宝安做棋牌网站建设哪家公司收费合理一个公司完整的组织架构
  • dede游戏网站模板俄罗斯最新消息今天新闻
  • 自己做网站怎么编代码学做网站论坛会员怎么样
  • 南庄顺德网站建设兰山区建设局网站
  • 银川网站网站建设wordpress页面大小修改
  • 上海网站备案审核交互式网站开发技术asp
  • 石家庄城乡建设局网站网站建设 面试
  • 网站建设售价多少钱解聘 人力资源网站上怎么做
  • 做电影网站失败会员系统免费版
  • 网站建设柒首先金手指6Wordpress怎么做引导页