tensorflow的类型问题

部署运行你感兴趣的模型镜像

tensorflow中的类型dtype与numpy中的类型一致可以互用,但是写程序时最好不要互用。

DType定义了以下对象:

  • tf.float16:16位半精度浮点。
  • tf.float32:32位单精度浮点。
  • tf.float64:64位双精度浮点。
  • tf.bfloat16:16位截断浮点。
  • tf.complex64:64位单精度复合体。
  • tf.complex128:128位双精度复合体。
  • tf.int8:8位有符号整数。
  • tf.uint8:8位无符号整数。
  • tf.uint16:16位无符号整数。
  • tf.uint32:32位无符号整数。
  • tf.uint64:64位无符号整数。
  • tf.int16:16位有符号整数。
  • tf.int32:32位有符号整数。
  • tf.int64:64位有符号整数。
  • tf.bool:布尔值。
  • tf.string:字符串。
  • tf.qint8:量化的8位有符号整数。
  • tf.quint8:量化的8位无符号整数。
  • tf.qint16:量化的16位有符号整数。
  • tf.quint16:量化的16位无符号整数。
  • tf.qint32:量化的32位有符号整数。
  • tf.resource:处理一个可变资源。
  • tf.variant:任意类型的值。

在项目训练时,对于生成和读取tfrecord文件时编码译码过程中出现了,没有考虑到传递变量的默认属性。

对于图片向量和标签向量先前作此处理:

               # 读取图片
                image_data = Image.open(filename)# type : Image
                # 灰度化
                image_data=image_data.convert('L')
                image_data = np.array(image_data)

                # 将图片转化为bytes
                image = image_data.tobytes()

                list=np.zeros(shape=[CaptchaNumber*AlphabetNumber])
                for i,num in enumerate(label):
                    list[i*AlphabetNumber+num]=1

                list=list.tobytes()
                example = tf.train.Example(features=tf.train.Features(feature={
                        'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image])),
                        'label': tf.train.Feature(bytes_list=tf.train.BytesList(value=[list])),
                    }))
                tfrecord_writer.write(example.SerializeToString())

而在另一个读取程序中核心代码是

features = tf.parse_single_example(serialized_example,
                                       features={
                                           'image': tf.FixedLenFeature([], tf.string),
                                           'label': tf.FixedLenFeature([], tf.string),
                                       })
# 获取图片数据
    image = tf.decode_raw(features['image'], tf.uint8)
#获取标签数据
label = tf.decode_raw(features['label'], tf.float64)

tf.decode_raw需要指定使用何种数据类型翻译出字节字符串,先前存入tfrecord的数据类型是什么,那么这里也要填入什么类型。

先前的语句  image_data = np.array(image_data) 好像没有指定类型,numpy对于np.array()未指定类型的操作方式是默认最小能支持
image_data的类型,这里image_data的数据大小是0-255,所以默认选择tf.uint8
对于label,先前语句是list=np.zeros(shape=[CaptchaNumber*AlphabetNumber])这里数据默认是np.float64,也就是tf.float64。



您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

### TensorFlow 中支持的数据类型 #### 数值类型 TensorFlow 支持多种数值类型的张量,这些类型对于执行高效的数学运算至关重要。常见的数值类型包括 `tf.float32`、`tf.float64`、`tf.int32` 和 `tf.int64` 等[^3]。 ```python import tensorflow as tf # 创建不同数值类型的张量 float_tensor = tf.constant([1.0, 2.0], dtype=tf.float32) int_tensor = tf.constant([1, 2], dtype=tf.int32) print("Float Tensor:", float_tensor) print("Int Tensor:", int_tensor) ``` #### 字符串类型 除了数值类型外,TensorFlow 还允许创建字符串类型的张量,这对于处理文本数据非常有用。 ```python string_tensor = tf.constant(['hello', 'world'], dtype=tf.string) print("String Tensor:", string_tensor) ``` #### 布尔类型 布尔类型的张量用于表示逻辑真或假,在条件判断和其他逻辑操作中有广泛应用。 ```python bool_tensor = tf.constant([True, False], dtype=tf.bool) print("Bool Tensor:", bool_tensor) ``` #### 复数类型 为了满足某些特定应用场景的需求,如信号处理领域,TensorFlow 提供了复数类型的支持,比如 `tf.complex64` 和 `tf.complex128`。 ```python complex_tensor = tf.constant([1 + 2j, 3 + 4j], dtype=tf.complex64) print("Complex Tensor:", complex_tensor) ``` #### 类型转换 当需要改变现有张量的数据类型时,可以使用 `tf.cast()` 函数来进行显式的类型转换。 ```python original_tensor = tf.constant([1, 2], dtype=tf.int32) converted_tensor = tf.cast(original_tensor, dtype=tf.float32) print("Original Int Tensor:", original_tensor) print("Converted Float Tensor:", converted_tensor) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值