示例#1
0
def ex_1_2_a(x_train, x_test, y_train, y_test):
    """
    Solution for exercise 1.2 a)
    :param x_train: The training dataset
    :param x_test: The testing dataset
    :param y_train: The training targets
    :param y_test: The testing targets
    :return:
    """
    ## TODO
    #pass
    N = 5000
    n_hidden = 50
    alpha_ = np.power(10, np.linspace(-8, 2, 11))
    mse_train = np.zeros([np.size(alpha_), N])
    mse_test = np.zeros([np.size(alpha_), N])

    for j in range(np.size(alpha_)):
        reg = MLPRegressor(hidden_layer_sizes=(n_hidden, ),
                           activation='logistic',
                           solver='lbfgs',
                           alpha=alpha_[j],
                           random_state=0,
                           warm_start=True,
                           max_iter=1)
        for r in range(N):
            reg.fit(x_train, y_train)

            mse_train[j, r] = calculate_mse(reg, x_train, y_train)
            mse_test[j, r] = calculate_mse(reg, x_test, y_test)

    plot_mse_vs_alpha(mse_train, mse_test, alpha_)
示例#2
0
def ex_1_2(x_train, x_test, y_train, y_test):
    """
    Solution for exercise 1.2 a)
    :param x_train: The training dataset
    :param x_test: The testing dataset
    :param y_train: The training targets
    :param y_test: The testing targets
    :return:
    """
    ## TODO#
    nh = 50
    alp = [0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1, 10, 100]
    seed = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    iterations = 5000
    mse_all_train = np.zeros(shape=(9, 10))
    mse_all_test = np.zeros(shape=(9, 10))
    for j in range(0, 9):
        for i in range(0, 10):
            nn = MLPRegressor(
                activation='logistic',
                solver='lbfgs',
                max_iter=5000,
                alpha=alp[j],
                hidden_layer_sizes=(50, ),
                random_state=seed[i])  #for i in range(0,iterations):
            nn.fit(x_train, y_train)
            mse_train = calculate_mse(nn, x_train, y_train)
            mse_test = calculate_mse(nn, x_test, y_test)
            mse_all_train[j][i] = mse_train
            mse_all_test[j][i] = mse_test

    plot_mse_vs_alpha(mse_all_train, mse_all_test, alp)

    pass
示例#3
0
def ex_1_2(x_train, x_test, y_train, y_test):
    """
    Solution for exercise 1.2 a)
    :param x_train: The training dataset
    :param x_test: The testing dataset
    :param y_train: The training targets
    :param y_test: The testing targets
    :return:
    """

    alphas = [10**-6, 10**-5, 10**-4, 10**-3, 10**-2, 10**-1, 1, 10, 100]
    randoms = np.random.randint(0, 1000, size=10)

    MSE_train, MSE_test = np.ndarray((len(alphas), len(randoms))), np.ndarray(
        (len(alphas), len(randoms)))
    n_h = 50
    for a, alpha in enumerate(alphas):
        for r, random in enumerate(randoms):
            nn = MLPRegressor(activation='logistic',
                              solver='lbfgs',
                              max_iter=5000,
                              hidden_layer_sizes=(n_h, ),
                              alpha=alpha,
                              random_state=random)
            nn.fit(x_train, y_train)
            MSE_train[a][r] = calculate_mse(nn, x_train, y_train)
            MSE_test[a][r] = calculate_mse(nn, x_test, y_test)

    plot_mse_vs_alpha(MSE_train, MSE_test, alphas)
def ex_1_2_a(x_train, x_test, y_train, y_test):
    """
    Solution for exercise 1.2 a)
    Remember to set alpha to 0 when initializing the model
    :param x_train: The training dataset
    :param x_test: The testing dataset
    :param y_train: The training targets
    :param y_test: The testing targets
    :return:
    """
    ## TODO
    hidden_neurons = 40
    alphas = [1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 10, 100]
    mses_train = np.zeros((len(alphas), 10))
    mses_test = np.zeros((len(alphas), 10))
    for alpha in alphas:
        for i in range(10):
            random_seed = randint(0, 1000)
            regressor = MLPRegressor(hidden_layer_sizes=(hidden_neurons, ),
                                     solver="lbfgs",
                                     activation="logistic",
                                     alpha=alpha,
                                     max_iter=200,
                                     random_state=random_seed)
            regressor.fit(x_train, y_train)
            mses_test[alphas.index(alpha)][i] = calculate_mse(
                regressor, x_test, y_test)
            mses_train[alphas.index(alpha)][i] = calculate_mse(
                regressor, x_train, y_train)

    plot_mse_vs_alpha(mses_train, mses_test, alphas)
示例#5
0
def ex_1_2_a(x_train, x_test, y_train, y_test):
    """
    Solution for exercise 1.2 a)
    :param x_train: The training dataset
    :param x_test: The testing dataset
    :param y_train: The training targets
    :param y_test: The testing targets
    :return:
    """
    ## TODO
    total_iter = 5000
    n_neuro = 50
    n_seeds = 10
    alph = np.power(10, np.linspace(-8, 2, 11))
    mse_train = np.zeros([np.size(alph), n_seeds])
    mse_test = np.zeros([np.size(alph), n_seeds])

    for j in range(np.size(alph)):
        for s in range(n_seeds):
            seed = np.random.randint(100)
            reg = MLPRegressor(hidden_layer_sizes=(n_neuro, ),
                               activation='logistic',
                               solver='lbfgs',
                               alpha=alph[j],
                               random_state=seed,
                               max_iter=total_iter)
            reg.fit(x_train, y_train)
            mse_train[j, s] = calculate_mse(reg, x_train, y_train)
            mse_test[j, s] = calculate_mse(reg, x_test, y_test)

    plot_mse_vs_alpha(mse_train, mse_test, alph)
示例#6
0
def ex_1_2_a(x_train, x_test, y_train, y_test):
    """
    Solution for exercise 1.2 a)
    Remember to set alpha to 0 when initializing the model
    :param x_train: The training dataset
    :param x_test: The testing dataset
    :param y_train: The training targets
    :param y_test: The testing targets
    :return:
    """
    alphas = [1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 10, 100]
    seeds = 10
    mse = np.zeros((len(alphas), seeds, 2))

    for i in range(len(alphas)):
        for j in range(seeds):
            regressor = MLPRegressor(hidden_layer_sizes=(40, ),
                                     activation='logistic',
                                     solver='lbfgs',
                                     alpha=alphas[i],
                                     max_iter=200,
                                     random_state=j,
                                     tol=1e-8)
            regressor.fit(x_train, y_train)
            # mse shape: [train_mses, test_mses]
            mse[i][j] = calculate_mse(regressor, [x_train, x_test],
                                      [y_train, y_test])
    plot_mse_vs_alpha(mse[:, :, 0], mse[:, :, 1], alphas)
    ## TODO
    pass
def ex_1_2_a(x_train, x_test, y_train, y_test):
    """
    Solution for exercise 1.2 a)
    Remember to set alpha to 0 when initializing the model
    :param x_train: The training dataset
    :param x_test: The testing dataset
    :param y_train: The training targets
    :param y_test: The testing targets
    :return:
    """
    ## TODO
    alpha_values = np.array([1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 10, 100])
    dim1 = alpha_values.shape[0]
    mse_test_matrix = np.zeros((dim1, 10))
    mse_train_matrix = np.zeros((dim1, 10))
    k = 0
    for i in alpha_values:
        alpha = i
        for j in range(10):
            nn = MLPRegressor(activation='logistic', solver='lbfgs', max_iter=200,
                              hidden_layer_sizes=(40,), alpha=i, random_state=j)
            nn.fit(x_train, y_train)
            predictions_test = nn.predict(x_test)
            mse_test_matrix[k, j] = calculate_mse(nn, x_test, y_test)
            mse_train_matrix[k, j] = calculate_mse(nn, x_train, y_train)
        k += 1
    plot_mse_vs_alpha(mse_train_matrix, mse_test_matrix, alpha_values)
    plt.show()
def ex_1_2_a(x_train, x_test, y_train, y_test):
    """
    Solution for exercise 1.2 a)
    Remember to set alpha to 0 when initializing the model
    :param x_train: The training dataset
    :param x_test: The testing dataset
    :param y_train: The training targets
    :param y_test: The testing targets
    :return:
    """
    ## TODO - done
    n_h = 40
    test_array = np.zeros((11, 10))
    train_array = np.zeros((11, 10))
    alphas = [pow(10, i) for i in range(-8, 3)]

    for alpha in alphas:
        for j in range(0, 10):
            index = alphas.index(alpha)
            nn = MLPRegressor(activation='logistic', solver='lbfgs', alpha=alpha, hidden_layer_sizes=(n_h,),
                              max_iter=200, random_state=j)

            nn.fit(x_train, y_train)
            test_array[index][j] = calculate_mse(nn, x_test, y_test)
            train_array[index][j] = calculate_mse(nn, x_train, y_train)

    plot_mse_vs_alpha(train_array, test_array, alphas)
示例#9
0
def ex_1_2_a(x_train, x_test, y_train, y_test):
    """
    Solution for exercise 1.2 a)
    Remember to set alpha to 0 when initializing the model
    :param x_train: The training dataset
    :param x_test: The testing dataset
    :param y_train: The training targets
    :param y_test: The testing targets
    :return:
    """
    m = 0
    n = 0

    # declaring variables used in MLP-Regressor
    hidden_layers = 40
    alphas = np.array([
        10**(-8), 10**(-7), 10**(-6), 10**(-5), 10**(-4), 10**(-3), 10**(-2),
        10**(-1), 1, 10, 100
    ])
    random_state = 10
    activation_mode = 'logistic'
    solver_mode = 'lbfgs'
    max_iter = 200
    train_mse = np.zeros((alphas.size, random_state))
    test_mse = np.zeros((alphas.size, random_state))

    for m in range(alphas.size):
        for n in range(random_state):

            nn = MLPRegressor(hidden_layer_sizes=(hidden_layers, ),
                              activation=activation_mode,
                              solver=solver_mode,
                              alpha=alphas[m],
                              max_iter=max_iter,
                              random_state=n)
            nn.fit(x_train, y_train)
            train_mse[m][n] = calculate_mse(nn, x_train, y_train)
            test_mse[m][n] = calculate_mse(nn, x_test, y_test)

    plot_mse_vs_alpha(train_mse, test_mse, alphas)

    pass
def ex_1_2_a(x_train, x_test, y_train, y_test):
    """
    Solution for exercise 1.2 a)
    Remember to set alpha to 0 when initializing the model
    :param x_train: The training dataset
    :param x_test: The testing dataset
    :param y_train: The training targets
    :param y_test: The testing targets
    :return:
    """
    alphas = [
        pow(10, -8),
        pow(10, -7),
        pow(10, -6),
        pow(10, -5),
        pow(10, -4),
        pow(10, -3),
        pow(10, -2),
        pow(10, -1), 1, 10, 100
    ]
    n_iterations = 10

    train_mses = numpy.zeros((11, n_iterations))
    test_mses = numpy.zeros((11, n_iterations))
    r = 0
    for alph in alphas:
        for i in range(n_iterations):
            trained_regressor = MLPRegressor(hidden_layer_sizes=(40, ),
                                             activation='logistic',
                                             solver='lbfgs',
                                             alpha=alph,
                                             tol=1e-8,
                                             max_iter=200,
                                             random_state=i)
            trained_regressor = trained_regressor.fit(x_train, y_train)
            train_mses[r][i] = calculate_mse(trained_regressor, x_train,
                                             y_train)
            test_mses[r][i] = calculate_mse(trained_regressor, x_test, y_test)
        r = r + 1
    plot_mse_vs_alpha(train_mses, test_mses, alphas)
    pass
示例#11
0
def ex_1_2_a(x_train, x_test, y_train, y_test):
    """
    Solution for exercise 1.2 a)
    Remember to set alpha to 0 when initializing the model
    :param x_train: The training dataset
    :param x_test: The testing dataset
    :param y_train: The training targets
    :param y_test: The testing targets
    :return:
    """
    '''
    Write code to train a neural network with n = 40 hidden neurons with values of alpha α =
    [10−8, 10−7, 10−6, 10−5, 10−4, 10−3, 10−2, 10−1, 1, 10, 100]. Stack your results in an array where
    the first axis correspond to the regularization parameter and the second to the number of
    random seeds. Use the training solver ‘lbfgs’, for 200 iterations and 10 different random seeds.
    '''
    params_alpha = np.array([
        10**-8, 10**-7, 10**-6, 10**-5, 10**-4, 10**-3, 10**-2, 10**-1, 1, 10,
        100
    ])
    seeds = np.array(range(1, 11))
    train_mses = np.zeros((params_alpha.shape[0], seeds.shape[0]))
    test_mses = np.zeros((params_alpha.shape[0], seeds.shape[0]))
    for index_seed, seed in np.ndenumerate(seeds):
        for index_alpha, alpha in np.ndenumerate(params_alpha):
            nn = MLPRegressor(solver='lbfgs',
                              max_iter=200,
                              activation='logistic',
                              hidden_layer_sizes=(40, ),
                              alpha=alpha,
                              random_state=seed)
            nn.fit(x_train, y_train)
            train_mses[index_alpha,
                       index_seed] = calculate_mse(nn, x_train, y_train)
            test_mses[index_alpha,
                      index_seed] = calculate_mse(nn, x_test, y_test)
    plot_mse_vs_alpha(train_mses, test_mses, params_alpha)
    pass