吴裕雄--天生自然TensorFlow2教程:单输出感知机及其梯度

 

 

import tensorflow as tf

x = tf.random.normal([1, 3])
w = tf.ones([3, 1])
b = tf.ones([1])
y = tf.constant([1])

with tf.GradientTape() as tape:
    tape.watch([w, b])
    prob = tf.sigmoid(x @ w + b)
    loss = tf.reduce_mean(tf.losses.MSE(y, prob))

grads = tape.gradient(loss, [w, b])
grads[0]
grads[1]

猜你喜欢

转载自www.cnblogs.com/tszr/p/12228174.html