【发布时间】:2018-10-06 18:47:05
【问题描述】:
我正在尝试使用线性回归程序来预测使用 mnist 数据集的手写数字。每当我尝试运行它时,梯度下降函数总是需要一段时间才能工作,并且需要很长时间才能接近正确的权重。在八小时内,它已经完成了 550 次函数,仍然有很多错误。有人可以告诉我通常需要这么长时间,还是我做错了什么。
import numpy as np
import pandas as pd
mnist = pd.read_csv('mnist_train.csv')[:4200]
x = np.array(mnist)[:4200,1:]
y = np.array(mnist)[:4200,0].reshape(4200,1)
#How many numbers in dataset
n = len(x)
#How many values in each number
n1 = len(x[0])
#sets all weights equal to 1
coef = np.array([1 for i in range(n1)])
epochs = 1000000000000
learning_rate = .000000000008999
for i in range(epochs):
cur_y = sum(x*coef)
error = y-cur_y
#Calculates Gradient
grad = (np.array([sum(sum([-2/n * (error)* x[j,i] for j in range(n)])) for i in range(n1)]))
#Updates Weights
coef = (-learning_rate * grad) + coef
print(i)
print(sum(y-(x*coef)))
【问题讨论】:
-
“谁能告诉我通常需要这么长时间” - 需要多长时间?
-
我将 epochs 设置为 1000000000000 并让它运行 8 小时以查看它会经历多少次,它经历了大约 550 次迭代。误差变小了,但仍然完全不准确。
-
猜测一下,您可能希望安排您的代码,以便在
numpy中对超过 10 次迭代的循环进行矢量化 -
我认为你设置的学习率太小了,以至于在合理的循环次数后你有很大的错误。