4X + 2 = 0 in tensorflow

Viewed 110

i want to solve this equation (4X + 2 = 0) using TensorFlow, i have two questions about this code:

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())

my first question is :

why if i declare the equation and its loss function outside the for loop it will lead to an error

y = 4 * x + 2
loss = tf.square(y)

i mean those variable every iteration will initiate a new address in the memory, so i wanted to put them away of for loop.

the second question is:

why should i make the cost function (4X + 2 )^2, then i differentiate the equation again?

2 Answers

On StackOverflow, it is better to ask only one question in a post. In this answer, I will look at your first question. Feel free to ask another question for the second one. I'll edit this answer and remove that part once you have edited your question

why if I declare the equation and its loss function outside the for loop it will lead to an error

Since TF2, the default design of tensorflow is eager execution. It means that coding with tensorflow is close to how you would develop in native python. If we write this tensorflow code in native python, we would have :

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!

In this native python code, it is clear why we have to recalculate the value of y in the loop: as we are changing the value of x, we need to update the value of y as well! This behaviour is a bit hidden under TF abstractions, but I hope that it is a bit clearer now.

why if i declare the equation and its loss function outside the for loop it will lead to an error

I think you are asking specifically about your loop but you can declare the function like this.

@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

Related