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)
相关推荐
FL162386312913 分钟前
智慧城市道路路面垃圾检测数据集VOC+YOLO格式3321张17类别
深度学习·yolo·机器学习
谷粒.13 分钟前
DevOps流水线中的质量门禁设计:从理论到实践的全景解析
运维·开发语言·网络·人工智能·python·devops
GOTXX14 分钟前
性能与可靠双突破:openEuler 服务器场景评测报告
运维·服务器·网络·人工智能·后端·python
weixin_4215850116 分钟前
vxm.networks.Unet
python·深度学习·.net
神算大模型APi--天枢64618 分钟前
智能协同与垂直深耕:聚合模型API算力平台重构软件开发生态
大数据·人工智能·科技·重构·架构·gpu算力
秋邱18 分钟前
AR 技术团队搭建与规模化接单:从个人到团队的营收跃迁
前端·人工智能·后端·python·html·restful
后端小肥肠24 分钟前
通吃网文投稿+AI漫剧版权!我用 n8n+飞书搭了个“万字爆款小说流水线”
人工智能·aigc·agent
Jerryhut24 分钟前
sklearn函数总结五——特征降维 压缩数据 - 特征选择
人工智能·python·机器学习·sklearn
deephub25 分钟前
自愈型RAG系统:从脆弱管道到闭环智能体的工程实践
人工智能·python·大语言模型·rag
Jerryhut26 分钟前
sklearn函数总结六——特征降维 压缩数据 - 特征提取(PCA&LDA)
人工智能·算法·机器学习·scikit-learn·sklearn