The tf.nn.l2_loss() function returns a tensor with 0 dimensions.
But I don’t need to manually apply this to every weight tensor, so storing weight tensors in the list is one way to solve the problem (as @mrry noted).
But instead of writing every time what you can do, use the following function
def l2_loss_sum(list_o_tensors): return tf.add_n([tf.nn.l2_loss(t) for t in list_o_tensors])
In your case, it will look like this:
total_loss = l2_loss_sum([layers[j].weights for j in range(self.n_layers)])
In addition, tf.nn.l2_loss() implicitly applies the squared operation to the values, as well as multiplying all the quadratic values by 1/2, so you used something like tf.nn.l2_loss(layers[j].weights**2 for j in range(self.n_layers)) , which you would tf.nn.l2_loss(layers[j].weights**2 for j in range(self.n_layers)) weight to the 4th degree. As a result, your derivative of this term loss would be strange: it would not cancel 1/2 to 1 (but implicitly double your β), and the weights would be cubed.
mpacer
source share