Perceptron函数的基本模型在运行迭代后无法给出所需的结果Python

问题描述

执行代码块后的迭代应该使权重值逐渐增加,最后应该显示最后到达的权重。但是输出对所有迭代都赋予相同的权重。我是新来的编码。我要去哪里错了?

所需的最终输出为[[0],[0],[0],[0]],预期权重为[[-0.0405],[-0.0288],[-0.0066]]

功能代码块如下。

from numpy import *
    import numpy as np

class pcn:
    def __init__(self,inputs,targets):
        if np.ndim(inputs)>1:
            self.nIn=np.shape(inputs)[1]
        else:
            self.nIn=1
        
        if np.ndim(targets)>1:
            self.nOut=np.shape(targets)[1]
        else:
            self.nOut=1
            
        self.nData=np.shape(inputs)[0]
        
        self.weights=np.random.rand(self.nIn+1,self.nOut)*0.1-0.05
        
        
        
    
    def pcntrain(self,targets,eta,nIterations):
        
        inputs=np.concatenate((inputs,-np.ones((self.nData,1))),axis=1)
        
        change = range(self.nData)
        
        for n in range(nIterations):
            
            self.activations=self.pcnfwd(inputs);
            self.weights=eta*np.dot(np.transpose(inputs),self.activations-targets)
            
            print('Iteration: {}'.format(n))
            print (self.weights)
            
            #activations=self.pcnfwd(inputs)
            
            print('Final outputs are : {}'.format(activations))
                  
            
           # return self.weights
        
        
    
    def pcnfwd(self,inputs):
        activations=np.dot(inputs,self.weights)
    
        return np.where(activations>0,1,0)
        

输入代码语句为

inputs=inputs = np.array([[0,0],[0,1],[1,1]])
targets=np.array([[0],[1],[0]])   # XOR Targets

主要代码块是

p=pcn(inputs,targets)
p.pcntrain(inputs,0.25,15)

现在的代码输出需要随着权重的逐渐变化进行校正,直到最终迭代为止。

Iteration: 0
[[ 0.25]
 [ 0.  ]
 [-0.25]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 1
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 2
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 3
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 4
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 5
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 6
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 7
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 8
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 9
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 10
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 11
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 12
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 13
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]
Iteration: 14
[[ 0.25]
 [ 0.25]
 [-0.5 ]]
Final outputs are : [[1 1]
 [1 1]]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...