-2

I'm trying to learn somehing on neural networks, and I wrote my first program. I would like not to waste all the time which is spent in training the net by saving the adjusted parameters in a file and by loading its later (in an hour, day, or year). Here's the structure constructor of the Network class, whose wariable I want to save:

def __init__(self, sizes):
    self.num_layers = len(sizes)
    self.sizes = sizes
    self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
    self.weights = [np.random.randn(y, x)
             for x, y in zip(sizes[:-1], sizes[1:])]
    return

and here's my attempt to save and load it,

def save(net, name = "noname"):
    with open("{0}_nn_sizes.nn".format(name),"w") as s:
        s.write(str(net.sizes))
    with open("{0}_nn_weights.nn".format(name),"w") as w:
        w.write(str(net.weights))
    with open("{0}_nn_biases.nn".format(name),"w") as b:
        b.write(str(net.biases))
    return

def load(name = "noname"):
    with open("{0}_nn_sizes.nn".format(name),"w") as s:
        net=nn.Network(list(s)) """ERROR HERE"""
    with open("{0}_nn_weights.nn".format(name),"w") as w:
        net.weights = list(w)
    with open("{0}_nn_biases.nn".format(name),"w") as b:
        net.biases = list(b)
    return net

which disastrously fails giving an io.UnsupportedOperation: not readable error where pointed.

Actually I'm pretty sure that even the approach is kind of bad, so I'd gladly accept any suggestion or hint on how solve my problem.

Dinisaur
  • 101
  • 2
  • 5
    Make sure you reproduce your indentation accurately when posting Python code. Otherwise you are introducing new errors into the code. – khelwood Mar 28 '17 at 08:25
  • 3
    what do you mean when you say it "rovinously fails"? Do you get an error message? Or is the output not what you'd expect? – asongtoruin Mar 28 '17 at 08:26
  • 2
    Are we supposed to know what "rovinously" means? Has it just been invented? – khelwood Mar 28 '17 at 08:29
  • The [pickle library](https://docs.python.org/3.6/library/pickle.html) might help you with your problem. – Mathieu Dhondt Mar 28 '17 at 08:35
  • thank you for the feedbacks and the corrections, I edited a bit in oreder to make it more comprehensible. – Dinisaur Mar 28 '17 at 09:05

1 Answers1

0

Thanks to LaundroMat for his hint, I found a working solution using the pickle library in the following code

import pickle

def save(net, name = "noname"):
    with open("{0}.nn".format(name),"wb") as save_file:
        pickle.dump(net,save_file)

def load(name = "noname"):
    with open("{0}.nn".format(name),"rb") as load_file:
        return pickle.load(load_file)

Any comment is still appreciate though.

Dinisaur
  • 101
  • 2