Ejemplo n.º 1
0
    def train(self, iterations=25, verbose=False):
        """Method to train Neural Network on a given dataset.

        Updates weight values according to:

            w.j = w.j + alpha * error * g'(in) * x.j

            where:
                in = w.dot(x) + b
                g'(in) = g(in)*(1-g(in))
                error = actual - hypothesis

        Updates biases parameters according to:

            b = b * alpha * error * g'(in)
        """
        # perform iterative training
        for i in range(iterations):

            if verbose:
                print("Epoch {}".format(i + 1))

            # perform calculations for each data point
            for count, data_point in enumerate(self.X):

                # calculate node outputs for each layer
                layer_outputs = self._calculate(array([data_point]))

                # perform bias and weight updates
                updates = dict()
                delta = (self.Y[0][count] - layer_outputs["sig_layer_4"]) * mltools.sigmoid(layer_outputs["layer_4"], True)
                updates["l4b"] = self.bias["layer_4"] + self.alpha * delta
                updates["l4w"] = self.weights["layer_4"] + self.alpha * layer_outputs["sig_layer_4"] * delta

                delta = self.weights["layer_4"].dot(delta) * mltools.sigmoid(layer_outputs["layer_3"], True)
                updates["l3b"] = self.bias["layer_3"] + self.alpha * delta
                updates["l3w"] = self.weights["layer_3"] + self.alpha * layer_outputs["sig_layer_3"] * delta

                delta = self.weights["layer_3"].T.dot(delta) * mltools.sigmoid(layer_outputs["layer_2"], True)
                updates["l2b"] = self.bias["layer_2"] + self.alpha * delta
                updates["l2w"] = self.weights["layer_2"] + self.alpha * layer_outputs["sig_layer_2"] * delta

                delta = self.weights["layer_2"].T.dot(delta) * mltools.sigmoid(layer_outputs["layer_1"], True)
                updates["l1b"] = self.bias["layer_1"] + self.alpha * delta
                updates["l1w"] = self.weights["layer_1"] + self.alpha * layer_outputs["sig_layer_1"] * delta

                self.bias["layer_4"] = updates["l4b"]
                self.weights["layer_4"] = updates["l4w"]
                self.bias["layer_3"] = updates["l3b"]
                self.weights["layer_3"] = updates["l3w"]
                self.bias["layer_2"] = updates["l2b"]
                self.weights["layer_2"] = updates["l2w"]
                self.bias["layer_1"] = updates["l1b"]
                self.weights["layer_1"] = updates["l1w"]
Ejemplo n.º 2
0
    def _calculate(self, input_value):
        """Method for predicting class label from trained model.
        in = W dot x
        a = g(in)
        :param input_value:
        :type input_value: :py:class:`~numpy.ndarray`
        """
        outputs = dict()
        outputs["layer_1"] = dot(self.weights["layer_1"], input_value.T) + self.bias["layer_1"]
        outputs["sig_layer_1"] = mltools.sigmoid(outputs["layer_1"])
        outputs["layer_2"] = self.weights["layer_2"].dot(outputs["sig_layer_1"]) + self.bias["layer_2"]
        outputs["sig_layer_2"] = mltools.sigmoid(outputs["layer_2"])
        outputs["layer_3"] = self.weights["layer_3"].dot(outputs["sig_layer_2"]) + self.bias["layer_3"]
        outputs["sig_layer_3"] = mltools.sigmoid(outputs["layer_3"])
        outputs["layer_4"] = self.weights["layer_4"].dot(outputs["sig_layer_3"]) + self.bias["layer_4"]
        outputs["sig_layer_4"] = mltools.sigmoid(outputs["layer_4"])

        return outputs
Ejemplo n.º 3
0
def train_accuracy(parameters, X_test, Y_test, arcitecture):
    pred, cache = mltools.forward_pass(parameters, X_test, arcitecture)
    pred = mltools.sigmoid(pred)
    mltools.accuracy(pred, Y_test)