2017-09-18 6 views
1

テンソルフロー1.01に取り組んでいます。 私が見つけ例を変更しようとしています: https://github.com/nfmcclure/tensorflow_cookbook/tree/master/03_Linear_Regression/07_Implementing_Elasticnet_RegressionテンソルフローのL-0行列ノルム

私のモデルは同じ方法、具体的には、私はモデルの損失に別のL0のペナルティ項を追加したいと思い、単純な線形モデル

x_data = tf.placeholder(shape=[None, 3], dtype=tf.float32) 
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32) 

# Create variables for linear regression 
A = tf.Variable(tf.random_normal(shape=[3,1])) 
b = tf.Variable(tf.random_normal(shape=[1,1])) 

# Declare model operations 
model_output = tf.add(tf.matmul(x_data, A), b) 

ですL2ノルムで行わ:しかし

l2_a_loss = tf.reduce_mean(tf.square(A)) 
elastic_param2 = tf.constant(1.) 
e2_term = tf.multiply(elastic_param2, l2_a_loss) 

、私はL0ノルムを使用して損失を計算することはできません

elastic_param0= tf.constant(1.) 
l0_a_loss= tf.reduce_mean(tf.norm(A,ord=0)) 
e0_term= tf.multiply(elastic_param0, l0_a_loss) 
また一方で、私は私が

l0_a_loss= tf.reduce_mean(tf.norm(A,ord=0,axis=(0,1))) 

で軸引数の値を変更すると、それを修正することを期待していたモデルの損失に追加用語で

loss = tf.expand_dims(tf.add(tf.add(tf.reduce_mean(tf.square(y_target -  model_output)), e0_term), e2_term), 0) 

戻り

ValueError: 'ord' must be a supported vector norm, got 0. 

を差し込む

まだ入手する

ValueError: 'ord' must be a supported matrix norm in ['euclidean', 'fro', 1, inf], got 0 

このモデルのAのL-0ノルムを最小にするにはどうすればよいですか?

答えて

1

テンソルフローのドキュメントは間違っています(現在のバージョン1.3でも)。

あなたがthis commitから見ることができるように:それは実際にord=0を受け入れないようtf.normの

修正の説明。

これは私がtemporarlyすることでこれを解決したtf.norm

+0

ノルム= + infの場合l_inf_a_loss = tf.reduce_max(A)と同じように試していますが、l0_a_loss = tf.count_nonzero(A)で動作しようとしましたが、動作しません。 – ErroriSalvo

0

を使用することはできません、あなたが自分でL0ノルムを実装する必要があることを意味:公式ドキュメント/コードを楽しみにしてい

l0_a_loss=tf.cast(tf.count_nonzero(A), tf.float32) 

テンソルフローの更新

+1

しかしこれは区別できないのですね。 – ncasas

+0

はL1微分可能?L1はafaik使用可能 – ErroriSalvo

関連する問題