I'm trying to setup a custom loss function for my dataset. I'm passing the constant initial parameters as part of the input layer and I want to slice the y_true tensor in the loss function to return only the dynamical variables in my system. i.e.
def modified_mse(y_true,y_pred):
return losses.mse(y_true[:,10:], y_pred)
However this loss function is giving wildly different losses than simply the Keras mse. Why are the losses so different? I would expect them to be the same. Minimum working example:
import pandas as pd
import numpy as np
from sklearn import metrics
import math, glob
import matplotlib.pyplot as plt
from collections import defaultdict
from functools import partial
from itertools import repeat
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import roc_curve, auc
from keras.utils import to_categorical
from keras import models
from keras import layers
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.layers import Dense, Dropout, Activation, Input
import keras.backend as K
import tensorflow as tf
from keras import losses
from keras.models import Model
from numpy.random import seed
df = pd.read_csv("julia_batch5_1.csv")
dfShuffle = shuffle(df,random_state=42)
i_col = ["m1","m2", "m3","x1", "x2", "x3", "y1", "y2", "y3", "tEnd"]
o_col = ["x1tEnd", "x2tEnd", "x3tEnd", "y1tEnd", "y2tEnd", "y3tEnd", "dx1tEnd", "dx2tEnd", "dx3tEnd", "dy1tEnd", "dy2tEnd", "dy3tEnd","eventID"]
X1 = dfShuffle[i_col].values
y1 = dfShuffle[o_col+i_col].values
X_train,X_test,y_train,y_test = train_test_split(X1,y1, test_size=0.2, random_state=42)
id_list_train = y_train[:,len(o_col+icol)-1]
id_list_test = y_test[:,len(o_col+icol)-1]
y_train = np.delete(y_train,len(o_col+icol)-1,1)
y_test = np.delete(y_test,len(o_col+icol)-1,1)
X_train = X_train.astype('float64')
X_test = X_test.astype('float64')
y_train = y_train.astype('float64')
y_test = y_test.astype('float64')
network = models.Sequential()
network.add(layers.Dense(128,activation='relu',input_dim=len(i_col)))
for i in range(9):
network.add(layers.Dense(128,activation='relu'))
network.add(layers.Dense(12,activation='linear'))
network.compile(optimizer='adam', loss=modified_mse, metrics=['accuracy'])
network.save_weights('model_init.h5')
history = network.fit(X_train,y_train,
epochs=30,
batch_size=128,
validation_data=(X_test,y_test),
verbose = 1)
Data file available at https://drive.google.com/open?id=1Jy5U3pjhNEoJUXSuyabzM_LMUNAOQJ3f