1

I'm trying to write a custom loss function for a Keras Model using TensorFlow 2.0. I followed the directions in a similar answer to get the input layer into the loss function like

here

Keras custom loss function: Accessing current input pattern

and here

https://datascience.stackexchange.com/questions/25029/custom-loss-function-with-additional-parameter-in-keras

but I also want to add the output of a second model to the loss function.

The error appears to be coming from v.predict(x). At first TensorFlow gives an error like ValueError: When using data tensors as input to a model, you should specify the steps argument.

So I try adding the steps arg v.predict(x,steps=n) where n some integer and I get AttributeError: 'Tensor' object has no attribute 'numpy'

X = some np.random array Y = some function of X plus noise

def build_model():
    il = tf.keras.Input(shape=(2,),dtype=tf.float32)
    outl = kl.Dense(100,activation='relu')(il)
    outl = kl.Dense(50,activation='relu')(outl)
    outl = kl.Dense(1)(outl)
    return il,outl

def f(X,a):
    return (X[:,0:1] + theta*a)*a

def F(x,a):
    eps = tf.random.normal(tf.shape(x),mean=loc,stddev=scale)[:,0:1]
    z = tf.stack([x[:,0:1] + theta*a + eps,x[:,1:] - a],axis=1)[:,:,0]
    return z

def c_loss(x=None,v=None):
    def loss(y_true,y_pred):
        xp = F(x,y_pred)
        return kb.mean(f(x,y_pred) + v.predict(xp)) 
    return loss

v_in,v_out = build_model()
v_model = tf.keras.Model(inputs=v_in,outputs=v_out)
v_model.compile(tf.keras.optimizers.Adam(),loss='mean_squared_error')
v_model.fit(x=X,y=Y)

c_in,c_out = build_model()
c_model = tf.keras.Model(inputs=c_in,outputs=c_out)
c_model.compile(tf.keras.optimizers.Adam(),loss=c_loss(x=c_in,v=v_model))
c_model.fit(x=X,y=Y_dummy)

Ideally I just expect the call of c_model.fit() to build a neural network to minimize the functional f(x,a) + v(x).

CJM
  • 23
  • 6

1 Answers1

0

I found the answer here. I had the "steps" error, but the actual problem was the "numpy" error. I added tf.enable_eager_execution() at the start of the program in order to take care of it.

Helen
  • 316
  • 3
  • 16