0

my function is :

def groupl1(x):
    return tf.reduce_sum(tf.sqrt(tf.to_float(x.get_shape()[1])) * tf.sqrt(tf.reduce_sum(x ** 2, axis=1)))

and when i put it in my code:

elif loss == 'rmse,gl':
weightss=tf.trainable_variables()
reg=tf.contrib.layers.apply_regularization(groupl1,weightss)
loss =  tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(x_, decoded)))
                        )+reg*0.0001

it doesn't work with an error:

Traceback (most recent call last):
File "L1_02.py", line 45, in <module>
train_X_=model.fit_transform(train_X)
File "/home/hjson/tmp/BRCA/libsdae/stacked_autoencoder.py", line 93, in fit_transform
self.fit(x)
File "/home/hjson/tmp/BRCA/libsdae/stacked_autoencoder.py", line 70, in fit
print_step=self.print_step, lambda_=self.lambda_)
File "/home/hjson/tmp/BRCA/libsdae/stacked_autoencoder.py", line 138, in run
reg=tf.contrib.layers.apply_regularization(groupl1,weightss)
NameError: global name 'groupl1' is not defined

I'm confused because I clearly stated groupl1 function in my code. What is my problem here?

Joo Sohn
  • 25
  • 1
  • 6
  • Where is groupl1 defined in relation to where it is used? – mkaran Jun 28 '17 at 07:18
  • Also, I see from the stack trace that the code runs in some tmp folder. Make sure that `groupl1` is **globally available** when you run your code, e.g. add the path/to/the/script that has `groupl1` defined to the Path. – mkaran Jun 28 '17 at 07:38

1 Answers1

0

Just guessing do you define your function before you call it?

def groupl1(x):
    return ...

.
.
.

groupl1(example_input)
mrk
  • 8,059
  • 3
  • 56
  • 78
  • Yes I defined my function before. And I figured out what my problem was. I defined the function in a class and I tried to use the function in the same class. I fixed it, and it worked well. thanks for your time. – Joo Sohn Jun 29 '17 at 05:21
  • Also the first argument in tf.contrib.layers.apply_regularization() needs to be a function without its value – Joo Sohn Jun 29 '17 at 05:23
  • You're welcome. Maybe you want to add an answer with your solution for others coming with the same problem. And accept an answer so the question is marked as answered. – mrk Jun 29 '17 at 06:49