Anshul Dubey
Anshul Dubey

Reputation: 143

Flatten layer incompatible with input

I am trying to run the code

import data_processing as dp
import numpy as np
test_set = dp.read_data("./data2019-12-01.csv")
import tensorflow as tf
import keras

def train_model():
    autoencoder = keras.Sequential([
        keras.layers.Flatten(input_shape=[400]),
        keras.layers.Dense(150,name='bottleneck'),
        keras.layers.Dense(400,activation='sigmoid')
    ])
    autoencoder.compile(optimizer='adam',loss='mse')
    return autoencoder

trained_model=train_model()
trained_model.load_weights('./weightsfile.h5')
trained_model.evaluate(test_set,test_set)

The test_set in line 3 is of numpy array of shape (3280977,400). I am using keras 2.1.4 and tensorflow 1.5.

However, this puts out the following error

ValueError: Input 0 is incompatible with layer flatten_1: expected min_ndim=3, found ndim=2

How can I solve it? I tried changing the input_shape in flatten layer and also searched on the internet for possible solutions but none of them worked out. Can anyone help me out here? Thanks

Upvotes: 0

Views: 388

Answers (2)

Anshul Dubey
Anshul Dubey

Reputation: 143

After much trial and error, I was able to run the code. This is the code which runs:-

import data_processing as dp
import numpy as np
test_set = np.array(dp.read_data("./datanew.csv"))
print(np.shape(test_set))
import tensorflow as tf
from tensorflow import keras
# import keras
def train_model():
    autoencoder = keras.Sequential([
        keras.layers.Flatten(input_shape=[400]),
        keras.layers.Dense(150,name='bottleneck'),
        keras.layers.Dense(400,activation='sigmoid')
    ])
    autoencoder.compile(optimizer='adam',loss='mse')
    return autoencoder

trained_model=train_model()
trained_model.load_weights('./weightsfile.h5')
trained_model.evaluate(test_set,test_set)

The change I made is I replaced

import keras

with

from tensorflow import keras

This may work for others also, who are using old versions of tensorflow and keras. I used tensorflow 1.5 and keras 2.1.4 in my code.

Upvotes: 1

Timbus Calin
Timbus Calin

Reputation: 15003

Keras and TensorFlow only accept batch input data for prediction.

You must 'simulate' the batch index dimension.

For example, if your data is of shape (M x N), you need to feed at the prediction step a tensor of form (K x M x N), where K is the batch_dimension.

Simulating the batch axis is very easy, you can use numpy to achieve that:

Using: np.expand_dims(axis = 0), for an input tensor of shape M x N, you now have the shape 1 x M x N. This why you get that error, that missing '1' or 'K', the third dimension is that batch_index.

Upvotes: 0

Related Questions