tensorflow的常用函数

简介: tensorflow的常用函数

直接print(a)是不能看见VALUE,因为张量是表示了过程态的数据格式,需要结合SESSION运行才能打印.

import tensorflow as tf
a = tf.constant([2,5],dtype=tf.int64)   # create a tensor(张量)
print(a)
print(a.dtype)
print(a.shape)
tf.Tensor([2 5], shape=(2,), dtype=int64)
<dtype: 'int64'>
(2,)

tf.random.truncated_normal生成截断式正态分布随机数,如果生成的数据在(μ±2σ)之外则重新生成,保证了随机数集中在均值附近。

b = tf.random.normal([3,2],mean=0,stddev=1)     # 正态分布
c = tf.random.truncated_normal([3,2],mean=0,stddev=1)   # 截断式正态分布
d = tf.random.uniform([3,2],minval=0,maxval=1)  # 均匀分布
print(b,'\n',c,'\n',d)
tf.Tensor(
[[ 0.7058287  -0.5170417 ]
 [ 0.45135102 -0.46539077]
 [-0.9698766  -0.14145553]], shape=(3, 2), dtype=float32) 
 tf.Tensor(
[[-0.18341348 -0.63185996]
 [ 0.18809798  0.30583274]
 [-0.2232003   0.13983348]], shape=(3, 2), dtype=float32) 
 tf.Tensor(
[[0.8689097  0.734738  ]
 [0.90087473 0.7620368 ]
 [0.4774233  0.02882552]], shape=(3, 2), dtype=float32)
e = tf.cast(b,tf.int64) # 强制tensor转换为该数据类型
print(e)
print(tf.reduce_min(b),'\n',tf.reduce_max(b))   # 计算张量维度上元素的最小值、最大值
print('--------')
print(tf.reduce_mean(b,axis=0),'\n',tf.reduce_sum(b,axis=0))
tf.Tensor(
[[0 0]
 [0 0]
 [0 0]], shape=(3, 2), dtype=int64)
tf.Tensor(-0.9698766, shape=(), dtype=float32) 
 tf.Tensor(0.7058287, shape=(), dtype=float32)
--------
tf.Tensor([ 0.06243438 -0.37462935], shape=(2,), dtype=float32) 
 tf.Tensor([ 0.18730313 -1.123888  ], shape=(2,), dtype=float32)
f = tf.random.normal([4,3,2])
print(f,'\n------\n')
print(tf.reduce_mean(f,axis=0),'\n------\n',tf.reduce_mean(f,axis=1),'\n------\n',tf.reduce_mean(f,axis=2))
tf.Tensor(
[[[ 1.8950557  -1.565545  ]
  [ 0.9876125   0.32145634]
  [ 0.03488503 -1.0272403 ]]
 [[-3.315065   -0.34581408]
  [ 1.7234309  -1.6426992 ]
  [-0.20039786  1.2526344 ]]
 [[ 2.0086255   0.5620748 ]
  [-0.88069105 -1.5739042 ]
  [ 0.14661889  2.0572565 ]]
 [[-0.61506957 -0.60781413]
  [-0.10987079 -3.2380192 ]
  [ 0.5165913  -0.88713425]]], shape=(4, 3, 2), dtype=float32) 
------
tf.Tensor(
[[-0.00661333 -0.48927462]
 [ 0.43012038 -1.5332916 ]
 [ 0.12442434  0.34887904]], shape=(3, 2), dtype=float32) 
------
 tf.Tensor(
[[ 0.97251767 -0.75710964]
 [-0.597344   -0.24529298]
 [ 0.4248511   0.3484757 ]
 [-0.06944969 -1.5776558 ]], shape=(4, 2), dtype=float32) 
------
 tf.Tensor(
[[ 0.16475534  0.6545344  -0.4961776 ]
 [-1.8304394   0.04036582  0.5261183 ]
 [ 1.2853501  -1.2272975   1.1019377 ]
 [-0.61144185 -1.673945   -0.18527147]], shape=(4, 3), dtype=float32)

tf.Variable()将变量标记为“可训练”,被标记的变量会在反向传播中记录梯度信息。神经网络训练中,常用该函数标记待训练参数。

w = tf.Variable(tf.random.normal([2,2],mean=0,stddev=1))

四则运算:tf.add,tf.subtract,tf.multiply,tf.divide

平方、次方、开方:tf.square,tf.pow,tf.sqrt

矩阵乘法:tf.matmul

神经网络训练时,是把输入特征和标签配对后喂入网络的。

tf.data.Dataset.from_tensor_slices可以实现这一功能:切分传入张量的第一维度,生成输入特征/标签对,构建数据集。

data = tf.data.Dataset.from_tensor_slices((输入特征,标签))

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

features = tf.constant([12,23,10,17])
labels = tf.constant([0,1,1,0])
dataset = tf.data.Dataset.from_tensor_slices((features,labels))
print(dataset)
for element in dataset:
    print(element)
<TensorSliceDataset element_spec=(TensorSpec(shape=(), dtype=tf.int32, name=None), TensorSpec(shape=(), dtype=tf.int32, name=None))>
(<tf.Tensor: shape=(), dtype=int32, numpy=12>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: shape=(), dtype=int32, numpy=23>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: shape=(), dtype=int32, numpy=10>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: shape=(), dtype=int32, numpy=17>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)

with结构可以记录计算的过程,返回as后面的那个值。tf.GradientTape函数可以求出张量的梯度。

with tf.GradientTape() as tape:

若干个计算过程

grad = tape.gradient(函数,对谁求导)

print(grad)

with tf.GradientTape() as tape:
    w = tf.Variable(tf.constant(3.0))
    loss = tf.pow(w,2)
grad = tape.gradient(loss,w)
print(grad)
tf.Tensor(6.0, shape=(), dtype=float32)

enumerate是python的内建函数,它可以遍历每个元素(如列表、元组或字符串),组合为:索引、元素,常在for循环中使用。

enumerate(列表名)

seq = ['one','two','three']
for i,element in enumerate(seq):
    print(i,element)
0 one
1 two
2 three

独热编码(one-hot encoding):在分类问题中,常用独热码做标签,标记类别:1表示是,0表示非。

tf.one_hot()函数将待转换数据转换为one-hot形式的数据输出。

tf.one_hot(待转换数据,depth=几分类)

classes = 3
labels = tf.constant([1,0,2])   # 输入的元素值最小为0,最大为2
output = tf.one_hot(labels,depth=classes)
print(output)
tf.Tensor(
[[0. 1. 0.]
 [1. 0. 0.]
 [0. 0. 1.]], shape=(3, 3), dtype=float32)

当n分类的n个输出(y0,y1,…,yn-1)通过softmax()函数,便符合概率分布了。

tf.nn.softmax()函数可实现这一功能。

y = tf.constant([1.01,2.01,-0.66])
y_pro = tf.nn.softmax(y)
print("After softmax,y_pro is:",y_pro)
After softmax,y_pro is: tf.Tensor([0.25598174 0.69583046 0.04818781], shape=(3,), dtype=float32)

assign_sub()函数

可实现复制操作,更新参数值并返回(相当于自加自减)。

调用assign_sub前,先用td.Variable定义变量w为可训练(可自更新)。

w.assign_sub(w要自减的内容)

w = tf.Variable(4)
w.assign_sub(1)
print(w)
<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>

tf.argmax()函数返回张量研指定维度最大值的索引

tf.argmax(张量名,axis=操作轴)

t = tf.constant([[1,2,3],[2,3,4],[5,4,3],[8,7,2]])
print(t)
print(tf.argmax(t,axis=0))  # 返回每一列(经度)最大值索引
print(tf.argmax(t,axis=1))  # 返回每一行(纬度)最大值索引
tf.Tensor(
[[1 2 3]
 [2 3 4]
 [5 4 3]
 [8 7 2]], shape=(4, 3), dtype=int32)
tf.Tensor([3 3 1], shape=(3,), dtype=int64)
tf.Tensor([2 2 0 0], shape=(4,), dtype=int64)


目录
相关文章
|
1月前
|
机器学习/深度学习 数据可视化 TensorFlow
Python用线性回归和TensorFlow非线性概率神经网络不同激活函数分析可视化
Python用线性回归和TensorFlow非线性概率神经网络不同激活函数分析可视化
|
6月前
|
TensorFlow 算法框架/工具 Python
把python函数转化为 tensorflow 函数 加速运算
把python函数转化为 tensorflow 函数 加速运算
26 1
|
6月前
|
机器学习/深度学习 人工智能 TensorFlow
人工智能实验 python tensorflow keras拟合正弦函数,工资预测,公司收益预测
人工智能实验 python tensorflow keras拟合正弦函数,工资预测,公司收益预测
48 0
|
TensorFlow 算法框架/工具
Tensorflow中常用的卷积函数
Tensorflow中常用的卷积函数
Tensorflow中常用的卷积函数
|
机器学习/深度学习 TensorFlow 算法框架/工具
Tensorflow中常用的池化函数
Tensorflow中常用的池化函数
|
TensorFlow API 算法框架/工具
TensorFlow利用函数API实现简易自编码器
TensorFlow利用函数API实现简易自编码器
59 0
TensorFlow利用函数API实现简易自编码器
|
机器学习/深度学习 TensorFlow 算法框架/工具
TensorFlow2.0(7):4种常用的激活函数
TensorFlow2.0(7):4种常用的激活函数
TensorFlow2.0(7):4种常用的激活函数
|
机器学习/深度学习 TensorFlow 算法框架/工具
【深度学习】Tensorflow学习(1)张量与常用函数 2
【深度学习】Tensorflow学习(1)张量与常用函数
227 0
【深度学习】Tensorflow学习(1)张量与常用函数 2
|
机器学习/深度学习 TensorFlow 算法框架/工具
【深度学习】Tensorflow学习(1)张量与常用函数 1
【深度学习】Tensorflow学习(1)张量与常用函数
111 0
【深度学习】Tensorflow学习(1)张量与常用函数 1
|
索引
tensorflow2.0的一些高级函数用法
tensorflow2.0的一些高级函数用法
83 0
tensorflow2.0的一些高级函数用法

热门文章

最新文章

相关实验场景

更多