lorserker / ben

a game engine for bridge
GNU General Public License v3.0
43 stars 31 forks source link

Tf2 #58

Closed ZiggerZZ closed 1 year ago

ZiggerZZ commented 1 year ago

Hi,

Thanks for open sourcing ben! I'm myself working on bridge and AI.

This PR aims to update Tensorflow to 2.0. It's useful since it's complicated to install tensorflow 1.15 with Python 3.7 since it's not supported by conda-forge anymore.

I made the changes a while ago, so when I tried to open a PR at first a had a conflict with sample.py. My changes to sample.py were something like (add a lambda function because numpy complained):

    ak_out_i = np.zeros((n_samples, len(ak_cards)), dtype=int)
    ak_out_i[:, :] = np.array(ak_cards)
    ak_out_i = np.vectorize(lambda x: np.random.permutation(np.copy(x)), signature='(n)->(n)')(ak_out_i)
    small_out_i = np.zeros((n_samples, len(small_cards)), dtype=int)
    small_out_i[:, :] = np.array(small_cards)
    small_out_i = np.vectorize(lambda x: np.random.permutation(np.copy(x)), signature='(n)->(n)')(small_out_i)

I was not able to run the current main branch locally with the following error:

  File "/Users/user/folder/ben/src/bots.py", line 118, in sample_hands
    lho_pard_rho = sample.sample_cards_auction(2048, n_steps, auction_so_far, turn_to_bid, self.hand, self.vuln, self.model, self.binfo_model)[:64]
AttributeError: module 'sample' has no attribute 'sample_cards_auction'

I'm happy to make additional changes to this PR to make it work with Tensorflow 2.0 and Python 3.9+.

ThorvaldAagaard commented 1 year ago

It would be great with an update to Tensorflow 2.0 (or later) - It has been on my wishlist for a long time :-)

Sample is changed to a class, so we can inject the different parameters for thresholds etc.

Looking at your commits sample is very different as your version loads the configuration, but latest gets it injected.

Primary reason for injecting it is it will allow us to have different version of the configuration for each bot.

So my best advise would be to get latest and then reapply your changes, but perhaps Lorand has better ideas.

Worst case I will look at your changes and do the work.

ZiggerZZ commented 1 year ago

My intention was to update sample.py to the latest version without any of my changes, isn't it the case now? I would be happy to make changes but unfortunately I'm unable to run gameserver.py even on the main branch.

ThorvaldAagaard commented 1 year ago

Clone the latest version in a new directory, and create an issue if gameserver.py can't run, or any other error you might see.

I have made some structural changes with directories regarding config files, and might have missed soemthing

ZiggerZZ commented 1 year ago

I'm unable to run Python 3.7 with TF 1.15 on my Mac, that's why I had to upgrade the code to TF 2.0 :)

Here's the error I'm getting:

(ben) zz@zz-MacBook-Pro src % python gameserver.py
Playing random deals or deals from the client
Reading configuration from ./config/default.conf
2023-08-26 22:27:25.171824: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:375] MLIR V1 optimization pass is not enabled
Traceback (most recent call last):
  File "/Users/zz/PycharmProjects/ben/src/gameserver.py", line 69, in <module>
    models = Models.from_conf(conf.load(configfile))
  File "/Users/zz/PycharmProjects/ben/src/nn/models.py", line 26, in from_conf
    bidder_model=Bidder('bidder', os.path.join(
  File "/Users/zz/PycharmProjects/ben/src/nn/bidder.py", line 17, in __init__
    self.output_softmax = tf.nn.softmax(self.graph.get_tensor_by_name('out_bid_logit:0'))
  File "/Users/zz/mambaforge/envs/ben/lib/python3.9/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/Users/zz/mambaforge/envs/ben/lib/python3.9/site-packages/tensorflow/python/eager/execute.py", line 53, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
TypeError: <tf.Tensor 'out_bid_logit:0' shape=(None, 40) dtype=float32> is out of scope and cannot be used here. Use return values, explicit Python locals or TensorFlow collections to access it.
Please see https://www.tensorflow.org/guide/function#all_outputs_of_a_tffunction_must_be_return_values for more information.

<tf.Tensor 'out_bid_logit:0' shape=(None, 40) dtype=float32> was defined here:
<unknown>

The tensor <tf.Tensor 'out_bid_logit:0' shape=(None, 40) dtype=float32> cannot be accessed from here, because it was defined in PyGraph<5628544912>, which is out of scope.
ThorvaldAagaard commented 1 year ago

Looking at the traceback there is a reference to Python 3.9 and as long we are on Tensorflow 1.15 we must stay at Python 3.7. Could that be the problem?

I have installed virtualenv instead of anaconda, and that is working OK with Python 3.7 - but I am on windows.

If we upgrade to TF 2.0 can we still use the trained models?

ZiggerZZ commented 1 year ago

Around 3 weeks ago my patched version with TF 2.0 and Python 3.9 was working. If you haven't made changes to the model, I suppose it should be working.

ThorvaldAagaard commented 1 year ago

Looks like simple changes as you are keeping V1 functionality.

But there is no Tensorflow 2.0 available for Python 3.9. When I tried to install it it looked like 2.5 was the lowest version. Am I missing something?

ThorvaldAagaard commented 1 year ago

Ok, I installed TF 2.5 and got the same error as you have, so I assume you was trying Python 3.9 and TF2.5 as Python 3.7 is End of life

Add tf.compat.v1.disable_eager_execution() and you should be able to use latest version

ZiggerZZ commented 1 year ago

For some reason I didn't get the same error when launching gameserver on my branch again. Maybe I was on a wrong branch. My conda_setup.sh contains conda create -n ben python=3.9 and pip install tensorflow==2.0 but I think we can even use latest tensorflow or >=2.0.

I pushed a commit fixing a few numpy issues, specifically

  File "/Users/zz/PycharmProjects/ben/src/sample.py", line 337, in shuffle_cards_bidding_info
    ak_out_i = np.vectorize(np.random.permutation,
  File "/Users/zz/mambaforge/envs/ben/lib/python3.9/site-packages/numpy/lib/function_base.py", line 2329, in __call__
    return self._vectorize_call(func=func, args=vargs)
  File "/Users/zz/mambaforge/envs/ben/lib/python3.9/site-packages/numpy/lib/function_base.py", line 2403, in _vectorize_call
    res = self._vectorize_call_with_signature(func, args)
  File "/Users/zz/mambaforge/envs/ben/lib/python3.9/site-packages/numpy/lib/function_base.py", line 2443, in _vectorize_call_with_signature
    results = func(*(arg[index] for arg in args))
  File "mtrand.pyx", line 4712, in numpy.random.mtrand.RandomState.permutation
  File "mtrand.pyx", line 4577, in numpy.random.mtrand.RandomState.shuffle

and

AttributeError: module 'numpy' has no attribute 'int'.
`np.int` was a deprecated alias for the builtin `int`.

I played a few games as a defender or a declarer and all went smoothly!

ZiggerZZ commented 1 year ago

Relaxed tf requirements. Also, there is still conda install tensorflow-estimator=1.15 in conda_setup.sh, we might need to update it. What do you use tensorflow-estimator for?

ThorvaldAagaard commented 1 year ago

I have manually merged the changes for TF 2.X into my repository and all seems to be working fine. Will du some more testing

I had some trouble with the training and to get it to run I had to ad tensorflow-estimator in a specific version. I will try removing it.

What do you think about trying BEN without conda and on latest Python version - would that cause trouble on Mac?

If we should use virtual environments anaconda is a bit annoying on windows, so virtual env would probably be better

ThorvaldAagaard commented 1 year ago

I have just tested game.py on Python 3.11 and TF 2.13 and it seems to work. Only trouble I had was that I had to use admin rights to install Tensorflow.

ThorvaldAagaard commented 1 year ago

I am no Github expert. I think all your changes is now in the main branch, but I am not sure what we then should do with this pull request. Can't find a place to see if there are any differences.

I will try to get latest and build the main branch on my mac without any environments

ThorvaldAagaard commented 1 year ago

Ah, I can see I have made my latest changes in my own fork. I will accept your changes, and sort out how my changes should be applied. Thx for contributing