TensorFlow中的常见函数

TensorFlow中的常见函数

import tensorflow as tf
1.tf.cast(张量名,dtype=数据类型)
效果为强制tensor转换为该数据类型,例如下代码:

x1 = tf.constant([1., 2., 3.], dtype=tf.float64)
print("x1:", x1)
x2 = tf.cast(x1, tf.int32)
print("x2", x2)

输出结果为:

x1: tf.Tensor([1. 2. 3.], shape=(3,), dtype=float64)
x2 tf.Tensor([1 2 3], shape=(3,), dtype=int32)

即将x1转化为int32型。

2、tf.reduce_min(张量名),tf.reduce_max(张量名)
用来计算张量维度上的最小值和最大值。代码如下:

print("minimum of x2:", tf.reduce_min(x2))
print("maxmum of x2:", tf.reduce_max(x2))

输出结果为:

minimum of x2: tf.Tensor(1, shape=(), dtype=int32)
maxmum of x2: tf.Tensor(3, shape=(), dtype=int32)

3、axis=操作轴
axis=0表示在列上操作,axis=1表示在行上操作。
可见实例程序如下:

 x = tf.constant([[1, 2, 3], [2, 2, 3]])
print("x:", x)
print("mean of x:", tf.reduce_mean(x))  # 求x中所有数的均值(结果取整数位)
print("sum of x:", tf.reduce_sum(x, axis=1))  # 求每一行的和

输出为:

x: tf.Tensor(
[[1 2 3]
 [2 2 3]], shape=(2, 3), dtype=int32)
mean of x: tf.Tensor(2, shape=(), dtype=int32)
sum of x: tf.Tensor([6 7], shape=(2,), dtype=int32)

4、tf.Variable()此函数可以把变量标记为“可训练的”,被标记的变量在反向传播中会记录自己的梯度信息。在神经网络训练中,常用该函数标记待训练参数。
例如使用代码: w=tf.Variable(tf.random.normal([2,2],mean=0,stddev=1))
初始化神经网络的W参数。

5、 tensorflow 中的数学运算。
对应元素的四则运算有:
tf.add(张量1,张量2) 、tf.subtract (张量1,张量2)、tf.multipy(张量1,张量2)、tf.divide(张量1,张量2)
注意:只有维度相同的两个张量才可以进行四则运算。
例:

 a = tf.ones([1, 3])
b = tf.fill([1, 3], 3.)
print("a:", a)
print("b:", b)
print("a+b:", tf.add(a, b))
print("a-b:", tf.subtract(a, b))
print("a*b:", tf.multiply(a, b))
print("b/a:", tf.divide(b, a))

输出为:

a: tf.Tensor([[1. 1. 1.]], shape=(1, 3), dtype=float32)
b: tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)
a+b: tf.Tensor([[4. 4. 4.]], shape=(1, 3), dtype=float32)
a-b: tf.Tensor([[-2. -2. -2.]], shape=(1, 3), dtype=float32)
a*b: tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)
b/a: tf.Tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)

平方: tf.square(张量名)
次方:tf.pow(张量,n次方数)
开方:tf.sqrt(张量)
例:

  a = tf.fill([1, 2], 3.)
print("a:", a)
print("a的平方:", tf.pow(a, 3))
print("a的平方:", tf.square(a))
print("a的开方:", tf.sqrt(a))

输出为:

a: tf.Tensor([[3. 3.]], shape=(1, 2), dtype=float32)
a的平方: tf.Tensor([[27. 27.]], shape=(1, 2), dtype=float32)
a的平方: tf.Tensor([[9. 9.]], shape=(1, 2), dtype=float32)
a的开方: tf.Tensor([[1.7320508 1.7320508]], shape=(1, 2), dtype=float32)

矩阵乘法:tf.matmul(矩阵1,矩阵2)
例:

  a = tf.ones([3, 2])
b = tf.fill([2, 3], 3.)
print("a:", a)
print("b:", b)
print("a*b:", tf.matmul(a, b))

输出为:

a: tf.Tensor(
[[1. 1.]
 [1. 1.]
 [1. 1.]], shape=(3, 2), dtype=float32)
b: tf.Tensor(
[[3. 3. 3.]
 [3. 3. 3.]], shape=(2, 3), dtype=float32)
a*b: tf.Tensor(
[[6. 6. 6.]
 [6. 6. 6.]
 [6. 6. 6.]], shape=(3, 3), dtype=float32)

6、tf.data.Dataset.from_tensor_slices((输入特征,标签))
此函数可以将数据的特征和标签配对。
例:

features = tf.constant([12, 23, 10, 17])
labels = tf.constant([0, 1, 1, 0])
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
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=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>)

7、tf.GradientTape() 可以求出张量梯度,用法如下:

#with结构记录计算过程,gradient计算张量梯度
with tf.Grandient Tape() as tape:
     若干个计算过程
    grad =tape.grandient(函数,对谁求导)

具体例子如下:

with tf.GradientTape() as tape:
    w = tf.Variable(tf.constant(3.0))
    lost = tf.pow(w, 2)
grad = tape.gradient(lost, w)
print(grad)

输出:

tf.Tensor(6.0, shape=(), dtype=float32)

8、枚举:enumerate
enumerate常常用于for循环中用来在遍历中返回索引及元素。
例:

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

结果为:

0 one
1 two
2 three

9、tf.one_hot()
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("result of labels1:", output)
print("\n")

结果为:

result of labels1: tf.Tensor(
[[0. 1. 0.]
 [1. 0. 0.]
 [0. 0. 1.]], shape=(3, 3), dtype=float32)

10、柔性最大化:tf.nn.softmax
当n个分类有n个输出,通过tf.nn.softmax()函数可以是他们符合概率分布。

y = tf.constant([1.01, 2.01, -0.66])#y为3分类的输出值
y_pro = tf.nn.softmax(y)
print("After softmax, y_pro is:", y_pro)  # y_pro 符合概率分布
print("The sum of y_pro:", tf.reduce_sum(y_pro))  # 通过softmax后,所有概率加起来和为1

猜你喜欢

转载自blog.csdn.net/waner_jiaki/article/details/109867147