def classify(self, testInstance): """Classify a single instance. Parameters ---------- testInstance : list of floats Returns ------- bool : True if the testInstance is recognized as a 7, False otherwise. """ # Write your code to do the classification on an input image return Activation.sign(np.dot(testInstance, self.weight))
def train(self, verbose=True): """Train the perceptron with the perceptron learning algorithm. Parameters ---------- verbose : boolean Print logging messages with validation accuracy if verbose is True. """ # Write your code to train the perceptron here pass for epoch in range(self.epochs): for i in range(self.trainingSet.input.shape[0]): pre_result = dot(self.weight, self.trainingSet.input[i, :]) result = Activation.sign(pre_result, threshold=0) error = self.trainingSet.label[i] - result self.updateWeights(self.trainingSet.input[i, :], error) correct = 0.0 for j in range(self.validationSet.input.shape[0]): valid_result = Activation.sign( dot(self.weight, self.validationSet.input[j, :])) if valid_result == self.validationSet.label[j]: correct += 1.0 accuracy = correct / self.validationSet.input.shape[0] # print('after %d times training, valiation accuracy : %.4f %d' %(epoch, accuracy, correct)) # Den Schwellwert hab ich selbst auf 0.98 gesetzt if accuracy >= 0.98: if verbose: print( 'After %d times training, Validation accuracy:%.4f>0.98' % (epoch, accuracy)) print('Stop training to avoid overfitting!') break if epoch == self.epochs - 1: print( 'No accuracy >= threshold, no need to break loop to avoid overfitting' )
def classify(self, testInstance): """Classify a single instance. Parameters ---------- testInstance : list of floats Returns ------- bool : True if the testInstance is recognized as a 7, False otherwise. """ # Write your code to do the classification on an input image pass testResult = Activation.sign(dot(self.weight, testInstance), threshold=0) return testResult
def fire(self, input): """Fire the output of the perceptron corresponding to the input """ return Activation.sign(np.dot(np.array(input), self.weight))
def fire(self, input): """Fire the output of the perceptron corresponding to the input """ # I already implemented it for you to see how you can work with numpy return Activation.sign(np.dot(np.array(input), self.weight[1:]) + self.weight[0])
def _fire(self, input): """Fire the output of the perceptron corresponding to the input """ # I already implemented it for you to see how you can work with numpy return Activation.sign(np.dot(np.array(input), self.weight))