Skip to content

Instantly share code, notes, and snippets.

@kolygri
Created April 4, 2019 17:21
Show Gist options
  • Save kolygri/c222adba4dff710c6c53bf83c0ed5d21 to your computer and use it in GitHub Desktop.
Save kolygri/c222adba4dff710c6c53bf83c0ed5d21 to your computer and use it in GitHub Desktop.
Custom loss function, silly example
from keras.models import Sequential
from keras.layers import Dense
import keras.backend as K
import numpy as np
def penalized_loss(reward):
def custom_loss(y_true, y_pred):
return K.mean(K.square(y_pred - y_true) - K.square(y_true - reward), axis=-1)
return custom_loss
if __name__ == '__main__':
input_data = np.array([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]])
x = input_data[:, :-2] # States
y = input_data[:, -2] # Actions
r = input_data[:, -1] # Rewards
# create model
model = Sequential()
model.add(Dense(12, input_dim=4, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile model
model.compile(loss=[penalized_loss(reward=r)], optimizer='adam', metrics=['accuracy'])
model.fit(x, y)
@kolygri
Copy link
Author

kolygri commented Apr 5, 2019

Tutorial on one hot encoding using sklearn and Keras:
https://machinelearningmastery.com/how-to-one-hot-encode-sequence-data-in-python/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment