Ejemplo n.º 1
0
    def model(self):
        input_tensor = Input(shape=self._input_shape, name='input')
        flattener = Flatten()(input_tensor)
        output = PolyFit(deg=self.num_hidden,
                         use_xbias=True,
                         name='output',
                         kernel_regularizer=regularizers.l2(
                             self.l2))(flattener)

        model = Model(inputs=input_tensor, outputs=output)

        return model
Ejemplo n.º 2
0
    def test_PolyFit(self):
        print('==========PolyFit tests==========')
        from astroNN.nn.layers import PolyFit

        # Data preparation
        polynomial_coefficient = [0.1, -0.05]
        random_xdata = np.random.normal(0, 3, (100, 1))
        random_ydata = polynomial_coefficient[
            1] * random_xdata + polynomial_coefficient[0]

        self.assertRaises(ValueError,
                          PolyFit,
                          deg=1,
                          use_xbias=False,
                          init_w=[2., 3., 4.])

        input = Input(shape=[
            1,
        ])
        output = PolyFit(deg=1,
                         use_xbias=False,
                         init_w=[[[0.1]], [[-0.05]]],
                         name='polyfit')(input)
        model = Model(inputs=input, outputs=output)
        model.compile(optimizer='sgd', loss='mse')

        model.fit(random_xdata, random_ydata, batch_size=32, epochs=1)
        # no gradient update thus the answer should equal to the real equation anyway
        self.assertEqual(
            np.allclose(model.predict(random_xdata), random_ydata), True)
        # no gradients update because initial weights are perfect
        npt.assert_almost_equal(np.squeeze(model.get_weights()[0]),
                                polynomial_coefficient)
        print(model.get_layer('polyfit').get_config())

        # Data preparation
        polynomial_coefficient = [0.075, -0.05]
        random_xdata = np.random.normal(0, 1, (100, 2))
        random_ydata = np.vstack(
            (np.sum(polynomial_coefficient[1] * random_xdata +
                    polynomial_coefficient[0],
                    axis=1),
             np.sum(polynomial_coefficient[1] * random_xdata +
                    polynomial_coefficient[0],
                    axis=1))).T

        input = Input(shape=[
            2,
        ])
        output = PolyFit(deg=1,
                         output_units=2,
                         use_xbias=False,
                         init_w=[[[0.075, 0.075], [0.075, 0.075]],
                                 [[-0.05, -0.05], [-0.05, -0.05]]],
                         name='PolyFit')(input)
        model = Model(inputs=input, outputs=output)
        model.compile(optimizer='sgd', loss='mse')

        model.fit(random_xdata, random_ydata, batch_size=32, epochs=1)
        npt.assert_almost_equal(model.predict(random_xdata), random_ydata)

        # no gradients update because initial weights are perfect
        npt.assert_almost_equal(np.squeeze(model.get_weights()[0]),
                                [[[0.075, 0.075], [0.075, 0.075]],
                                 [[-0.05, -0.05], [-0.05, -0.05]]])