Perceptron算法的结果不一致

时间:2014-01-28 07:10:36

标签: machine-learning statistics neural-network perceptron

我正在尝试实施感知器算法,但结果不一致;我注意到权重的初始化产生了很大的影响。有什么我公然做错了吗?谢谢!

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

1 个答案:

答案 0 :(得分:0)

Perceptron 全局优化,因此训练结果不会保持一致(每次运行算法时它们都会有所不同),并且取决于(其中包括) )权重初始化。这是非凸函数梯度优化的特征(将感知器作为示例),而不是实现问题。