张量流中的 4X + 2 = 0
4X + 2 = 0 in tensorflow
我想用 TensorFlow 求解这个方程 (4X + 2 = 0),我有两个问题 关于这段代码:
import tensorflow as tf
x = tf.Variable(0.0)
for i in range(10000):
with tf.GradientTape() as tape:
y = 4 * x + 2
loss = tf.square(y)
gradient=tape.gradient(loss,[x])
tf.optimizers.SGD(learning_rate=0.01).apply_gradients(zip(gradient,[x]))
print(x.numpy())
我的第一个问题是:
为什么如果我在 for 循环之外声明方程及其损失函数会导致错误
y = 4 * x + 2
loss = tf.square(y)
我的意思是那些变量每次迭代都会在内存中启动一个新地址,所以我想把它们放在 for 循环之外。
第二个问题是:
为什么要把成本函数设为(4X + 2 )^2,然后再对方程微分?
在 Whosebug 上,最好在 post 中只问一个问题。在这个答案中,我将看看你的第一个问题。随意问第二个问题。我将编辑此答案并在您编辑问题后删除该部分
why if I declare the equation and its loss function outside the for loop it will lead to an error
从TF2开始,tensorflow的默认设计就是eager execution。这意味着使用 tensorflow 编码与使用原生 python 进行开发的方式很接近。
如果我们用原生 python 编写此 tensorflow 代码,我们将有:
learning_rate = 0.01
x = 0.0
training_steps = 100
for _ in range(training_steps):
# We calculate Y with the current value of X
y = 4*x + 2
# we calculate the mean squared error
error = (y-0)**2
# We calculate the gradient.
# In TF, this is done with a call to tape.gradient
# dy = 4 * dx
gradient = 4 * error
# We correct x with the new gradient.
# In TF this is done by calling the optimizer
x = x - (learning_rate*gradient)
print(x)
# this code results in x=-0.4853790481319223. Not bad!
在本机 python 代码中,很清楚为什么我们必须在循环中重新计算 y
的值:因为我们正在更改 x
的值,我们需要也更新 y
的值!这种行为在 TF 抽象下有点隐藏,但我希望它现在更清楚一些。
why if i declare the equation and its loss function outside the for
loop it will lead to an error
我想你是在专门询问你的循环,但你可以像这样声明函数。
@tf.function
def f(x):
Y = 4 * x + 2
Y = tf.square(Y)
return Y
x = tf.Variable(0.0)
opt = tf.keras.optimizers.SGD(learning_rate=0.01)
loss_fn = lambda: f(x)
var_list = [x]
for _ in range(10000):
opt.minimize(loss_fn, var_list)
print(x.numpy())
-0.49999997
我想用 TensorFlow 求解这个方程 (4X + 2 = 0),我有两个问题 关于这段代码:
import tensorflow as tf
x = tf.Variable(0.0)
for i in range(10000):
with tf.GradientTape() as tape:
y = 4 * x + 2
loss = tf.square(y)
gradient=tape.gradient(loss,[x])
tf.optimizers.SGD(learning_rate=0.01).apply_gradients(zip(gradient,[x]))
print(x.numpy())
我的第一个问题是:
为什么如果我在 for 循环之外声明方程及其损失函数会导致错误
y = 4 * x + 2
loss = tf.square(y)
我的意思是那些变量每次迭代都会在内存中启动一个新地址,所以我想把它们放在 for 循环之外。
第二个问题是:
为什么要把成本函数设为(4X + 2 )^2,然后再对方程微分?
在 Whosebug 上,最好在 post 中只问一个问题。在这个答案中,我将看看你的第一个问题。随意问第二个问题。我将编辑此答案并在您编辑问题后删除该部分
why if I declare the equation and its loss function outside the for loop it will lead to an error
从TF2开始,tensorflow的默认设计就是eager execution。这意味着使用 tensorflow 编码与使用原生 python 进行开发的方式很接近。 如果我们用原生 python 编写此 tensorflow 代码,我们将有:
learning_rate = 0.01
x = 0.0
training_steps = 100
for _ in range(training_steps):
# We calculate Y with the current value of X
y = 4*x + 2
# we calculate the mean squared error
error = (y-0)**2
# We calculate the gradient.
# In TF, this is done with a call to tape.gradient
# dy = 4 * dx
gradient = 4 * error
# We correct x with the new gradient.
# In TF this is done by calling the optimizer
x = x - (learning_rate*gradient)
print(x)
# this code results in x=-0.4853790481319223. Not bad!
在本机 python 代码中,很清楚为什么我们必须在循环中重新计算 y
的值:因为我们正在更改 x
的值,我们需要也更新 y
的值!这种行为在 TF 抽象下有点隐藏,但我希望它现在更清楚一些。
why if i declare the equation and its loss function outside the for loop it will lead to an error
我想你是在专门询问你的循环,但你可以像这样声明函数。
@tf.function
def f(x):
Y = 4 * x + 2
Y = tf.square(Y)
return Y
x = tf.Variable(0.0)
opt = tf.keras.optimizers.SGD(learning_rate=0.01)
loss_fn = lambda: f(x)
var_list = [x]
for _ in range(10000):
opt.minimize(loss_fn, var_list)
print(x.numpy())
-0.49999997