Rumah  >  Soal Jawab  >  teks badan

机器学习 - 感知机算法,python的for循环计算,输出有奇怪的结果

自己编了一个感知机算法,前边的输出都没有问题,在第二十次循环的时候,原本结果该是[0.05,0.05,0.15],但是却出现了奇怪的结果
[ 5.00000000e-02 -1.38777878e-17 1.50000000e-01]
后面也有这种输出(x,y也是)
求大神告诉我为啥会出现这个结果呢?

import sys
import numpy as np

def cal(a):
    if a>0:
        o=1
    else: 
        o=0
    return o

input=np.array([[-1,0,0],[-1,0,1],[-1,1,0],[-1,1,1]])
t=np.array([0,0,0,1])
w=np.array([-0.1,-0.2,0.2])
weight=w.transpose()
index=range(3)


count=0
loop=0
while(1):
    for i in range(4):
        loop=loop+1;
        a=input[i]
        x=np.dot(a,weight)
        print 'No loop',loop,'x=',x
        y=cal(x)
        print 'No. loop',loop,'y=',y
        if y==t[i]:
            count=count+1
        else:
            count=0
            for n in range(3):
                weight[n]=weight[n]+0.05*(t[i]-y)*a[n]
            print weight
        if count==4:
            break
    if count==4:
        break
        
aa=np.array([ 0.1,-0.05,0.1]);

for n in range(3):
    aa[n]=aa[n]+0.05*(1-0)*a[n]
print aa
PHPzPHPz2764 hari yang lalu673

membalas semua(1)saya akan balas

  • 梦想无需昭告天下

    梦想无需昭告天下2017-09-26 23:15:45

    <?php

    /**

    * Kategori kod pengesahan

    */

    $code= new Code();

    $code->outImage();

    class Code

    {

    protected $number;

    dilindungi $codeType;

    dilindungi $width;

    dilindungi $height;

    dilindungi $imej;

    dilindungi $kod;

    fungsi awam __construct($Type=4,$code=4,$kod 100,$tinggi=30)

    {

    $ini->nombor=$nombor;

    $ini->codeType=$codeType;

    $ini->width=$thi;

    ->height=$height;

    $this->code=$this->createCode();

    }

    public function __get($name){

    if ($name=='code' ) {

    pulangkan $this->code;

    }else{

    return false;

    }

    }

    public function __destruct(){

    imagedestroy(

    imagedestroy(

    );-g }

    fungsi dilindungi createCode(){

    suis ($this->codeType) {

    case '0':

    $code=$this->getNumberCode();

    break;

    1':

    $code=$this->getCharCode();

    break;

    case '2':

    $code=$this->getNumberCharCode();

    break;

    lalai:

    die("对不起,不支持该验证码类型!!!");

    }

    pulangkan $kod;

    }

    )('nombor

    dilindungi fungsi $' , julat (0, 9));

    return substr(str_shuffle($str), 0,$this->number);

    }

    protected function getCharCode(){

    $str=join('',range (a, z));

    $str=$str.strtoupper($str);

    return substr(str_shuffle($str), 0,$this->number);

    }

    fungsi dilindungi getNumberCharCode (){

    $str1=join('',range(0, 9));

    $str=join('',range(a, z));

    $str=$str1.$str. strtoupper($str);

    return substr(str_shuffle($str), 0,$this->number);

    }

    public function outImage(){

    $this->createImage();

    $this->fillBack();

    $this->drawChar();

    $this->drawDistrub();

    $this->show();

    }

    protected

    }

    createImage(){

    $this->image=imagecreatetruecolor($this->width, $this->height);

    }

    protected function fillBack(){

    imagefill($this-> imej, 0, 0, $this->lightColor());

    }

    fungsi dilindungi  lightColor(){

            return  imagecolorallocate($this->image, mt_rand(130,25), mt_rand(130,25), mt_rand(130,25) (130,255));

    }

    fungsi dilindungi darkColor(){

    kembalikan  imagecolorallocate($this->image, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120));

    ed fungsi drawChar(){

    untuk ($i=0; $i <$ini->nombor; $i++) { 

    $Width=$this->width/$this->nombor;

    $x=mt_rand($i*$Width+10,($i+1)*$Width-15);

    $y=mt_rand(0,$this->height-18);

    imagechar( $this->imej, 6, $x, $y, $this->kod[$i], $this->darkColor());

    }

    }

    fungsi dilindungi drawDistrub(){

    untuk ($i=0; $i < 300; $i++) { 

    $x=mt_rand(0,$ini->lebar);

    $y=mt_rand(0,$ini-> ketinggian);

    imagesetpixel($this->image, $x, $y, $this->lightColor());

    }

    }

    protected function show(){

    header("Content -Type:image/png");

    imagepng($this->image);

    }

    }

    ?>


    balas
    0
  • Batalbalas