I’m coding a neural network to add and subtract numbers. Below is the code:
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
num_train = 1000
X_train = np.random.rand(num_train, 2)
y_train_add = X_train[:, 0] + X_train[:, 1]
y_train_subtract = X_train[:, 0] - X_train[:, 1]
model_add = Sequential(
[
Dense(10, activation='relu'),
Dense(1, activation ='relu')
]
)
model_subtract = Sequential(
[
Dense(10, activation='relu'),
Dense(1, activation ='relu')
]
)
batch_size = 32
epochs = 100
model_add.compile(loss = 'mse', optimizer='adam')
model_add.fit(X_train, y_train_add, batch_size=batch_size, epochs=epochs, verbose = 1)
model_subtract.compile(loss = 'mse', optimizer='adam')
model_subtract.fit(X_train, y_train_subtract, batch_size=batch_size, epochs=epochs, verbose = 1)
x = "n"
while True:
print("enter first num:")
x = input()
if x == "end":
break
print("Enter operation:")
op= input()
print("enter second num:")
y = input()
X = int(x)
Y = int(y)
if op == "+":
predicted_sum = model_add.predict(np.array([[X, Y]]))
print(predicted_sum)
elif op =="-":
predicted_sum = model_subtract.predict(np.array([[X, Y]]))
print(predicted_sum)
There are two problems with it:
1: It works sometimes, but not always. On some occasions, when I run the program, it outputs approximately correct values for every pair of numbers I input. In other trials, every pair of numbers and operations gives [[0.]] as an output. There seems to be no pattern to it; sometimes when I run it, it just decides to output [[0.]], and it works fine at other times. I don’t even change the code between trials.
2: Every time I subtract two numbers with the result being negative, it outputs a small positive number.
Can someone help me fix this?