张量流中的L-0矩阵范数

时间:2017-09-18 06:42:49

标签: python tensorflow linear

我正在研究tensorflow 1.01。 我正在尝试修改在以下位置找到的示例: https://github.com/nfmcclure/tensorflow_cookbook/tree/master/03_Linear_Regression/07_Implementing_Elasticnet_Regression

我的模型是一个简单的线性模型

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)

具体来说,我想为模型损失添加另一个L0惩罚项,与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)

插入模型丢失中的附加术语

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.

我希望更改轴参数值可以修复它,同时使用

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

我还是

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

如何在此模型中最小化A的L-0范数?

2 个答案:

答案 0 :(得分:1)

tensorflow文档错误(即使在当前的1.3版本中)。

正如您从this commit所见:

  

修正tf.norm的说明,因为它实际上并不接受ord=0

这意味着您必须自己实施L0规范,不能使用tf.norm

答案 1 :(得分:0)

我暂时通过以下方式解决了这个问题:

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

期待tensorflow中的官方文档/代码更新

相关问题