OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Inconsistency in input shape to neural network

  • Thread starter Thread starter harry
  • Start date Start date
H

harry

Guest
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.

Code:
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:

Code:
x_train = np.random.random((200000,1))*100-50

For the addition model, I trained it on

Code:
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;

Code:
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?
<p>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.</p>
<pre><code>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)
</code></pre>
<p>I had specified an input shape of (1,) for the square model and trained it on this data:</p>
<pre><code>x_train = np.random.random((200000,1))*100-50
</code></pre>
<p>For the addition model, I trained it on</p>
<pre><code>X_train = np.random.rand(num_train, 2)
</code></pre>
<p>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.</p>
<p>But when I run this program, the following error shows up;</p>
<pre><code>Invalid input shape for input Tensor("sequential_1/Cast:0", shape=(2,), dtype=float32).
Expected shape (None, 2), but input has incompatible shape (2,)
</code></pre>
<p>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?</p>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S
Top