TensorFlow笔记——数据类型dtype

本文详细介绍了TensorFlow中各种数据类型的使用,包括有符号整型、无符号整型、浮点型、字符串型、布尔型及复数型,涵盖了从基本整数到复杂数据结构的全面解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tensorflow中,主要有以下几种数据类型(dtype),在旧版本中,不用加tf也能使用。
有符号整型

tf.int8:8位整数。
tf.int16:16位整数。
tf.int32:32位整数。
tf.int64:64位整数。

无符号整型

tf.uint8:8位无符号整数。
tf.uint16:16位无符号整数。

浮点型

tf.float16:16位浮点数。
tf.float32:32位浮点数。
tf.float64:64位浮点数。
tf.double:等同于tf.float64。

字符串型

tf.string:字符串。

布尔型

tf.bool:布尔型。

复数型

tf.complex64:64位复数。
tf.complex128:128位复数。

原文链接:tensorflow中的数据类型dtype

### 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) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值