5

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.

Dave
  • 81
  • 4

3 Answers3

5

Basically, 'self' tells which variables and methods belongs to a class. So you have to tell that (x, func, diff_func and sess) belong to the MyClass. So modify the code as below:

import tensorflow as tf


class MyClass:
    def __init__(self):
        self.x = tf.Variable(tf.zeros(2))
        self.func = tf.cos(14.5 * self.x[0] - 0.3) + (self.x[1] + 0.2) * self.x[1] + (self.x[0] + 0.2) * self.x[0]
        self.diff_func = tf.gradients(self.func, self.x)

        self.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])
print(MyClass.getGrad([0.362, 0.556]))
Aravindh Kuppusamy
  • 1,893
  • 3
  • 14
  • 21
1

Replace sess = tf.Session() with self.sess = tf.Session().

  • Didn't work. Still the error "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 'func'" – Dave Mar 29 '18 at 22:34
  • 1
    This problem is solved a similar way. You must write `self.func` if you want `func` to be accessible anywhere in the class. In the snippet you've posted, the variables defined in `__init__` are only accessible from within `__init__`. [This post](https://stackoverflow.com/a/625097/4030272) elaborates on how it works. On a broader note, learning Python and TensorFlow by themselves is a challenge. Learning them at the same time is a recipe for frustration. You can save yourself some hassle by getting the fundamentals of Python down before moving on to TensorFlow. Happy hacking! – Christian NH Mar 29 '18 at 22:59
  • I have a very specific question. I have carved out a minimal code where the problem can be reproduced. Wouldn't it be more efficient to provide the corrected full working code? Then, I can build on it for more complicated cases. – Dave Mar 29 '18 at 23:06
0

If you are creating a variable inside constructor viz __init__. Then you need to associate self for showing that this variable belongs to that particular class. In this case, __init__ is the constructor of MyClass, then for defining sess, you need to associate self before it such as self.var_name. So that class functions getFunc() and getGrad() can understand that sess variable is an object variable and can use it. So just replace self with self.sess in the constructor.

Lucky Suman
  • 342
  • 3
  • 7