Ejemplo n.º 1
0
        # Create matrix
        A = np.random.randn(M, N)
        A /= la.norm(A, 2)

        # Create noisy observation vector
        b = A @ x + sigma * np.random.randn(M)

        # Initial iterate
        x0 = np.zeros(N)

        return LASSOProblem(LinearOperator.from_matrix(A), b, mu, x=x), x0

    def plot(self, solution: Vector) -> None:
        """Plot the recovered signal against the original unknown signal.

        :param solution: The recovered signal
        """
        plots.plot_signals("LASSO", self.x, solution)


if __name__ == "__main__":
    problem, x0 = LASSOProblem.construct()
    print("Constructed LASSO problem.")

    adaptive, accelerated, plain = test_modes(problem, x0)

    plots.plot_convergence("LASSO", [adaptive[1], accelerated[1], plain[1]],
                           ["Adaptive", "Accelerated", "Plain"])
    problem.plot(adaptive[0])
    plt.show()
Ejemplo n.º 2
0
        X = U @ S @ V

        # Create observation matrix
        P = 1 / (1 + np.exp(-X))
        B = 2.0 * (np.random.rand(M, N) < P) - 1

        # Initial iterate
        X0 = np.zeros((M, N))

        return LogisticMatrixCompletionProblem(B, mu, X=B), X0

    def plot(self, solution: Matrix) -> None:
        """Plot the recovered matrix against the original unknown matrix.

        :param solution: The recovered matrix
        """
        plots.plot_matrices("Logistic Matrix Completion", self.X, solution)


if __name__ == "__main__":
    problem, X0 = LogisticMatrixCompletionProblem.construct()
    print("Constructed 1-bit logistic matrix completion problem.")

    adaptive, accelerated, plain = test_modes(problem, X0)

    plots.plot_convergence("Logistic Matrix Completion",
                           [adaptive[1], accelerated[1], plain[1]],
                           ["Adaptive", "Accelerated", "Plain"])
    problem.plot(adaptive[0])
    plt.show()
Ejemplo n.º 3
0
        M = ascent().astype(float)

        # Normalize M
        M /= np.max(M)

        # Add noise
        M += sigma * np.random.randn(*M.shape)

        # Initial iterate
        Y0 = np.zeros(M.shape + (2,))

        return TVDenoisingProblem(M, mu), Y0

    def plot(self, solution: Matrix) -> None:
        """Plot the recovered, denoised image against the original noisy image.

        :param solution: The denoised image
        """
        plots.plot_matrices("Total Variation Denoising", self.M, solution)


if __name__ == "__main__":
    problem, Y0 = TVDenoisingProblem.construct()
    print("Constructed total-variation denoising problem.")

    adaptive, accelerated, plain = test_modes(problem, Y0)

    plots.plot_convergence("Total-Variation Denoising", [adaptive[1], accelerated[1], plain[1]], ["Adaptive", "Accelerated", "Plain"])
    problem.plot(adaptive[0])
    plt.show()
Ejemplo n.º 4
0
        x[np.random.permutation(N)[:K]] = 1

        # Create matrix
        A = np.random.randn(M, N)
        A /= la.norm(A, 2)

        # Create noisy observation vector
        b = A @ x + sigma * np.random.randn(M)

        # Initial iterate
        x0 = np.zeros(N)

        return NNLeastSquaresProblem(A, A.T, b, x=x), x0

    def plot(self, solution: Vector) -> None:
        # Plot the recovered signal
        plots.plot_signals("Non-Negative Least Squares", self.x, solution)


if __name__ == "__main__":
    problem, x0 = NNLeastSquaresProblem.construct()
    print("Constructed non-negative least squares problem.")

    adaptive, accelerated, plain = test_modes(problem, x0)

    plots.plot_convergence("Non-Negative Least Squares",
                           [adaptive[1], accelerated[1], plain[1]],
                           ["Adaptive", "Accelerated", "Plain"])
    problem.plot(adaptive[0])
    plt.show()
Ejemplo n.º 5
0
        # Create observation matrix
        S = X @ Y.T + sigma * np.random.randn(M, N)

        # Initial iterates
        X0 = np.zeros((M, K))
        Y0 = np.random.rand(N, K)

        return NNFactorizationProblem(S, mu, X=X, Y=Y), (X0, Y0)

    def plot(self, solutions: Tuple["Matrix", "Matrix"]):
        """Plot the recovered factor matrices against the original unknown factor matrices.

        :param solutions: A tuple containing the two recovered factor matrices
        """
        plots.plot_matrices("Factor X", self.X, solutions[0])
        plots.plot_matrices("Factor Y", self.Y, solutions[1])


if __name__ == "__main__":
    problem, inits = NNFactorizationProblem.construct()
    print("Constructed non-negative matrix factorization problem.")

    adaptive, accelerated, plain = test_modes(problem, inits)

    plots.plot_convergence("Non-Negative Matrix Factorization",
                           [adaptive[1], accelerated[1], plain[1]],
                           ["Adaptive", "Accelerated", "Plain"])
    problem.plot(adaptive[0])
    plt.show()
Ejemplo n.º 6
0
        A = np.random.randn(M, N)

        # Create observation vector
        p = 1 / (1 + np.exp(-A @ x))
        b = 2.0 * (np.random.rand(M) < p) - 1

        # Initial iterate
        x0 = np.zeros(N)

        return SparseLogisticProblem(A, A.T, b, mu, x=x), x0

    def plot(self, solution: Vector) -> None:
        """Plot the recovered signal against the original unknown signal.

        :param solution: The recovered signal
        """
        plots.plot_signals("Sparse Logistic Least Squares", self.x, solution)


if __name__ == "__main__":
    problem, x0 = SparseLogisticProblem.construct()
    print("Constructed sparse logistic least squares problem.")

    adaptive, accelerated, plain = test_modes(problem, x0)

    plots.plot_convergence("Sparse Logistic Least Squares",
                           [adaptive[1], accelerated[1], plain[1]],
                           ["Adaptive", "Accelerated", "Plain"])
    problem.plot(adaptive[0])
    plt.show()
Ejemplo n.º 7
0
        D_train, l_train = generate(M_train, N, self.w)

        accuracy = np.sum(np.sign(D_train @ solution) == l_train) / M_train

        # Plot a histogram of the residuals
        figure, axes = plt.subplots()
        figure.suptitle("Support Vector Machine (Accuracy: {}%)".format(
            accuracy * 100))

        axes.set_xlabel("Predicted value")
        axes.set_ylabel("Frequency")

        axes.hist((D_train[l_train == 1] @ solution,
                   D_train[l_train == -1] @ solution),
                  hist_size,
                  label=("Positive", "Negative"))
        axes.legend()


if __name__ == "__main__":
    problem, y0 = SVMProblem.construct()
    print("Constructed support vector machine problem.")

    adaptive, accelerated, plain = test_modes(problem, y0)

    plots.plot_convergence("Support Vector Machine",
                           [adaptive[1], accelerated[1], plain[1]],
                           ["Adaptive", "Accelerated", "Plain"])
    problem.plot(adaptive[0])
    plt.show()
        b = np.zeros(N)
        b[samples] = np.random.randn(M)

        # Initial iterate
        x0 = np.zeros(N)

        return DemocraticRepresentationProblem(
            LinearOperator(lambda x: mask * dct(x, norm='ortho'),
                           lambda x: idct(mask * x, norm='ortho'), x0.shape),
            b, mu), x0

    def plot(self, solution: Vector) -> None:
        """Plot the computed democratic representation against the original signal.

        :param solution: The computed democratic representation
        """
        plots.plot_signals("Democratic Representation", self.b, solution)


if __name__ == "__main__":
    problem, x0 = DemocraticRepresentationProblem.construct()
    print("Constructed democratic representation problem.")

    adaptive, accelerated, plain = test_modes(problem, x0)

    plots.plot_convergence("Democratic Representation",
                           [adaptive[1], accelerated[1], plain[1]],
                           ["Adaptive", "Accelerated", "Plain"])
    problem.plot(adaptive[0])
    plt.show()
Ejemplo n.º 9
0
        return MaxNormProblem(points, mu), X0

    def plot(self, solution: Matrix) -> None:
        """Plot the results of the computed classifier on the two-moons dataset.

        :param solution: The problem's computed solution
        """
        labels = np.sign(adaptive[0] @ np.random.randn(solution.shape[1]))

        figure, axes = plt.subplots()
        figure.suptitle("Max-Norm Optimization")

        axes.set_xlabel("Predicted value")
        axes.set_ylabel("Frequency")

        axes.plot(self.points[labels < 0, 0], self.points[labels < 0, 1], 'b.')
        axes.plot(self.points[labels > 0, 0], self.points[labels > 0, 1], 'r.')


if __name__ == "__main__":
    problem, X0 = MaxNormProblem.construct()
    print("Constructed max-norm problem.")

    adaptive, accelerated, plain = test_modes(problem, X0)

    plots.plot_convergence("Max-Norm Problem",
                           [adaptive[1], accelerated[1], plain[1]],
                           ["Adaptive", "Accelerated", "Plain"])
    problem.plot(adaptive[0])
    plt.show()