-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/lorserker/ben into main
- Loading branch information
Showing
4 changed files
with
101 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import sys | ||
sys.path.append('../../../src') | ||
import datetime | ||
import numpy as np | ||
import os | ||
import logging | ||
from tensorflow import keras | ||
from batcher import Batcher | ||
|
||
model_path = './models/lead_model.h5' # Specify the .h5 file | ||
seed = 1337 | ||
batch_size = 64 | ||
n_iterations = 10000 | ||
display_step = 1000 | ||
|
||
X_train = np.load('./lead_bin/X.npy') | ||
B_train = np.load('./lead_bin/B.npy') | ||
y_train = np.load('./lead_bin/y.npy') | ||
|
||
n_examples = X_train.shape[0] | ||
n_ftrs = X_train.shape[1] | ||
n_cards = 32 | ||
n_bi = B_train.shape[1] | ||
|
||
n_hidden_units = 512 | ||
|
||
# Define the input layers with specific shapes | ||
x_ftrs = keras.layers.Input(shape=(n_ftrs,), name='X') | ||
b_ftrs = keras.layers.Input(shape=(n_bi,), name='B') | ||
|
||
# Concatenate x_ftrs and b_ftrs | ||
XB = keras.layers.concatenate([x_ftrs, b_ftrs], name='XB') | ||
|
||
# Define the rest of the model architecture based on your requirements | ||
model = keras.Sequential() | ||
|
||
model.add(keras.layers.Dense(n_hidden_units, activation='relu', kernel_initializer='glorot_uniform', name='w1')) | ||
model.add(keras.layers.Dropout(0.6, name='a1')) | ||
model.add(keras.layers.Dense(n_hidden_units, activation='relu', kernel_initializer='glorot_uniform', name='w2')) | ||
model.add(keras.layers.Dropout(0.6, name='a2')) | ||
model.add(keras.layers.Dense(n_hidden_units, activation='relu', kernel_initializer='glorot_uniform', name='w3')) | ||
model.add(keras.layers.Dropout(0.6, name='a3')) | ||
model.add(keras.layers.Dense(32, kernel_initializer='glorot_uniform', name='w_out')) | ||
|
||
# Define the output of the model | ||
lead_softmax = model(XB) | ||
|
||
# Compile the model | ||
compiled_model = keras.Model(inputs=[x_ftrs, b_ftrs], outputs=lead_softmax) | ||
compiled_model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss='categorical_crossentropy') | ||
|
||
# Train the Keras model | ||
batch = Batcher(n_examples, batch_size) | ||
cost_batch = Batcher(n_examples, 10000) | ||
|
||
for i in range(n_iterations): | ||
x_batch, b_batch, y_batch = batch.next_batch([X_train, B_train, y_train]) | ||
if i % display_step == 0: | ||
x_cost, b_cost, y_cost = cost_batch.next_batch([X_train, B_train, y_train]) | ||
c_train = compiled_model.evaluate([x_cost, b_cost], y_cost, batch_size=64, verbose=0) | ||
l_train = compiled_model.predict([x_cost, b_cost], batch_size=64) | ||
print('{}. c_train={}'.format(i, c_train)) | ||
print(np.mean(np.argmax(l_train, axis=1) == np.argmax(y_cost, axis=1))) | ||
|
||
sys.stdout.flush() | ||
compiled_model.save(model_path + '_keras.h5') # Save the Keras model in HDF5 format | ||
|
||
compiled_model.fit([x_batch, b_batch], y_batch, batch_size=64, epochs=1, verbose=0) | ||
|
||
compiled_model.save(model_path + '_keras.h5') |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import numpy as np | ||
import tensorflow as tf | ||
from tensorflow.keras.models import load_model | ||
|
||
class LeaderH5: | ||
|
||
def __init__(self, model_path): | ||
self.model_path = model_path | ||
self.model = self.init_model() | ||
|
||
def load_model(self): | ||
model = load_model(self.model_path) | ||
return model | ||
|
||
def init_model(self): | ||
model = self.load_model() | ||
|
||
def pred_fun(x, b): | ||
x = np.array(x) # Ensure that input data is in the right format | ||
b = np.array(b) | ||
result = model.predict([x, b]) | ||
result_with_softmax = tf.nn.softmax(result, axis=-1).numpy() # Apply softmax activation | ||
return result_with_softmax | ||
return pred_fun |