【问题标题】:python function does not work in Tensorflow: tf.contrib.layers.apply_regularizationpython函数在Tensorflow中不起作用:tf.contrib.layers.apply_regularization
【发布时间】:2017-06-28 07:15:36
【问题描述】:

我的功能是:

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)))

当我把它放在我的代码中时:

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

它不起作用并出现错误:

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

我很困惑,因为我在代码中明确说明了 groupl1 函数。 我的问题是什么?

【问题讨论】:

  • groupl1 是在哪里定义的?
  • 另外,我从堆栈跟踪中看到代码在某个 tmp 文件夹中运行。确保 groupl1 在您运行代码时全局可用,例如将定义了groupl1 的 path/to/the/script 添加到 Path。

标签: python tensorflow autoencoder


【解决方案1】:

只是猜测您是否在调用函数之前定义函数

def groupl1(x):
    return ...

.
.
.

groupl1(example_input)

【讨论】:

  • 是的,我之前定义了我的函数。我弄清楚我的问题是什么。我在一个类中定义了该函数,并尝试在同一个类中使用该函数。我修好了,效果很好。感谢您的宝贵时间。
  • tf.contrib.layers.apply_regularization() 中的第一个参数也需要是一个没有值的函数
  • 不客气。也许您想为遇到相同问题的其他人添加解决方案的答案。并接受答案,以便将问题标记为已回答。
猜你喜欢
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 2019-01-22
  • 2019-11-19
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多