Beispiel #1
0
    def test_saveload_model(self):

        # make and train a very small model
        inputs = Input(shape=(2, ))
        dense1 = Dense(10, activation='relu', name='FC_1')(inputs)
        dense2 = Dense(5, activation='relu', name='FC_2')(dense1)
        output1 = Dense(1, name='output1')(dense2)
        output2 = Dense(1, name='output2')(dense2)
        output3 = Dense(2, activation='softmax', name='output3')(dense2)

        model = Model(inputs, [output1, output2, output3])

        # v1 = np.random.normal(0, 2, 100)
        # v2 = np.random.normal(0.3, 0.5, 100)
        # v3 = np.random.normal(-0.3, 0.5, 100)

        # X = np.array([v1, v2]).T
        # y = [v3, v3]

        model.compile(optimizer='adam',
                      loss='mean_squared_error',
                      metrics=['mae'])
        # history = model.fit(X, y, epochs=1, batch_size=20, shuffle=True)

        try:
            fileio.safe_model(model, {}, [], [], [], {}, 'test.h5')
            model_new, _, _, _, _, _, _ = fileio.load_model('test.h5')
        finally:
            os.remove('test.h5')

        assert model.to_json() == model_new.to_json()
Beispiel #2
0
    def test_saveload_history(self):

        # the save function NEEDS a model to work
        model = Sequential([
            Dense(32, input_shape=(5, )),
            Activation('relu'),
            Dense(10),
            Activation('softmax'),
        ])

        data = {
            'M1final_mae': [0.3, 0.2],
            'val_M1final_mae': [0.31, 0.21],
            'M1final_loss': [1.5, 1.3],
            'val_M1final_loss': [1.6, 1.4],
            'training_run': [1, 1]
        }
        history = pd.DataFrame(data=data)
        history.index.name = 'epoch'

        try:
            fileio.safe_model(model, {}, [], [], [], {},
                              'test.h5',
                              history=history)
            _, _, _, _, _, _, history_new = fileio.load_model('test.h5')
        finally:
            os.remove('test.h5')

        np.testing.assert_array_equal(history.columns, history_new.columns)

        np.testing.assert_array_equal(history.values, history_new.values)

        assert history_new.index.name == 'epoch'
Beispiel #3
0
    def save_model(self, filename, include_history=False):
        """
        Save a trained model to hdf5 file for later use
        """
        history = self.history if include_history else None

        fileio.safe_model(self.model,
                          self.processors,
                          self.features,
                          self.regressors,
                          self.classifiers,
                          self.setup,
                          filename,
                          history=history)
Beispiel #4
0
    def save_model(self, filename):
        """
        Save a trained model to hdf5 file for later use
        """

        fileio.safe_model(self.model,
                          self.processors,
                          self.features,
                          self.regressors,
                          self.classifiers,
                          self.setup,
                          filename,
                          history=None,
                          method='pickle')
Beispiel #5
0
    def save_model(self, filename):
        """
        Save a trained model to a pickled file for later use.
        Gradient boosted models can for now not be saved to hdf5 format.
        """

        fileio.safe_model(self.model,
                          self.processors,
                          self.features,
                          self.regressors,
                          self.classifiers,
                          self.setup,
                          filename,
                          history=None,
                          method='pickle')