fromPyInquirerimportprompt,Separator# Define the sigmoid activation function
defsigmoid(x):return1/(1+math.exp(-x))# Define the neural network class
classNeuralNetwork:def__init__(self,input_size,hidden_size,output_size):# Initialize the weights and biases randomly
self.weights1=[[random.uniform(-1,1)for_inrange(input_size)]for_inrange(hidden_size)]self.bias1=[random.uniform(-1,1)for_inrange(hidden_size)]self.weights2=[[random.uniform(-1,1)for_inrange(hidden_size)]for_inrange(output_size)]self.bias2=[random.uniform(-1,1)for_inrange(output_size)]defpredict(self,inputs):# Compute the output of the hidden layer
hidden_output=[sigmoid(sum(w*xforw,xinzip(self.weights1[i],inputs))+self.bias1[i])foriinrange(len(self.bias1))]# Compute the output of the output layer
output=[sigmoid(sum(w*xforw,xinzip(self.weights2[i],hidden_output))+self.bias2[i])foriinrange(len(self.bias2))]# Return the predicted output
returnoutput# Define the questions for the PyInquirer prompt
questions=[{'type':'input','name':'input1','message':'Input 1:','default':'0.5','validate':lambdax:float(x)>=0andfloat(x)<=1or'Input must be between 0 and 1',},{'type':'input','name':'input2','message':'Input 2:','default':'0.5','validate':lambdax:float(x)>=0andfloat(x)<=1or'Input must be between 0 and 1',},Separator(),{'type':'input','name':'hidden_size','message':'Hidden layer size:','default':'3','validate':lambdax:int(x)>=1or'Hidden layer size must be greater than or equal to 1',},]# Prompt the user for input
answers=prompt(questions)# Parse the input
input1=float(answers['input1'])input2=float(answers['input2'])hidden_size=int(answers['hidden_size'])# Create the neural network
neural_network=NeuralNetwork(input_size=2,hidden_size=hidden_size,output_size=1)# Predict the output for the input
output=neural_network.predict([input1,input2])[0]# Display the output on the Numworks screen
screen.print_at(f"Input 1: {input1:.2f}",0,0)screen.print_at(f"Input 2: {input2:.2f}",0,1)screen.print_at(f"Output: {output:.2f}",0,2)
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.