maxpumperla / deep_learning_and_the_game_of_go

Code and other material for the book "Deep Learning and the Game of Go"
https://www.manning.com/books/deep-learning-and-the-game-of-go
953 stars 387 forks source link

fix bug: Loading pickled version of mnist crashes on python 3 #12 #15

Closed jeffhgs closed 5 years ago

jeffhgs commented 5 years ago

Incorporates pickling arguments suggested by maxpumperla https://github.com/maxpumperla/deep_learning_and_the_game_of_go/issues/12

Before submitting a pull request, note that master and the respective chapter branches need to stay in sync with the print version of the book.

Should your current PR mix these types of changes, please consider to split it accordingly. Thank you!

jeffhgs commented 5 years ago

Closing:

Under python 2.7 the new unpickling code crashes like so:

  File "/Users/jehenrik/srchome/3pmod/deep_learning_and_the_game_of_go/code/dlgo/nn/load_mnist.py", line 27, in load_data
    data = pickle._Unpickler(f)
AttributeError: 'module' object has no attribute '_Unpickler'
maxpumperla commented 5 years ago

@jeffhgs why not do this:

def load_data():
    with gzip.open('mnist.pkl.gz', 'rb') as f:

        if hasattr(pickle, "_Unpickler"):  # Python 3
            data = pickle._Unpickler(f)
            data.encoding = 'latin1'  # set encoding
            train_data, validation_data, test_data = data.load()
        else:  # Python 2
            train_data, validation_data, test_data = pickle.load(f)