1

I designed an artificial neural networks model following the tutorial in here: https://www.tensorflow.org/tutorials/keras/regression

Afterwards, I saved the model using model.save(), and I tried loading it into a different notebook because that's how i expect people use trained models (importing them). Also I'm trying to design a code that allows me to predict any number of values i want (6, 7, 8, 2, whatever), so I'm trying to get this prediction data into an array to feed it to model_predict.

I was trying to make a simple prediction, but I'm failing everytime. How do I use model.predict() in situations like this?

Here's the code I was trying to use:

import pandas as pd
from sklearn import datasets
import tensorflow as tf
import itertools

model = tf.keras.models.load_model('MPG_Model.model')

prediction_input = {
    'Cylinders' : [4],
    'Displacement' : [140.0],
    'Horsepower' : [86.0],
    'Weight' : [2790.0],
    'Acceleration' : [15.6],
    'Model Year' : [82],
    'Origin' : [1],
}

dataset = tf.convert_to_tensor(prediction_input)
predictions = model.predict(dataset).flatten()

It returns the following error message:

ValueError: Attempt to convert a value ({'Cylinders': [4], 'Displacement': [140.0], 'Horsepower': [86.0], 'Weight': [2790.0], 'Acceleration': [15.6], 'Model Year': [82], 'Origin': [1]}) with an unsupported type (<class 'dict'>) to a Tensor.

What should I do?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Does `model.predict()` work when you use it in the same notebook as the one you perform the training in? – k-venkatesan Apr 25 '20 at 16:19
  • Nope.. It returns me the following error message: ValueError: Attempt to convert a value ({'Cylinders': [4], 'Displacement': [140.0], 'Horsepower': [86.0], 'Weight': [2790.0], 'Acceleration': [15.6], 'Model Year': [82], 'Origin': [1]}) with an unsupported type () to a Tensor – Gustavo Rangel Apr 26 '20 at 14:29
  • I'm not sure what format your data is in, but I rarely see dictionaries fed into a network - it's usually just a vector/matrix/tensor. Which in your case would be [4, 140.0, 86.0, 2790.0, 15.6, 82, 1]. – k-venkatesan Apr 26 '20 at 20:42
  • I tried that and the response was: Error when checking input: expected dense_input to have shape (9,) but got array with shape (1,) - The data is all numeric, except for "Origin" which is a dummy variable, which the model converts into one-hot encoding. I thought I had to specify to the program to which category each number is allocated to, e.g "There are 4 cylinders, displacement is 140,0", etc. – Gustavo Rangel Apr 27 '20 at 12:15
  • did you figure out the answer to your original question about passing in dictionaries / – bonobo Nov 11 '20 at 09:48

1 Answers1

0

The error you describe in your comment arises because your model expects an input of size (9, n), where 'n' is the number of data points you are feeding in - that's why it says (9,) is expected. But when you're feeding in while attempting to predict is actually a vector of size 9, which in two-dimensions is (1, 9) - this is why it says that it is getting (1,). You can fix this by reshaping the input from (1, 9) to (9, 1). Do this just before you call the predict() method:

dataset = tf.reshape(dataset, [9, 1])
k-venkatesan
  • 655
  • 1
  • 8
  • 15
  • How do I know that it is assuming the right values for the right variables? It returned me a value of 4130 which makes no sense... The model was supposed to run correctly with a mean absolute error with 1.80, and MPG values are around the two digits all the time.. I think it's because it's not allocating the numbers to the right classes. THank you a lot for you help so far ! :)) – Gustavo Rangel Apr 28 '20 at 08:08
  • Is your question "When I give on the inputs as 2790.0, how does it know that it's the weight"? It doesn't - the important thing is that the inputs are always in the same format - that means everything in the training set and testing set (as well as validations set, if any). The number of inputs and the number of outputs have to be constant and in the same order every time. – k-venkatesan Apr 28 '20 at 10:32