I coded two simple neural networks to add two numbers, and to square a number. I used them to create a program to multiply two numbers.
import tensorflow as tf
import numpy as np
model_add = tf.keras.models.load_model('model_add.keras')
model_sqr = tf.keras.models.load_model('model_sqr.keras')
predicted_product = model_sqr.predict(model_add.predict(np.array([3, 4]))) - model_sqr.predict(np.array([3])) - model_sqr.predict(np.array([4]))
print(predicted_product/2)
I had specified an input shape of (1,) for the square model and trained it on this data:
x_train = np.random.random((200000,1))*100-50
For the addition model, I trained it on
X_train = np.random.rand(num_train, 2)
I assume that since each element of the training array is what is inputted to the model each time during training, similarly, it is that same shape that is to be used for inputting testing data to the model. This is as it is for the square model, where I trained it on inputs of shape (200000,1) and used numpy arrays of shape (1,) as inputs.
But when I run this program, the following error shows up;
Invalid input shape for input Tensor("sequential_1/Cast:0", shape=(2,), dtype=float32).
Expected shape (None, 2), but input has incompatible shape (2,)
It looks like I should have used np.array([[3, 4]]) instead of np.array([3, 4]). But since each element of the training data for the addition model is of the shape (2,), shouldn’t that be what I use?