I recently came across this neural network and wanted to run it, but as it turned out, it can’t solve the equation a+b*c/d correctly, apparently it needs to be trained somehow, but I don’t quite understand how to do it. I have only recently started studying neural networks, I would appreciate it if you could help.
import numpy as np
# Define the neural network architecture
class NeuralNetwork:
def __init__(self):
self.weights = np.array([0.5, 0.5, 0.5]) # Weights for the input values
def forward(self, inputs):
return np.dot(inputs, self.weights)
# Define the equation to evaluate
equation = "2+3*10/9"
# Preprocess the equation
equation = equation.replace(" ", "") # Remove any spaces
# Split the equation into operands and operators
operands = []
operators = []
current_operand = ""
for char in equation:
if char.isdigit() or char == ".":
current_operand += char
else:
operands.append(float(current_operand))
current_operand = ""
operators.append(char)
# Add the last operand
operands.append(float(current_operand))
# Create input array
inputs = np.array(operands[:-1]) # Exclude the last operand (result)
# Evaluate the equation using the neural network
neural_network = NeuralNetwork()
output = neural_network.forward(inputs)
# Apply the operators to the output sequentially
for operator, operand in zip(operators, operands[1:]):
if operator == "+":
output += operand
elif operator == "-":
output -= operand
elif operator == "*":
output *= operand
elif operator == "/":
output /= operand
# Print the result
print(f"Equation: {equation}")
print(f"Output: {output}")
I tried to look for solutions, but nothing came of it.
New contributor