我将 Python 3.6 和 Tensorflow 2.0 用于以下线性回归代码:
tx = tf.constant(x, dtype=tf.float32)
ty = tf.constant(y, dtype=tf.float32)
tw = tf.Variable(initial_value=np.random.randn(), dtype=tf.float32, trainable=True)
tb = tf.Variable(initial_value=0, dtype=tf.float32, trainable=True)
tyhat = tx * tw + tb
cost = lambda: tf.reduce_mean(tf.square(tyhat-ty))
optimizer = tf.optimizers.SGD(0.01)
train = optimizer.minimize(cost, var_list = [tw,tb])
并得到一个错误:没有为任何变量提供渐变:['Variable:0', 'Variable:0']
有什么帮助吗?