0

I am creating my own loss function (that I want to use in eager execution in Keras). I would like to add to it a term similar to a l1 loss function.

That is the loss function I am using now

def loss(model, x, y, x_dev, y_dev, variables):
  y_ = model(x)
  y_dev_ = model(x_dev)

  y_temp = 1.5

  return loss_mae(y_true=y, y_pred=y_)+y_temp*
                K.mean(tf.convert_to_tensor(variables))

with

import keras.backend as K
def loss_mae(y_true, y_pred):
    return K.mean(K.abs(y_pred-y_true))

my idea is to add to my loss function a constant (y_temp) and then I would like to multiply it for the trainable variables (to achieve something similar to a l1 regularisation term).

I tried passing to the loss() function model.trainable_variables but that does not work and I get a

TypeError: can't multiply sequence by non-int of type 'numpy.float32'

anyone has any suggestions?

Umberto
  • 1,387
  • 1
  • 13
  • 29

1 Answers1

1

The reason you are getting this error is because in Python your expression y_temp*variables means something like "take variables y_temp times and merge them in one sequence".

In other words, 2 * [1, 2, 3] is not [4, 5, 6], but [1, 2, 3, 1, 2, 3]. Obviously, it makes little sense to use anything but integer in this case.

If I understand you correctly, you are trying to perform element-wise multiplication. To achieve this you should use list comprehension, something like [x * 1.5 for x in [1, 2, 3]]

By the way, you could also check these answers to similar questions: Python can't multiply sequence by non-int of type 'float' and How do I multiply each element in a list by a number?

Roelant
  • 4,508
  • 1
  • 32
  • 62
farabhi
  • 11
  • 1
  • 2
  • Thanks... I just noticed that and was about to correct it... Dumb error. But I still have the same problem... I will probably solve it by using low-level API in Tensorflow. Thanks anyway... – Umberto Aug 21 '19 at 15:45
  • Not quite. I modified my question further to add more information of what I tried. It possibly too complicated to explain here... But I will try. Will updated it before lunch. – Umberto Aug 22 '19 at 05:50