【发布时间】:2021-06-28 08:41:12
【问题描述】:
出于教育目的,我试图在 TensorFlow 主页上的 Basic training loops 教程的基础上创建一个简单的神经网络,用于对平面中的点进行分类。
所以,我在[0,1]x[0,1] 中有一些点存储在形状为(250, 2, 1) 的张量x 中,相应的标签(1. or 0.) 存储在形状为y 的张量y 中。然后我做
import tensorflow as tf
w0 = tf.Variable(tf.random.normal([4,2]), name = 'w0')
w1 = tf.Variable(tf.random.normal([1,4]), name = 'w1')
b1 = tf.Variable(tf.zeros([4,1]), name = 'b1')
b2 = tf.Variable(tf.zeros([1,1]), name = 'b2')
loss = tf.keras.losses.CategoricalCrossentropy()
def forward(x):
x0 = x
z1 = tf.matmul(w0, x0) + b1
x1 = tf.nn.relu(z1)
z2 = tf.matmul(w1, x1) + b2
x2 = tf.nn.sigmoid(z2)
return x2
with tf.GradientTape() as t:
current_loss = loss(y, forward(x))
gradients = t.gradient(current_loss, [b1, b2, w0, w1])
我得到的是一个预期形状的张量列表,但只包含零。有人给点建议吗?
【问题讨论】:
-
比
None好零!你期待什么?
标签: python tensorflow neural-network classification gradienttape