Created
April 4, 2019 17:21
-
-
Save kolygri/c222adba4dff710c6c53bf83c0ed5d21 to your computer and use it in GitHub Desktop.
Custom loss function, silly example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tutorial on one hot encoding using sklearn and Keras:
https://machinelearningmastery.com/how-to-one-hot-encode-sequence-data-in-python/