注意这里的bh维度为 1 * h,计算时使用广播机制,进行计算
所以引入激活函数
%matplotlib inline
import tensorflow as tf
from matplotlib import pyplot as plt
import numpy as np
import random
def use_svg_display():
# 用矢量图显示
%config InlineBackend.figure_format = 'svg'
def set_figsize(figsize=(3.5, 2.5)):
use_svg_display()
# 设置图的尺寸
plt.rcParams['figure.figsize'] = figsize
def xyplot(x_vals, y_vals, name):
set_figsize(figsize=(5, 2.5))
plt.plot(x_vals.numpy(), y_vals.numpy())
plt.xlabel('x')
plt.ylabel(name + '(x)')
with tf.GradientTape() as t:
t.watch(x)
y=y = tf.nn.relu(x)
dy_dx = t.gradient(y, x)
xyplot(x, dy_dx, 'grad of relu')
部分参考自:
https://trickygo.github.io/Dive-into-DL-TensorFlow2.0/#/chapter03_DL-basics/3.8_mlp