<commit_before>import unittest
import numpy as np
import numpy.testing as npt


from astroNN import keras_import_manager
keras = keras_import_manager()

Input = keras.layers.Input
Dense = keras.layers.Dense
Model = keras.models.Model


class LayerCase(unittest.TestCase):
    def test_BayesianDropout(self):
        print('==========BayesianDropout test==========')
        from astroNN.nn.layers import BayesianDropout

        # Data preparation
        random_xdata = np.random.normal(0, 1, (1000, 7514))
        random_ydata = np.random.normal(0, 1, (1000, 25))

        input = Input(shape=[7514])
        dense = Dense(100)(input)
        b_dropout = BayesianDropout(0.2)(dense)
        output = Dense(25)(b_dropout)
        model = Model(inputs=input, outputs=output)
        model.compile(optimizer='sgd', loss='mse')

        model.fit(random_xdata, random_ydata, batch_size=128)

        # make sure dropout is on even in testing phase
        x = model.predict(random_xdata)
        y = model.predict(random_xdata)
        npt.assert_equal(np.any(np.not_equal(x, y)), True)

    def test_ConcreteDropout(self):
        print('==========ConcreteDropout test==========')
        from astroNN.nn.layers import ConcreteDropout

        # Data preparation
        random_xdata = np.random.normal(0, 1, (1000, 7514))
        random_ydata = np.random.normal(0, 1, (1000, 25))

        input = Input(shape=[7514])
        dense = ConcreteDropout(Dense(100))(input)
        output = Dense(25)(dense)
        model = Model(inputs=input, outputs=output)
        model.compile(optimizer='sgd', loss='mse')

        model.fit(random_xdata, random_ydata, batch_size=128)

        # make sure dropout is on even in testing phase
        x = model.predict(random_xdata)
        y = model.predict(random_xdata)
        npt.assert_equal(np.any(np.not_equal(x, y)), True)


if __name__ == '__main__':
    unittest.main()<commit_msg>add SpatialDropout 1D and 2D test case<commit_after>import unittest
import numpy as np
import numpy.testing as npt

from astroNN.shared.nn_tools import gpu_memory_manage
from astroNN import keras_import_manager
keras = keras_import_manager()

Input = keras.layers.Input
Dense = keras.layers.Dense
Conv1D = keras.layers.Conv1D
Conv2D = keras.layers.Conv2D
Flatten = keras.layers.Flatten
Model = keras.models.Model

gpu_memory_manage()


class LayerCase(unittest.TestCase):
    def test_BayesianDropout(self):
        print('==========BayesianDropout test==========')
        from astroNN.nn.layers import BayesianDropout

        # Data preparation
        random_xdata = np.random.normal(0, 1, (100, 7514))
        random_ydata = np.random.normal(0, 1, (100, 25))

        input = Input(shape=[7514])
        dense = Dense(100)(input)
        b_dropout = BayesianDropout(0.2)(dense)
        output = Dense(25)(b_dropout)
        model = Model(inputs=input, outputs=output)
        model.compile(optimizer='sgd', loss='mse')

        model.fit(random_xdata, random_ydata, batch_size=128)

        # make sure dropout is on even in testing phase
        x = model.predict(random_xdata)
        y = model.predict(random_xdata)
        npt.assert_equal(np.any(np.not_equal(x, y)), True)

    def test_ConcreteDropout(self):
        print('==========ConcreteDropout test==========')
        from astroNN.nn.layers import ConcreteDropout

        # Data preparation
        random_xdata = np.random.normal(0, 1, (100, 7514))
        random_ydata = np.random.normal(0, 1, (100, 25))

        input = Input(shape=[7514])
        dense = ConcreteDropout(Dense(100))(input)
        output = Dense(25)(dense)
        model = Model(inputs=input, outputs=output)
        model.compile(optimizer='sgd', loss='mse')

        model.fit(random_xdata, random_ydata, batch_size=128)

        # make sure dropout is on even in testing phase
        x = model.predict(random_xdata)
        y = model.predict(random_xdata)
        npt.assert_equal(np.any(np.not_equal(x, y)), True)

    def test_SpatialDropout1D(self):
        print('==========SpatialDropout1D test==========')
        from astroNN.nn.layers import BayesianSpatialDropout1D

        # Data preparation
        random_xdata = np.random.normal(0, 1, (100, 7514, 1))
        random_ydata = np.random.normal(0, 1, (100, 25))

        input = Input(shape=[7514, 1])
        conv = Conv1D(kernel_initializer='he_normal', padding="same", filters=2, kernel_size=16)(input)
        dropout = BayesianSpatialDropout1D(0.2)(conv)
        flattened = Flatten()(dropout)
        output = Dense(25)(flattened)
        model = Model(inputs=input, outputs=output)
        model.compile(optimizer='sgd', loss='mse')

        model.fit(random_xdata, random_ydata, batch_size=128)

        # make sure dropout is on even in testing phase
        x = model.predict(random_xdata)
        y = model.predict(random_xdata)
        npt.assert_equal(np.any(np.not_equal(x, y)), True)

    def test_SpatialDropout12D(self):
        print('==========SpatialDropout2D test==========')
        from astroNN.nn.layers import BayesianSpatialDropout2D

        # Data preparation
        random_xdata = np.random.normal(0, 1, (100, 28, 28, 1))
        random_ydata = np.random.normal(0, 1, (100, 25))

        input = Input(shape=[28, 28, 1])
        conv = Conv2D(kernel_initializer='he_normal', padding="same", filters=2, kernel_size=16)(input)
        dropout = BayesianSpatialDropout2D(0.2)(conv)
        flattened = Flatten()(dropout)
        output = Dense(25)(flattened)
        model = Model(inputs=input, outputs=output)
        model.compile(optimizer='sgd', loss='mse')

        model.fit(random_xdata, random_ydata, batch_size=128)

        # make sure dropout is on even in testing phase
        x = model.predict(random_xdata)
        y = model.predict(random_xdata)
        npt.assert_equal(np.any(np.not_equal(x, y)), True)


if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.1321518987341772, 'fraction_numerical': 0.033924050632911394, 'mean_word_length': 3.15859649122807, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7178982', 'n_tokens_mistral': 2102, 'n_tokens_neox': 1918, 'n_words': 396}