目錄python
\(y = XW + b\)dom
\(y = \sum{x_i}*w_i + b\)函數
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])
[<tf.Tensor: id=203, shape=(3, 1), dtype=float32, numpy= array([[-0.00047306], [-0.00288958], [-0.00280226]], dtype=float32)>, <tf.Tensor: id=201, shape=(1,), dtype=float32, numpy=array([-0.00275796], dtype=float32)>]
grads[0]
<tf.Tensor: id=203, shape=(3, 1), dtype=float32, numpy= array([[-0.00047306], [-0.00288958], [-0.00280226]], dtype=float32)>
grads[1]
<tf.Tensor: id=201, shape=(1,), dtype=float32, numpy=array([-0.00275796], dtype=float32)>