【问题标题】:How do I implement the optimization function in tensorflow?如何在tensorflow中实现优化功能?
【发布时间】:2016-11-17 12:07:00
【问题描述】:

minΣ(||xi-Xci||^2+ λ||ci||),

s.t cii = 0,

其中X是d * n形状的矩阵,C是n * n形状的矩阵,xi和ci分别表示X和C的列。

X 在这里是已知的,我们想根据 X 找到 C。

【问题讨论】:

    标签: machine-learning tensorflow theano deep-learning convex-optimization


    【解决方案1】:

    通常会有这样的损失,您需要对其进行矢量化,而不是使用列:

    loss = X - tf.matmul(X, C)
    loss = tf.reduce_sum(tf.square(loss))
    
    reg_loss = tf.reduce_sum(tf.square(C), 0)  # L2 loss for each column
    reg_loss = tf.reduce_sum(tf.sqrt(reg_loss))
    
    total_loss = loss + lambd * reg_loss
    

    要在 C 的对角线上实现零约束,最好的方法是用另一个常数 lambd2 将其添加到损失中:

    reg_loss2 = tf.trace(tf.square(C))
    total_loss = total_loss + lambd2 * reg_loss2
    

    【讨论】:

    • 非常感谢!我正在考虑使用 tf.slice() 来获取矩阵的列,您认为这也可以吗? tf.slice()的机制是什么?
    • 这也可以,因为梯度会反向传播到原始变量 C(和 X),但效率非常低
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    相关资源
    最近更新 更多