Esempio n. 1
0
    def test_save_and_load_weights(self):
        # create a net with random weights
        net = NeuralNet(model=self.model1)
        w = net.layers[0].weights
        b = net.layers[0].bias

        # save weights
        net.save_weights(self.weights_fname)

        # create a new net with random weights
        net = NeuralNet(model=self.model1)

        # load saved weights
        net.load_weights(self.weights_fname)
        new_w = net.layers[0].weights
        new_b = net.layers[0].bias

        # check that weights are equal
        self.assertTrue(np.alltrue(w == new_w),
                        msg="Saving and loading weights works")
        self.assertTrue(np.alltrue(b == new_b),
                        msg="Saving and loading biases works")

        # remove the temporarily saved weights
        os.remove(self.weights_fname)
Esempio n. 2
0
    def test_save_and_load_weights_fails(self):
        # create a net with random weights
        net = NeuralNet(model=self.model1)
        w = net.layers[0].weights
        b = net.layers[0].bias

        # save weights
        net.save_weights(self.weights_fname)

        # create a new net with random weights
        net = NeuralNet(model=self.model2)

        # load saved weights - this should fail
        self.assertRaises(RuntimeError, net.load_weights, self.weights_fname)

        # remove the temporarily saved weights
        os.remove(self.weights_fname)
Esempio n. 3
0
        default='../data/saved_weights_linear.dat',
        help=
        'Path and filename for saving and loading the weights (../data/saved_weights_linear.dat)'
    )

    args = parser.parse_args()

    weights_fname = args.weights_fname

    # create the model
    model = LinearModel()
    net = NeuralNet(model=model)

    if args.load_weights:
        print('- Loading weights from:', weights_fname)
        net.load_weights(weights_fname)

    # create the optimizer
    optimizer = SGD(net=net,
                    dataloader=RegressionDataLoader(),
                    batch_size=args.batch_size)

    # fit the model
    print('- Training model for', args.epochs, 'epoch, with learning rate',
          args.lr)
    optimizer.fit(n_epochs=args.epochs, learning_rate=args.lr)

    if args.save_weights:
        print("- Saving weights to:", weights_fname)
        net.save_weights(weights_fname)