Exemplo n.º 1
0
    def request(self, a, b, p):
        """(p, a, b) -> a

        Parameters
        ----------
        a: tuple[numpy.array]
        b: numpy.array
        p: tuple[numpy.array]

        Returns
        -------
        tuple[numpy.array]
        """
        i = self.implement(a, p)

        global g_counter
        if g_counter % 100 == 0:
            print(g_counter, self.test_request_differential(a, b, p))
        g_counter += 1

        a = a[0]
        p00, p01, p10, b00, b01, q0, q1, b1 = p

        beta = [
            sigmoid_derivative(p00 * a[0] + p10 * a[1] + b00),
            sigmoid_derivative(p01 * a[0] + b01)
        ]

        gamma = 1

        return numpy.array([
            a[0] - (i - b) * gamma * (q0 * beta[0] * p00 + q1 * beta[1] * p10),
            a[1] - (i - b) * gamma * (q0 * beta[0] * p01)
        ]).reshape(2),
Exemplo n.º 2
0
    def test_request_differential(self, a, b, p):
        i = self.implement(a, p)

        a = a[0]
        p00, p01, p10, b00, b01, q0, q1, b1 = p

        beta = [
            sigmoid_derivative(p00 * a[0] + p10 * a[1] + b00),
            sigmoid_derivative(p01 * a[0] + b01)
        ]

        beta2 = [
            sigmoid_derivative_n(p00 * a[0] + p10 * a[1] + b00, 2),
            sigmoid_derivative_n(p01 * a[0] + b01, 2)
        ]

        h = q0 * beta[0] * p00 + q1 * beta[1] * p10
        k = q0 * beta[0] * p01

        return numpy.array([
            1 - (h * h + (i - b) * (q0 * beta2[0] * p00 * p00 + q1 * beta2[1] * p10 * p10)),
            - (h * k + (i - b) * (q0 * beta2[0] * p00 * p01)),
            - (h * k + (i - b) * (q0 * beta2[0] * p00 * p01)),
            1 - (k * k + (i - b) * (q0 * beta2[0] * p01 * p01)),
        ]).reshape(2, 2),
Exemplo n.º 3
0
    def request(self, a, b, p):
        """(p, a, b) -> a

        Parameters
        ----------
        a: tuple[numpy.array]
        b: numpy.array
        p: tuple[numpy.array]

        Returns
        -------
        tuple[numpy.array]
        """
        i = self.implement(a, p)

        a = a[0]
        w00, w01, w10, b0, b1 = p

        beta = [
            sigmoid_derivative(w00 * a[0] + w10 * a[1] + b0),
            sigmoid_derivative(w01 * a[0] + b1)
        ]

        return numpy.array([
            a[0] - (i[0] - b[0]) * beta[0] * w00 - (i[1] - b[1]) * beta[1] * w10,
            a[1] - (i[0] - b[1]) * beta[0] * w01
        ]).reshape(2),
Exemplo n.º 4
0
    def update(self, a, b, p):
        """(p, a, b) -> p

        Parameters
        ----------
        a : tuple[numpy.array]
        b : numpy.array
        p : tuple[numpy.array]

        Returns
        -------
        tuple[numpy.array]
        """
        i = self.implement(a, p)

        a = a[0]
        w00, w01, w10, b0, b1 = p

        beta = [
            sigmoid_derivative(w00 * a[0] + w10 * a[1] + b0),
            sigmoid_derivative(w01 * a[0] + b1)
        ]

        return (
            w00 - self.eps * (i[0] - b[0]) * beta[0] * a[0],
            w01 - self.eps * (i[1] - b[1]) * beta[1] * a[0],
            w10 - self.eps * (i[0] - b[0]) * beta[0] * a[1],
            b0 - self.eps * (i[0] - b[0]) * beta[0],
            b1 - self.eps * (i[1] - b[1]) * beta[1],
        )
Exemplo n.º 5
0
    def request(self, a, b, p):
        """(p, a, b) -> a

        Parameters
        ----------
        a: tuple[numpy.array]
        b: numpy.array
        p: tuple[numpy.array]

        Returns
        -------
        tuple[numpy.array]
        """
        i = self.implement(a, p)

        a = a[0]
        p00, p01, p10, b00, b01, q0, q1, b1 = p

        beta = [
            sigmoid_derivative(p00 * a[0] + p10 * a[1] + b00),
            sigmoid_derivative(p01 * a[0] + b01)
        ]

        gamma = sigmoid_derivative(q0 *
                                   sigmoid(p00 * a[0] + p10 * a[1] + b00) +
                                   q1 * sigmoid(p01 * a[0] + b01) + b1)

        return numpy.array([
            a[0] - (i - b) * gamma * (q0 * beta[0] * p00 + q1 * beta[1] * p10),
            a[1] - (i - b) * gamma * (q0 * beta[0] * p01)
        ]).reshape(2),
Exemplo n.º 6
0
    def test_request_differential(self, a, b, p):
        i = self.implement(a, p)

        a = a[0]
        w00, w01, w10, b0, b1 = p

        beta = [
            sigmoid_derivative(w00 * a[0] + w10 * a[1] + b0),
            sigmoid_derivative(w01 * a[0] + b1)
        ]

        beta_2 = [
            sigmoid_derivative_n(w00 * a[0] + w10 * a[1] + b0, 2),
            sigmoid_derivative_n(w01 * a[0] + b1, 2)
        ]
Exemplo n.º 7
0
    def request(self, a, b, p):
        """(p, a, b) -> a

        Parameters
        ----------
        a: tuple[numpy.array]
        b: numpy.array
        p: tuple[numpy.array]

        Returns
        -------
        tuple[numpy.array]
        """
        i = self.implement(a, p)

        w00, w10, b0 = p
        a = a[0]
        beta = sigmoid_derivative(w00 * a[0] + w10 * a[1] + b0)

        return numpy.array(
            [
                a[0] - (i[0] - b[0]) * beta[0] * w00[0],
                a[1] - (i[0] - b[0]) * beta[0] * w10[0]
            ]
        ).reshape(2),
Exemplo n.º 8
0
    def update(self, a, b, p):
        """(p, a, b) -> p

        Parameters
        ----------
        a : tuple[numpy.array]
        b : numpy.array
        p : tuple[numpy.array]

        Returns
        -------
        tuple[numpy.array]
        """
        i = self.implement(a, p)

        a = a[0]
        p00, p01, p10, b00, b01, q0, q1, b1 = p

        beta = [
            p00 * a[0] + p10 * a[1] + b00,
            p01 * a[0] + b01
        ]

        beta_sigma_derivative = [
            sigmoid_derivative(beta[0]),
            sigmoid_derivative(beta[1])
        ]
        beta_sigma = [
            sigmoid(beta[0]),
            sigmoid_derivative(beta[1])
        ]

        gamma = 1

        return (
            p00 - self.eps * (i - b) * gamma * q0 * beta_sigma_derivative[0] * a[0],
            p01 - self.eps * (i - b) * gamma * q0 * beta_sigma_derivative[0] * a[1],
            p10 - self.eps * (i - b) * gamma * q1 * beta_sigma_derivative[1] * a[0],
            b00 - self.eps * (i - b) * gamma * q0 * beta_sigma_derivative[0],
            b01 - self.eps * (i - b) * gamma * q1 * beta_sigma_derivative[1],
            q0 - self.eps * (i - b) * gamma * beta_sigma[0],
            q1 - self.eps * (i - b) * gamma * beta_sigma[1],
            b1 - self.eps * (i - b) * gamma
        )