Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RCFR test and example to work with Keras 3 #1265

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions open_spiel/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,7 @@ if (OPEN_SPIEL_ENABLE_TENSORFLOW)
algorithms/nfsp_test.py
algorithms/policy_gradient_test.py
algorithms/psro_v2/strategy_selectors_test.py
# Broken in Python 3.12. Must port to Keras 3. https://github.com/google-deepmind/open_spiel/issues/1207.
# algorithms/rcfr_test.py
algorithms/rcfr_test.py
)
if (OPEN_SPIEL_ENABLE_PYTHON_MISC)
set(PYTHON_TESTS ${PYTHON_TESTS}
Expand Down
35 changes: 22 additions & 13 deletions open_spiel/python/algorithms/rcfr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def _new_model():
_GAME,
num_hidden_layers=1,
num_hidden_units=13,
num_hidden_factors=1,
use_skip_connections=True)


Expand Down Expand Up @@ -476,12 +475,16 @@ def test_rcfr_functions(self):
data = data.batch(12)
data = data.repeat(num_epochs)

optimizer = tf.keras.optimizers.Adam(lr=0.005, amsgrad=True)
optimizer = tf.keras.optimizers.Adam(learning_rate=0.005, amsgrad=True)

model = models[regret_player]
for x, y in data:
optimizer.minimize(
lambda: tf.losses.huber_loss(y, models[regret_player](x)), # pylint: disable=cell-var-from-loop
models[regret_player].trainable_variables)
with tf.GradientTape() as tape:
loss = tf.losses.huber_loss(y, model(x))
optimizer.apply_gradients(
zip(
tape.gradient(loss, model.trainable_variables),
model.trainable_variables))

regret_player = reach_weights_player

Expand All @@ -504,12 +507,15 @@ def _train(model, data):
data = data.batch(12)
data = data.repeat(num_epochs)

optimizer = tf.keras.optimizers.Adam(lr=0.005, amsgrad=True)
optimizer = tf.keras.optimizers.Adam(learning_rate=0.005, amsgrad=True)

for x, y in data:
optimizer.minimize(
lambda: tf.losses.huber_loss(y, model(x)), # pylint: disable=cell-var-from-loop
model.trainable_variables)
with tf.GradientTape() as tape:
loss = tf.losses.huber_loss(y, model(x))
optimizer.apply_gradients(
zip(
tape.gradient(loss, model.trainable_variables),
model.trainable_variables))

average_policy = patient.average_policy()
self.assertGreater(pyspiel.nash_conv(_GAME, average_policy), 0.91)
Expand Down Expand Up @@ -565,12 +571,15 @@ def _train(model, data):
data = data.batch(12)
data = data.repeat(num_epochs)

optimizer = tf.keras.optimizers.Adam(lr=0.005, amsgrad=True)
optimizer = tf.keras.optimizers.Adam(learning_rate=0.005, amsgrad=True)

for x, y in data:
optimizer.minimize(
lambda: tf.losses.huber_loss(y, model(x)), # pylint: disable=cell-var-from-loop
model.trainable_variables)
with tf.GradientTape() as tape:
loss = tf.losses.huber_loss(y, model(x))
optimizer.apply_gradients(
zip(
tape.gradient(loss, model.trainable_variables),
model.trainable_variables))

average_policy = patient.average_policy()
self.assertGreater(pyspiel.nash_conv(_GAME, average_policy), 0.91)
Expand Down
11 changes: 7 additions & 4 deletions open_spiel/python/examples/rcfr_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@ def _train_fn(model, data):
data = data.batch(FLAGS.batch_size)
data = data.repeat(FLAGS.num_epochs)

optimizer = tf.keras.optimizers.Adam(lr=FLAGS.step_size, amsgrad=True)
optimizer = tf.keras.optimizers.Adam(learning_rate=FLAGS.step_size, amsgrad=True)

@tf.function
def _train():
for x, y in data:
optimizer.minimize(
lambda: tf.losses.huber_loss(y, model(x), delta=0.01), # pylint: disable=cell-var-from-loop
model.trainable_variables)
with tf.GradientTape() as tape:
loss = tf.losses.huber_loss(y, model(x), delta=0.01)
optimizer.apply_gradients(
zip(
tape.gradient(loss, model.trainable_variables),
model.trainable_variables))

_train()

Expand Down
Loading