Tensor数据类型

  • list: [1,1.2,'hello'] ,存储图片占用内存非常大
  • np.array,存成一个静态数组,但是numpy在深度学习之前就出现了,所以不适合深度学习
  • tf.Tensor,为了弥补numpy的缺点,更多的是为了深度学习而生
  • tensor:
    • scalar:标量,1.1
    • vector:向量,[1.1],[1.1,2.2,...]
    • matrix: 矩阵,[[1.1,2.2],[3.3,4.4]]
    • tensor:rank>2
  • 数据类型:
    • Int, float, double
    • bool
    • string
  • 定义tensor
tf.constant(1)  # 定义常量,普通的tensor
tf.constant(1.) # 定义常量,普通的tensor
tf.constant([True, False]) # 定义常量,普通的tensor
tf.constant('hello nick')

属性

with tf.device('cpu'):
a = tf.constant([1])
with tf.device('gpu'):
b = tf.constant([1]) a.device # 设备属性
a.gpu() # cpu转gpu
a.numpy() # 获取numpy数据类型
a.shape # 获取a的属性
a.ndim # 获取维度
tf.rank(a) # 获取维度
a.name # 1.+历史遗留问题

数据类型判断

instance(a,tf.Tensor) # 判断是否为tensor
tf.is_tensor(a) # 判断是否为tensor
a.dtype,b.dtype,c.dtype # 判断数据类型

数据类型转换

a = np.arange(5)
aa = tf.convert_to_tensor(a,dtype=tf.int32) # numpy转tensor tf.cast(aa,dtype=tf.float32) # tensor之间数据类型转换 # int --》 bool
b = tf.constant([0,1])
tf.cast(b,dtype=tf.bool) # int --》bool # tf.Variable
a = tf.range(5)
b = tf.Variable(a) # tensor转为Variable后具有求导的特性,即自动记录a的梯度相关信息
b.name # Variable:0 b = tf.Variable(a, name='input_data')
b.name # input_data:0
b.trainable # True isinstance(b,tf.Tensor) # False
isinstance(b,tf.Variable) # True
tf.is_tensor(b) # True # 推荐使用

tensor转numpy

a= tf.range(5)
a.numpy() # a必须是scalar
a = tf.ones([])
a.numpy()
int(a)
float(a)

Tensor数据类型的更多相关文章

  1. Pytorch的tensor数据类型

    基本类型 torch.Tensor是一种包含单一数据类型元素的多维矩阵. Torch定义了七种CPU tensor类型和八种GPU tensor类型: Data tyoe CPU tensor GPU ...

  2. 吴裕雄--天生自然TensorFlow2教程:Tensor数据类型

    list: [1,1.2,'hello'] ,存储图片占用内存非常大 np.array,存成一个静态数组,但是numpy在深度学习之前就出现了,所以不适合深度学习 tf.Tensor,为了弥补nump ...

  3. [Pytorch]Pytorch的tensor变量类型转换

    原文:https://blog.csdn.net/hustchenze/article/details/79154139 Pytorch的数据类型为各式各样的Tensor,Tensor可以理解为高维矩 ...

  4. pytorch入坑一 | Tensor及其基本操作

    由于之前的草稿都没了,现在只有重写…. 我好痛苦 本章只是对pytorch的常规操作进行一个总结,大家看过有脑子里有印象就好,知道有这么个东西,需要的时候可以再去详细的看,另外也还是需要在实战中多运用 ...

  5. pytorch中tensor张量数据基础入门

    pytorch张量数据类型入门1.对于pytorch的深度学习框架,其基本的数据类型属于张量数据类型,即Tensor数据类型,对于python里面的int,float,int array,flaot ...

  6. 05_pytorch的Tensor操作

    05_pytorch的Tensor操作 目录 一.引言 二.tensor的基础操作 2.1 创建tensor 2.2 常用tensor操作 2.2.1 调整tensor的形状 2.2.2 添加或压缩t ...

  7. Tensor基本理论

    Tensor基本理论 深度学习框架使用Tensor来表示数据,在神经网络中传递的数据均为Tensor. Tensor可以将其理解为多维数组,其可以具有任意多的维度,不同Tensor可以有不同的数据类型 ...

  8. Tensor基础实践

    Tensor基础实践 飞桨(PaddlePaddle,以下简称Paddle)和其他深度学习框架一样,使用Tensor来表示数据,在神经网络中传递的数据均为Tensor. Tensor可以将其理解为多维 ...

  9. 学习笔记TF048:TensorFlow 系统架构、设计理念、编程模型、API、作用域、批标准化、神经元函数优化

    系统架构.自底向上,设备层.网络层.数据操作层.图计算层.API层.应用层.核心层,设备层.网络层.数据操作层.图计算层.最下层是网络通信层和设备管理层.网络通信层包括gRPC(google Remo ...

随机推荐

  1. bzoj 2618【半平面交模板】

    #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> usin ...

  2. (数论 欧拉筛法)51NOD 1181 质数中的质数(质数筛法)

    如果一个质数,在质数列表中的编号也是质数,那么就称之为质数中的质数.例如:3 5分别是排第2和第3的质数,所以他们是质数中的质数.现在给出一个数N,求>=N的最小的质数中的质数是多少(可以考虑用 ...

  3. python爬虫抓取哈尔滨天气信息(静态爬虫)

    python 爬虫 爬取哈尔滨天气信息 - http://www.weather.com.cn/weather/101050101.shtml 环境: windows7 python3.4(pip i ...

  4. Android Dialogs(2)最好用DialogFragment创建Dialog

    Creating a Dialog Fragment You can accomplish a wide variety of dialog designs—including custom layo ...

  5. 416 Partition Equal Subset Sum 分割相同子集和

    详见:https://leetcode.com/problems/partition-equal-subset-sum/description/ C++: class Solution { publi ...

  6. Android的handler消息机制

    Hnadler机制中有这么几部分构成,包括 handler.Message.Looper和MessageQueue.要想在一个线程中使用Handler的话必须要有Looper和MessageQueue ...

  7. HDU 5996 博弈

    http://acm.hdu.edu.cn/showproblem.php?pid=5996 博弈论待补. 这题变化了一下,因为注意到奇数层的东西(层数从1开始),对手可以模仿地动,那就相当于没动. ...

  8. Ubuntu卸载软件包

    sudo apt-get autoremove --purge mysql-server-5.0 ,purge连同配置文件一起删除,autoremove自动卸载依赖包sudo apt-get remo ...

  9. 关于如何读取XML文件的一个简单方法

    在平时开发系统功能的时候,我们经常会碰到一些需求需要经常性的发生变化,比如 系统版本.更新日志 等等.这个时候用一个XML文件来替代数据库,就会变的简便很多. 前段时候我也正好需要改个需求,是关于客户 ...

  10. Java文件上传(基础性)

    /** * * 上传文件 * */ public class FileUploadServlet2 extends HttpServlet { protected void doGet(HttpSer ...