Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I would like to modify this code in order for it to have 4 input, 3 hidden nodes and one output. Receiving Error: mse =

I would like to modify this code in order for it to have 4 input, 3 hidden nodes and one output.

Receiving Error:

mse = mean_squared_error(A2, y_train) File "/Users/liasmith/Desktop/Ask Python Code.py", line 42, in mean_squared_error return ((y_pred - y_true)**2).sum() / (2*y_pred.size) ValueError: operands could not be broadcast together with shapes (130,3) (130,)

import numpy as np import pandas as pd from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt

# Loading dataset data = load_iris() # Dividing the dataset into target variable and features X=data.data y=data.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=20, random_state=4)

learning_rate = 0.1 iterations = 5000 N = y_train.size # Input features input_size = 4 # Hidden layers hidden_size = 2 # Output layer output_size = 3 results = pd.DataFrame(columns=["mse", "accuracy"])

np.random.seed(10) # Hidden layer W1 = np.random.normal(scale=0.5, size=(input_size, hidden_size)) # Output layer W2 = np.random.normal(scale=0.5, size=(hidden_size , output_size))

def sigmoid(x): return 1 / (1 + np.exp(-x)) def mean_squared_error(y_pred, y_true): return ((y_pred - y_true)**2).sum() / (2*y_pred.size) def accuracy(y_pred, y_true): acc = y_pred.argmax(axis=1) == y_true.argmax(axis=1) return acc.mean()

for itr in range(iterations): # Implementing feedforward propagation on hidden layer Z1 = np.dot(X_train, W1) A1 = sigmoid(Z1) # Implementing feed forward propagation on output layer Z2 = np.dot(A1, W2) A2 = sigmoid(Z2) # Calculating the error mse = mean_squared_error(A2, y_train) acc = accuracy(A2, y_train) results=results.append({"mse":mse, "accuracy":acc},ignore_index=True ) # Backpropagation phase E1 = A2 - y_train dW1 = E1 * A2 * (1 - A2) E2 = np.dot(dW1, W2.T) dW2 = E2 * A1 * (1 - A1) # Updating the weights W2_update = np.dot(A1.T, dW1) / N W1_update = np.dot(X_train.T, dW2) / N W2 = W2 - learning_rate * W2_update W1 = W1 - learning_rate * W1_update

Z1 = np.dot(X_test, W1) A1 = sigmoid(Z1) Z2 = np.dot(A1, W2) A2 = sigmoid(Z2) acc = accuracy(A2, y_test) print("Accuracy: {}".format(acc))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Jay Greenspan, Brad Bulger

1st Edition

978-0764535376

More Books

Students also viewed these Databases questions