首页 > 其他分享 >Tensorflow数据的基本操作

Tensorflow数据的基本操作

时间:2023-07-29 18:56:34浏览次数:27  
标签:Tensor dtype 张量 print shape Tensorflow tf 基本操作 数据

# tensorflow里引入一个新的数据类型-张量(tensor),与numpy的ndarray类似,是一个多维数组。和numpy的区别在于:numpy的ndarray只支持CPU计算,而张量支持GPU,可以通过GPU加速,提高速度,同时张量还支持自动微分计算,更适合深度学习
## 创建数字序列的张量
import tensorflow as tf
x = tf.range(10)
x
<tf.Tensor: shape=(10,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)>
# 查看张量形状
x.shape
TensorShape([10])
# 修改形状
x1 = tf.reshape(x,(2,5))
x1
<tf.Tensor: shape=(2, 5), dtype=int32, numpy=
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]], dtype=int32)>
# 全1的张量
tf.ones((3,3))
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]], dtype=float32)>
# 全0的张量
tf.zeros((3,3))
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]], dtype=float32)>
# 均值为0,方差为1的正态分布张量
tf.random.normal((3,4),mean=0,stddev=1)
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[-0.78743833,  0.16588742,  0.05436951, -0.5504127 ],
       [ 0.6029112 ,  1.1076114 ,  0.0166753 , -1.5274208 ],
       [ 1.5660976 ,  0.7945377 , -1.6707777 , -0.29749963]],
      dtype=float32)>
# 创建常量,不可修改的张量
const1 = tf.constant(1)
const2 = tf.constant([1,2])
const3 = tf.constant([[1,2],[3,4]])
print(const1)
print(const2)
print(const3)
tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor([1 2], shape=(2,), dtype=int32)
tf.Tensor(
[[1 2]
 [3 4]], shape=(2, 2), dtype=int32)
# 张量的基础运算
x = tf.constant([1.0, 2, 4, 8])
y = tf.constant([2.0, 2, 2, 2])
x + y, x - y, x * y, x / y, x ** y, tf.exp(x)  
(<tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 3.,  4.,  6., 10.], dtype=float32)>,
 <tf.Tensor: shape=(4,), dtype=float32, numpy=array([-1.,  0.,  2.,  6.], dtype=float32)>,
 <tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 2.,  4.,  8., 16.], dtype=float32)>,
 <tf.Tensor: shape=(4,), dtype=float32, numpy=array([0.5, 1. , 2. , 4. ], dtype=float32)>,
 <tf.Tensor: shape=(4,), dtype=float32, numpy=array([ 1.,  4., 16., 64.], dtype=float32)>,
 <tf.Tensor: shape=(4,), dtype=float32, numpy=
 array([2.7182817e+00, 7.3890562e+00, 5.4598148e+01, 2.9809580e+03],
       dtype=float32)>)
# 张量的拼接
X = tf.reshape(tf.range(12, dtype=tf.float32), (3, 4))
Y = tf.constant([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
tf.concat([X, Y], axis=0), tf.concat([X, Y], axis=1)
(<tf.Tensor: shape=(6, 4), dtype=float32, numpy=
 array([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.],
        [ 2.,  1.,  4.,  3.],
        [ 1.,  2.,  3.,  4.],
        [ 4.,  3.,  2.,  1.]], dtype=float32)>,
 <tf.Tensor: shape=(3, 8), dtype=float32, numpy=
 array([[ 0.,  1.,  2.,  3.,  2.,  1.,  4.,  3.],
        [ 4.,  5.,  6.,  7.,  1.,  2.,  3.,  4.],
        [ 8.,  9., 10., 11.,  4.,  3.,  2.,  1.]], dtype=float32)>)
# 张量的比较
X == Y
<tf.Tensor: shape=(3, 4), dtype=bool, numpy=
array([[False,  True, False,  True],
       [False, False, False, False],
       [False, False, False, False]])>
# 张量元素求和
## 所有元素求和
a = tf.reduce_sum(X)
## 每列求和
r = tf.reduce_sum(X,axis=0)
## 每行求和
c = tf.reduce_sum(X,axis=1)
print(X)
print(a)
print(r)
print(c)
tf.Tensor(
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]], shape=(3, 4), dtype=float32)
tf.Tensor(66.0, shape=(), dtype=float32)
tf.Tensor([12. 15. 18. 21.], shape=(4,), dtype=float32)
tf.Tensor([ 6. 22. 38.], shape=(3,), dtype=float32)
# 广播
a = tf.reshape(tf.range(3), (3, 1))
b = tf.reshape(tf.range(2), (1, 2))
print(a)
print(b)
A = tf.broadcast_to(a,(3,2))   # 广播
B = tf.broadcast_to(b,(3,2))   # 广播
print(a + b)  # 自动广播成相同的形状
print(A + B)  # 和上面的a+b结果相同
tf.Tensor(
[[0]
 [1]
 [2]], shape=(3, 1), dtype=int32)
tf.Tensor([[0 1]], shape=(1, 2), dtype=int32)
tf.Tensor(
[[0 1]
 [1 2]
 [2 3]], shape=(3, 2), dtype=int32)
tf.Tensor(
[[0 1]
 [1 2]
 [2 3]], shape=(3, 2), dtype=int32)
# 索引和切片
print(X)
print(X[-1], X[1:3])
print(X[:,1:2])
tf.Tensor(
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]], shape=(3, 4), dtype=float32)
tf.Tensor([ 8.  9. 10. 11.], shape=(4,), dtype=float32) tf.Tensor(
[[ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]], shape=(2, 4), dtype=float32)
tf.Tensor(
[[1.]
 [5.]
 [9.]], shape=(3, 1), dtype=float32)
# 创建变量,可以修改
X_var = tf.Variable(X)
X_var[1, 2].assign(9)
print(X)
print(X_var)
tf.Tensor(
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]], shape=(3, 4), dtype=float32)
<tf.Variable 'Variable:0' shape=(3, 4) dtype=float32, numpy=
array([[ 0.,  1.,  2.,  3.],
       [ 4.,  5.,  9.,  7.],
       [ 8.,  9., 10., 11.]], dtype=float32)>
# Tensor和numpy互转
A = X.numpy()
B = tf.constant(A)
print(A)
print(B)
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]]
tf.Tensor(
[[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]], shape=(3, 4), dtype=float32)

标签:Tensor,dtype,张量,print,shape,Tensorflow,tf,基本操作,数据
From: https://www.cnblogs.com/navysummer/p/17590284.html

相关文章

  • 【MySQL技术专题】「实战开发系列」一同探索一下数据库的加解密函数开发实战指南之AES
    MySQL的加解密及压缩函数许多加密和压缩函数返回结果可能包含任意字节值的字符串。如果要存储这些结果,请使用具有VARBINARY或BLOB二进制字符串数据类型的列。这避免了删除尾随空格或转换字符集可能改变数据值的潜在问题,例如使用非二进制字符串数据类型(CHAR、VARCHAR、TEXT)时可能发......
  • 大数据总结
    这周我学了hive表数据导出、分区表的使用、分桶表创建和分桶表数据加载等,我在这期间也学了学java爬虫和ssm等。hive表数据导出   第二种,是放到了本地的不是放在HFDS里的分区表的使用  分桶表创建 分桶表数据加载 ......
  • Java学习-2.简介、关键字、标识符、变量、数据类型、运算符
    一、Java简介Java最早是由SUN公司(已被Oracle收购)的詹姆斯·高斯林(高司令,人称Java之父)在上个世纪90年代初开发的一种编程语言,最初被命名为Oak,目标是针对小型家电设备的嵌入式应用,结果市场没啥反响。谁料到互联网的崛起,让Oak重新焕发了生机,于是SUN公司改造了Oak,在1995年以Java的名......
  • SAP CDS view 定义的数据库视图和传统 SQL 语句定义视图的区别
    SAPCDS(CoreDataServices)是SAPHANA数据模型开发的一种技术。它提供了一种领域特定的语言,用于定义数据模型,以及对数据进行查询、转换和展示。与传统的SQL(StructuredQueryLanguage)相比,SAPCDSview语法具有许多独特的特点和优势。在本文中,我将详细比较SAPCDSview语......
  • 云数据库压测
    MySQL性能压测或者基准测试看起来很简单,使用sysbench,tpcc工具跑跑拿到数据就好,其实压测是一个技术活儿,尤其是涉及到性能对比的测试,因为不同场景/不同厂商的产品的参数设置不同,测试的结果也不一样。如果不阐明具体的参数配置差异,直接给出压测结果可能给其他人带来误导。本文针对......
  • [粘贴]使用 Dumpling 导出数据
     https://docs.pingcap.com/zh/tidb/stable/dumpling-overview#dumpling-%E4%B8%BB%E8%A6%81%E9%80%89%E9%A1%B9%E8%A1%A8 使用数据导出工具 Dumpling,你可以把存储在TiDB或MySQL中的数据导出为SQL或CSV格式,用于逻辑全量备份。Dumpling也支持将数据导出......
  • EF 管理数据库架构
    本章会主要了解EF提供的独立迁移项目,用独立迁移项目自动创建dgml设计关系图和sql脚本。迁移项目通常也叫(CodeFirst代码优先),在EF中迁移项目是在,在代码中设计数据库,每次对数据库的设计都将被保留记录。这种模式只会向前修改,不会向后修改。因为一旦数据已经存在,不易删除改变结构,只能......
  • 【Matlab】基于粒子群优化算法优化BP神经网络的数据分类预测
    【Matlab】基于粒子群优化算法优化BP神经网络的数据分类预测(Excel可直接替换数据)1.模型原理2.数学公式3.文件结构4.Excel数据5.分块代码5.1fun.m5.2main.m6.完整代码6.1fun.m6.2main.m7.运行结果1.模型原理“基于粒子群优化算法优化BP神经网络的数据分类预测”是一种结合了粒......
  • SAP Fiori Elements 应用 OData 元数据请求 url 里的模型名称决定逻辑
    问题我用yarnstart本地启动一个SAPFioriElements应用,在Chrome开发者工具network面板,观察到一个ODatametadata请求的url如下:http://localhost:8080/sap/opu/odata/sap/SEPMRA_PROD_MAN/$metadata?sap-value-list=none&sap-language=EN这个OData服务名称SEPM......
  • el-select 无限下拉滚动加载数据
     <template> <div>  <el-form   ref="saveParameter"   :model="saveParameter"   inline   inline-message   style="margin:10px"  >   <el-form-itemlabel="供应商"prop=&......