Tensor数据类型
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数据类型的更多相关文章
- Pytorch的tensor数据类型
基本类型 torch.Tensor是一种包含单一数据类型元素的多维矩阵. Torch定义了七种CPU tensor类型和八种GPU tensor类型: Data tyoe CPU tensor GPU ...
- 吴裕雄--天生自然TensorFlow2教程:Tensor数据类型
list: [1,1.2,'hello'] ,存储图片占用内存非常大 np.array,存成一个静态数组,但是numpy在深度学习之前就出现了,所以不适合深度学习 tf.Tensor,为了弥补nump ...
- [Pytorch]Pytorch的tensor变量类型转换
原文:https://blog.csdn.net/hustchenze/article/details/79154139 Pytorch的数据类型为各式各样的Tensor,Tensor可以理解为高维矩 ...
- pytorch入坑一 | Tensor及其基本操作
由于之前的草稿都没了,现在只有重写…. 我好痛苦 本章只是对pytorch的常规操作进行一个总结,大家看过有脑子里有印象就好,知道有这么个东西,需要的时候可以再去详细的看,另外也还是需要在实战中多运用 ...
- pytorch中tensor张量数据基础入门
pytorch张量数据类型入门1.对于pytorch的深度学习框架,其基本的数据类型属于张量数据类型,即Tensor数据类型,对于python里面的int,float,int array,flaot ...
- 05_pytorch的Tensor操作
05_pytorch的Tensor操作 目录 一.引言 二.tensor的基础操作 2.1 创建tensor 2.2 常用tensor操作 2.2.1 调整tensor的形状 2.2.2 添加或压缩t ...
- Tensor基本理论
Tensor基本理论 深度学习框架使用Tensor来表示数据,在神经网络中传递的数据均为Tensor. Tensor可以将其理解为多维数组,其可以具有任意多的维度,不同Tensor可以有不同的数据类型 ...
- Tensor基础实践
Tensor基础实践 飞桨(PaddlePaddle,以下简称Paddle)和其他深度学习框架一样,使用Tensor来表示数据,在神经网络中传递的数据均为Tensor. Tensor可以将其理解为多维 ...
- 学习笔记TF048:TensorFlow 系统架构、设计理念、编程模型、API、作用域、批标准化、神经元函数优化
系统架构.自底向上,设备层.网络层.数据操作层.图计算层.API层.应用层.核心层,设备层.网络层.数据操作层.图计算层.最下层是网络通信层和设备管理层.网络通信层包括gRPC(google Remo ...
随机推荐
- bzoj 3894 文理分科【最小割+dinic】
谁说这道和2127是双倍经验的来着完全不一样啊? 数组开小会TLE!数组开小会TLE!数组开小会TLE! 首先sum统计所有收益 对于当前点\( (i,j) \)考虑,设\( x=(i-1)*m+j ...
- Java之简单的四则运算
简单的四则运算 请你编写程序实现能处理两个数的+.-.*./.%的表达式程序.数据的输入/输出全部使用标准输入/输出,输入数据的第一行为你需要计算表达式的个数,从第2行开始,每一行为你计算的一个表达式 ...
- Code:Blocks 中文乱码问题原因分析和解决方法
下面说说修改的地方. 1.修改源文件保存编码在:settings->Editor->gernal settings 看到右边的Encoding group Box了吗?如下图所示: Use ...
- 《windows核心编程系列》十六谈谈内存映射文件
内存映射文件允许开发人员预订一块地址空间并为该区域调拨物理存储器,与虚拟内存不同的是,内存映射文件的物理存储器来自磁盘中的文件,而非系统的页交换文件.将文件映射到内存中后,我们就可以在内存中操作他们了 ...
- Linux的防火墙概念
#linux的防火墙概念#因为如果你不关防火墙,很可能运行 django.nginx.mysql出错#防火墙可能会阻挡端口流量的 出口#也会阻挡外来请求的 入口 #selinux iptables f ...
- NDK(18)eclipse 使用C++ STL
1.引用库 在Application.mk 中使用 APP_STL := stlport_static 等. APP_ABI := x86 armeabi APP_PLATFORM := androi ...
- python general
everything in python is object assignment is binding a name to an object one object can have several ...
- 【学习笔记】SIFT尺度不变特征 (配合UCF-CRCV课程视频)
SIFT尺度不变特征 D. Lowe. Distinctive image features from scale-invariant key points, IJCV 2004 -Lecture 0 ...
- 191 Number of 1 Bits 位1的个数
编写一个函数,输入是一个无符号整数,返回的是它所有 位1 的个数(也被称为汉明重量).例如,32位整数 '11' 的二进制表示为 00000000000000000000000000001011,所以 ...
- Android 给按钮添加监听事件
在安卓开发中,如果要给一个按钮添加监听事件的话,有以下三种实现方式 1.方式一 public class MainActivity extends ActionBarActivity { @Overr ...