python - 为什么即使在 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)
解决方案
推荐阅读
- excel - vba 函数从另一个特定工作表上的同一单元格中获取值
- rust - 相同的函数总是为相同的输入返回不同的答案
- html - 相对于父元素对齐 html 元素,独立于其他元素/兄弟元素
- arrays - SWIFT:索引超出范围显示文本但在打印上正常工作
- android - Android 模拟器不显示状态栏
- javascript - 从 API 获取数据后如何在数组中渲染单个对象?
- reactjs - 如何在 typescript+webpack 项目中使用 js/jsx 文件?
- reactjs - 警告:上下文类型失败:上下文“可见”被标记为必需,但其值为“未定义”
- github - 我应该如何更正我的链接代码以便它可以在 Github 上运行?
- android-studio - 面临错误>>无法解决方法'with(匿名com.android.volley.Response.Listner
)'