Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/lorserker/ben into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ThorvaldAagaard committed Oct 27, 2023
2 parents b0aafd0 + ff9a2c1 commit 5fd3b8c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 5 deletions.
70 changes: 70 additions & 0 deletions scripts/training/opening lead/lead_nn_tf2.py
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')
10 changes: 6 additions & 4 deletions src/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,11 @@ def get_lead_binary(auction, hand, binfo, vuln, ns, ew):
n_steps = 1 + len(auction) // 4
A = get_auction_binary(n_steps, auction, lead_index, hand, vuln, ns, ew)

p_hcp, p_shp = binfo.model(A)

b[:3] = p_hcp.reshape((-1, n_steps, 3))[:,-1,:].reshape(3)
b[3:] = p_shp.reshape((-1, n_steps, 12))[:,-1,:].reshape(12)
if binfo:
p_hcp, p_shp = binfo.model(A)

b[:3] = p_hcp.reshape((-1, n_steps, 3))[:,-1,:].reshape(3)
b[3:] = p_shp.reshape((-1, n_steps, 12))[:,-1,:].reshape(12)
print(x)
print(b)
return x.reshape((1, -1)), b.reshape((1, -1))
2 changes: 1 addition & 1 deletion src/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ async def main():
rdeal = random_deal()

# example of to use a fixed deal
# rdeal = ('AQ9.543.6.AKJ876 762.A96.KQJ42.Q2 KJ83.KJ2.T753.T5 T54.QT87.A98.943', 'S Both')
rdeal = ('AQ9.543.6.AKJ876 762.A96.KQJ42.Q2 KJ83.KJ2.T753.T5 T54.QT87.A98.943', 'S Both')

driver.set_deal(None, *rdeal, ns, ew, False)
else:
Expand Down
24 changes: 24 additions & 0 deletions src/nn/leaderH5.py
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

0 comments on commit 5fd3b8c

Please sign in to comment.