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

扬中网站建设好么抖音推广运营

扬中网站建设好么,抖音推广运营,淘宝联盟怎么做自已的网站,网站关键词的使用系列文章目录 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.mmbaike.com/news/71635.html

相关文章:

  • 企业网站建设组织人员可行性分析快速排名软件案例
  • 教育行业网站制作品牌广告
  • 济宁做网站建设的公司企业员工培训总结
  • 宿迁网站建设开发磁力岛引擎
  • 大良网站建设价位广东最新消息
  • 安徽芜湖网站建设网络推广优化品牌公司
  • 网站制作模板图片临沂头条新闻今日头条
  • 我自己怎么建网站百度天眼查
  • 课程网站怎么做网络营销的方式都有哪些
  • 魅族官方网站挂失手机找到怎么做seo推广营销公司
  • 杭州网站建设网络公司友情链接买卖平台
  • 建设网站公司哪好360摄像头海澳門地区限制解除
  • 一个网站可以有几个关键词今日全国疫情最新消息
  • 英文学习网站html静态网页制作
  • wordpress 页面跳转seo方案书案例
  • wordpress 头像打岔seo搜索引擎优化就业指导
  • 一个网站有哪些优势网络广告的优势有哪些
  • 相册网站怎么做的软件编程培训学校排名
  • 漂亮的网站维护页面网络营销主要做什么
  • web素材网资源企业网站排名优化价格
  • 网站制作软件安卓版百度运营公司
  • wordpress知更鸟配置seo兼职接单平台
  • 网站用wordpress还是wp推广的公司
  • 上海营销网站制作网站权重怎么提高
  • 做学校网站白杨seo
  • 辽河油田建设有限公司网站百度一下app下载安装
  • 微信网站 手机网站网络营销渠道策略
  • 网站建设开源项目github公司企业网站建设
  • 正规的网站制作电话手机端网站排名
  • 自己做服务器网站网站做成app