【问题标题】:Weights are not getting updated while training logistic regression by using iris dataset使用 iris 数据集训练逻辑回归时权重未更新
【发布时间】:2019-02-14 23:27:43
【问题描述】:

Python 代码:

我使用了 Python code,如下所示。在这里,机器使用 Logistic Regression algorithmwine dataset 进行训练。在这里,问题是权重没有更新。我不明白问题出在哪里。

from sklearn import datasets
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

dataset = datasets.load_wine()
x = dataset.data
y = dataset.target
y = y.reshape(178,1)

x_train,x_test,y_train,y_test =   train_test_split(x,y,test_size=0.15,shuffle=True)
print(x_train.shape)
class log_reg():
    def __init__(self):
        pass
    def sigmoid(self,x):
        return 1 / (1 + np.exp(-x))
    def train(self,x,y,w1,w2,alpha,iterations):
        cost_history = [0] * iterations
        Y_train = np.zeros([y.shape[0],3])
        for i in range(Y_train.shape[0]):
            for j in range(Y_train.shape[1]):
                if(y[i] == j):
                    Y_train[i,j] = 1
        for iteration in range(iterations):
            z1 = x.dot(w1)
            a1 = self.sigmoid(z1)
            z2 = a1.dot(w2)
            a2 = self.sigmoid(z2)
            sig_sum = np.sum(np.exp(a2),axis=1)
            sig_sum = sig_sum.reshape(a2.shape[0],1)
            op = np.exp(a2) / sig_sum
            loss = (Y_train * np.log(op))
            dl =  (op-Y_train)
            dz1 = ((dl*(self.sigmoid(z2))*(1-self.sigmoid(z2))).dot(w2.T))*(self.sigmoid(z1))*(1-self.sigmoid(z1))
            dz2 = (dl * (self.sigmoid(z2))*(1-self.sigmoid(z2)))
            dw1 = x.T.dot(dz1)
            dw2 = a1.T.dot(dz2)
            w1 += alpha * dw1 
            w2 += alpha * dw2 
            cost_history[iteration] = (np.sum(loss)/len(loss))
        return w1,w2,cost_history
    def predict(self,x,y,w1,w2):
        z1 = x.dot(w1)
        a1 = self.sigmoid(z1)
        z2 = a1.dot(w2)
        a2 = self.sigmoid(z2)
        sig_sum = np.sum(np.exp(a2),axis=1)
        sig_sum = sig_sum.reshape(a2.shape[0],1)
        op = np.exp(a2) / sig_sum
        y_preds = np.argmax(op,axis=1)
        acc = self.accuracy(y_preds,y)
        return y_preds,acc
    def accuracy(self,y_preds,y):
        y_preds = y_preds.reshape(len(y_preds),1)
        correct = (y_preds == y)
        accuracy = (np.sum(correct) / len(y)) * 100
        return (accuracy)

if __name__ == "__main__":
    network = log_reg()
    w1 = np.random.randn(14,4) * 0.01
    w2 = np.random.randn(4,3) * 0.01
    X_train = np.ones([x_train.shape[0],x_train.shape[1]+1])
    X_train[:,:-1] = x_train
    X_test = np.ones([x_test.shape[0],x_test.shape[1]+1])
    X_test[:,:-1] = x_test
    new_w1,new_w2,cost = network.train(X_train,y_train,w1,w2,0.0045,10000)
    y_preds,accuracy = network.predict(X_test,y_test,new_w1,new_w2)
    print(y_preds,accuracy)

在上面的代码中,parameters如下所述

x--training set,
y--target(output),
w1--weights for first layer,
w2--weights for second layer,

我用过logistic regression with 2 hidden layers

我正在尝试从 sklearn 训练数据集 wine。我不知道问题出在哪里,但权重没有更新。任何帮助将不胜感激。

【问题讨论】:

  • 能否请您写完整的代码并正确缩进
  • 好的,我做到了。
  • 请提供完整代码
  • 是的,完成了。
  • 尝试绘制与迭代相关的损失值。如果值在减少,则权重正在更新。

标签: python-3.x machine-learning scikit-learn neural-network


【解决方案1】:

您的权重正在更新,但我认为您无法看到它们更改,因为您在执行后打印它们。 Python具有Numpy阵列的对象参考方法,因此当您传递W1时,其值也更改值,因此新_W1和W1变得相同。 拿这个例子

import numpy as np
x=np.array([1,2,3,4])
def change(x):
    x+=3
    return x
print(x)
change(x)
print(x)

如果您看到它出来的输出为

[1 2 3 4]
[4 5 6 7]

我建议您添加偏差并修复您的准确性功能,因为我的准确性为1000。

我在执行代码

时执行

W1和W2值确实改变。 我唯一更改的是主代码并启用原始数据集,请执行相同并告诉您的权重仍未更新

if __name__ == "__main__":
    network = log_reg()
    w1 = np.random.randn(13,4) * 0.01
    w2 = np.random.randn(4,3) * 0.01
    print(w1)
    print(" ")
    print(w2)
    print(" ")
    new_w1,new_w2,cost = network.train(x_train,y_train,w1,w2,0.0045,10000)
    print(w1)
    print(" ")
    print(w2)
    print(" ")
    y_preds,accuracy = network.predict(x_test,y_test,new_w1,new_w2)
    print(y_preds,accuracy)

【讨论】:

  • 我认为'dz1'值是零的。我打印了它。只有W1未更新,W2正在更新。你能解释为什么? span>
  • 尝试从预测打印op,它对测试集中的所有样本的类进行相同的值。你能解决它吗! span>
  • 在您的实现中存在几个问题 span>
  • 请您解决它们吗? span>
  • 生病指出了我可以在代码中看到的错误,但如果你想要一个更详细的答案,那么我建议你问另一个问题1)没有偏见2)OP 3中的随机数据集)我不是完全的当然,我从未使用过向前的指数确保您的公式是正确的4)使用Relu用于输出的隐藏层和Softmax,我建议您进行功能编程而不是OOP编程,OOP对于ML太僵硬,而且其难以查找错误 span>
猜你喜欢
  • 2016-01-31
  • 2015-01-07
  • 2020-02-25
  • 2018-02-27
  • 2020-10-26
  • 2015-04-10
  • 2016-02-01
  • 1970-01-01
  • 2015-09-20
相关资源
最近更新 更多