前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TensorFlow常用函数

TensorFlow常用函数

作者头像
皮大大
发布2023-08-23 16:58:42
1620
发布2023-08-23 16:58:42
举报

Tensoflow常用函数

本文记录的是TensorFlow中常用的函数

  • tf.cast:强制数据类型转换
  • tf.reduct_mean/sum:求和或均值
  • tf.reduce_max/min:求最值
  • tf.Variable:标记变量
  • 四则运算
  • tf.data.Dataset.from_tensor_slices:特征和标签配对
代码语言:javascript
复制
import tensorflow as tf
import numpy as np

理解axis

在一个二维张量或者数组中,通过改变axis=0或1来控制执行的维度

  • 0:表示经度,跨行,down
  • 1:表示纬度,跨列,across

如果不指定的话,则全员参与计算

tf.cast

强制tensor转换为该数据类型

代码语言:javascript
复制
tf.cast(张量名, dtype=数据类型)

In [2]:

代码语言:javascript
复制
x1 = tf.constant([1,2,3],dtype=tf.float64)
x1

Out[2]:

代码语言:javascript
复制
<tf.Tensor: shape=(3,), dtype=float64, numpy=array([1., 2., 3.])>

In [3]:

代码语言:javascript
复制
x2 = tf.cast(x1, dtype=tf.int64)  # 转换数据类型
x2

Out[3]:

代码语言:javascript
复制
<tf.Tensor: shape=(3,), dtype=int64, numpy=array([1, 2, 3])>

tf.reduce_max/min

计算张量维度上的最值

In [4]:

代码语言:javascript
复制
print(tf.reduce_max(x2))
tf.Tensor(3, shape=(), dtype=int64)

In [5]:

代码语言:javascript
复制
print(tf.reduce_min(x2))
tf.Tensor(1, shape=(), dtype=int64)

tf.reduct_mean/sum

计算张量沿着指定维度的平均值或者和

In [6]:

代码语言:javascript
复制
i = tf.constant([[1,2,3],
                 [4,5,6]
                ],dtype=tf.float64)
i

Out[6]:

代码语言:javascript
复制
<tf.Tensor: shape=(2, 3), dtype=float64, numpy=
array([[1., 2., 3.],
       [4., 5., 6.]])>

In [7]:

代码语言:javascript
复制
tf.reduce_mean(i)  # 默认是全部数值的均值

Out[7]:

代码语言:javascript
复制
<tf.Tensor: shape=(), dtype=float64, numpy=3.5>

In [8]:

代码语言:javascript
复制
tf.reduce_mean(i, axis=0)

Out[8]:

代码语言:javascript
复制
<tf.Tensor: shape=(3,), dtype=float64, numpy=array([2.5, 3.5, 4.5])>

In [9]:

代码语言:javascript
复制
tf.reduce_mean(i, axis=1)

Out[9]:

代码语言:javascript
复制
<tf.Tensor: shape=(2,), dtype=float64, numpy=array([2., 5.])>

In [10]:

代码语言:javascript
复制
tf.reduce_sum(i)  # 1+2+3+4+5+6

Out[10]:

代码语言:javascript
复制
<tf.Tensor: shape=(), dtype=float64, numpy=21.0>

In [11]:

代码语言:javascript
复制
tf.reduce_sum(i, axis=0)

Out[11]:

代码语言:javascript
复制
<tf.Tensor: shape=(3,), dtype=float64, numpy=array([5., 7., 9.])>

In [12]:

代码语言:javascript
复制
tf.reduce_sum(i, axis=1)

Out[12]:

代码语言:javascript
复制
<tf.Tensor: shape=(2,), dtype=float64, numpy=array([ 6., 15.])>

tf.Variable

tf.Variable()将函数标记为可训练,被标记的变量会在反向传播中记录梯度信息。

神经网络中常用该函数来标记待训练的参数。

In [13]:

代码语言:javascript
复制
w = tf.Variable(tf.random.normal([2,2], mean=0, stddev=1))
w

Out[13]:

代码语言:javascript
复制
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[-1.3200597 ,  1.123157  ],
       [ 0.4855043 , -0.06241844]], dtype=float32)>

上面变量w的解释:

  1. 先生成正态分布的随机数
  2. 再将随机数标记为可训练,这样在神经网络的反向传播中就可以通过梯度下降更新参数w了

数学运算

  • 四则运算:tf.add(t1,t2)、tf.subtract、tf.multiply、tf.divide
  • 平方、次方与开方:tf.square、tf.pow(t,n次方)、tf.sqrt
  • 矩阵乘:tf.matmul

注意:只有维度相同的两个张量才能进行四则运算

tf.data.Dataset.from_tensor_slices

切分传入张量的第一维度,生成输入特征和标签对,构建数据集:特征和标签配对

Numpy和Tensor格式都可以使用该语句读入数据

代码语言:javascript
复制
data = tf.data.Dataset.from_tensor_slices((输入特征,标签))

In [14]:

代码语言:javascript
复制
features = tf.constant([12,15,20,27])
labels = tf.constant([0,1,0,1])

dataset = tf.data.Dataset.from_tensor_slices((features,labels))
print(dataset)
<TensorSliceDataset element_spec=(TensorSpec(shape=(), dtype=tf.int32, name=None), TensorSpec(shape=(), dtype=tf.int32, name=None))>

In [15]:

代码语言:javascript
复制
for element in dataset:
    print(element)
(<tf.Tensor: shape=(), dtype=int32, numpy=12>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: shape=(), dtype=int32, numpy=15>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: shape=(), dtype=int32, numpy=20>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: shape=(), dtype=int32, numpy=27>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-4-4,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Tensoflow常用函数
  • 理解axis
  • tf.cast
  • tf.reduce_max/min
  • tf.reduct_mean/sum
  • tf.Variable
  • 数学运算
  • tf.data.Dataset.from_tensor_slices
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档