def test_xor(): X = np.array(([3, 5], [5, 1], [10, 2]), dtype=float) y = np.array(([75], [82], [93]), dtype=float) X = X / np.amax(X, axis=0) y = y / 100 # Max test score is 100 X = np.array(([1, 1], [0, 1], [0, 0], [1, 0]), dtype=float) y = np.array(([0], [1], [0], [1]), dtype=float) NN = Neural_Network() train(NN, X, y) X = np.array(([1, 1]), dtype=float) yHat = NN.forward(X) print('estimate for {}: {}'.format(X, yHat)) X = np.array(([0, 1]), dtype=float) yHat = NN.forward(X) print('estimate for {}: {}'.format(X, yHat)) X = np.array(([1, 0]), dtype=float) yHat = NN.forward(X) print('estimate for {}: {}'.format(X, yHat)) X = np.array(([0, 0]), dtype=float) yHat = NN.forward(X) print('estimate for {}: {}'.format(X, yHat))
def test_train_ocr(): X1 = np.array(([3, 5], [5, 1], [10, 2]), dtype=float) y1 = np.array(([75], [82], [93]), dtype=float) a, b, c, c_to_recognized = alphabet() inputLayerSize = len(a[0]) hiddenLayerSize = 3 * inputLayerSize outputLayerSize = 1 NN = Neural_Network(inputLayerSize=inputLayerSize, hiddenLayerSize=hiddenLayerSize, outputLayerSize=outputLayerSize) X = np.array((a[0], b[0], c[0]), dtype=float) y = np.array((a[1], b[1], c[1]), dtype=float) train(NN, X, y) X = np.array((c[0]), dtype=float) yHat = NN.forward(X) print('estimate for good C: {}'.format(yHat)) X = np.array((c_to_recognized), dtype=float) yHat = NN.forward(X) print('estimate for bad C: {}'.format(yHat))
def classify(self, show_output=False): """Send the preprocessed images to the NN classifier""" print('{0} Numbers to be classified'.format(len(self.cropped_images))) return_list = [] self.apply_cropping(show_output=show_output) net = Neural_Network() net.load_state_dict(torch.load(TENSOR_LOCATION)) net.eval() for image in self.cropped_images: image = Image.fromarray(image) # Resizes the number and adds a 10 px border transfrom = transforms.Compose([ transforms.Grayscale(), transforms.Resize(self.output_size - self.border_size), transforms.CenterCrop(self.output_size), transforms.ToTensor(), ]) img_tensor = transfrom(image) if show_output: plt.imshow(np.array(img_tensor)[0, :, :], cmap=plt.cm.gray_r, interpolation='nearest') plt.title('Image used for classification') plt.show() img_tensor.unsqueeze_(0) outputs = net.forward(Variable(img_tensor)) dummy, predicted_labels = torch.max(outputs.data, 1) return_list.append(int(predicted_labels.numpy().max())) print('Classified: {0}'.format(predicted_labels.numpy().max())) return return_list
print ("Results : ") print (Y) #normalized training data X = NeuralNetwork.normalize(X) Y = Y/100 print ("Input : ") print (X) print ("Results :") print (Y) TrainObject = Trainer(NeuralNetwork) x, y, z = TrainObject.train(X,Y) yHat = NeuralNetwork.forward(X) print("YHat") print(yHat) print("Y") print(Y) print(x.shape, y.shape, z.shape) #to take on 1000 rows of datav(limited no of rows) x = np.resize(x, 1000) y = np.resize(y, 1000) z = np.resize(z, 1000) #plot wireframe f = plot.figure()
import time from neural_network import Neural_Network, X, y import numpy as np weightsToTry = np.linspace(-5, 5, 1000) costs = np.zeros(1000) NN = Neural_Network() startTime = time.clock() for i in range(1000): NN.W1[0, 0] = weightsToTry[i] yHat = NN.forward(X) costs[i] = 0.5 * sum((y - yHat) ** 2) endTime = time.clock() print(endTime)