随机梯度下降

随机梯度下降,第1张

随机梯度下降
import numpy as np
import random
import math
import matplotlib.pyplot as plt
from functools import reduce
class Perceptron(object):
    w = np.array([[1],[1]])
    x = np.array([[1, 1], [0, 0], [1, 0], [0, 1]])
    t = [1,0,0,0]
    x1 = np.array(x[:,0])
    x2 = np.array(x[:,1])
    e0 = 9
    e1 = 9
    eps = 1e-4
    abc = 0

    def __init__(self):
        self.eps = 1e-4
        self.a = 0.01

    def jia(x, y):
        a = list(map(lambda x: x*y,x))
        c = np.sum(a)
        return c


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


    for abc in range(1000):
        i=0
        w1 = w[0]
        w2 = w[1]
        for i in range(0,4):
            i = random.randint(0, 3)
            e0 = np.sum(2*(f(jia(w1,x1[i])-t[i])*(1-f(jia(w1,x1)))*x1))
            e1 = np.sum(2*(f(jia(w2,x2[i])-t[i])*(1-f(jia(w2,x2)))*x2))
            w1 = w1-0.1*e0
            w2 = w2-0.1*e1
        abc=abc+1

    if __name__ == '__main__':
        print(w1,w2 )








 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5480186.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存