2017-06-28 11 views
0

私の機能は次のとおりです。Pythonの機能はTensorflowでは動作しません:tf.contrib.layers.apply_regularization

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関数が明示されているので、私は混乱しています。 私の問題は何ですか?

+0

groupl1はどこで使用されていますか? – mkaran

+0

また、スタックトレースから、コードがいくつかのtmpフォルダで実行されていることがわかります。あなたのコードを実行するとき、 'groupl1'が**グローバルに利用可能**であることを確認してください。 Pathに 'groupl1'が定義されたパス/ to/the/scriptを追加してください。 – mkaran

答えて

0

あなたはそれをと呼ぶ前に関数を定義していますか?

def groupl1(x): 
    return ... 

. 
. 
. 

groupl1(example_input) 
+0

はい前に私の関数を定義しました。そして、私の問題が何であるかを知りました。私はクラスで関数を定義し、同じクラスで関数を使用しようとしました。私はそれを修正し、それはうまくいった。御時間ありがとうございます。 –

+0

また、tf.contrib.layers.apply_regularization()の最初の引数は値なしの関数である必要があります –

+0

ようこそ。同じ問題を抱えている他の人のためにあなたの解答を追加したいかもしれません。質問に回答があるように回答を受け入れます。 – vanilla

関連する問題