• tf.cast()数据类型转换


    tf.cast()函数的作用是执行 tensorflow 中张量数据类型转换,比如读入的图片如果是int8类型的,一般在要在训练前把图像的数据格式转换为float32。

    cast定义:

    cast(x, dtype, name=None)
    第一个参数 x:   待转换的数据(张量)
    第二个参数 dtype: 目标数据类型
    第三个参数 name: 可选参数,定义操作的名称

    int32转换为float32:

    import tensorflow as tf

    t1 = tf.Variable([1,2,3,4,5])
    t2 = tf.cast(t1,dtype=tf.float32)

    print 't1: {}'.format(t1)
    print 't2: {}'.format(t2)

    with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(t2)
    print t2.eval()
    # print(sess.run(t2))
    输出:

    t1: <tf.Variable 'Variable:0' shape=(5,) dtype=int32_ref>
    t2: Tensor("Cast:0", shape=(5,), dtype=float32)
    [ 1. 2. 3. 4. 5.]

    tensorflow中的数据类型列表:


    数据类型 Python 类型 描述
    DT_FLOAT tf.float32 32 位浮点数.
    DT_DOUBLE tf.float64 64 位浮点数.
    DT_INT64 tf.int64 64 位有符号整型.
    DT_INT32 tf.int32 32 位有符号整型.
    DT_INT16 tf.int16 16 位有符号整型.
    DT_INT8 tf.int8 8 位有符号整型.
    DT_UINT8 tf.uint8 8 位无符号整型.
    DT_STRING tf.string 可变长度的字节数组.每一个张量元素都是一个字节数组.
    DT_BOOL tf.bool 布尔型.
    DT_COMPLEX64 tf.complex64 由两个32位浮点数组成的复数:实数和虚数.
    DT_QINT32 tf.qint32 用于量化Ops的32位有符号整型.
    DT_QINT8 tf.qint8 用于量化Ops的8位有符号整型.
    DT_QUINT8 tf.quint8 用于量化Ops的8位无符号整型.
    ---------------------
    作者:-牧野-
    来源:CSDN
    原文:https://blog.csdn.net/dcrmg/article/details/79747814
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    devexpress toolbar 填充整行宽度
    2. Rust的三板斧 安全,迅速,并发
    1. rust的优点
    谈谈我对sku的理解(3)----页面效果
    谈谈我对sku的理解(2)----数据库设计
    谈谈我对sku的理解(1)
    我眼里的奇酷手机360OS
    Oracle中的wm_concat()函数
    获取java本地系统信息 Properties
    java 获取用户的ip都是 127.0.0.1
  • 原文地址:https://www.cnblogs.com/jfdwd/p/11184172.html
Copyright © 2020-2023  润新知