Zeta36 / chess-alpha-zero

Chess reinforcement learning by AlphaGo Zero methods.
MIT License
2.13k stars 481 forks source link

Question... Is this possible to replace Tensorflow with Microsoft CNTK? #62

Closed xyhan88 closed 6 years ago

xyhan88 commented 6 years ago

Great stuff! I got work in both Windows and Ubuntu... Now I get a question to ask, Since it write in Keras which should should support both Google Tensorflow and MS CNTK. Is any simple way to replace Tensorflow with CNTK?

brianprichardson commented 6 years ago

Depends on what you mean by simple. It is like when someone says "if you use your imagination, it tastes like chicken", to which the reply is how much imagination :)

Search for keras in the repo and there are several places where the keras backend is used that would have to be changed I suspect. While I have never worked with CNTK, it seems possible.

However, I am a bit curious as to why you might want to.

Of course, the very active successor project below is more C and hard-coded tensorflow (no Keras). If you are interested in deep diving, I suggest looking there.

https://github.com/glinscott/leela-chess

xyhan88 commented 6 years ago

Took a look at keras menu and played around it a bit... Now I switched keras background to CNTK. It works. It seems it is a bit fast ruining at my Laptop ( I only can run CPU version on laptop)... Why am I going to try CNTK? Well I let make comparison between Tensorflow and CNTK about their performance...

brianprichardson commented 6 years ago

Glad it works. I started with Theano and have been moving to TensorFlow. Not ready to add CNTK yet.

I am not the owner of this repo, just trying to learn from it. Connect4 is another game. In fact, there are quite a few Alpha Zero projects with various games besides Chess and Go.

brianprichardson commented 6 years ago

@xyhan88 Please let us know what you had to change for CNTK. Installing it under Win7 was pretty painless and some of the demos seem to run ok for me, so I thought it would be interesting to see how much faster than TF it is. Thanks.

xyhan88 commented 6 years ago

@brianprichardson, First, you need to install the CNTK. See the following fro MS. I installed CNTK 2.4 https://docs.microsoft.com/en-us/cognitive-toolkit/Setup-CNTK-on-your-machine

The simplest way to switch between Tensorflow and CNTK is to modify keras's settings at .keras/keras.json file. See the following link from Keras: https://keras.io/backend/

In the keras.json file, just replace "tensorflow" with "cntk", That is.

Another way is to change the Chess_zero's code and assign keras's backend as cntk. i.e. in model_chess.py file. But i don't thing it is best way to do it...

In my laptop without using GPU card, In average, It takes about 60s plus for every move with tensorflow backend playing in Arena. CNT backend takes less 50s per move...

Let me know if it doesn't work for me. I am going to try theano too if I have time... Also once I get better understand this Repo and others, I will develop new system for Chinese Chess, a different kind of Chess which is popular Asian....

brianprichardson commented 6 years ago

Duh, should have just tried it first. I had assumed a bunch of changes would need to be made in chess-alpha-zero.

I commented out the pyperclip references and forced the cntk device to either cpu() or gpu(0) in manager.py per below. Just to be safe, I re-build a new random best model, and things seems to work.

I may look at more demanding performance scenarios with CNTK as making a move only takes about 8s on my slow gpu (a GTX 770), although it is also about 50s on my old Q6600 quad at 2.4GHz. Tensorflow gpu version is also about 8s.

The training (optimize.py) time is quite important for me, so I tried that copying the cntk device code to optimize.py (still a Python noob so not sure how to globally set it and did not want to go thru another full CNTK install in my virtual environment on this PC), although I seem to recall speed differences between TF and CNTK can vary quite a bit with batch size, so FWIW: TF 26s, CNTK 112s in one small test case.

import cntk as C print(C.device.all_devices()) print(C.device.try_set_default_device(C.device.gpu(0))) print(C.device.use_default_device())

xyhan88 commented 6 years ago

@brianprichardson, Thanks for the updating...