Hot questions for Using Neural networks in jupyter notebook
learning how perceptron works and attempted to created a function out of it.
I recently watched a video in youtube as an introduction to the said topic.
Right now, I tried to mimic his function and I would like to try applying it in a sample dataset:
# x1 x2 y data = [ [3.5, 1.5, 1], [2.0, 1.0, 0], [4.0, 1.5, 1], [3.0, 1.0, 0], [3.5, 0.5, 1], [2.0, 0.5, 0], [5.5, 1.0, 1], [1.0, 1.0, 0], [4.5, 1.0, 1] ] data = pd.DataFrame(data, columns = ["Length", "Width", "Class"])
def sigmoid(x): x = 1 / (1 + np.exp(-x)) return x
w1 = np.random.randn() w2 = np.random.randn() b = np.random.randn() def perceptron(x1,x2, w1, w2, b): z = (w1 * x1) + (w2 * x2) + b return sigmoid(z)
My question here is how can I add the cost function inside the Perceptron and loop it n times based from a parameter to adjust the weights using the cost function?
def get_cost_slope(b,a): """ b = predicted value a = actual value """ sqrerror = (b - a) ** 2 slope = 2 * (b-a) return sqrerror, slope
You need to create a method which would backpropagate through the perceptron and optimize the weights.
def optimize( a , b ): sqrerror = (b - a) ** 2 cost_deriv = 2 * (b-a) sigmoid_deriv = z * ( 1 - z ) # derivative of sigmoid function learning_rate = 0.001 # Used to scale the gradients w1 -= ( cost_deriv * sigmoid_deriv * x1 ) * learning_rate # Gradient Descent update rule w2 -= ( cost_deriv * sigmoid_deriv * x2 ) * learning_rate b -= ( cost_deriv * sigmoid_deriv ) * learning_rate
Where $J$ is the cost function.
I am training a neural network and a part of my code has returned the following error:
def plot_confusion_matrix(truth, predictions, classes, normalize=False, save=False, cmap=plt.cm.Oranges, path="confusion_matrix.png"): acc = (np.array(truth) == np.array(predictions)) size = float(acc.shape) #error acc = np.sum(acc.astype("int32")) / size (...) AttributeError: 'bool' object has no attribute 'shape'
pred = pred.numpy() plot_confusion_matrix(truth=labels.numpy(), predictions=pred, save=False, path="logref_confusion_matrix.png", classes=["forward", "left", "right"])
Where the thuth represents the labels of Y and predictions the array of prediction, both with shape 32, 3. I checked the update on numpy, ipython etc and all are updated, tried some modification, but without success.
The only reason that acc would be a boolean and not a numpy array of booleans is that you are passing in a singular value for truth and predictions. In the code you provided, there would be no error for an actual array of 32x3. Look at the rest of your code and make sure you actually pass in an array to np.array() instead of singular values.