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)

吴裕雄--天生自然TensorFlow2教程:Tensor数据类型的更多相关文章

  1. 吴裕雄--天生自然TensorFlow2教程:创建Tensor

    import numpy as np import tensorflow as tf tf.convert_to_tensor(np.ones([2, 3])) tf.convert_to_tenso ...

  2. 吴裕雄--天生自然TensorFlow2教程:测试(张量)- 实战

    import tensorflow as tf from tensorflow import keras from tensorflow.keras import datasets import os ...

  3. 吴裕雄--天生自然TensorFlow2教程:合并与分割

    import tensorflow as tf # 6个班级的学生分数情况 a = tf.ones([4, 35, 8]) b = tf.ones([2, 35, 8]) c = tf.concat( ...

  4. 吴裕雄--天生自然TensorFlow2教程:手写数字问题实战

    import tensorflow as tf from tensorflow import keras from keras import Sequential,datasets, layers, ...

  5. 吴裕雄--天生自然TensorFlow2教程:函数优化实战

    import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himme ...

  6. 吴裕雄--天生自然TensorFlow2教程:反向传播算法

  7. 吴裕雄--天生自然TensorFlow2教程:链式法则

    import tensorflow as tf x = tf.constant(1.) w1 = tf.constant(2.) b1 = tf.constant(1.) w2 = tf.consta ...

  8. 吴裕雄--天生自然TensorFlow2教程:多输出感知机及其梯度

    import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...

  9. 吴裕雄--天生自然TensorFlow2教程:单输出感知机及其梯度

    import tensorflow as tf x = tf.random.normal([1, 3]) w = tf.ones([3, 1]) b = tf.ones([1]) y = tf.con ...

随机推荐

  1. eclipse中svn重新设置账户

    查看svn版本:windows > preference > Team > SVN 1.如果svn插件是svnkit版 只需找到.keyring文件,一般目录是:eclipse安装目 ...

  2. 面试题(9)之 leetcode-189

    题目描述 解法一: /** * @param {number[]} nums * @param {number} k * @return {void} Do not return anything, ...

  3. JQuery 多属性选择节点

    JQuery 1.6.0+以后用prop()代替attr(); 多属性选择节点 $("input[type=checkbox][name='first2'][value='first4']& ...

  4. 实验吧-密码学-Decode、困在栅栏里的凯撒

    Decode 这是一个多重加密. 0x253464253534253435253335253433253661253435253737253464253531253666253738253464253 ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-adjust

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  6. SciKit-Learn 加载数据集

    章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Learn 可视化数据:主成分分析(P ...

  7. 批量go get 代码

    批量 go get 代码 Go

  8. CCCC L3-013. 非常弹的球

    题意: 刚上高一的森森为了学好物理,买了一个“非常弹”的球.虽然说是非常弹的球,其实也就是一般的弹力球而已.森森玩了一会儿弹力球后突然想到,假如他在地上用力弹球,球最远能弹到多远去呢?他不太会,你能帮 ...

  9. 【pwnable.kr】leg

    pwnable从入门到放弃第八题. Download : http://pwnable.kr/bin/leg.cDownload : http://pwnable.kr/bin/leg.asm ssh ...

  10. tableau中图形分析相关设置

    1.柱形堆叠图单元格顶部显示总计值(可通过参考线实现) 2.调节图形单元格的宽窄度 (ctrl + 右键/左键) 3.折线图预测区间 趋势区间线 分析中预测并不是针对所有的日期格式均其作用,比如日期格 ...