Tensorflow2.0笔记 - 基本数据类型,数据类型转换

【TensorFlow2.0】(1) tensor数据类型,类型转换_tensorflow tensor转int-CSDN博客文章浏览阅读1.5w次,点赞8次,收藏28次。各位同学好,今天和大家分享一下TensorFlow2.0中的tensor数据类型,以及各种类型之间的相互转换方法。1. tf.tensor 基础操作scaler标量:1.2vector向量:1.2、1.1,2.2,3.3 注意:此处的1.2是一维的,而1.2是0维的matrix矩阵:\[1.1,2.2,3.3,4.4]tensor张量:代表任意维度的数据1.1 创建一个tensor创建方法: tf.constant(value, shape=维度, d..._tensorflow tensor转inthttps://blog.csdn.net/dgvv4/article/details/121478256

关于tensorflow的数据类型的文章,网上有很多。本笔记只记录代码。

复制代码
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

tf.__version__

#constant
a = tf.constant([1.5])
b = tf.constant([True, False])
c = tf.constant('Hello TensorFlow')
d = np.arange(4)

#检查是否是tensor类型
print(tf.is_tensor(a))
print(tf.is_tensor(b))
print(tf.is_tensor(c))
print(tf.is_tensor(d))

#返回数据类型
print(a.dtype)
print(b.dtype)
print(c.dtype)
print(d.dtype)

print(a.dtype == tf.float32)
print(b.dtype == tf.bool)
print(c.dtype == tf.string)

#数据类型转换
a = np.arange(5)
print(a)
#将numpy数组转换为tensor
tensor_a = tf.convert_to_tensor(a)
print(tensor_a)
tensor_a_int64 = tf.convert_to_tensor(a, dtype=tf.int64)
print(tensor_a_int64)
tensor_a_cast_float32 = tf.cast(tensor_a, dtype=tf.float32)
print(tensor_a_cast_float32)
#整型转换为bool
b = tf.constant([1,0,1,0])
b = tf.cast(b, dtype=tf.bool)
print(b)

#TensorFlow变量Variable, Variable的trainable属性为Ture,表示可以进行求导更新
a = tf.range(5)
b = tf.Variable(a)
print(b)
print(tf.is_tensor(b))
print(b.trainable)

#Tensor转回numpy
a = tf.range(4)
print(a)
a_numpy = a.numpy()
print(a_numpy)
相关推荐
Yolanda_20222 小时前
17.卷积操作
人工智能·深度学习
`流年づ2 小时前
卷积核与反卷积知识补充
人工智能·深度学习·计算机视觉
百无聊赖是也非也2 小时前
当 AI 闯入理论物理:终结算力困境,重塑科研边界
人工智能
szxinmai主板定制专家2 小时前
RK3568+FPGA+CODESYS异构控制方案:赋能半导体设备与工业自动化升级
人工智能·fpga开发·自动化·rk3576·半导体设备
龍德明宇2 小时前
裁断为什么没有变便宜-龍德明宇
人工智能·大语言模型llm·负主体性·ai存在论
Ai-_Man2 小时前
您您这可以把Google AI Studio的多个会话比如说。左侧的多个会话一次性导出吗?不是单条会话里面的多次会对话。AI导出鸭
javascript·人工智能·ai·小程序·电脑
丁兰子2 小时前
OpenVLA 模型详解
人工智能
Martina_03212 小时前
AI生成3D场景第一次进入总卡一下?用6步排查Shader编译、纹理上传与预加载
图像处理·人工智能·游戏·3d·ai·自然语言处理·游戏策划
DP DPharness2 小时前
dsh-univer-office 报错了?按这个顺序排查
人工智能·dpharness
正经教主2 小时前
【FDE系列】阶段3:Day 57:Promptfoo 实战 — A/B 对比让数据说话
人工智能·fde