(一)tensorflow笔记:Tensor数据类型

常见的数据类型载体

  • list
  • np.array
  • tf.tensor

list: 可以存储不同数据类型,缺点不适合存储较大的数据,如图片

np.array: 解决同类型大数据数据的载体,方便数据运算,缺点是在深度学习之前就设计好的,不支持GPU

tf.tensor:更适合深度学习,支持GPU

Tensor是什么

scalar: 1.1
vector:[1.1] , [1.1,2.2,……]
matrix:[[1,2,3,],[4,5,6],[7,8,9]]
torsor:rank > 2 (一般指的是维度大于2的数据)

但是,在tensorflow里面我们把数据的数据都叫tensor

Tensor支持的类型

  • int, float, double
  • bool
  • string

创建不同类型的Tensor

import tensorflow as tf
# 创建一个整型的数据
tf.constant(1)
# Out[3]: <tf.Tensor: shape=(), dtype=int32, numpy=1>
# 注意因为这里的constant就是一个普通的tensor,不要理解为常量了(TF1.0是代表一个常量)# 创建一个浮点类型的数据
tf.constant(1.)
# Out[4]: <tf.Tensor: shape=(), dtype=float32, numpy=1.0># 若给定一个浮点型的数据,但是指定为int类型会报错
tf.constant(2.2,dtype=tf.int32)
# TypeError: Cannot convert 2.2 to EagerTensor of dtype int32# 给一数指定双精度
tf.constant(2.,dtype=tf.double)
# Out[6]: <tf.Tensor: shape=(), dtype=float64, numpy=2.0># 创建bool类型的数据
tf.constant([True,False])
# Out[7]: <tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, False])># 创建字符串型数据(很少用)
tf.constant("hello,world")
# Out[8]: <tf.Tensor: shape=(), dtype=string, numpy=b'hello,world'>

Tensor Property

下面开始介绍Tensor常用的属性

tf.device

import tensorflow as tf
with tf.device("cpu"):a = tf.constant([1])
with tf.device("gpu"):b = tf.range(6)print(a.device)
print(b.device)
# 数据在CPU和GPU上的转换
aa = a.gpu()
print(aa.device)
bb = b.cpu()
print(bb.device)

输出结果:
/job:localhost/replica:0/task:0/device:CPU:0
/job:localhost/replica:0/task:0/device:GPU:0
/job:localhost/replica:0/task:0/device:GPU:0
/job:localhost/replica:0/task:0/device:CPU:0

转换为numpy

c = tf.range(10)
#Out[14]: <tf.Tensor: shape=(10,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>
c.numpy()
#Out[15]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 

Tensor的维度与形状

d = tf.range(10)
d.shape
# Out[17]: TensorShape([10])d.ndim
# Out[18]: 1# 用rank查看tensor的维度(秩):返回的是一个tensor类型的数据
tf.rank(d)
# Out[19]: <tf.Tensor: shape=(), dtype=int32, numpy=1>
tf.rank(tf.ones([3,4,2]))
# Out[20]: <tf.Tensor: shape=(), dtype=int32, numpy=3># tf.name
# 是Tensorflow1.0中的概念,现在基本已经淘汰了

python中判断一个数据是不是Tensor

import numpy as np
import tensorflow as tfa = tf.constant(1.)
b = tf.constant([True,False])
c = tf.constant("hello,world")
d = np.arange(4)isinstance(a,tf.Tensor)
# Out[27]: True
tf.is_tensor(b)
# Out[28]: True
tf.is_tensor(d)
# Out[29]: Falsea.dtype,b.dtype,c.dtype,d.dtype
# Out[32]: (tf.float32, tf.bool, tf.string, dtype('int32'))a.dtype == tf.float32
Out[33]: True
c.dtype == tf.string
Out[34]: True

数据类型的转换

a = np.arange(5)
a.dtype
Out[36]: dtype('int32')
aa = tf.convert_to_tensor(a)  # numpy数据转化方法为.astype(np.int64)
# Out[38]: <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>
aa = tf.convert_to_tensor(a, dtype=tf.float32)
# Out[40]: <tf.Tensor: shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.], dtype=float32)># 用头tf.cast()数据转化
tf.cast(aa,dtype = tf.float32)
# Out[41]: <tf.Tensor: shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.], dtype=float32)>
aaa = tf.cast(aa,dtype=tf.double)
# Out[43]: <tf.Tensor: shape=(5,), dtype=float64, numpy=array([0., 1., 2., 3., 4.])>
tf.cast(aaa,dtype=tf.int32)
# Out[44]: <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])># bool 与 int 的转化
b = tf.constant([0,1])
tf.cast(b,tf.bool)
# Out[46]: <tf.Tensor: shape=(2,), dtype=bool, numpy=array([False,  True])>
bb = tf.cast(b,dtype=tf.bool)
tf.cast(bb,tf.int32)
# Out[48]: <tf.Tensor: shape=(2,), dtype=int32, numpy=array([0, 1])>

tf.Variable

tf.Variable在tensorflow中相比tf.constan一样也是Tensor,tf.Variable特指Tensorflow中哪些可以优化的参数,比如自动求导。tf.Variable可以理解为是专门为神经网络所设立的一个类型。

a = tf.range(5)
b = tf.Variable(a)
# Out[51]: <tf.Variable 'Variable:0' shape=(5,) dtype=int32, numpy=array([0, 1, 2, 3, 4])>
b.dtype
# Out[52]: tf.int32
b.name
# Out[53]: 'Variable:0'
b = tf.Variable(a, name = "input_data")
b.name
# Out[55]: 'input_data:0'
b.trainable
# Out[56]: Trueisinstance(b,tf.Tensor)
# Out[57]: False
isinstance(b,tf.Variable)
# Out[58]: True
tf.is_tensor(b)
# Out[59]: Trueb.numpy()
# Out[60]: array([0, 1, 2, 3, 4])

将Tensor类型转化为python中的数据类型

a = tf.ones([])
# Out[63]: <tf.Tensor: shape=(), dtype=float32, numpy=1.0>
a.numpy()
# Out[64]: 1.0
int(a)
# Out[65]: 1
float(a)
# Out[66]: 1.0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/311201.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

吃鸡蛋-优先队列

题目描述 小林养了一只母鸡&#xff0c;一连 n 天&#xff0c;每天都可以生下若干个鸡蛋。在第 i 天&#xff0c;母鸡会生下 eggs[i] 个鸡蛋&#xff0c;这些鸡蛋将会在days[i] 天后&#xff08;也就是说&#xff0c;第 i days[i] 天时&#xff09;腐烂&#xff0c;变得无法食…

交通标志识别教程(二)

项目结构图 下载好项目压缩包后解压&#xff0c;得到以上的文件&#xff0c;首先将画红圈的文件删除&#xff08;如果有&#xff09; 安装软件 解压软件包 安装Anaconda 直接下一步&#xff0c;到了这个页面全部勾选&#xff0c;否则不会添加添加环境变量。 安装Pycharm …

深度长文:Power Automation 帮助企业实现数字化转型

01自动化始于您在Ignite 2019上&#xff0c;我们宣布将Flow更改为Power Automate&#xff0c;并在UI Flow连接器的公开预览中引入了机器人流程自动化&#xff08;RPA&#xff09;。我们对几种激动人心的功能感到兴奋&#xff0c;这些功能将在今年全面上市&#xff0c;并想花一点…

数字电路技术可能出现的简答题_数字电子技术复习题(本科)

1数字电子技术复习题(本科)一、简答题&#xff1a;1、简述组合电路和时序电路各自的特点是什么&#xff1f;答&#xff1a;组合电路的特点&#xff1a;任何时刻电路的稳定输出&#xff0c;仅取决于该时刻各个输入变量的取值&#xff0c;组合电路是由门电路组合而成&#xff0c;…

hdu2544 最短路-Floyd算法

Problem Description 在每年的校赛里&#xff0c;所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候&#xff0c;却是非常累的&#xff01;所以现在他们想要寻找最短的从商店到赛场的路线&#xff0c;你可以帮助他们…

Pandas的Timestamp转为datetime 类型

在Pandas中我们在处理时间序列的时候常用的方法有&#xff1a; pd.to_datetime()pd.date_range() pandas生成时间索引 # pd.date_range() index pd.date_range("20210101",periods20) index Out[29]: DatetimeIndex([2021-01-01, 2021-01-02, 2021-01-03, 2021-…

什么样的女生适合学计算机?

我需要在这一行中加一些字数&#xff0c;为什么呢&#xff1f;因为我的字数不够300字&#xff0c;无法声明原创&#xff0c;所以我会在这里加一些字数&#xff0c;它们是白色的&#xff0c;你应该看不到&#xff0c;如果你此刻看到了&#xff0c;那你真的太机智了。300字&#…

ora-00923数据类型不一致_小白学 Python(2):基础数据类型(上)

如果我的文章对您有帮助&#xff0c;请关注支持下作者的公众号&#xff1a;极客挖掘机&#xff0c;获取最新干货推送&#xff1a;)人生苦短&#xff0c;我选Python引言前文传送门小白学 Python(1)&#xff1a;开篇接触一门新的语言&#xff0c;肯定要先了解它的基础数据类型。啥…

Floyd最短路径算法

Floyd最短路径算法适用于节点(n<200)的图&#xff0c;允许边权值为负。 代码如下&#xff1a; #include <iostream> using namespace std; const int N 110; const int INF 1 << 30; int g[N][N]; int n, m;void Floyd() {for (int k 1; k < n; k)for (i…

如何将项目上传到github详细完整版

今天介绍如何利用pycharm创建一个新的项目&#xff0c;然后将项目上传到github&#xff0c;以便日后的学习记录&#xff0c;和版本管理。比如现在我想创建一个项目专门用来学习和研究时间序列算法。 创建虚拟环境 # 创建一个新的虚拟环境 conda create -n TimeSeries python3…

[Abp vNext微服务实践] - 搭建租户管理服务

一、简介ABP模板项目中已经提供了租户登录和管理功能&#xff0c;但是模板项目是单体应用结构&#xff0c;无法单独部署租户服务&#xff0c;所以难以满足微服务的需求。本篇文章将会介绍搭建ABP租户管理服务&#xff0c;并单独部署应用。二、创建工程2.1 创建TenantService.Ho…

hdu2544 最短路-邻接表+优先队列实现dijkstra

Problem Description 在每年的校赛里&#xff0c;所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候&#xff0c;却是非常累的&#xff01;所以现在他们想要寻找最短的从商店到赛场的路线&#xff0c;你可以帮助他们…

cli2弃用了吗 vue_vue cli - 2 升级到 3的问题汇总

基于已有项目从cli 2项目升级到cli 3项目中&#xff0c;需要修改的几项多页面更改vue.config.js配置&#xff0c; 遍历src/views目录下的所有入口文件&#xff0c;生成多个entry对象const site require(yargs).argv.siteconst glob require(glob)const path require(path)mo…

C++实现dijkstra单源最短路径算法-邻接表+优先队列

dijkstra单源最短路径算法不允许边权值为负&#xff0c;适用的图范围可以很大。 代码如下&#xff1a; #include <iostream> #include <queue> #include <vector> #include <string> using namespace std; const int N 1e8; bool done[N]; int dis[N…

编写高性能的C#代码(三)使用SPAN

原文来自互联网&#xff0c;由长沙DotNET技术社区编译。如译文侵犯您的署名权或版权&#xff0c;请联系小编&#xff0c;小编将在24小时内删除。作者介绍&#xff1a;史蒂夫戈登&#xff08;Steve Gordon&#xff09;是Microsoft MVP&#xff0c;Pluralsight的作者&#xff0c;…

pycharm配置git拉取项目代码,并添加版本控制

安装Git 打开网页进入git官网&#xff0c;找到git官网下载地址&#xff0c;下载git工具并且安装。 pycharm配置git 点击File -> Settings -> Version Control -> Git 选择Git安装的路径&#xff0c;点击OK 选择一个项目 进入我们需要拉取的项目&#xff0c;点击…

mpu 配置内存空间_mpu内存保护单元功能及工作原理

一些嵌入式系统使用多任务的操作和控制。这些系统必须提供一种机制来保证正在运行的任务不破坏其他任务的操作。即要防止系统资源和其他一些任务不受非法访问。嵌入式系统有专门的硬件来检测和限制系统资源的访问。它能保证资源的所有权&#xff0c;任务需要遵守一组由操作环境…

poj3981 字符串替换-字符串的基本操作

Description 编写一个C程序实现将字符串中的所有"you"替换成"we" Input 输入包含多行数据 每行数据是一个字符串&#xff0c;长度不超过1000 数据以EOF结束 Output 对于输入的每一行&#xff0c;输出替换后的字符串 Sample Input you are what you do…

.NET Core开发实战(第22课:异常处理中间件:区分真异常与逻辑异常)--学习笔记(上)...

22 | 异常处理中间件&#xff1a;区分真异常与逻辑异常这一节我们来讲解一下错误处理的最佳实践系统里面异常处理&#xff0c;ASP.NET Core 提供了四种方式1、异常处理页2、异常处理匿名委托方法3、IExceptionFilter4、ExceptionFilterAttribute源码链接&#xff1a;https://gi…

MYSQL开窗函数详解

基本概念 MYSQL8.0支持窗口函数&#xff08;Window Function&#xff09;&#xff0c;也称分析函数。窗口函数与组分聚合函数类似&#xff0c;但是每一行数据都会生成一个结果。如果我们将mysql与pandas中的DataFrame做类比学习的话他们的对应关系如下&#xff1a; SQL分组聚…