Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine input shape in keras?

I am having difficulty finding where my error is while building deep learning models, but I typically have issues when setting the input layer input shape.

This is my model:

model = Sequential([
Dense(32, activation='relu', input_shape=(1461, 75)),
Dense(32, activation='relu'),
Dense(ytrain.size),])

It is returning the following error:

 ValueError: Error when checking input: expected dense_1_input to have 3

 dimensions, but got array with shape (1461, 75)

The array is the training set from the kaggle housing price competition and my dataset has 75 columns and 1461 rows. My array is 2 dimensional, so why are 3 dimensions expected? I have tried adding a redundant 3rd dimension of 1 or flattening the array before the first dense layer but the error simply becomes:

ValueError: Input 0 is incompatible with layer flatten_1: expected 

min_ndim=3, found ndim=2

How do you determine what the input size should be and why do the dimensions it expects seem so arbitrary?

For reference, I attached the rest of my code:

xtrain = pd.read_csv("pricetrain.csv")
test = pd.read_csv("pricetest.csv")
xtrain.fillna(xtrain.mean(), inplace=True)
xtrain.drop(["Alley"], axis=1, inplace=True)
xtrain.drop(["PoolQC"], axis=1, inplace=True)
xtrain.drop(["Fence"], axis=1, inplace=True)
xtrain.drop(["MiscFeature"], axis=1, inplace=True)
xtrain.drop(["PoolArea"], axis=1, inplace=True)
columns = list(xtrain)
for i in columns:
    if xtrain[i].dtypes == 'object':
        xtrain[i] = pd.Categorical(pd.factorize(xtrain[i])[0])
from sklearn import preprocessing

le = preprocessing.LabelEncoder()
for i in columns:
    if xtrain[i].dtypes == 'object':
        xtrain[i] = le.fit_transform(xtrain[i])
ytrain = xtrain["SalePrice"]
xtrain.drop(["SalePrice"], axis=1, inplace=True)
ytrain = ytrain.values
xtrain = xtrain.values
ytrain.astype("float32")

size = xtrain.size
print(ytrain)
model = Sequential(
    [Flatten(),
     Dense(32, activation='relu', input_shape=(109575,)),
     Dense(32, activation='relu'),
     Dense(ytrain.size),
     ])
model.compile(loss='mse', optimizer='adam')
model.fit(xtrain, ytrain, epochs=10, verbose=1)

Any advice would be incredibly helpful!

Thank you.

like image 632
Josh Zwiebel Avatar asked Jun 12 '19 15:06

Josh Zwiebel


People also ask

How do you find the input shape of a Keras model?

We can use the “. shape” function to find the shape of the image. It returns a tuple of integers that represent the shape and color mode of the image. In the above output, the first two integer values represent the shape of the image.

What is the shape of input?

Input Shape In A Keras Layer In a Keras layer, the input shape is generally the shape of the input data provided to the Keras model while training. The model cannot know the shape of the training data. The shape of other tensors(layers) is computed automatically.

How do you determine the size of an input layer?

You choose the size of the input layer based on the size of your data. If you data contains 100 pieces of information per example, then your input layer will have 100 nodes. If you data contains 56,123 pieces of data per example, then your input layer will have 56,123 nodes.

What is input shape in neural network?

In this neural network, the input shape is given as (32, ). 32 refers to the number of features in each input sample. Instead of not mentioning the batch-size, even a placeholder can be given. Another way to give the input dimension in the above model is (None, 32, ).


1 Answers

The 0th dimension (sample-axis) is determined by the batch_size of the training. You omit it when defining the input shape. That makes sense since otherwise your model would be dependent on the number of samples in the dataset.

Same goes for the output. It seems you're only predicting a single value per example ("SalePrice"). So the output layer has shape 1.

model = Sequential([
    Dense(32, activation='relu', input_shape=(75, )),
    Dense(32, activation='relu'),
    Dense(1),
])
like image 135
ascripter Avatar answered Oct 23 '22 12:10

ascripter