Beispiel #1
0
def visualize_beta():
    L = 10
    nn = pickle.load(open('nn5_final.p', 'rb'))
    #for w in nn.weights[:1] :
    #    with np.printoptions(precision=2, suppress=True) :
    #        print(w.reshape((-1,L)))
    #plt.imshow(nn.weights[0].reshape((L,-1)))
    #plt.show()

    N = 5000
    training_fraction = 0.4
    ising = Ising(L, N)
    #X, y  = ising.generateTrainingData1D()
    D, ry = ising.generateDesignMatrix1D()
    #y    /= L

    ols = LeastSquares(method='ols', backend='manual')
    ols.setLambda(0.1)
    ols.fit(D, ry)

    print(ising.states.shape)
    #W = nn.weights[0].reshape((-1,L))*nn.weights[1]
    W = ols.beta.reshape((-1, L))
    J = ising.J
    for i in range(10):
        row = ising.states[i, :]
        des = D[i, :]
        E = ry[i]
        print(row.shape)
        row = np.expand_dims(row, 1)
        print("s W s:", row.T @ W @ row)
        print("s (W+W')/2 s:", row.T @ (W + W.T) / 2 @ row)
        print("s J s:", row.T @ J @ row)
        #print("D w:  ", W.T @ des * nn.weights[1])
        #print("pred: ", nn.predict(np.expand_dims(des.T,1)))
        print("E:    ", E)
        print("")

    for i in range(N):
        row = ising.states[i, :]
        des = D[i, :]
        E = ry[i]
        atol = 1e-14
        rtol = 1e-14
        #assert np.allclose(row.T @ W @ row, row.T @ (W+W.T)/2 @ row, atol=atol, rtol=rtol)
        #assert np.allclose(row.T @ (W+W.T)/2 @ row, row.T @ J @ row, atol=atol, rtol=rtol)

    with np.printoptions(precision=2, suppress=True):
        for a in np.linalg.eig(W):
            print(a)

    with np.printoptions(precision=2, suppress=True):

        print("det=", np.linalg.det(W))
        print("cond=", np.linalg.cond(W))
        print("")
        print("J:\n:", J)
        print("W+W'/2\n", (W + W.T) / 2)
        print("J+J'/2\n", (J + J.T) / 2)
        print("Tr D:", np.sum(np.diag((W + W.T) / 2)))
Beispiel #2
0
def part_e(plotting=False):
    x_train, y_train, x_test, y_test = real_data(file_number=2, plotting=False)

    for method in ['ols', 'ridge', 'lasso']:
        designMatrix = DesignMatrix('polynomial2D', 10)
        leastSquares = LeastSquares(backend='manual', method=method)
        leastSquares.setLambda(1e-3)
        if method == 'lasso':
            leastSquares.setLambda(1e-4)

        X = designMatrix.getMatrix(x_train)
        leastSquares.fit(X, y_train)

        X_test = designMatrix.getMatrix(x_test)
        leastSquares.predict()
        leastSquares.y = y_test
        print(leastSquares.MSE())

        if plotting:
            x = np.linspace(0, 1, 60)
            y = np.copy(x)
            XX, YY = np.meshgrid(x, y)
            ZZ = np.reshape(leastSquares.yHat, XX.shape)

            fig = plt.figure()
            ax = fig.gca(projection='3d')
            ax.plot_surface(XX,
                            YY,
                            ZZ,
                            cmap=cm.coolwarm,
                            linewidth=0,
                            antialiased=False)
            ax.set_zlim(-0.10, 1.40)
            ax.zaxis.set_major_locator(LinearLocator(5))
            ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
            ax.view_init(30, 45 + 90)
            #plt.savefig(os.path.join(os.path.dirname(__file__), 'figures', method+'terrain.png'), transparent=True, bbox_inches='tight')
            plt.show()
    if plotting:
        x = np.linspace(0, 1, 60)
        y = np.copy(x)
        XX, YY = np.meshgrid(x, y)
        ZZ = np.reshape(y_test, XX.shape)

        fig = plt.figure()
        ax = fig.gca(projection='3d')
        ax.plot_surface(XX,
                        YY,
                        ZZ,
                        cmap=cm.coolwarm,
                        linewidth=0,
                        antialiased=False)
        ax.set_zlim(-0.10, 1.40)
        ax.zaxis.set_major_locator(LinearLocator(5))
        ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
        ax.view_init(30, 45 + 90)
        #plt.savefig(os.path.join(os.path.dirname(__file__), 'figures', 'test_terrain.png'), transparent=True, bbox_inches='tight')
        plt.show()
Beispiel #3
0
def test_LeastSquares_fit_lasso() :
    """Tests the fit method of the Least Squares class with method='lasso'
    """
    N = 500
    P = 5
    x = np.linspace(0,1,N)
    y = 2.0 + x + x**2
    
    X = np.zeros(shape=(N,P))
    X[:,0] = 1.0
    for j in range(1,P) :
        X[:,j] = x**j

    lasso = LeastSquares(method='lasso', backend='skl')
    lasso.setLambda(0.01)
    beta_lasso = lasso.fit(X,y)
    
    # Make sure lasso regression zeroes out x*3 and x**4 beta terms.
    assert beta_lasso[-2:] == pytest.approx(np.zeros(2), abs=1e-15)
Beispiel #4
0
def test_LeastSquares_fit_ridge() :
    """Tests the fit method of the Least Squares class with method='ridge'
    """
    N = 5
    P = 5
    x = np.linspace(0,1,N)
    y = x + x**2 - (1.0 - x)**5
    
    X = np.zeros(shape=(N,P))
    X[:,0] = 1.0
    for j in range(1,P) :
        X[:,j] = x**j

    OLS = LeastSquares(method='ols', backend='manual')
    beta_ols = OLS.fit(X,y)

    OLS = LeastSquares(method='ridge', backend='manual')
    OLS.setLambda(0.0)
    beta_lambda0 = OLS.fit(X,y)

    assert beta_lambda0 == pytest.approx(beta_ols, abs=1e-15)

    # Make sure the skl and the manual backends give the same result
    SKL = LeastSquares(method='ridge', backend='skl')
    SKL.setLambda(0.0)
    beta_skl = SKL.fit(X,y)
    
    assert beta_lambda0 == pytest.approx(beta_skl, abs=1e-10)

    SKL.setLambda = 0.5
    OLS.setLambda = 0.5
    beta_skl = SKL.fit(X,y)
    beta_lambda0 = OLS.fit(X,y)
    print(beta_lambda0)
    print(beta_skl)

    assert beta_lambda0 == pytest.approx(beta_skl, abs=1e-10)
Beispiel #5
0

if __name__ == '__main__':

    Degree = 1 + np.arange(20)

    train_MSE = []
    test_MSE = []
    print("#########################################################")
    for degree in Degree:

        degree = int(degree)
        designMatrix = DesignMatrix('polynomial2D', degree)
        leastSquares = LeastSquares(method="ridge", backend='manual')
        Lambda = 4
        leastSquares.setLambda(Lambda)
        crossvalidation = CrossValidation(leastSquares, designMatrix)

        N = int(1e4)
        x = np.random.rand(N)
        y = np.random.rand(N)
        x_data = np.zeros(shape=(N, 2))
        x_data[:, 0] = x
        x_data[:, 1] = y
        y_data = np.zeros(shape=(N))

        X = designMatrix.getMatrix(x_data)
        XT_X = np.dot(X.T, X)
        print("det(X^T*X): %g" %
              (np.linalg.det(XT_X + Lambda * np.eye(XT_X.shape[0]))))
Beispiel #6
0
def part_e_2():
    x_train, y_train, x_test, y_test = real_data(file_number=2, plotting=False)

    degree = 10

    designMatrix = DesignMatrix('polynomial2D', degree)
    leastSquares = LeastSquares(backend='manual', method='ols')
    X = designMatrix.getMatrix(x_train)
    leastSquares.fit(X, y_train)

    X_test = designMatrix.getMatrix(x_test)
    leastSquares.predict()
    leastSquares.y = y_test
    ols_MSE = leastSquares.MSE()
    print(ols_MSE)
    ols_MSE = np.array([ols_MSE, ols_MSE])
    ols_lambda = np.array([1e-5, 1])

    ridge_lambda = np.logspace(-5, 0, 20)
    ridge_MSE = []
    for lambda_ in ridge_lambda:
        print("ridge " + str(lambda_))

        designMatrix = DesignMatrix('polynomial2D', degree)
        leastSquares = LeastSquares(backend='manual', method='ridge')
        leastSquares.setLambda(lambda_)

        X = designMatrix.getMatrix(x_train)
        leastSquares.fit(X, y_train)

        X_test = designMatrix.getMatrix(x_test)
        leastSquares.predict()
        leastSquares.y = y_test
        ridge_MSE.append(leastSquares.MSE())
        print(leastSquares.MSE())

    ridge_MSE = np.array(ridge_MSE)

    lasso_lambda = np.logspace(-4, 0, 20)
    lasso_MSE = []
    for lambda_ in lasso_lambda:
        print("lasso " + str(lambda_))
        designMatrix = DesignMatrix('polynomial2D', degree)
        leastSquares = LeastSquares(backend='manual', method='lasso')
        leastSquares.setLambda(lambda_)

        X = designMatrix.getMatrix(x_train)
        leastSquares.fit(X, y_train)

        X_test = designMatrix.getMatrix(x_test)
        leastSquares.predict()
        leastSquares.y = y_test
        lasso_MSE.append(leastSquares.MSE())

    lasso_MSE = np.array(ridge_MSE)

    ########################################################
    plt.rc('text', usetex=True)

    plt.loglog(ols_lambda,
               ols_MSE,
               'k--o',
               markersize=1,
               linewidth=1,
               label=r'OLS')
    plt.loglog(ridge_lambda,
               ridge_MSE,
               'r-o',
               markersize=1,
               linewidth=1,
               label=r'Ridge')
    plt.loglog(lasso_lambda,
               lasso_MSE,
               'b-o',
               markersize=1,
               linewidth=1,
               label=r'Lasso')

    plt.xlabel(r"$\lambda$", fontsize=10)
    plt.ylabel(r"MSE", fontsize=10)
    #plt.subplots_adjust(left=0.2,bottom=0.2)
    plt.legend(fontsize=10)
    plt.savefig(os.path.join(os.path.dirname(__file__), 'figures',
                             'lambda_terrain.png'),
                transparent=True,
                bbox_inches='tight')
    plt.show()
Beispiel #7
0
def part_b():

    R2 = []
    MSE = []

    R2_noise = []
    MSE_noise = []
    beta_noise = []
    betaVariance_noise = []

    noise = np.linspace(0, 1.0, 50)
    k = 1
    fig, ax1 = plt.subplots()
    plt.rc('text', usetex=True)

    @jit(nopython=True, cache=True)
    def computeFrankeValues(x_data, y):
        N = x_data.shape[0]
        for i in range(N):
            y[i] = franke(x_data[i, 0], x_data[i, 1])

    ind = -1
    for lambda_ in np.logspace(-2, 0, 3):
        ind += 1
        MSE_noise = []

        for eta in noise:
            designMatrix = DesignMatrix('polynomial2D', 10)
            if ind == 0:
                leastSquares = LeastSquares(backend='manual', method='ols')
            else:
                leastSquares = LeastSquares(backend='manual', method='ridge')

            leastSquares.setLambda(lambda_)
            bootstrap = Bootstrap(leastSquares, designMatrix)

            N = int(1000)
            x = np.random.rand(N)
            y = np.random.rand(N)
            x_data = np.zeros(shape=(N, 2))
            x_data[:, 0] = x
            x_data[:, 1] = y
            y_data = np.zeros(shape=(N))
            computeFrankeValues(x_data, y_data)
            y_data_noise = y_data + eta * np.random.standard_normal(size=N)

            bootstrap.resample(x_data, y_data_noise, k)

            MSE_noise.append(leastSquares.MSE())
            R2_noise.append(leastSquares.R2())
            beta_noise.append(bootstrap.beta)
            betaVariance_noise.append(bootstrap.betaVariance)

            # Different noise, test data
            N = int(1000)
            x = np.random.rand(N)
            y = np.random.rand(N)
            x_data = np.zeros(shape=(N, 2))
            x_data[:, 0] = x
            x_data[:, 1] = y
            y_data = np.zeros(shape=(N))
            computeFrankeValues(x_data, y_data)
            y_data_noise = y_data + eta * np.random.standard_normal(size=N)

            X = designMatrix.getMatrix(x_data)
            leastSquares.X = X
            leastSquares.predict()
            leastSquares.y = y_data_noise

            MSE.append(leastSquares.MSE())
            R2.append(leastSquares.R2())

        colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']

        if ind == 0:
            ax1.loglog(noise,
                       np.array(MSE_noise),
                       colors[ind] + '--',
                       markersize=1,
                       label=r"OLS")
        else:
            ax1.loglog(noise,
                       np.array(MSE_noise),
                       colors[ind] + '-',
                       markersize=1,
                       label=r"$\lambda=10^{%d}$" % (int(np.log10(lambda_))))
        plt.ylabel(r"MSE", fontsize=10)
        plt.xlabel(r"noise scale $\eta$", fontsize=10)
        plt.subplots_adjust(left=0.2, bottom=0.2)

        #ax1.set_ylim([0.95*min(min(MSE_noise), min(R2_noise)), 1.05*(max(max(MSE_noise), max(R2_noise)))])

    ax1.legend()
    #plt.savefig(os.path.join(os.path.dirname(__file__), 'figures', 'MSE_ridge_noise.png'), transparent=True, bbox_inches='tight')
    plt.show()
Beispiel #8
0
def plot_beta_ridge():
    beta = []
    betaVariance = []
    MSE = []
    R2 = []

    k = 10000
    fig, ax1 = plt.subplots()
    plt.rc('text', usetex=True)

    @jit(nopython=True, cache=True)
    def computeFrankeValues(x_data, y):
        N = x_data.shape[0]
        for i in range(N):
            y[i] = franke(x_data[i, 0], x_data[i, 1])

    ind = -1
    lam = np.logspace(-3, 5, 20)

    for lambda_ in lam:
        if ind == 0:
            leastSquares = LeastSquares(backend='manual', method='ols')
        else:
            leastSquares = LeastSquares(backend='manual', method='ridge')

        designMatrix = DesignMatrix('polynomial2D', 3)
        bootstrap = Bootstrap(leastSquares, designMatrix)
        leastSquares.setLambda(lambda_)
        ind += 1

        N = int(1e4)
        x = np.random.rand(N)
        y = np.random.rand(N)
        x_data = np.zeros(shape=(N, 2))
        x_data[:, 0] = x
        x_data[:, 1] = y
        y_data = np.zeros(shape=(N))
        computeFrankeValues(x_data, y_data)
        eta = 1.0
        y_data_noise = y_data + eta * np.random.standard_normal(size=N)

        bootstrap.resample(x_data, y_data_noise, k)

        MSE.append(leastSquares.MSE())
        R2.append(leastSquares.R2())
        beta.append(bootstrap.beta)
        betaVariance.append(bootstrap.betaVariance)

        leastSquares.y = y_data
        MSE.append(leastSquares.MSE())
        R2.append(leastSquares.R2())

    beta = np.array(beta)
    betaVariance = np.array(betaVariance)

    monomial = [
        '1', 'x', 'y', 'x^2', 'xy', 'y^2', 'x^3', 'x^2y', 'xy^2', 'y^3'
    ]
    colors = [
        '#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b',
        '#e377c2', '#7f7f7f', '#bcbd22', '#17becf'
    ]

    for i in range(10):
        plt.errorbar(lam[1:],
                     beta[1:, i],
                     yerr=2 * betaVariance[1:, i],
                     fmt='-o',
                     markersize=2,
                     linewidth=1,
                     color=colors[i],
                     elinewidth=0.5,
                     capsize=2,
                     capthick=0.5,
                     label=r"$\beta_{%s}$" % (monomial[i]))
    plt.rc('text', usetex=True)
    plt.ylabel(r"$\beta_j$", fontsize=10)
    plt.xlabel(r"shrinkage parameter $\lambda$", fontsize=10)
    plt.subplots_adjust(left=0.2, bottom=0.2)
    plt.legend(fontsize=8)

    for i in range(10):
        plt.errorbar(1e-3,
                     beta[0, i],
                     yerr=2 * betaVariance[0, i],
                     fmt='-o',
                     markersize=2,
                     linewidth=1,
                     color=colors[i],
                     elinewidth=0.5,
                     capsize=2,
                     capthick=0.5)

    fig.gca().set_xscale('log')
    #plt.savefig(os.path.join(os.path.dirname(__file__), 'figures', 'beta_ridge.png'), transparent=True, bbox_inches='tight')

    plt.show()
Beispiel #9
0
def R2_versus_lasso():
    L = 3
    N = 10000
    training_fraction = 0.4
    ising = Ising(L, N)
    D, ry = ising.generateDesignMatrix1D()
    X, y = ising.generateTrainingData1D()
    y /= L

    D_train = D[int(training_fraction * N):, :]
    ry_train = ry[int(training_fraction * N):]
    D_validation = D[:int(training_fraction * N), :]
    ry_validation = ry[:int(training_fraction * N)]

    lasso = LeastSquares(method='lasso', backend='skl')
    lasso.setLambda(1e-2)
    lasso.fit(D_train, ry_train)
    lasso.y = ry_validation
    lasso_R2 = sklearn.metrics.mean_squared_error(
        ry_validation / L,
        lasso.predict(D_validation) / L)

    n_samples, n_features = X.shape

    nn = NeuralNetwork(inputs=L * L,
                       neurons=L,
                       outputs=1,
                       activations='identity',
                       cost='mse',
                       silent=False)
    nn.addLayer(neurons=1)
    nn.addOutputLayer(activations='identity')

    validation_skip = 100
    epochs = 50000
    nn.fit(D.T,
           ry,
           shuffle=True,
           batch_size=2000,
           validation_fraction=1 - training_fraction,
           learning_rate=0.0001,
           verbose=False,
           silent=False,
           epochs=epochs,
           validation_skip=validation_skip,
           optimizer='adam')

    plt.rc('text', usetex=True)
    validation_loss = nn.validation_loss_improving
    validation_ep = np.linspace(0, epochs, len(nn.validation_loss_improving))
    plt.semilogy(validation_ep, validation_loss, 'r-', label=r'NN')
    plt.semilogy([0, epochs],
                 np.array([lasso_R2, lasso_R2]),
                 'k--',
                 label=r'Lasso')
    plt.xlabel(r'Epoch', fontsize=10)
    plt.ylabel(r'Mean squared error', fontsize=10)
    plt.legend(fontsize=10)
    plt.xlim((0, epochs))
    ax = plt.gca()
    ymin, ymax = ax.get_ylim()
    if ymin > pow(10, -5):
        ymin = pow(10, -5)
    #plt.ylim((ymin,ymax))
    plt.savefig(os.path.join(os.path.dirname(__file__), 'figures',
                             'NN_compare_lasso.png'),
                transparent=True,
                bbox_inches='tight')