Mehdi
Mehdi

Reputation: 1306

TypeError: Inputs to a layer should be tensors. Got: <PrefetchDataset shapes:

I am trying to read data from a few files with tf data pipeline and train a multi input neural network. I train model successfully but when I want to test model then I get this error:

TypeError: Inputs to a layer should be tensors. Got: <PrefetchDataset shapes: (((None, None), (None, None)), (None,)), types: ((tf.float32, tf.float32), tf.float32)>

Preprocessing Functions:

def split_chr_by_tab(*lines):
    sep = ","
    features = [tf.strings.to_number(tf.strings.split(line, sep=sep), out_type=tf.float32) for line in lines]
    return tf.concat(features, axis=0)

def separate_input_output(input):
    return tuple([input[:-6], input[-6:-1]]), input[-1]

Data Pipeline:

Train_dataset = tf.data.Dataset.list_files(Train_file_list)
Train_dataset= Train_dataset.shuffle(epoch).repeat(epoch).interleave(lambda filename: tf.data.TextLineDataset(filename).skip(1), num_parallel_calls=25)
Train_dataset= Train_dataset.map(split_chr_by_tab,num_parallel_calls=tf.data.experimental.AUTOTUNE).map(separate_input_output,num_parallel_calls=tf.data.experimental.AUTOTUNE)
Train_dataset= Train_dataset.batch(batch_size).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)

Test_dataset = tf.data.Dataset.list_files(Test_file_list)
Test_dataset= Test_dataset.interleave(lambda filename: tf.data.TextLineDataset(filename).skip(1), num_parallel_calls=25)
Test_dataset= Test_dataset.map(split_chr_by_tab,num_parallel_calls=tf.data.experimental.AUTOTUNE).map(separate_input_output,num_parallel_calls=tf.data.experimental.AUTOTUNE)
Test_dataset= Test_dataset.batch(batch_size).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)

Model Training and Evaluation (I get error when predicting on test data):

inp1 = Input(shape=(1000, ),name="inp1")
inp2 = Input(shape=(4, ),name="inp2")
merge1=concatenate([inp1, inp2])
first_dense = Dense(32,kernel_initializer=initializer, )(merge1)
output=dense(1,)(first_dense)
model = Model(inputs=[inp1, inp2], outputs=output)
model.fit(Train_dataset,callbacks=callbacks_list,verbose=1,epochs=epoch,steps_per_epoch=10)
prediction = model(Test_dataset)

Could you please tell me why I get error during prediction? For some reasons, I need to use model(Test_dataset) instead of model.predict(Test_dataset). Also, how can I store prediction values and true labels?

Upvotes: 3

Views: 5719

Answers (2)

Kaveh
Kaveh

Reputation: 4960

Test_dataset is a PrefetchDataset object and you can not pass it to model.__call__,since it expects a tensor as input. Use instead model.predict():

prediction = model.predict(Test_dataset)

If you insist to make predictions with calling model, then you can use it like this:

for p, _ in Test_dataset.take(1): # Takes 1 batch
    prediction = model(p)         # Predict 1 batch
print(prediction)

Upvotes: 3

Agust&#237;n Begue
Agust&#237;n Begue

Reputation: 128

You can also use:

Test_dataset.map(model)

assuming Test_dataset is already batched

Upvotes: 0

Related Questions