机器学习 - 感知机算法,python的for循环计算,输出有奇怪的结果
PHPz
PHPz 2017-04-18 09:45:17
0
1
631

自己编了一个感知机算法,前边的输出都没有问题,在第二十次循环的时候,原本结果该是[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
PHPz
PHPz

学习是最好的投资!

모든 응답(1)
梦想无需昭告天下

<?php

/**

* 인증코드 카테고리

*/

$code= new Code();

$code->outImage();

class Code

{

protected $number;

protected $codeType;

protected $width;

protected $height;

protected $image;

protected $code;

공개 함수 __construct($number=4,$codeType=2,$width= 100,$height=30)

{

$this->number=$number;

$this->codeType=$codeType;

$this->width=$width;

$this ->height=$height;

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

}

공개 함수 __get($name){

if ($name=='code' ) {

return $this->code;

}else{

return false;

}

}

공개 함수 __destruct(){

imagedestroy($this->image);

}

보호 함수 createCode(){

스위치 ($this->codeType) {

케이스 '0':

$code=$this->getNumberCode();

break;

케이스 ' 1':

$code=$this->getCharCode();

break;

케이스 '2':

$code=$this->getNumberCharCode();

break;

기본값:

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

}

return $code;

}

보호 함수 getNumberCode(){

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

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

}

보호 함수 getCharCode(){

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

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

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

}

보호 함수 getNumberCharCode (){

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

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

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

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

}

공개 함수 outImage(){

$this->createImage();

$this->fillBack();

$this->drawChar();

$this->drawDistrub();

$this->show();

}

보호 함수 createImage(){

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

}

보호 함수 fillBack(){

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

}

보호 함수  lightColor(){

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

}

보호된 함수 darkColor(){

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

}

protected 함수 drawChar(){

for ($i=0; $i <$this->번호; $i++) { 

$Width=$this->너비/$this->number;

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

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

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

}

}

보호 함수 drawDistrub(){

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

$x=mt_rand(0,$this->width);

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

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

}

}

보호 함수 show(){

header("콘텐츠 -유형:이미지/png");

imagepng($this->image);

}

}

?>


최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!