Tensorflow④——常用TensorFlow 学习率函数、激活函数、损失函数API及代码实现

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# 导入所需模块
import tensorflow as tf
from sklearn import datasets
from matplotlib import pyplot as plt
import numpy as np

一、学习率策略

tf.keras.optimizers.schedules.ExponentialDecay

tf.keras.optimizers.schedules.ExponentialDecay(
	initial_learning_rate, decay_steps, decay_rate, staircase=False, name=None
)

功能:指数衰减学习率策略.
等价API:tf.optimizers.schedules.ExponentialDecay

参数

  • initial_learning_rate: 初始学习率.
  • decay_steps: 衰减步数, staircase为True时有效.
  • decay_rate: 衰减率.
  • staircase: Bool型变量.如果为True, 学习率呈现阶梯型下降趋势.

返回:tf.keras.optimizers.schedules.ExponentialDecay(step)返回计算得到的学习率

举例

N = 400
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
                0.5,
                decay_steps=10,
                decay_rate=0.9,
                staircase=False)
y = []
for global_step in range(N):
    lr = lr_schedule(global_step)
    y.append(lr)
    x = range(N)
plt.figure(figsize=(8,6))
plt.plot(x, y, 'r-')
plt.ylim([0,max(plt.ylim())])
plt.xlabel('Step')
plt.ylabel('Learning Rate')
plt.title('ExponentialDecay')
plt.show()

在这里插入图片描述

tf.keras.optimizers.schedules.PiecewiseConstantDecay

tf.keras.optimizers.schedules.PiecewiseConstantDecay(
	boundaries, values, name=None
)

功能:分段常数衰减学习率策略.
等价API:tf.optimizers.schedules.PiecewiseConstantDecay

参数

  • boundaries: [step_1, step_2, …, step_n]定义了在第几步进行学习率衰减.
  • values: [val_0, val_1, val_2, …, val_n]定义了学习率的初始值和后续衰减时的具体取值.

返回:tf.keras.optimizers.schedules.PiecewiseConstantDecay(step)返回计算得到的学习率.

N = 400
lr_schedule = tf.keras.optimizers.schedules.PiecewiseConstantDecay(
                boundaries=[100, 200, 300],
                values=[0.1, 0.05, 0.025, 0.001])
y = []
for global_step in range(N):
    lr = lr_schedule(global_step)
    y.append(lr)
x = range(N)
plt.figure(figsize=(8,6))
plt.plot(x, y, 'r-')
plt.ylim([0,max(plt.ylim())])
plt.xlabel('Step')
plt.ylabel('Learning Rate')
plt.title('PiecewiseConstantDecay')
plt.show()

在这里插入图片描述

激活函数

tf.math.sigmoid

tf.math.sigmoid(
	x, name=None
)

功能:计算x每一个元素的sigmoid值.
等价API:tf.nn.sigmoid, tf.sigmoid

参数

  • x: 张量x.

返回:与x shape相同的张量.
例子

x = tf.constant([1., 2., 3.], )
print(tf.math.sigmoid(x))

# 等价实现
print(1/(1+tf.math.exp(-x)))

tf.math.tanh

tf.math.tanh(
x, name=None
)

功能:计算x每一个元素的双曲正切值.
等价API:tf.nn.tanh, tf.tanh

参数

  • x: 张量x.

返回:与x shape相同的张量.

x = tf.constant([-float("inf"), -5, -0.5, 1, 1.2, 2, 3, float("inf")])
print(tf.math.tanh(x))

# 等价实现
print((tf.math.exp(x)-tf.math.exp(-x))/(tf.math.exp(x)+tf.math.exp(-x)))

tf.nn.relu

tf.nn.relu(
features, name=None
)

功能:计算修正线性值(rectified linear):max(features, 0).

参数

  • features: 张量.

返回:与features shape相同的张量.

print(tf.nn.relu([-2., 0., -0., 3.]))

tf.nn.leaky_relu

tf.nn.leaky_relu(
features, alpha=0.2, name=None
)

功能:计算Leaky ReLU值.

参数

  • features: 张量.
  • alpha: x<0时的斜率值.

返回:与features shape相同的张量.

tf.nn.softmax

tf.nn.softmax(
logits, axis=None, name=None
)

功能:计算softmax激活值.
等价API:tf.math.softmax

参数

  • logits: 张量.
  • axis: 计算softmax所在的维度. 默认为-1,即最后一个维度.

返回:与logits shape相同的张量.

logits = tf.constant([4., 5., 1.])
print(tf.nn.softmax(logits))

# 等价实现
print(tf.exp(logits) / tf.reduce_sum(tf.exp(logits)))

损失函数

tf.keras.losses.MSE

tf.keras.losses.MSE(
	y_true, y_pred
)

功能:计算y_truey_pred的均方误差.

y_true = tf.constant([0.5, 0.8])
y_pred = tf.constant([1.0, 1.0])
print(tf.keras.losses.MSE(y_true, y_pred))

# 等价实现
print(tf.reduce_mean(tf.square(y_true - y_pred)))

tf.keras.losses.categorical_crossentropy

tf.keras.losses.categorical_crossentropy(
	y_true, y_pred, from_logits=False, label_smoothing=0
)

功能:计算交叉熵.
等价API:tf.losses.categorical_crossentropy

参数

  • y_true: 真实值
  • y_pred: 预测值.
  • from_logits: y_pred是否为logits张量.
  • label_smoothing: [0,1]之间的小数.

返回:交叉熵损失值

y_true = [1, 0, 0]
y_pred1 = [0.5, 0.4, 0.1]
y_pred2 = [0.8, 0.1, 0.1]
print(tf.keras.losses.categorical_crossentropy(y_true, y_pred1))
print(tf.keras.losses.categorical_crossentropy(y_true, y_pred2))

# 等价实现
print(-tf.reduce_sum(y_true * tf.math.log(y_pred1)))
print(-tf.reduce_sum(y_true * tf.math.log(y_pred2)))

tf.nn.softmax_cross_entropy_with_logits

tf.nn.softmax_cross_entropy_with_logits(
	labels, logits, axis=-1, name=None
)

功能:logits经过softmax后,与labels进行交叉熵计算.

在机器学习中,对于多分类问题,把未经softmax归一化的向量值称为logits。logits经过softmax
层后,输出服从概率分布的向量。

参数

  • labels: 在类别这一维度上,每个向量应服从有效的概率分布. 例如,在labels的shape为[batch_size, num_classes]的情况下,labels[i]应服从概率分布.
  • logits: 每个类别的激活值,通常是线性层的输出. 激活值需要经过softmax归一化.
  • axis: 类别所在维度,默认是-1,即最后一个维度.

返回:softmax交叉熵损失值.

labels = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
logits = [[4.0, 2.0, 1.0], [0.0, 5.0, 1.0]]
print(tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits))

# 等价实现
print(-tf.reduce_sum(labels * tf.math.log(tf.nn.softmax(logits)), axis=1))

tf.nn.sparse_softmax_cross_entropy_with_logits

tf.nn.sparse_softmax_cross_entropy_with_logits(
	labels, logits, name=None
)

功能:labels经过one-hot编码,logits经过softmax,两者进行交叉熵计算. 通常labels的shape为[batch_size],logits的shape为[batch_size, num_classes]. sparse可理解为对labels进行稀疏化处理(即进行one-hot编码).

参数

  • labels: 标签的索引值.
  • logits: 每个类别的激活值,通常是线性层的输出. 激活值需要经过softmax归一化.

返回:softmax交叉熵损失值.

(下例中先对labels进行one-hot编码为[[1,0,0], [0,1,0]],logits经过softmax变为[[0.844,0.114,0.042], [0.007,0.976,0.018]],两者再进行交叉熵运算)

labels = [0, 1]
logits = [[4.0, 2.0, 1.0], [0.0, 5.0, 1.0]]
print(tf.nn.sparse_softmax_cross_entropy_with_logits(labels1, logits))

# 等价实现
print(-tf.reduce_sum(tf.one_hot(labels, tf.shape(logits)[1]) *
tf.math.log(tf.nn.softmax(logits)), axis=1))
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值