Skip to content

Instantly share code, notes, and snippets.

@evmcheb
Created April 9, 2020 03:55
Show Gist options
  • Save evmcheb/2afc66bb480f865b338d2186216e042a to your computer and use it in GitHub Desktop.
Save evmcheb/2afc66bb480f865b338d2186216e042a to your computer and use it in GitHub Desktop.
from keras import backend as K
from keras.models import Model
from keras.models import Sequential
from keras.layers import Input, Dense, Dropout, Conv2D, Flatten, Activation, concatenate
from keras.optimizers import Adam
c = 0.6
def decorrelation_loss(neuron):
def loss(y_actual, y_predicted):
return K.mean(
K.square(y_actual-y_predicted) - c * K.square(y_predicted - neuron))
return loss
# split the two input streams
box_scores_train, odds_train = map(list, zip(*x_train))
box_scores_test, odds_test = map(list, zip(*x_test))
# box model turns stats into a vector
box_model = Sequential()
shape = box_scores_train[0].shape
print(shape)
box_model.add(Conv2D(filters=32, kernel_size=(1, 8), input_shape=shape,
data_format="channels_first", activation="relu"))
box_model.add(Flatten())
box_input = Input(shape=shape)
box_encoded = box_model(box_input)
odds_input = Input(shape=(1,), dtype="float32") #(opening or closing weight)
merged = concatenate([odds_input, box_encoded])
output = Dense(32, activation="relu")(merged)
output = Dropout(0.5)(output)
output = Dense(8, activation="relu")(output)
output = Dropout(0.5)(output)
signal = Dense(1, activation="sigmoid")(output)
opt = Adam(lr=0.0001)
nba_model = Model(inputs=[box_input, odds_input], outputs=signal)
print(nba_model.summary())
nba_model.compile(optimizer=opt,
#loss="binary_crossentropy",
loss=decorrelation_loss(odds_input), # Call the loss function with the selected layer
metrics=['accuracy'])
nba_model.fit([box_scores_train, odds_train], y_train,
batch_size=16,validation_data=([box_scores_test, odds_test], y_test), verbose=1,epochs=20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment