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. oracle基础知识小结

    一.查看oracle用户.角色信息1.查看所有用户 select * from all_users; select * from user_users; 2.查看用户或角色系统权限 select * ...

  2. super方法

    继承 __init__是python中的构造函数,用于属性的初始化. 如果在子类中定义了构造函数,python默认不会调用父类的构造函数,父类里的属性就不会集成到子类. super 用于调用父类(超类 ...

  3. js模式-观察者模式

    // 主题,接收状态变化,触发每个观察者 class Subject { constructor() { this.state = 0 this.observers = [] } getState() ...

  4. ProxyPass与ProxyPassReverse及ProxyPassMatch的概述

    转载自:https://blog.csdn.net/xiaokui_wingfly/article/details/51481653 apache中的mod_proxy模块主要作用就是进行url的转发 ...

  5. Day1-T4

    原题目 Describe:注意是“两次及以上”而不是“两种及以上”!! code: #include<bits/stdc++.h> using namespace std; int k,m ...

  6. ng-options用法详解

    ng-options属性可以在表达式中使用数组或对象来自动生成一个select中的option列表.ng-options与ng-repeat很相似,很多时候可以用ng-repeat来代替ng-opti ...

  7. [Python3] RSA的加解密和签名/验签实现 -- 使用pycrytodome

    Crypto 包介绍: pycrypto,pycrytodome 和 crypto 是一个东西,crypto 在 python 上面的名字是 pycrypto 它是一个第三方库,但是已经停止更新,所以 ...

  8. 八、SAP中使用MOVE来赋值

    一.使用MOVE和使用等号的效果是等同的,代码如下: 二.效果如下:

  9. 四个因素决定Essay写作段落长度

    段落是一篇Essay的基石,写好Essay应从写好段落开始.那么Essay写作中一个段落多长为好?英语和修辞学教授理查德·诺德奎斯特著文介绍了一些专家的观点.从以下的译文可以看到,段落长度虽然没有固定 ...

  10. DevOps元数据管理

    元数据是自动化运维的基础,对元数据的管理和查询贯穿整个运维的生命周期.我们从一个元数据的使用场景开始: 双十一抢购火热进行中,某电商后端实例的日志中出现了502错误码,运维平台监测到该异常并发送告警给 ...