首页 > 解决方案 > 神经网络中的 SciPy 优化警告

问题描述

fmin_bfgs()NeuralNetwork. 一切都应该清晰简单,遵循Backpropagation算法。

1 前馈训练示例。
2 计算每个单位的误差项。
3 累积梯度(对于第一个示例,我跳过了正则化项)。

Starting Loss: 7.26524579601
Check gradient: 2.02493576268
Warning: Desired error not necessarily achieved due to precision loss.
         Current function value: 5.741300
         Iterations: 3
         Function evaluations: 104
         Gradient evaluations: 92
Trained Loss: 5.74130012926

我刚刚用 MATLAB 完成了同样的任务,它使用fmin优化函数成功执行,但无法理解我在 Python 实现中错过了什么。如您所见,甚至scipy.optimize.check_grad返回太大的值。

def feed_forward(x, theta1, theta2):
    hidden_dot = np.dot(add_bias(x), np.transpose(theta1))
    hidden_p = sigmoid(hidden_dot)

    p = sigmoid(np.dot(add_bias(hidden_p), np.transpose(theta2)))
    return hidden_dot, hidden_p, p


def cost(thetas, x, y, hidden, lam):
    theta1, theta2 = get_theta_from(thetas, x, y, hidden)
    _, _, p = feed_forward(x, theta1, theta2)

    # regularization = (lam / (len(x) * 2)) * (
    #     np.sum(np.square(np.delete(theta1, 0, 1)))
    #     + np.sum(np.square(np.delete(theta2, 0, 1))))

    complete = -1 * np.dot(np.transpose(y), np.log(p)) \
               - np.dot(np.transpose(1 - y), np.log(1 - p))
    return np.sum(complete) / len(x)  # + regularization


def vector(z):
    # noinspection PyUnresolvedReferences
    return np.reshape(z, (np.shape(z)[0], 1))



def gradient(thetas, x, y, hidden, lam):
    theta1, theta2 = get_theta_from(thetas, x, y, hidden)
    hidden_dot, hidden_p, p = feed_forward(x, theta1, theta2)

    error_o = p - y
    error_h = np.multiply(np.dot(
        error_o, np.delete(theta2, 0, 1)), sigmoid_gradient(hidden_dot))

    x = add_bias(x)
    hidden_p = add_bias(hidden_p)

    theta1_grad, theta2_grad = \
        np.zeros(theta1.shape[::-1]), np.zeros(theta2.shape[::-1])
    records = y.shape[0]

    for i in range(records):
        theta1_grad = theta1_grad + np.dot(
            vector(x[i]), np.transpose(vector(error_h[i])))
        theta2_grad = theta2_grad + np.dot(
            vector(hidden_p[i]), np.transpose(vector(error_o[i])))

    theta1_grad = np.transpose(
        theta1_grad / records)  # + (lam / records * theta1)

    theta2_grad = np.transpose(
        theta2_grad / records)  # + (lam / records * theta2)

    return np.append(theta1_grad, theta2_grad)


def get_theta_shapes(x, y, hidden):
    return (hidden, x.shape[1] + 1), \
           (y.shape[1], hidden + 1)


def get_theta_from(thetas, x, y, hidden):
    t1_s, t2_s = get_theta_shapes(x, y, hidden)
    split = t1_s[0] * t1_s[1]

    theta1 = np.reshape(thetas[:split], t1_s)
    theta2 = np.reshape(thetas[split:], t2_s)
    return theta1, theta2



def train(x, y, hidden_size, lam):
    y = get_binary_y(y)

    t1_s, t2_s = get_theta_shapes(x, y, hidden_size)
    thetas = np.append(
        rand_init(t1_s[0], t1_s[1]),
        rand_init(t2_s[0], t2_s[1]))

    initial_cost = cost(thetas, x, y, hidden_size, lam)
    print("Starting Loss: " + str(initial_cost))

    check_grad1 = scipy.optimize.check_grad(
        cost, gradient, thetas, x, y, hidden_size, lam)
    print("Check gradient: " + str(check_grad1))

    trained_theta = scipy.optimize.fmin_bfgs(
        cost, thetas, fprime=gradient, args=(x, y, hidden_size, lam))

    print("Trained Loss: " +
          str(cost(trained_theta, x, y, hidden_size, lam)))

标签: pythonmachine-learningscipyneural-network

解决方案


只是想知道,你为什么跳过正则化步骤?您是否尝试过使用正则化运行程序?


推荐阅读