Custom loss function in keras/tensorflow

Viewed 79

I am trying to create an unsupervised neural network that can model this function: f(x1,x2) = x1+x2^2. To do this, I need a custom loss function. Here is what I have:

import keras
import numpy as np
import pandas as pd
import tensorflow as tf

def custom_loss(X1,X2):
    def loss(y_true,y_pred):
        # I don't have any values for y_true
        z = tf.math.add(X1,tf.math.multiply(X2,X2))
        return tf.math.square(tf.math.subtract(z,y_pred))
        # The "loss" should be [(X1+X2^2)-y_pred]^2
    return loss

df = pd.read_csv('inputs.csv')
INPUT = df.drop(columns=['OUTPUT']).astype(np.float32)
# "INPUT" contains the values for X1 and X2
X1 = df['X1'].astype(np.float32)
# "X1" contains the values for X1 only
X2 = df['X2'].astype(np.float32)
# "X2" contains the values for X2 only
y_true = df['OUTPUT'].astype(np.float32)
# I don't have a y_true. So I made an extra column full of zeros.

model = keras.models.Sequential()
model.add(keras.layers.Dense(20, activation='tanh', input_shape=(2,)))
model.add(keras.layers.Dense(20, activation='tanh'))
model.add(keras.layers.Dense(1, activation='tanh'))

model.compile(optimizer='adam', loss=custom_loss(X1, X2))
model.fit(INPUT, y_true, epochs=400)

test_data = np.array([0.5, 0.6])
print(model.predict(test_data.reshape(1, 2), batch_size=1))

If the code was correct, the test output should be 0.5+(0.6)^2 = 0.86, but it is totally off. I apologize for my poor programming. I am still very inexperienced. Thank you so much for any help.

0 Answers
Related