How do I implement the optimization function in tensorflow?

二次信任 提交于 2019-12-02 17:05:09

问题


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

s.t cii = 0,

where X is a matrix of shape d * n and C is of the shape n * n, xi and ci means a column of X and C separately.

X is known here and based on X we want to find C.


回答1:


Usually with a loss like that you need to vectorize it, instead of working with columns:

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

To implement the zero constraint on the diagonal of C, the best way is to add it to the loss with another constant lambd2:

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


来源:https://stackoverflow.com/questions/38376473/how-do-i-implement-the-optimization-function-in-tensorflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!