How do I implement the optimization function in tensorflow?

后端 未结 1 592
梦谈多话
梦谈多话 2021-01-27 18:48

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 separatel

相关标签:
1条回答
  • 2021-01-27 19:30

    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
    
    0 讨论(0)
提交回复
热议问题