Beispiel #1
0
 def test_raw_moore_penrose(self):
     from dlnn.tests.ml.repos_helper import corr_step_11_a, categorical_label_init
     from dlnn.util import MoorePenrose
     x = corr_step_11_a
     t = categorical_label_init
     r = K.dot(MoorePenrose.pinv2(K.variable(x), 1e-31), K.variable(t))
     self.assertIsNotNone(r)
Beispiel #2
0
    def test_training_model_bka_to_step_12_c(self):
        from keras import Model
        from dlnn.tests.ml.cnn_func_test import inputs
        from dlnn.util import MoorePenrose
        #
        # Feed Beta
        #
        feed = Model(inputs=inputs, outputs=step_11_c_activation)
        output = feed.predict(normalized)
        w_10_b = feed.get_layer(index=10).get_weights()
        w_12_b = [K.eval(K.dot(MoorePenrose.pinv3(output), K.variable(categorical_label_init)))]
        for i in range(12):
            layer = feed.get_layer(index=i).get_weights()
            self.assertIsNotNone(layer)
            # print("step_%d" % (i + 1), layer)

        #
        # Training Model
        #
        network = Model(inputs=inputs, outputs=step_12_c_dense)
        network.compile(optimizer=keras.optimizers.RMSprop(lr=0.0, rho=0.0, epsilon=None, decay=0.0),
                        loss=keras.losses.categorical_crossentropy,
                        metrics=[keras.metrics.categorical_accuracy, keras.metrics.mape])
        network.get_layer(index=10).set_weights(w_10_b)
        network.get_layer(index=12).set_weights(w_12_b)
        network.fit(normalized, categorical_label_init, batch_size=normalized.shape[0])
        self.assertTrue(numpy.allclose(w_10_b[0], network.get_layer(index=10).get_weights()[0], rtol=0))
        self.assertTrue(numpy.allclose(w_12_b[0], network.get_layer(index=12).get_weights()[0], rtol=0))
        result = network.predict(normalized, batch_size=normalized.shape[0])
        self.assertIsNotNone(result)
Beispiel #3
0
 def test_input_to_beta_c(self):
     from keras import Model
     from dlnn.util import MoorePenrose
     network = Model(inputs=inputs, outputs=step_11_c_activation)
     network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
     output = network.predict(normalized)
     beta = K.dot(MoorePenrose.pinv3(output), K.variable(categorical_label_init))
     self.assertIsNotNone(beta)
Beispiel #4
0
 def __train(self, x, y):
     assert self.layer is not None
     yc = to_categorical(y, self.category_num)
     elm_1_beta_net = Model(inputs=self.layer['input'],
                            outputs=self.layer['elm_1_activation_1'])
     elm_1_activation_1_o = elm_1_beta_net.predict(x)
     elm_1_dense_1_w = elm_1_beta_net.get_layer(
         name='elm_1_dense_1').get_weights()
     elm_1_dense_2_w = [
         K.eval(K.dot(MoorePenrose.pinv3(elm_1_activation_1_o), yc))
     ]
     elm_2_beta_net = Model(inputs=self.layer['input'],
                            outputs=self.layer['elm_2_activation_1'])
     elm_2_activation_1_o = elm_2_beta_net.predict(x)
     elm_2_dense_1_w = elm_2_beta_net.get_layer(
         name='elm_2_dense_1').get_weights()
     elm_2_dense_2_w = [
         K.eval(K.dot(MoorePenrose.pinv3(elm_2_activation_1_o), yc))
     ]
     elm_3_beta_net = Model(inputs=self.layer['input'],
                            outputs=self.layer['elm_3_activation_1'])
     elm_3_activation_1_o = elm_3_beta_net.predict(x)
     elm_3_dense_1_w = elm_3_beta_net.get_layer(
         name='elm_3_dense_1').get_weights()
     elm_3_dense_2_w = [
         K.eval(K.dot(MoorePenrose.pinv3(elm_3_activation_1_o), yc))
     ]
     network = Model(inputs=self.layer['input'],
                     outputs=self.layer['fully_connected_merge'])
     network.compile(optimizer=RMSprop(lr=0.0,
                                       rho=0.0,
                                       epsilon=None,
                                       decay=0.0),
                     loss=categorical_crossentropy,
                     metrics=[categorical_accuracy, mape])
     network.get_layer(name='elm_1_dense_1').set_weights(elm_1_dense_1_w)
     network.get_layer(name='elm_1_dense_2').set_weights(elm_1_dense_2_w)
     network.get_layer(name='elm_2_dense_1').set_weights(elm_2_dense_1_w)
     network.get_layer(name='elm_2_dense_2').set_weights(elm_2_dense_2_w)
     network.get_layer(name='elm_3_dense_1').set_weights(elm_3_dense_1_w)
     network.get_layer(name='elm_3_dense_2').set_weights(elm_3_dense_2_w)
     network.fit(x, yc)
     network.save(self.network_path)
Beispiel #5
0
    def test_manual_merge_categorical_value(self):
        from keras import Model
        from dlnn.tests.ml.cnn_func_test import inputs
        from dlnn.util import MoorePenrose
        import tensorflow as tf
        #
        # Feed Beta
        #
        feed = Model(inputs=inputs, outputs=step_11_a_activation)
        output = feed.predict(normalized)
        w_10_a = feed.get_layer(index=10).get_weights()
        w_12_a = [K.eval(K.dot(MoorePenrose.pinv3(output), K.variable(categorical_label_init)))]
        feed = Model(inputs=inputs, outputs=step_11_b_activation)
        output = feed.predict(normalized)
        w_10_b = feed.get_layer(index=10).get_weights()
        w_12_b = [K.eval(K.dot(MoorePenrose.pinv3(output), K.variable(categorical_label_init)))]
        feed = Model(inputs=inputs, outputs=step_11_c_activation)
        output = feed.predict(normalized)
        w_10_c = feed.get_layer(index=10).get_weights()
        w_12_c = [K.eval(K.dot(MoorePenrose.pinv3(output), K.variable(categorical_label_init)))]

        #
        # Training Model
        #
        network = Model(inputs=inputs, outputs=step_14_reshape)
        network.compile(optimizer=keras.optimizers.RMSprop(lr=0.0, rho=0.0, epsilon=None, decay=0.0),
                        loss=keras.losses.categorical_crossentropy,
                        metrics=[keras.metrics.categorical_accuracy, keras.metrics.mape])
        network.get_layer(index=10).set_weights(w_10_a)
        network.get_layer(index=11).set_weights(w_10_b)
        network.get_layer(index=12).set_weights(w_10_c)
        network.get_layer(index=16).set_weights(w_12_a)
        network.get_layer(index=17).set_weights(w_12_b)
        network.get_layer(index=18).set_weights(w_12_c)
        result = network.predict(normalized, batch_size=normalized.shape[0])
        # print(result)
        result = K.cast(K.argmax(result), dtype=tf.int32)
        # print(K.eval(result))
        result = tf.map_fn(lambda x: tf.bincount(x, minlength=3), result)
        # print(K.eval(result))
        result = K.argmax(result)
        # print(K.eval(result))
        self.assertIsNotNone(result)
Beispiel #6
0
    def test_baked_dlnndump(self):
        from keras import Model
        from dlnn.util import MoorePenrose
        #
        # Feed Beta
        #
        feed = Model(inputs=inputs, outputs=step_11_a_activation)
        output = feed.predict(corpus_data)
        w_10_a = feed.get_layer(index=13).get_weights()
        w_12_a = [
            K.eval(
                K.dot(MoorePenrose.pinv3(output),
                      to_categorical(label_init, 3)))
        ]
        feed = Model(inputs=inputs, outputs=step_11_b_activation)
        output = feed.predict(corpus_data)
        w_10_b = feed.get_layer(index=13).get_weights()
        w_12_b = [
            K.eval(
                K.dot(MoorePenrose.pinv3(output),
                      to_categorical(label_init, 3)))
        ]
        feed = Model(inputs=inputs, outputs=step_11_c_activation)
        output = feed.predict(corpus_data)
        w_10_c = feed.get_layer(index=13).get_weights()
        w_12_c = [
            K.eval(
                K.dot(MoorePenrose.pinv3(output),
                      to_categorical(label_init, 3)))
        ]

        self.assertTrue(True)
        from dlnn.Dlnn import DLNN_DEFAULT_CONFIG
        yc = keras.utils.to_categorical(label_init,
                                        len(numpy.unique(label_init)))
        dlnn = DlnnDump(**DLNN_DEFAULT_CONFIG)
        # dlnn.train(corpus_data, corpus_label - 1)
        network = dlnn.get_model()
        train_eval = network.evaluate(corpus_data, yc)
        self.assertIsNotNone(train_eval)
        # network.summary()
        # print(train_eval)

        from keras import Model
        layer_name = 'pre_tiling'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output, normalized, rtol=1e-6))

        layer_name = 'cnn_conv_1'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output[0], corr_step_1, rtol=1e-6))

        layer_name = 'cnn_activation_1'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output[0], corr_step_2, rtol=1e-6))

        layer_name = 'cnn_conv_2'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output[0], corr_step_3, rtol=1e-6))

        layer_name = 'cnn_activation_2'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output[0], corr_step_4, rtol=1e-6))

        layer_name = 'cnn_pooling_1'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output[0], corr_step_5, rtol=1e-6))

        layer_name = 'cnn_conv_3'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output[0], corr_step_6, rtol=1e-6))

        layer_name = 'cnn_activation_3'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output[0], corr_step_7, rtol=1e-6))

        layer_name = 'cnn_pooling_2'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output[0], corr_step_8, rtol=1e-6))
        self.assertTrue(
            numpy.allclose(intermediate_output, corr_step_8_full, rtol=1e-6))

        layer_name = 'bridge_flatten'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output, corr_step_9, rtol=1e-6))

        layer_name = 'elm_1_dense_1'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output, corr_step_10_a, rtol=1e-6))
        self.assertTrue(
            numpy.allclose(corr_step_10_a_kernel_init,
                           network.get_layer(layer_name).get_weights()[0],
                           rtol=1e-6))
        self.assertTrue(
            numpy.allclose(corr_step_10_a_bias_init,
                           network.get_layer(layer_name).get_weights()[1],
                           rtol=1e-6))
        self.assertTrue(
            numpy.allclose(w_10_a[0],
                           network.get_layer(layer_name).get_weights()[0],
                           rtol=1e-6))
        self.assertTrue(
            numpy.allclose(w_10_a[1],
                           network.get_layer(layer_name).get_weights()[1],
                           rtol=1e-6))

        layer_name = 'elm_1_activation_1'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output, corr_step_11_a, rtol=1e-6))

        layer_name = 'elm_2_dense_1'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output, corr_step_10_b, rtol=1e-6))
        self.assertTrue(
            numpy.allclose(corr_step_10_b_kernel_init,
                           network.get_layer(layer_name).get_weights()[0],
                           rtol=1e-6))
        self.assertTrue(
            numpy.allclose(corr_step_10_b_bias_init,
                           network.get_layer(layer_name).get_weights()[1],
                           rtol=1e-6))
        self.assertTrue(
            numpy.allclose(w_10_b[0],
                           network.get_layer(layer_name).get_weights()[0],
                           rtol=1e-6))
        self.assertTrue(
            numpy.allclose(w_10_b[1],
                           network.get_layer(layer_name).get_weights()[1],
                           rtol=1e-6))

        layer_name = 'elm_2_activation_1'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output, corr_step_11_b, rtol=1e-6))

        layer_name = 'elm_3_dense_1'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output, corr_step_10_c, rtol=1e-6))
        self.assertTrue(
            numpy.allclose(corr_step_10_c_kernel_init,
                           network.get_layer(layer_name).get_weights()[0],
                           rtol=1e-6))
        self.assertTrue(
            numpy.allclose(corr_step_10_c_bias_init,
                           network.get_layer(layer_name).get_weights()[1],
                           rtol=1e-6))
        self.assertTrue(
            numpy.allclose(w_10_c[0],
                           network.get_layer(layer_name).get_weights()[0],
                           rtol=1e-6))
        self.assertTrue(
            numpy.allclose(w_10_c[1],
                           network.get_layer(layer_name).get_weights()[1],
                           rtol=1e-6))

        layer_name = 'elm_3_activation_1'
        intermediate = Model(inputs=network.input,
                             outputs=network.get_layer(layer_name).output)
        intermediate_output = intermediate.predict(corpus_data)
        self.assertTrue(
            numpy.allclose(intermediate_output, corr_step_11_c, rtol=1e-6))

        layer_name = 'elm_1_dense_2'
        self.assertTrue(
            numpy.allclose(w_12_a[0],
                           network.get_layer(layer_name).get_weights()[0],
                           rtol=1e-6))

        layer_name = 'elm_2_dense_2'
        self.assertTrue(
            numpy.allclose(w_12_b[0],
                           network.get_layer(layer_name).get_weights()[0],
                           rtol=1e-6))

        layer_name = 'elm_3_dense_2'
        self.assertTrue(
            numpy.allclose(w_12_c[0],
                           network.get_layer(layer_name).get_weights()[0],
                           rtol=1e-6))
Beispiel #7
0
    def test_functional_dlnn(self):
        from keras import Model
        from dlnn.util import MoorePenrose
        #
        # Feed Beta
        #
        feed = Model(inputs=inputs, outputs=step_11_a_activation)
        output = feed.predict(corpus_data)
        w_10_a = feed.get_layer(index=13).get_weights()
        w_12_a = [
            K.eval(
                K.dot(MoorePenrose.pinv3(output),
                      to_categorical(label_init, 3)))
        ]
        feed = Model(inputs=inputs, outputs=step_11_b_activation)
        output = feed.predict(corpus_data)
        w_10_b = feed.get_layer(index=13).get_weights()
        w_12_b = [
            K.eval(
                K.dot(MoorePenrose.pinv3(output),
                      to_categorical(label_init, 3)))
        ]
        feed = Model(inputs=inputs, outputs=step_11_c_activation)
        output = feed.predict(corpus_data)
        w_10_c = feed.get_layer(index=13).get_weights()
        w_12_c = [
            K.eval(
                K.dot(MoorePenrose.pinv3(output),
                      to_categorical(label_init, 3)))
        ]

        #
        # Training Model
        #
        network = Model(inputs=inputs, outputs=step_15_output)
        network.compile(
            optimizer=keras.optimizers.RMSprop(lr=0.0,
                                               rho=0.0,
                                               epsilon=None,
                                               decay=0.0),
            loss=keras.losses.categorical_crossentropy,
            metrics=[keras.metrics.categorical_accuracy, keras.metrics.mape])
        for i in range(25):
            layer = network.get_layer(index=i).get_weights()
            self.assertIsNotNone(layer)
            # print("step_%d" % (i + 1), layer)
        network.get_layer(index=13).set_weights(w_10_a)
        network.get_layer(index=14).set_weights(w_10_b)
        network.get_layer(index=15).set_weights(w_10_c)
        network.get_layer(index=19).set_weights(w_12_a)
        network.get_layer(index=20).set_weights(w_12_b)
        network.get_layer(index=21).set_weights(w_12_c)
        network.fit(corpus_data,
                    to_categorical(label_init, 3),
                    batch_size=corpus_data.shape[0])
        self.assertTrue(
            numpy.allclose(w_10_a[0],
                           network.get_layer(index=13).get_weights()[0],
                           rtol=0))
        self.assertTrue(
            numpy.allclose(w_10_b[0],
                           network.get_layer(index=14).get_weights()[0],
                           rtol=0))
        self.assertTrue(
            numpy.allclose(w_10_c[0],
                           network.get_layer(index=15).get_weights()[0],
                           rtol=0))
        self.assertTrue(
            numpy.allclose(w_12_a[0],
                           network.get_layer(index=19).get_weights()[0],
                           rtol=0))
        self.assertTrue(
            numpy.allclose(w_12_b[0],
                           network.get_layer(index=20).get_weights()[0],
                           rtol=0))
        self.assertTrue(
            numpy.allclose(w_12_c[0],
                           network.get_layer(index=21).get_weights()[0],
                           rtol=0))
        result = network.predict(corpus_data, batch_size=corpus_data.shape[0])
        # print(result.argmax(axis=1))
        self.assertIsNotNone(result)