zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然TensorFlow2教程: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)
  • 相关阅读:
    iptables防火墙(RHEL6)
    漏洞扫描与网络抓包
    服务安全与监控
    Typecho反序列化漏洞
    python类,魔术方法等学习&&部分ssti常见操作知识点复习加深
    PHAR伪协议&&[CISCN2019 华北赛区 Day1 Web1]Dropbox
    [GXYCTF2019]禁止套娃 1 &无参数RCE
    PHP代码审计学习(1)
    Yii2安装完kartik组件后,使用时报错
    收藏博客
  • 原文地址:https://www.cnblogs.com/tszr/p/12104220.html
Copyright © 2011-2022 走看看