我试图为线性回归做一个简单的TensorFlow2.0代码
import tensorflow as tf
import numpy as np
x = tf.random.uniform([3,10])
coeff = tf.constant([[1.,2.,3.]])
intercept = 5.
def calcy(x=x, coeff=coeff, intercept=intercept):
return tf.linalg.matmul(coeff, x)+intercept
y = calcy()
@tf.function
def train(x=x, y=y):
train_coeff = tf.Variable([[0,0,0]], dtype = tf.float32)
train_intercept = tf.Variable(0, dtype = tf.float32)
result_y = calcy(x, train_coeff, train_intercept)
loss = tf.math.reduce_mean(tf.math.square(result_y-y))
for _ in range(10):
tf.compat.v1.train.GradientDescentOptimizer(0.5).minimize(loss)
train()
它返回ValueError:没有要优化的变量。你知道吗
我换了火车的部分,现在可以用了
相关问题 更多 >
编程相关推荐