首页 > 解决方案 > 为什么即使在 numpy 中写入明确的行之后矩阵也不会增加?

问题描述

def inti():
    ini = {'theta' : np.array((1e6 , 1e7 , 1e7, 1e7 , 1e7) , dtype = 'float64').reshape(5 , 1) , 
       'b' : 1.2}
    return ini

def sigmoid(x):
    return 1 / (1 + np.exp(-x)) 

def net_input(theta, x):
# Computes the weighted sum of inputs
    return np.dot(x , theta)

def prob(theta , x):
    return sigmoid(np.array(net_input(theta , x)) + inti()['b'])

def loss(theta , x , y):
    temp1 = np.dot(y.T , np.log(prob(theta , x)))
    temp2 = np.dot((1- y).T ,(1 - np.log(pdef grad(theta , x , y):
    return (np.dot(x.T , prob(theta , x ) - y))
    #temp = (-1/m)* temp
    return ((-1/m)* temp[0])

def grad(theta , x , y):
    return (np.dot(x.T , prob(theta , x ) - y))

cost =[]
#inti()
def params(x , y , theta , learning_rate):
    theta = inti()['theta']
    for i in np.arange(10000):
        cost.append((loss(theta , x, y)))
        theta = theta + (learning_rate) * grad(theta , x , y)
        print(grad(theta , x, y))
    return theta
# Function to get theta matrix 
parameters = params(X_train , y_train , inti()['theta'] , 20)`

我是 Python 和 ML 的初学者,并且编写了上述函数来执行逻辑回归。所有的矩阵维度都以在矩阵乘法中不发生错误的方式正确定义。我在这里面临一个相当不可思议的问题。theta 矩阵不会只是增加。我打印了每个 epoch 的成本,以查看初始化的效果如何,但成本始终相同,而梯度矩阵也不为零。梯度矩阵在每个时期也是恒定的。

我认为这可能是因为学习率低。但即使将学习率提高到 20,我发现它并没有改变。

我查看了代码,它似乎在逻辑上是正确的。

矩阵维度是
X_train : (700 , 5) y_train : (700 , 1) theta : (5 , 1)

标签: pythonmatrixlogistic-regression

解决方案


推荐阅读