【问题标题】:Network Cost Function code Python Implementation网络成本函数代码 Python 实现
【发布时间】:2021-09-20 11:29:16
【问题描述】:

我在 Python 中实施 Andrew NG 的 ML 课程,在第 5 周的练习 4 中,我指的是代码。我不明白的是需要在最终输出中使用 np.trace() 。在可视化矩阵时遇到问题

import numpy as np
from scipy.optimize import minimize
import scipy.io
import matplotlib.pyplot as plt

data_dict = scipy.io.loadmat('ex4_orig_octave/ex4data1.mat')

X = data_dict['X']
y = data_dict['y'].ravel()

M = X.shape[0]
N = X.shape[1] 
L = 26 # = number of nodes in the hidden layer (including bias node)
K = len(np.unique(y))

X = np.hstack((np.ones((M, 1)), X))

Y = np.zeros((M, K), dtype='uint8') 

for i, row in enumerate(Y):
    Y[i, y[i] - 1] = 1

weights_dict = scipy.io.loadmat('ex4_orig_octave/ex4weights.mat')

theta_1 = weights_dict['Theta1']
theta_2 = weights_dict['Theta2']

nn_params_saved = np.concatenate((theta_1.flatten(), theta_2.flatten()))

def nn_cost_function(nn_params, X, Y, M, N, L, K):
    """Python version of nnCostFunction.m after completing 'Part 1'."""

    # Unroll the parameter vector.
    theta_1 = nn_params[:(L - 1) * (N + 1)].reshape(L - 1, N + 1)
    theta_2 = nn_params[(L - 1) * (N + 1):].reshape(K, L)
    
    # Calculate activations in the second layer.
    a_2 = sigmoid(theta_1.dot(X.T))
    
    # Add the second layer's bias node.
    a_2_p = np.vstack((np.ones(M), a_2))
    
    # Calculate the activation of the third layer.
    a_3 = sigmoid(theta_2.dot(a_2_p))
    
    # Calculate the cost function.
    cost = 1 / M * np.trace(- Y.dot(np.log(a_3)) - (1 - Y).dot(np.log(1 - a_3)))
    
    return cost

cost_saved = nn_cost_function(nn_params_saved, X, Y, M, N, L, K) 

print 'Cost at parameters (loaded from ex4weights): %.6f' % cost_saved
print '(this value should be about 0.287629)'

【问题讨论】:

  • 请阅读ml标签的描述。
  • @molbdnilo 对此感到抱歉

标签: python numpy machine-learning neural-network


【解决方案1】:

1/M * np.trace() 操作正在计算 M 批次的平均成本:

可读性稍差,但速度要快得多:

np.sum(np.sum(Y.multiply(np.log(a_3.T)),axis=1),axis=0)

,如果Y.shape==(M,K)a_3.shape==(K,M)

Y = lambda : np.random.uniform(size=(5000,10)) # (M,K)
a3 = lambda : np.random.uniform(size=(10,5000)) # (K,M)
timeit.timeit('import numpy as np; np.trace(Y().dot(a3()))', number=10, globals=globals())
# 0.5633535870001651
timeit.timeit('import numpy as np; np.sum(np.sum(np.multiply(Y(),a3().T),axis=1),axis=0)', number=10, globals=globals())
# 0.013223066000136896

【讨论】:

  • cost 是一个 ndarray 数组,'.multiply' 不起作用
  • Y和a_3有什么类型和形状? .multiply 进行元素乘法。如果两者都是 np.ndarray 和 Y 类型,并且 a_3 的转置 a_3.T 具有相同的形状 (M,K),为什么 .multiply 不起作用?
猜你喜欢
  • 2018-07-01
  • 1970-01-01
  • 2021-05-08
  • 1970-01-01
  • 2022-01-01
  • 2014-02-21
  • 1970-01-01
  • 2018-10-28
  • 1970-01-01
相关资源
最近更新 更多