title: Reassigning Variables in Tensorflow and scope
body: <p>I'm trying to understand the difference between why one of these implementations works, and one doesn't.  I'm trying to represent some geometry in tensorflow.</p>
  <p>First, a helper file, d_math.py</p>
  <h1>!/usr/bin/env python3</h1>
  <p>import numpy as np
  import tensorflow as tf</p>
  <p>dtype = tf.float64</p>
  <pre><code>def skew_symmetric(vector):
      #Creates a tensorflow matrix which is a skew-symmetric version of the input vector    
      return tf.stack([(0., -vector[2], vector[1]), (vector[2], 0., -vector[0]), (-vector[1], vector[0], 0.)], axis=0)
  </code></pre>
  <p>Here is implementation 1:</p>
  <pre><code>#!/usr/bin/env python3
  import numpy as np
  import tensorflow as tf
  import d_math as d
  import math
  import time
  
  
  class Joint():
      def __init__(self, axis, pos): #TODO: right now only revolute:
          axis_ = tf.Variable(axis, dtype=d.dtype)
          axis_ /= tf.linalg.norm(axis)
          theta_ = tf.Variable(0.0, dtype=d.dtype) #Always at the 0 angle config
          self.theta_ = theta_
          self.R_ = tf.cos(theta_) * tf.eye(3, dtype=d.dtype) + d.skew_symmetric(axis_) + (1. - tf.cos(theta_)) * tf.einsum('i,j-&gt;ij', axis_, axis_)
          
  
  
  joint = Joint(np.array([1.0, 1.0, 1.0]), 0.0)
  init = tf.global_variables_initializer()    
  
  with tf.Session() as session:
      session.run(init)    
      print(joint.R_)
      print(joint.R_.eval())
      joint.theta_ = joint.theta_.assign(math.pi/4.)
      session.run(joint.theta_)
      print(joint.R_.eval())
      
  </code></pre>
  <p>The above version updates theta, and then I get the evaluation of two rotation matrices, one for theta = 0, and one for theta = pi/4.</p>
  <p>I then tried to refactor my code a bit, adding a global session variable, created in a separate file, and hiding away as much about tensorflow as I could for now in the API:</p>
  <p>version 2:</p>
  <pre><code>#!/usr/bin/env python3
  import numpy as np
  import tensorflow as tf
  import d_math as d
  import math
  import time
  import session as s
  
  
  class Joint():
      def __init__(self, axis, pos): #TODO: right now only revolute:
          axis_ = tf.Variable(axis, dtype=d.dtype)
          axis_ = axis_ / tf.linalg.norm(axis)
          theta_ = tf.Variable(0.0, dtype=d.dtype) #Always at the 0 angle config
          self.theta_ = theta_
          self.R_ = tf.cos(theta_) * tf.eye(3, dtype=d.dtype) + d.skew_symmetric(axis_) + (1. - tf.cos(theta_)) * tf.einsum('i,j-&gt;ij', axis_, axis_)
          
      def set_theta(self, theta):
          self.theta_.assign(theta)
          s.session.run(self.theta_)
          
  
  joint = Joint(np.array([1.0, 1.0, 1.0]), 0.0)
  init = tf.global_variables_initializer()    
  
  with s.session as session:
      session.run(init)  
      print(joint.R_)
      print(joint.R_.eval())
      #joint.theta_ = joint.theta_.assign(math.pi/4.)
      joint.set_theta(math.pi/4.)
      print(joint.R_.eval())
  
      
  </code></pre>
  <p>session.py can be seen here:</p>
  <pre><code>#!/usr/bin/env python3
  import tensorflow as tf
  
  session = tf.Session()
      
  </code></pre>
  <p>This gives the R matrix with theta = 0 for both evaluations.</p>
  <p>Can someone please explain to me why implementation 2 isn't working?</p>