face.py

Created by naul

Created on March 21, 2023

2.41 KB


from PyInquirer import prompt, Separator

# Define the sigmoid activation function
def sigmoid(x):
    return 1 / (1 + math.exp(-x))

# Define the neural network class
class NeuralNetwork:
    def __init__(self, input_size, hidden_size, output_size):
        # Initialize the weights and biases randomly
        self.weights1 = [[random.uniform(-1, 1) for _ in range(input_size)] for _ in range(hidden_size)]
        self.bias1 = [random.uniform(-1, 1) for _ in range(hidden_size)]
        self.weights2 = [[random.uniform(-1, 1) for _ in range(hidden_size)] for _ in range(output_size)]
        self.bias2 = [random.uniform(-1, 1) for _ in range(output_size)]

    def predict(self, inputs):
        # Compute the output of the hidden layer
        hidden_output = [sigmoid(sum(w * x for w, x in zip(self.weights1[i], inputs)) + self.bias1[i]) for i in range(len(self.bias1))]
        # Compute the output of the output layer
        output = [sigmoid(sum(w * x for w, x in zip(self.weights2[i], hidden_output)) + self.bias2[i]) for i in range(len(self.bias2))]
        # Return the predicted output
        return output

# Define the questions for the PyInquirer prompt
questions = [
    {
        'type': 'input',
        'name': 'input1',
        'message': 'Input 1:',
        'default': '0.5',
        'validate': lambda x: float(x) >= 0 and float(x) <= 1 or 'Input must be between 0 and 1',
    },
    {
        'type': 'input',
        'name': 'input2',
        'message': 'Input 2:',
        'default': '0.5',
        'validate': lambda x: float(x) >= 0 and float(x) <= 1 or 'Input must be between 0 and 1',
    },
    Separator(),
    {
        'type': 'input',
        'name': 'hidden_size',
        'message': 'Hidden layer size:',
        'default': '3',
        'validate': lambda x: int(x) >= 1 or '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)