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

您好,请问怎么保存训练好的神经网络模型 #9

Closed
liuluyeah opened this issue Jan 20, 2018 · 1 comment
Closed

您好,请问怎么保存训练好的神经网络模型 #9

liuluyeah opened this issue Jan 20, 2018 · 1 comment

Comments

@liuluyeah
Copy link

No description provided.

@hailiang-wang
Copy link
Member

将network中的参数dump到存储空间,然后增加load方法。
比如:

 def dumps(self, file_path):
        '''
        Dump nn parameters into file
        '''
        dumps.write(dict({
                         "input_layer_size": self.input_layer_size,
                         "output_layer_size": self.output_layer_size,
                         "layers": self.layers,
                         "layers_num": self.layers_num,
                         "weights": [ y.tolist() for y in self.weights],
                         "biases": [ y.tolist() for y in self.biases],
                         "epoch": self.epoch,
                         "lr": self.lr,
                         "batch_size": self.batch_size,
                         "eval_every_N_steps": self.eval_every_N_steps,
                         "train_data_file": self.train_data_file,
                         "test_data_file": self.test_data_file
                         }), file_path)



    def loads(self, file_path):
        '''
        load neural network parameters
        '''
        data = dumps.read(file_path)
        if "input_layer_size" in data:
            self.input_layer_size = data['input_layer_size']


        if "output_layer_size" in data:
            self.output_layer_size = data['output_layer_size']


        if "layers" in data:
            self.layers = data['layers']


        if "layers_num" in data:
            self.layers_num = data['layers_num']
            print("loads: %s" % "layers_num")

        if "weights" in data:
            self.weights = [ np.array(x) for x in data['weights']]
            print("loads: %s" % "weights")

        if "biases" in data:
            self.biases = [ np.array(x) for x in data['biases']]


        if "epoch" in data:
            self.epoch = data['epoch']


        if "lr" in data:
            self.lr = data['lr']


        if "batch_size" in data:
            self.batch_size = data['batch_size']


        if "eval_every_N_steps" in data:
            self.eval_every_N_steps = data['eval_every_N_steps']


        if "train_data_file" in data:
            self.train_data_file = data['train_data_file']



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants