def test_forward_prop_sigmoid_small_input(self):
        layer = ActivationLayer("sigmoid")
        layer.set_input_shape((4,))

        input = np.array([-427562359, -329756, -86, 0], dtype=np.float64)
        expected_output = np.array([0, 0, 0, 0.5], dtype=np.float64)

        output = layer.forward_prop(input)
        numpy.testing.assert_array_almost_equal(output, expected_output)
    def test_forward_prop_relu(self):
        layer = ActivationLayer("ReLU")
        layer.set_input_shape((4,))

        input = np.array([1536, -83741, 84597, -1543], dtype=np.float64)
        expected_output = np.array([1536, 0, 84597, 0], dtype=np.float64)

        output = layer.forward_prop(input)
        numpy.testing.assert_array_equal(output, expected_output)
    def test_forward_prop_sigmoid(self):
        layer = ActivationLayer("sigmoid")
        layer.set_input_shape((4,))

        input = np.array([0, 0, 0, 0], dtype=np.float64)
        expected_output = np.array([0.5, 0.5, 0.5, 0.5], dtype=np.float64)

        output = layer.forward_prop(input)
        numpy.testing.assert_array_equal(output, expected_output)
    def test_forward_prop_leaky_relu(self):
        layer = ActivationLayer("leakyReLU")
        layer.set_input_shape((4,))

        input = np.array([57287, -6154385, 14938, -54787], dtype=np.float64)
        expected_output = np.array([57287, -61543.85, 14938, -547.87], dtype=np.float64)

        output = layer.forward_prop(input)
        numpy.testing.assert_array_almost_equal(output, expected_output)
 def __init__(self,
              optimizer=Optimizer.batch_gradient_descent_fixed,
              initializer=Initializer.xavier,
              batch_size=16,
              weights_decay=0.001):
     self.optimizer = optimizer
     self.initializer = initializer
     self.batch_size = batch_size
     self.weights_decay = weights_decay
     self.fc1 = FullyConnectedlayer(13, 16, self.batch_size,
                                    self.weights_decay)
     self.ac1 = ActivationLayer('relu')
     self.fc2 = FullyConnectedlayer(16, 1, self.batch_size,
                                    self.weights_decay)
     self.loss = Losslayer("LeastSquareLoss")
Beispiel #6
0
    def predict(self, data):
        nn = NeuralNetwork()
        l1 = Layer(56, 54)
        l2 = Layer(54, 25)

        nn.add(l1)
        nn.add(ActivationLayer(relu, relu_derivative))
        nn.add(l2)
        nn.add(ActivationLayer(sigmoid, sigmoid_derivative))

        l1.weights = np.load('weights1.npy')
        l2.weights = np.load('weights2.npy')

        l1.bias = np.load('bias1.npy')
        l2.bias = np.load('bias2.npy')

        out = nn.predict(data)
        pred = np.argmax(out)

        return pred
Beispiel #7
0
def crossValidation(trainingData,
                    atr,
                    days,
                    DAYS_AHEAD,
                    intervals=10,
                    EPOCHS=300,
                    LEARNING_RATE=0.2):
    reshaped_x = np.reshape(trainingData, (len(trainingData), 1, atr * days))
    reshaped_y = np.reshape(trainingData[:, 2], (len(trainingData), 1, 1))

    x = makeBatch(reshaped_x, intervals)
    y = makeBatch(reshaped_y, intervals)

    errors = []
    for i in range(len(x)):
        x_train = getSelection(x, i)[:-DAYS_AHEAD]
        y_train = getSelection(y, i)[DAYS_AHEAD:]

        x_test = x[i][:-DAYS_AHEAD]
        y_test = y[i][DAYS_AHEAD:]

        inputSize = 2 * days
        outputSize = int(inputSize / 2)
        net = Network()
        net.add(FCLayer(inputSize, outputSize * 3))
        net.add(ActivationLayer(tanh, tanh_prime))
        net.add(FCLayer(outputSize * 3, outputSize))
        net.add(ActivationLayer(tanh, tanh_prime))
        net.add(FCLayer(outputSize, 1))
        net.add(ActivationLayer(tanh, tanh_prime))

        # train
        net.use(mse, mse_prime)
        net.fit(x_train, y_train, epochs=EPOCHS, learning_rate=LEARNING_RATE)

        # test
        out, err = net.predict(x_test, y_test)
        errors.append(err)
        print(err)
    print(np.average(errors))
    def test_back_prop_sigmoid(self):
        layer = ActivationLayer("sigmoid")
        layer.set_input_shape((4,))

        input = np.array([0, 2365836, 0, -154366], dtype=np.float64)
        layer.forward_prop(input)

        expected_in_grad = np.array([0.25, 0, 0.25, 0], dtype=np.float64)
        out_grad = np.ones(4)

        in_grad = layer.back_prop(out_grad)
        numpy.testing.assert_array_almost_equal(in_grad, expected_in_grad)
    def test_back_prop_leaky_relu(self):
        layer = ActivationLayer("leakyReLU")
        layer.set_input_shape((4,))

        input = np.array([1, -1, 1, -1], dtype=np.float64)
        layer.forward_prop(input)

        out_grad = np.ones(4)
        expected_in_grad = np.array([1, 0.01, 1, 0.01], dtype=np.float64)

        in_grad = layer.back_prop(out_grad)
        numpy.testing.assert_array_almost_equal(in_grad, expected_in_grad)
Beispiel #10
0
        return quicksort(t1) + [pivot] + quicksort(t2)


## init
population = 10  #nombre total de cas tester en meme temps
nombre = 1000  #nombre de géneration
q = 0.3  #facteur de mutation (entre 0 et 1)

x_train = np.array([[[0, 0]], [[0, 1]], [[1, 0]],
                    [[1, 1]]])  #ce qui est en entrée
##init des reseaux de neurones aléatoirement
liste = []
for k in range(0, population):  #on crée "population" réseaux de neurones
    liste.append([0, Network()])
    liste[-1][1].add(FCLayer(2, 3))  # construction des couches
    liste[-1][1].add(ActivationLayer(tanh))  #choix fonction d'activation
    liste[-1][1].add(FCLayer(3, 3))
    liste[-1][1].add(ActivationLayer(tanh))
    liste[-1][1].add(FCLayer(3, 1))
    liste[-1][1].add(ActivationLayer(tanh))
    n = 0
while (n < nombre):  #tant que l'on n'est pas à la "nombre"ème génerations
    n += 1
    for k in range(0, population):  #pour tout les réseaux de neurones
        y_trouve = []
        y_voulu = []
        for x in x_train:  #on met les 4 entrées
            y = liste[k][1].predict(x)  #on teste les reseaux de neurones
            y_attendu = x[0][0] ^ x[0][1]  #la valeur que l'on doit avoir
            y_voulu.append(y_attendu)  #on met les resultats dans un tableau
            y_trouve.append(y)
Beispiel #11
0
from softmax_layer import SoftmaxLayer
from activation_layer import ActivationLayer
from activations import tanh, tanh_prime, softmax, softmax_prime
from losses import mse, mse_prime, cross_entropy, cross_entropy_prime

# TRAINING DATA
# input x_train shape is (4,1,2):
x_train = np.array([[[0, 0]], [[0, 1]], [[1, 0]], [[1, 1]]])
#y_train =np.array([ [[0]], [[1]], [[1]], [[0]] ])
y_train = np.array([[1, 0], [0, 1], [0, 1], [1, 0]])  # 1 hot vector

# NETWORK
net = Network()
net.add(FCLayer(2, 3))  # input co 2 dimension [0,0], 3 neron
# FCLayer sử dụng hàm activate là tanh, đạo hàm của tanh là tanh_prime:
net.add(ActivationLayer(tanh, tanh_prime))
#net.add(FCLayer(3, 1)) # input co 3 dimension, 1 neron
net.add(FCLayer(3, 2))  # input co 3 dimension, 2 neron
#net.add(ActivationLayer(tanh, tanh_prime))
net.add(ActivationLayer(softmax, softmax_prime))
"""
Thêm lớp ở trên thì bị lỗi sau:
Traceback (most recent call last):
  File "example_xor.py", line 36, in <module>
    net.fit(x_train, y_train, epochs=1000, learning_rate=0.1)
  File "E:\MyProg\Python\medium_nn\network.py", line 58, in fit
    error = layer.backward_propagation(error, learning_rate)
  File "E:\MyProg\Python\medium_nn\fc_layer.py", line 29, in backward_propagation
    weights_error = np.dot(self.input.T, output_error)
  File "<__array_function__ internals>", line 6, in dot
ValueError: shapes (3,1) and (2,2) not aligned: 1 (dim 1) != 2 (dim 0)
Beispiel #12
0
    def __init__(self, ds, classes, model, created, *args, **kwargs):
        super(TrainingWindow, self).__init__(*args, **kwargs)

        if created:
            # Initialising network
            self.network = Network()
            self.network.use(mean_squared_error, mean_squared_error_prime)
        else:
            self.network = model[0]

        # Getting inputs and outputs
        self.__dataset = ds
        self.__classes = classes
        #fill_missing_values(self.__dataset)
        #min_max_normalize_dataset(self.__dataset)
        ((x_train, y_train),
         (x_test, y_test)) = self.network.regular_split(self.__dataset, 0.5)

        # Getting inputs
        if len(x_train.shape) == 2:
            inputs = x_train.shape[1]
        else:
            inputs = x_train.shape[1:]
            first = inputs[0]
            second = inputs[1]
            third = inputs[2]

        # Getting expected outputs
        expected_output = y_train.shape[1]

        # Getting network name
        self.networkName = model[1]

        if created:
            # Getting model list
            self.__modelList = model[0]
            self.__modelList[0].setOutput(inputs)

            for i in range(1, len(self.__modelList)):
                # Getting the layer name
                name = self.__modelList[i].text(
                )[:len(self.__modelList[i].text()) - 6]
                activation = None
                activ_prime = None
                # Getting the activation function
                if self.__modelList[i].activation() == 0:
                    activation = sigmoid
                    activ_prime = sigmoid_prime
                elif self.__modelList[i].activation() == 1:
                    activation = tanh
                    activ_prime = tanh_prime
                elif self.__modelList[i].activation() == 2:
                    activation = rectified_linear_unit
                    activ_prime = rectified_linear_unit_prime
                elif self.__modelList[i].activation() == 3:
                    activation = softmax
                    activ_prime = softmax_prime
                # Adding layer to the network
                if name == "Dense":
                    if self.__modelList[i - 1].text()[:2] == "Fl":
                        self.network.add(
                            FullyConnectedLayer(first * second * third,
                                                self.__modelList[i].output()))
                        self.network.add(
                            ActivationLayer(activation, activ_prime))
                    else:
                        self.network.add(
                            FullyConnectedLayer(
                                self.__modelList[i - 1].output(),
                                self.__modelList[i].output()))
                        self.network.add(
                            ActivationLayer(activation, activ_prime))
                elif name == "Flatten":
                    self.network.add(FlattenLayer())
                elif name == "Convolutional":
                    self.network.add(
                        ConvLayer((first, second, third),
                                  (self.__modelList[i].kernelRows,
                                   self.__modelList[i].kernelColumns), 1))
                    self.network.add(ActivationLayer(activation, activ_prime))
                    first = first - self.__modelList[i].kernelRows + 1
                    second = second - self.__modelList[i].kernelColumns + 1

            self.network.add(
                FullyConnectedLayer(
                    self.__modelList[len(self.__modelList) - 1].output(),
                    expected_output))
            self.network.add(ActivationLayer(sigmoid, sigmoid_prime))

        # Loading Fonts
        QFontDatabase.addApplicationFont("fonts/BebasNeue-Light.ttf")

        # Window Settings
        self.setFixedSize(1280, 720)
        self.setWindowTitle("Training window")
        #background = QPixmap("images/menu")
        #palette = QPalette()
        #palette.setBrush(QPalette.Background, background)
        #self.setAttribute(Qt.WA_StyledBackground, True)
        #self.setPalette(palette)
        self.setAutoFillBackground(True)

        # Stylesheet Settings
        styleFile = QFile("stylesheets/training.qss")
        styleFile.open(QFile.ReadOnly)
        style = str(styleFile.readAll())
        self.setStyleSheet(style)

        # Title Settings
        self.title = QLabel("Training", self)
        self.title.setFont(QFont("BebasNeue", 30, QFont.Bold))
        self.title.setAlignment(Qt.AlignCenter)
        self.title.setGeometry(600, 10, 300, 120)

        # Epochs line edit settings
        self.__epochsLineEdit = QLineEdit(self)
        self.__epochsLineEdit.setValidator(QIntValidator(0, 100000, self))

        # Epochs label settings
        self.__epochsLabel = QLabel("Epoch number", self)
        self.__epochsLabel.setFont(QFont("BebasNeue", 20, QFont.Bold))

        # Learning rate line edit settings
        self.__learningRateLineEdit = QLineEdit(self)
        self.__learningRateLineEdit.setValidator(
            QDoubleValidator(0.0, 1.0, 3, self))

        # Learning rate label settings
        self.__learningRateLabel = QLabel("Learning rate", self)
        self.__learningRateLabel.setFont(QFont("BebasNeue", 20, QFont.Bold))

        # Learning rate checkboxsettings (auto or not)
        self.__learningRateCheckBox = QCheckBox("Auto adjustment", self)
        self.__learningRateCheckBox.setFont(QFont("BebasNeue", 15, QFont.Bold))

        # Dataset split settings label
        self.__datasetSplitLabel = QLabel("Dataset split percentage", self)
        self.__datasetSplitLabel.setFont((QFont("BebasNeue", 20, QFont.Bold)))

        # Dataset split mode buttons
        self.__datasetSplitRegular = QRadioButton("Regular split")
        self.__datasetSplitRandom = QRadioButton("Random split")

        # Dataset split mode buttons groupbox
        self.__datasetSplitModeButtonsLayout = QHBoxLayout(self)
        self.__datasetSplitModeButtonsGroupBox = QGroupBox(self)
        self.__datasetSplitModeButtonsGroupBox.setObjectName("setting")
        self.__datasetSplitModeButtonsLayout.addWidget(
            self.__datasetSplitRegular)
        self.__datasetSplitModeButtonsLayout.addWidget(
            self.__datasetSplitRandom)
        self.__datasetSplitModeButtonsGroupBox.setLayout(
            self.__datasetSplitModeButtonsLayout)
        self.__datasetSplitRegular.setChecked(True)

        # Dataset split combo box settings
        self.__datasetSplitComboBox = QComboBox(self)
        self.__datasetSplitComboBox.addItems(
            ['90% - 10%', '80% - 20%', '70% - 30%', '60% - 40%'])

        # Dataset split form layout settings
        self.__datasetSplitLayout = QFormLayout(self)
        self.__datasetSplitGroupBox = QGroupBox(self)
        self.__datasetSplitGroupBox.setObjectName("setting")
        self.__datasetSplitLayout.addWidget(self.__datasetSplitLabel)
        self.__datasetSplitLayout.addWidget(self.__datasetSplitComboBox)
        self.__datasetSplitGroupBox.setLayout(self.__datasetSplitLayout)

        # Epochs form layout settings
        self.__epochsFormLayout = QFormLayout(self)
        self.__epochsGroupBox = QGroupBox(self)
        self.__epochsGroupBox.setObjectName("setting")
        self.__epochsFormLayout.addWidget(self.__epochsLabel)
        self.__epochsFormLayout.addWidget(self.__epochsLineEdit)
        self.__epochsGroupBox.setLayout(self.__epochsFormLayout)

        # Learning rate form layout settings
        self.__learningRateFormLayout = QFormLayout(self)
        self.__learningRateGroupBox = QGroupBox(self)
        self.__learningRateGroupBox.setObjectName("setting")
        self.__learningRateFormLayout.addWidget(self.__learningRateLabel)
        self.__learningRateFormLayout.addWidget(self.__learningRateCheckBox)
        self.__learningRateFormLayout.addWidget(self.__learningRateLineEdit)
        self.__learningRateGroupBox.setLayout(self.__learningRateFormLayout)

        # Epochs number label
        self.__epochNumberLabel = QLabel("Epoch : ", self)
        self.__epochNumberLabel.setFont((QFont("BebasNeue", 15, QFont.Bold)))

        # Training accuracy label
        self.__trainingAccuracyLabel = QLabel("Accuracy : ", self)
        self.__trainingAccuracyLabel.setFont((QFont("BebasNeue", 15,
                                                    QFont.Bold)))

        # Training stats layout
        self.__trainingStatsLayout = QVBoxLayout(self)
        self.__trainingStatsGroupBox = QGroupBox(self)
        self.__trainingStatsLayout.addWidget(self.__epochNumberLabel)
        self.__trainingStatsLayout.addWidget(self.__trainingAccuracyLabel)
        self.__trainingStatsGroupBox.setLayout(self.__trainingStatsLayout)
        self.__trainingStatsGroupBox.setGeometry(1000, -30, 300, 150)

        # Training button settings
        self.__trainingButton = QPushButton("Start", self)
        self.__trainingButton.setCursor(Qt.PointingHandCursor)
        self.__trainingButton.setFont((QFont("BebasNeue", 30, QFont.Bold)))
        self.__trainingButton.clicked.connect(self.startTraining)

        # Go back button
        self.goBackButton = QPushButton("Back", self)
        self.goBackButton.setObjectName("retour")

        # Customising go back button
        self.goBackButton.setCursor(Qt.PointingHandCursor)
        self.goBackButton.setIcon(QIcon("images/goback_icon"))
        self.goBackButton.setIconSize(QSize(30, 30))
        self.goBackButton.setFont(QFont("BebasNeue", 20, QFont.Bold))

        # Confusion matrix button
        self.__confusionMatrixButton = QPushButton("Show confusion matrix",
                                                   self)
        self.__confusionMatrixButton.setCursor(Qt.PointingHandCursor)
        self.__confusionMatrixButton.setFont((QFont("BebasNeue", 17,
                                                    QFont.Bold)))
        self.__confusionMatrixButton.clicked.connect(self.showStats)
        self.__confusionMatrixButton.setGeometry(420, 20, 250, 80)
        self.__confusionMatrixButton.hide()

        # Parameters group box settings
        self.__parametersGroupBox = QGroupBox("Training parameters", self)
        self.__parametersGroupBox.setObjectName("parameters")
        self.__parametersLayout = QVBoxLayout(self)
        self.__parametersLayout.addWidget(self.__epochsGroupBox)
        self.__parametersLayout.addWidget(self.__datasetSplitGroupBox)
        self.__parametersLayout.addWidget(
            self.__datasetSplitModeButtonsGroupBox)
        self.__parametersLayout.addWidget(self.__learningRateGroupBox)
        self.__parametersLayout.addWidget(self.__trainingButton)
        self.__parametersLayout.addWidget(self.goBackButton)
        self.__parametersGroupBox.setLayout(self.__parametersLayout)
        self.__parametersGroupBox.setGeometry(0, 0, 400, 720)

        # Chart axis settings
        self.__xAxis = QtCharts.QValueAxis()
        self.__xAxis.setRange(0, 5)

        self.__yAxis = QtCharts.QValueAxis()
        self.__yAxis.setRange(0, 100)

        # Chart settings
        self.__series = QtCharts.QLineSeries()
        self.__chart = QtCharts.QChart()
        self.__chart.addAxis(self.__xAxis, Qt.AlignBottom)
        self.__chart.addAxis(self.__yAxis, Qt.AlignLeft)
        self.__chart.addSeries(self.__series)
        self.__series.attachAxis(self.__xAxis)
        self.__series.attachAxis(self.__yAxis)
        self.__chart.setTitle("Accuracy")
        self.__chartView = QtCharts.QChartView(self.__chart)
        self.__chartView.setRenderHint(QPainter.Antialiasing)

        # Chart layout settings
        self.__chartLayout = QVBoxLayout(self)
        self.__chartGroupBox = QGroupBox(self)
        self.__chartGroupBox.setObjectName("chart")
        self.__chartLayout.addWidget(self.__chartView)
        self.__chartGroupBox.setLayout(self.__chartLayout)
        self.__chartGroupBox.setGeometry(390, 100, 900, 600)

        # Update timer settings
        #self.__timer = QTimer(self)
        #self.__timer.timeout.connect(self.autoSave)
        #self.__timer.start(1000)


#app = QApplication(sys.argv)
#window = TrainingWindow()
#window.show()
#app.exec_()
def relu(z):
    return np.maximum(0, z)


def relu_prime(z):
    z[z < 0] = 0
    z[z > 0] = 1
    return z


def loss(y_true, y_predict):
    return 0.5 * (y_predict - y_true)**2


def loss_prime(y_true, y_predict):
    return y_predict - y_true


x_train = np.array([[[0, 0]], [[0, 1]], [[1, 0]], [[1, 1]]])
y_train = np.array([[[0]], [[1]], [[1]], [[0]]])

net = Network()
net.add(FCLayer((1, 2), (1, 3)))
net.add(ActivationLayer((1, 3), (1, 3), relu, relu_prime))
net.add(FCLayer((1, 3), (1, 1)))
net.add(ActivationLayer((1, 1), (1, 1), relu, relu_prime))
net.setup_loss(loss, loss_prime)
net.fit(x_train, y_train, epochs=1000, learning_rate=0.01)
out = net.predict(np.array([[0, 1]]))

print(out)
class DNNNet:
    def __init__(self,
                 optimizer=Optimizer.batch_gradient_descent_fixed,
                 initializer=Initializer.xavier,
                 batch_size=16,
                 weights_decay=0.001):
        self.optimizer = optimizer
        self.initializer = initializer
        self.batch_size = batch_size
        self.weights_decay = weights_decay
        self.fc1 = FullyConnectedlayer(13, 16, self.batch_size,
                                       self.weights_decay)
        self.ac1 = ActivationLayer('relu')
        self.fc2 = FullyConnectedlayer(16, 1, self.batch_size,
                                       self.weights_decay)
        self.loss = Losslayer("LeastSquareLoss")

    def forward_train(self, input_data, input_label):
        self.fc1.get_inputs_for_forward(input_data)
        self.fc1.forward()
        self.ac1.get_inputs_for_forward(self.fc1.outputs)
        self.ac1.forward()
        self.fc2.get_inputs_for_forward(self.ac1.outputs)
        self.fc2.forward()

        print(
            "predict label: \n ",
            np.concatenate((self.fc2.outputs[:10], input_label[:10]), axis=1))
        self.loss.get_inputs_for_loss(self.fc2.outputs)
        self.loss.get_label_for_loss(input_label)
        self.loss.compute_loss()
        print("loss: ", self.loss.loss)

    def backward_train(self):
        self.loss.compute_gradient()
        self.fc2.get_inputs_for_backward(self.loss.grad_inputs)
        self.fc2.backward()
        self.ac1.get_inputs_for_backward(self.fc2.grad_inputs)
        self.ac1.backward()
        self.fc1.get_inputs_for_backward(self.ac1.grad_inputs)
        self.fc1.backward()

    def predict(self, input_data):
        self.fc1.get_inputs_for_forward(input_data)
        self.fc1.forward()
        self.ac1.get_inputs_for_forward(self.fc1.outputs)
        self.ac1.forward()

        self.fc2.get_inputs_for_forward(self.ac1.outputs)
        self.fc2.forward()
        return self.fc2.outputs

    def eval(self, input_data, input_label):
        self.fc1.update_batch_size(input_data.shape[0])
        self.fc1.get_inputs_for_forward(input_data)
        self.fc1.forward()
        self.ac1.get_inputs_for_forward(self.fc1.outputs)
        self.ac1.forward()
        self.fc2.update_batch_size(input_data.shape[0])
        self.fc2.get_inputs_for_forward(self.ac1.outputs)
        self.fc2.forward()
        print("predict: \n ", self.fc2.outputs[:10])
        print("label: \n", input_label[:10])
        metric = MetricCalculator(label=input_label, predict=self.fc2.outputs)
        metric.get_mae()
        metric.get_mse()
        metric.get_rmse()
        metric.print_metrics()

    def update(self):
        self.fc1.update(self.optimizer)
        self.fc2.update(self.optimizer)

    def initial(self):
        self.fc1.initialize_weights(self.initializer)
        self.fc2.initialize_weights(self.initializer)
    def test_get_output_shape(self):
        layer = ActivationLayer("ReLU")
        layer.set_input_shape((100,))

        self.assertEqual(layer.get_output_shape(), (100,))
x_train /= 255
# encode output which is a number in range [0,9] into a vector of size 10
# e.g. number 3 will become [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
y_train = np_utils.to_categorical(y_train)

# same for test data : 10000 samples
x_test = x_test.reshape(x_test.shape[0], 1, 28 * 28)
x_test = x_test.astype('float32')
x_test /= 255
y_test = np_utils.to_categorical(y_test)

# Network
net = Network()
net.add(FCLayer(28 * 28,
                100))  # input_shape=(1, 28*28)    ;   output_shape=(1, 100)
net.add(ActivationLayer(tanh, tanh_prime))
net.add(FCLayer(100, 50))  # input_shape=(1, 100)      ;   output_shape=(1, 50)
net.add(ActivationLayer(tanh, tanh_prime))
net.add(FCLayer(50, 10))  # input_shape=(1, 50)       ;   output_shape=(1, 10)
net.add(ActivationLayer(tanh, tanh_prime))

# train on 1000 samples
# as we didn't implemented mini-batch GD, training will be pretty slow if we update at each iteration on 60000 samples...
net.use(mse, mse_prime)
net.fit(x_train[0:1000], y_train[0:1000], epochs=50, learning_rate=0.01)

# test on 3 samples
out = net.predict(x_test[0:3])
print("\n")
print("predicted values : ")
print(out, end="\n")
import numpy as np

from network import Network
from fc_layer import FCLayer
from activation_layer import ActivationLayer
#from activations import tanh, tanh_prime
from losses import mse, mse_prime
from activations import sigmoid, sigmoid_prime

# training data
x_train = np.array([[[0, 0]], [[0, 1]], [[1, 0]], [[1, 1]]])
y_train = np.array([[[0]], [[1]], [[1]], [[0]]])

# network
net = Network()
net.add(FCLayer(2, 3))
net.add(ActivationLayer(sigmoid, sigmoid_prime))
net.add(FCLayer(3, 1))
net.add(ActivationLayer(sigmoid, sigmoid_prime))

# train
net.use(mse, mse_prime)
cost_, myerr = net.fit(x_train, y_train, epochs=10000, learning_rate=0.2)

# test
out = net.predict(x_train)
print(out)

import matplotlib.pyplot as plt
plt.plot(cost_)
def neural_network_loader(hdf5_file_path, json_file_path):
    """
        This function loads a json file and a hd5 file. Then it creates the model and the matrix of the neural network.

        Arguments :
        hdf5_file_path -- The hdf5 file's path
        json_file_path -- The json file's path

        Return :
        an object of type network
    """
    network_info = list()
    layer = list()
    information = list()
    parameter = list()
    # Loading json file and  hdf5 file
    with open(json_file_path) as json_file, h5py.File(hdf5_file_path,
                                                      'r') as hdf5File:
        data = json.load(json_file)
        group1 = hdf5File.get('Fully')
        group2 = hdf5File.get('Conv')
        len_groupFully = len(group1)
        len_groupConv = len(group2)
        groupFully = 0
        groupConv = 0
        for i in range(len(data)):
            for key in data:
                if (key[-1] == str(i)):
                    if key[:-2] == "Fully":
                        groupFully += 2
                        for x in data[key]:
                            layer.append("Fully")
                            parameter.append(x['input_size'])
                            parameter.append(x['output_size'])
                            information.append(
                                np.array(group1.get("FullyW_" + key[-1:])))
                            information.append(
                                np.array(group1.get("FullyB_" + key[-1:])))
                    if key[:-2] == "Flatten":
                        layer.append("Flatten")
                    if key[:-2] == "Activation":
                        for x in data[key]:
                            layer.append("Activation")
                            parameter.append(x['activation'])
                            parameter.append(x['activation_prime'])
                    if key[:-2] == "Conv":
                        groupConv += 2
                        for x in data[key]:
                            layer.append("Conv")
                            parameter.append(x['input_shape'])
                            parameter.append(x['kernel_shape'])
                            parameter.append(x['layer_depth'])
                            information.append(
                                np.array(group2.get("ConvW_" + key[-1:])))
                            information.append(
                                np.array(group2.get("ConvB_" + key[-1:])))
    # Creation of a network object
    if (groupFully != len_groupFully) or (groupConv != len_groupConv):
        return 0
    else:
        net = Network()
        i = 0
        j = 0
        k = 0
        for l in layer:
            if l == "Conv":
                net.add(
                    ConvLayer(parameter[i], parameter[i + 1],
                              parameter[i + 2]))
                net.layers[k].weights = np.copy(information[j])
                net.layers[k].bias = np.copy(information[j + 1])
                i += 3
                j += 2
                k += 1
            if l == "Flatten":
                net.add(FlattenLayer())
                k += 1
            if l == "Activation":
                if (parameter[i] == "sigmoid"):
                    net.add(ActivationLayer(sigmoid, sigmoid_prime))
                if (parameter[i] == "tanh"):
                    net.add(ActivationLayer(tanh, tanh))
                if (parameter[i] == "rectified_linear_unit"):
                    net.add(
                        ActivationLayer(rectified_linear_unit,
                                        rectified_linear_unit_prime))
                if (parameter[i] == "softmax"):
                    net.add(ActivationLayer(softmax, softmax_prime))
                k += 1
                i += 2
            if l == "Fully":
                net.add(FullyConnectedLayer(parameter[i], parameter[i + 1]))
                net.layers[k].weight = np.copy(information[j])
                net.layers[k].bias = np.copy(information[j + 1])
                k += 1
                j += 2
                i += 2
        net.use(mean_squared_error, mean_squared_error_prime)
    return net