首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于Perceptron算法的不一致结果

基于Perceptron算法的不一致结果
EN

Stack Overflow用户
提问于 2014-01-28 07:10:36
回答 1查看 723关注 0票数 0

我试图实现感知器算法,但是得到的结果不一致;我注意到权值的初始化有很大的影响。有什么我明目张胆做错的吗?谢谢!

代码语言:javascript
复制
import numpy as np

def train(x,y):

    lenWeights = len(x[1,:]);
    weights = np.random.uniform(-1,1,size=lenWeights)
    bias = np.random.uniform(-1,1);
    learningRate = 0.01;
    t = 1;
    converged = False;

# Perceptron Algorithm

while not converged and t < 100000:
    targets = [];
    for i in range(len(x)):

            # Calculate output of the network
            output = ( np.dot(x[i,:],weights) ) + bias;

            # Perceptron threshold decision
            if (output > 0):
                target = 1;
            else:
                target = 0;

            # Calculate error and update weights
            error = target - y[i];

            weights = weights + (x[i,:] * (learningRate * error) );

            bias = bias + (learningRate * error);

            targets.append(target);

            t = t + 1;

    if ( list(y) == list(targets) ) == True:
        converged = True;


return weights,bias

def test(weights, bias, x):

    predictions = [];

    for i in range(len(x)):

        # Calculate w'x + b
        output = ( np.dot(x[i,:],weights) ) + bias;

        # Get decision from hardlim function
        if (output > 0):
            target = 1;
        else:
            target = 0;

        predictions.append(target);

    return predictions

if __name__ == '__main__':

    # Simple Test

    x = np.array( [  [0,1], [1,1] ] );
    y = np.array( [ 0, 1 ] );

    weights,bias = train(x,y);
    predictions = test(weights,bias,x);

    print predictions
    print y
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-28 07:20:55

Perceptron是而不是全局优化的,因此的训练结果不会是一致的(每次运行算法时都会有所不同),并且取决于(除其他外)初始化权值。这是非凸函数梯度优化的特点(尝试感知器就是其中的一个例子),而不是实现问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21399090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档