# Keras exception: Error when checking input: expected dense_input to have shape (2,) but got array with shape (1,)

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:
line = 0
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)

• Can we get some sample data of your houses.csv file so we know what input_size you are trying to use? – JahKnows Mar 8 '19 at 3:26
• Input size is 2. I'm passing house_size and bhk as two factors of X [house_size, bhk] against the Y (house_rate) – Rohit Nair Mar 8 '19 at 3:38

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.

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