0
$\begingroup$

I have an understanding of this error, it means that the input that I'm passing to the model is of a different dimension that what was expected. The error also states that the input that I'm passing is of the dimension (1,) while it was expecting (2,)

I have tested the input value dimension by using x.shape and it prints out (2,) still the error exists. As a counter-intuitive move I picked one of the data that was in the training data and printed the shape of the zeroth element x1[0].shape also used that as an input, the error still exists.

model.fit works well, having error with model.predict (tried passing one of the training data hardcoded, still doesn't work)

CODE:

import tensorflow as tf
import numpy as np
from tensorflow import keras
import csv

x1, ys = [], []

with open('./house.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line = 0
    for row in csv_reader:
        if line > 0:
            x1.append([row[1], row[3]])
            ys.append(row[5])
        line += 1


model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[2])])
model.compile(optimizer='sgd', loss='mean_squared_error')
x1 = np.asarray(x1, dtype=float)
ys = np.asarray(ys, dtype=float)
model.fit(x1, ys, epochs=500)

print(x1[0].shape)
while True:
    house_size = float(input('Enter the house size: '))
    house_size = house_size/3000
    bhks = float(input('Enter the BHK: '))
    bhks = bhks/3
    x = np.array([house_size, bhks])
    try:
        value = model.predict(x)
    except Exception as e:
        print(e)
        print(x)
        print(x.shape)
    else:
        value = value[0][0] * 500
        print(value)
$\endgroup$
2
  • $\begingroup$ Can we get some sample data of your houses.csv file so we know what input_size you are trying to use? $\endgroup$
    – JahKnows
    Mar 8, 2019 at 3:26
  • $\begingroup$ Input size is 2. I'm passing house_size and bhk as two factors of X [house_size, bhk] against the Y (house_rate) $\endgroup$
    – Rohit Nair
    Mar 8, 2019 at 3:38

1 Answer 1

1
$\begingroup$

Yoy always need to pass the data for prediction in batches, although this batch is of size one (one sample). Try changing this line:

x = np.array([house_size, bhks])

into this:

x = np.array([[house_size, bhks]])

This should work.

$\endgroup$
1
  • $\begingroup$ I also had a doubt, can't we pass multiple X values? Like, model.fit([x1, x2], ys, epochs=500) ? $\endgroup$
    – Rohit Nair
    Mar 8, 2019 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.