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

【TensorFlow2.0】(1) tensor数据类型,类型转换_tensorflow tensor转int-CSDN博客文章浏览阅读1.5w次,点赞8次,收藏28次。各位同学好,今天和大家分享一下TensorFlow2.0中的tensor数据类型,以及各种类型之间的相互转换方法。1. tf.tensor 基础操作scaler标量:1.2vector向量:1.21.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)
相关推荐
oort12313 小时前
VLStream:全开源决策式AI视频平台,赋能企业构建自主可控、降本增效的智能视觉应用介绍
大数据·开发语言·人工智能·开源·音视频·数据库架构
Agent_Sea13 小时前
IDC/Omdia/Gartner AI平台排名可信度穿透判断:第三方数据该怎么读
人工智能·大模型·ai平台
视***间13 小时前
算力筑基,智领人形机器人新时代 —— 英伟达 × 宇树科技携手推进具身智能,视程空间基于 NVIDIA 全栈算力产品助力机器人产业落地
人工智能·机器人·nvidia·机器狗·gpt-oss·视程空间·宇树机器人
EAIReport13 小时前
Spring AI 详解:Java 开发者快速落地 AI 应用
java·人工智能·spring
人工智能AI技术14 小时前
【VibeCoding系列教程07】 零代码平台——Bolt.new
人工智能
深蓝电商API14 小时前
大模型 + 爬虫 = ?我用 AI 做了一个自适应反反爬引擎
人工智能·爬虫
新酱爱学习14 小时前
手搓 10 个 Skill 后,我把重复劳动收敛成了一套零依赖 CLI 工具
前端·javascript·人工智能
IT_陈寒14 小时前
Python的线程池居然把我坑在了垃圾回收这块
前端·人工智能·后端
刘一说14 小时前
AI科技热点日报 | 2026年6月1日
人工智能·科技
阿里云大数据AI技术14 小时前
性能提升20倍:阿里云 Milvus 深度优化磁盘索引,重新定义亿级向量检索
人工智能