I want to use tensorflow gradients for a computation of other quantities later on. I need to numerically compute the objective function and gradients as functions in a class (This class then is used in the remaining suite). However, I am getting error for the below code:
import tensorflow as tf
class MyClass:
def __init__(self):
x=tf.Variable(tf.zeros(2))
func = tf.cos(14.5 * x[0] - 0.3) + (x[1] + 0.2) * x[1] + (x[0] + 0.2) * x[0]
diff_func = tf.gradients(func,x)
sess = tf.Session()
def getFunc(self,coords):
return self.sess.run(self.func,feed_dict={self.x:coords})
def getGrad(self,coords):
grad = self.sess.run(self.diff_func,feed_dict={self.x:coords})
return grad
MyClass = MyClass()
MyClass.getFunc([0.362,0.556])
MyClass.getGrad([0.362,0.556])
The error I am getting is:
Traceback (most recent call last):
File "", line 19, in MyClass.getFunc([0.362,0.556])
File "", line 11, in getFunc return self.sess.run(self.func,feed_dict={self.x:coords})
AttributeError: MyClass instance has no attribute 'sess'
Not sure how I can get this class running correctly. Thanks.