ahmed osama
ahmed osama

Reputation: 633

grabbing variables from the tf session

I have introduced to this code snippet , this is a very simple linear regression model using gradient descent.

What confuses me is the last line final_slope , final_intercept = sess.run([m,b]), is this the best way to grab the variables from the session, instead to run the session again?

I hope to understand how this statement operate under hood

My code:

import tensorflow as tf
import numpy as np

x_data = np.linspace(0,10,10) + np.random.uniform(-1.5,1.5,10)
y_label = np.linspace(0,10,10) + np.random.uniform(-1.5,1.5,10)

m = tf.Variable(0.29220241)
b = tf.Variable(0.84038402)

error = 0

for x,y in zip(x_data,y_label):

    y_hat = m*x + b

    error += (y-y_hat)**2 

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
train = optimizer.minimize(error)

init = tf.global_variables_initializer()


with tf.Session() as sess:

    sess.run(init)

    epochs = 1

    for i in range(epochs):

        sess.run(train)


    # Fetch Back Results
    final_slope , final_intercept = sess.run([m,b])

as per documentation

   a = tf.constant([10, 20])
   b = tf.constant([1.0, 2.0])
   # 'fetches' can be a singleton
   v = session.run(a)
   # v is the numpy array [10, 20]
   # 'fetches' can be a list.
   v = session.run([a, b])
   # v a Python list with 2 numpy arrays: the numpy array [10, 20] and the
   # 1-D array [1.0, 2.0]
   # 'fetches' can be arbitrary lists, tuples, namedtuple, dicts:
   MyData = collections.namedtuple('MyData', ['a', 'b'])
   v = session.run({'k1': MyData(a, b), 'k2': [b, a]})
   # v is a dict with
   # v['k1'] is a MyData namedtuple with 'a' the numpy array [10, 20] and
   # 'b' the numpy array [1.0, 2.0]
   # v['k2'] is a list with the numpy array [1.0, 2.0] and the numpy array
   # [10, 20].

still don't get the meaningful information of running the session again to extract the variable . i am would like to understand if running session every time is the best case to grabbing the variables or there is another way much better and faster

Upvotes: 0

Views: 925

Answers (1)

Him
Him

Reputation: 5551

The misunderstanding here is illustrated by your use of the phrase 'running the session'. A session is not 'run'. A session 'runs' a thing. The thought process is that, within a session, some portion of the compute graph is run, determined by the particular node that you ask for. So, when you perform session.run([y_hat]), then our tensorflow session (which is, basically, just the bare necessity to be able to do any calculations at all) 'runs' the parts of our computation graph necessary to compute the tensor y_hat. In your case, y_hat requires grabbing the values of a couple of variables, and doing some tensor multiplication and addition.

If you want other tensors from the graph you can 'run' those as well. Sometimes it is the case that certain tensors are computed en route to others. e.g. when we compute (y-y_hat)**2, y_hat is computed along the way. Instead of having to perform the entire compute graph for each, we can session.run([y_hat, (y-y_hat)**2]), and y_hat (afaik) will only be computed once.

The key insight here is that tensors are not stored between runs. So, if you call session.run([y_hat]) ; session.run([(y-y_hat)**2]), then all of the calculations leading up to y_hat have to be performed twice.

Hopefully this helps.

Upvotes: 3

Related Questions