基本感知器代码在运行时不显示任何内容

时间:2020-04-06 09:34:30

标签: python machine-learning neural-network jupyter-notebook perceptron

当前正在阅读斯蒂芬·马斯兰德(Stephen Marsland)的教科书“机器学习:算法观点”。我将以下代码粘贴到Jupyter Notebook中,然后运行它,但没有任何反应。对于ML,Python和Jupyter来说是非常新的,因此可以提供任何帮助。我了解感知器的概念及其在概念上的工作原理,但是由于某种原因,我很难将其转换为代码。

import numpy as np
class pcn:
    """ A basic Perceptron"""

    def __init__(self,inputs,targets):
        """ Constructor """
        # Set up network size
        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]

        # Initialise network
        self.weights = np.random.rand(self.nIn+1,self.nOut)*0.1-0.05

    def pcntrain(self,inputs,targets,eta,nIterations):
        """ Train the thing """ 
        # Add the inputs that match the bias node
        inputs = np.concatenate((inputs,-np.ones((self.nData,1))),axis=1)
        # Training
        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)

            # Randomise order of inputs
            #np.random.shuffle(change)
            #inputs = inputs[change,:]
            #targets = targets[change,:]

        return self.weights

    def pcnfwd(self,inputs):
        """ Run the network forward """
        # Compute activations
        activations =  np.dot(inputs,self.weights)

        # Threshold the activations
        return np.where(activations>0,1,0)


    def confmat(self,inputs,targets):
        """Confusion matrix"""

        # Add the inputs that match the bias node
        inputs = np.concatenate((inputs,-np.ones((self.nData,1))),axis=1)

        outputs = np.dot(inputs,self.weights)

        nClasses = np.shape(targets)[1]

        if nClasses==1:
            nClasses = 2
            outputs = np.where(outputs>0,1,0)
        else:
            # 1-of-N encoding
            outputs = np.argmax(outputs,1)
            targets = np.argmax(targets,1)

        cm = np.zeros((nClasses,nClasses))
        for i in range(nClasses):
            for j in range(nClasses):
                cm[i,j] = np.sum(np.where(outputs==i,1,0)*np.where(targets==j,1,0))

        print(cm)
        print(np.trace(cm)/np.sum(cm))

def logic():
    import pcn
    """ Run AND and XOR logic functions"""

    a = np.array([[0,0,0],[0,1,0],[1,0,0],[1,1,1]])
    b = np.array([[0,0,0],[0,1,1],[1,0,1],[1,1,0]])

    p = pcn.pcn(a[:,0:2],a[:,2:])
    p.pcntrain(a[:,0:2],a[:,2:],0.25,10)
    p.confmat(a[:,0:2],a[:,2:])

    q = pcn.pcn(b[:,0:2],b[:,2:])
    q.pcntrain(b[:,0:2],b[:,2:],0.25,10)
    q.confmat(b[:,0:2],b[:,2:])

0 个答案:

没有答案