批梯度下降给出奇怪的结果

时间:2018-09-06 19:05:28

标签: python machine-learning gradient-descent

我使用以下网站的数据进行了预测:http://archive.ics.uci.edu/ml/datasets/Concrete+Slump+Test

数据集的快捷键:http://archive.ics.uci.edu/ml/machine-learning-databases/concrete/slump/slump_test.data

代码如下:

import numpy as np

class Gradient:
def __init__(self,dataIn,dataOut): #constructor
    self.dataIn=dataIn
    self.dataOut=dataOut
def constructCoeffMatrix(self): #we have 7 features for each data row so we have to initialize 7+1(free) coefficient which will later be learnt
    l=np.random.rand(8,1)
    l=np.array(l)
    return l
def constructInputMatrix(self): #data initialy given as bidimensional list
    input=self.dataIn
    #standardization
    for i in range(len(input)):
        for j in range(len(input[i])):
            input[i][j]=input[i][j]/1000
    for i in range(len(input)):#our vectors are in the form of (1,x) (1 for the free scalar coeff)
        input[i] = [1] + input[i]
    Matrix=np.array(input)
    return Matrix
def constructOutMatrix(self):
    out=self.dataOut
    Matrix=np.array(out)
    #standardization
    return np.multiply(Matrix,1.0/1000)
#gradient function
def gradientDescent(self,x, y, theta, alpha, num_iters): #BGD , the cost function is 1/2m sum_1,m (y_hat-y)^2
    """
      Performs gradient descent to learn theta
   """
    m = y.size  # number of training examples
    for i in range(num_iters):
        y_hat = np.dot(x, theta)
        theta = theta - alpha * (1.0 / m) * np.dot(x.T, y_hat - y)
    return theta
def run_gradient(self):
    maxiter=1000
    input=self.constructInputMatrix()
    out=self.constructOutMatrix()
    coef=self.constructCoeffMatrix()
    learning_rate=0.05
    return self.gradientDescent(input,out,coef,learning_rate,maxiter)
def model(self,input):
    input[0]=[1]+input[0]
    input=np.array(input)
    res = input.dot(self.run_gradient())
    return res

如您所见,每个示例有7个输入功能和3个输出编号。在我的代码中,为简单起见,我只考虑了一个输出列,因此,基本上dataOut是一个n by 1列表(代表坍落度,流量或强度)。

我希望很容易理解,我确实添加了解释每个功能的注释,问题是...测试用例的结果完全是垃圾,总的来说它们太大了,如果我希望数值介于(0,100 ),预测值将超过500,我什至得到了令人反感的负面结果。

enter image description here

我尝试不使用标准化,但是错误会从milion到-milion到bilion到-bilion到+ -inf和“ nan”,因此每个预测都将是“ nan”。将所有内容除以1000至少得到的结果少于数百万。

0 个答案:

没有答案