0

I have recreated the Tensorflow structured data tutorial on a local jupyter notebook. This tutorial can be found at https://www.tensorflow.org/tutorials/structured_data/feature_columns.

I added the following the get a prediction for new data.

x = {
 'age': np.array([1,1]),
 'ca': np.array([1,1]),
 'chol': np.array([1,1]),
 'oldpeak': np.array([1,1]),
 'slope': np.array([1,1]),
 'thalach': np.array([1,1]),
 'trestbps': np.array([1,1]), 
 'sex': np.array([1,1]), 
 'restecg': np.array([1,1]),
 'cp': np.array([1,1]), 
 'exang': np.array([1,1]), 
 'fbs': np.array([1,1]),
 'thal': np.array(['fixed','fixed'])
  }

model.predict(x)

which successfully provides 2 predictions.

array([[0.1833743],[0.1833743]], dtype=float32)

However I cannot use this same dict to make a prediction from the saved model. I've save the model with

model.save('tf_tutorial_2020_05_15/test_1')

In a new notebook I have the following code.

import numpy as np
import pandas as pd

import tensorflow as tf

from tensorflow import feature_column
from tensorflow.keras import layers
from sklearn.model_selection import train_test_split

new_model = tf.keras.models.load_model('tf_tutorial_2020_05_15/test_1')

x = {
 'age': np.array([1,1]),
 'ca': np.array([1,1]),
 'chol': np.array([1,1]),
 'oldpeak': np.array([1,1]),
 'slope': np.array([1,1]),
 'thalach': np.array([1,1]),
 'trestbps': np.array([1,1]), 
 'sex': np.array([1,1]), 
 'restecg': np.array([1,1]),
 'cp': np.array([1,1]), 
 'exang': np.array([1,1]), 
 'fbs': np.array([1,1]),
 'thal': np.array(['fixed','fixed'])
  }

new_model.predict(x)

This throws the error below. What structure do I need to pass to new_model.predict() to get a prediction? or is there something else I'm doing wrong with this?

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-26ee037cb00c> in <module>
     15   }
     16 
---> 17 new_model.predict(x)

~/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
   1011         max_queue_size=max_queue_size,
   1012         workers=workers,
-> 1013         use_multiprocessing=use_multiprocessing)
   1014 
   1015   def reset_metrics(self):

~/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py in predict(self, model, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing, **kwargs)
    496         model, ModeKeys.PREDICT, x=x, batch_size=batch_size, verbose=verbose,
    497         steps=steps, callbacks=callbacks, max_queue_size=max_queue_size,
--> 498         workers=workers, use_multiprocessing=use_multiprocessing, **kwargs)
    499 
    500 

~/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py in _model_iteration(self, model, mode, x, y, batch_size, verbose, sample_weight, steps, callbacks, max_queue_size, workers, use_multiprocessing, **kwargs)
    424           max_queue_size=max_queue_size,
    425           workers=workers,
--> 426           use_multiprocessing=use_multiprocessing)
    427       total_samples = _get_total_number_of_samples(adapter)
    428       use_sample = total_samples is not None

~/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_v2.py in _process_inputs(model, mode, x, y, batch_size, epochs, sample_weights, class_weights, shuffle, steps, distribution_strategy, max_queue_size, workers, use_multiprocessing)
    644     standardize_function = None
    645     x, y, sample_weights = standardize(
--> 646         x, y, sample_weight=sample_weights)
    647   elif adapter_cls is data_adapter.ListsOfScalarsDataAdapter:
    648     standardize_function = standardize

~/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset)
   2344     # First, we build the model on the fly if necessary.
   2345     if not self.inputs:
-> 2346       all_inputs, y_input, dict_inputs = self._build_model_with_inputs(x, y)
   2347       is_build_called = True
   2348     else:

~/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in _build_model_with_inputs(self, inputs, targets)
   2570     else:
   2571       cast_inputs = inputs
-> 2572     self._set_inputs(cast_inputs)
   2573     return processed_inputs, targets, is_dict_inputs
   2574 

~/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in _set_inputs(self, inputs, outputs, training)
   2657           kwargs['training'] = training
   2658       try:
-> 2659         outputs = self(inputs, **kwargs)
   2660       except NotImplementedError:
   2661         # This Model or a submodel is dynamic and hasn't overridden

~/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    771                     not base_layer_utils.is_in_eager_or_tf_function()):
    772                   with auto_control_deps.AutomaticControlDependencies() as acd:
--> 773                     outputs = call_fn(cast_inputs, *args, **kwargs)
    774                     # Wrap Tensors in `outputs` in `tf.identity` to avoid
    775                     # circular dependencies.

~/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/sequential.py in call(self, inputs, training, mask)
    279         kwargs['training'] = training
    280 
--> 281       outputs = layer(inputs, **kwargs)
    282 
    283       # `outputs` will be the inputs to the next layer.

~/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    771                     not base_layer_utils.is_in_eager_or_tf_function()):
    772                   with auto_control_deps.AutomaticControlDependencies() as acd:
--> 773                     outputs = call_fn(cast_inputs, *args, **kwargs)
    774                     # Wrap Tensors in `outputs` in `tf.identity` to avoid
    775                     # circular dependencies.

~/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/saved_model/utils.py in return_outputs_and_add_losses(*args, **kwargs)
     57     inputs = args[inputs_arg_index]
     58     args = args[inputs_arg_index + 1:]
---> 59     outputs, losses = fn(inputs, *args, **kwargs)
     60     layer.add_loss(losses, inputs)
     61     return outputs

~/.local/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py in __call__(self, *args, **kwds)
    566         xla_context.Exit()
    567     else:
--> 568       result = self._call(*args, **kwds)
    569 
    570     if tracing_count == self._get_tracing_count():

~/.local/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py in _call(self, *args, **kwds)
    613       # This is the first call of __call__, so we have to initialize.
    614       initializers = []
--> 615       self._initialize(args, kwds, add_initializers_to=initializers)
    616     finally:
    617       # At this point we know that the initialization is complete (or less

~/.local/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
    495     self._concrete_stateful_fn = (
    496         self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
--> 497             *args, **kwds))
    498 
    499     def invalid_creator_scope(*unused_args, **unused_kwds):

~/.local/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
   2387       args, kwargs = None, None
   2388     with self._lock:
-> 2389       graph_function, _, _ = self._maybe_define_function(args, kwargs)
   2390     return graph_function
   2391 

~/.local/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py in _maybe_define_function(self, args, kwargs)
   2701 
   2702       self._function_cache.missed.add(call_context_key)
-> 2703       graph_function = self._create_graph_function(args, kwargs)
   2704       self._function_cache.primary[cache_key] = graph_function
   2705       return graph_function, args, kwargs

~/.local/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
   2591             arg_names=arg_names,
   2592             override_flat_arg_shapes=override_flat_arg_shapes,
-> 2593             capture_by_value=self._capture_by_value),
   2594         self._function_attributes,
   2595         # Tell the ConcreteFunction to clean up its graph once it goes out of

~/.local/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
    976                                           converted_func)
    977 
--> 978       func_outputs = python_func(*func_args, **func_kwargs)
    979 
    980       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

~/.local/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py in wrapped_fn(*args, **kwds)
    437         # __wrapped__ allows AutoGraph to swap in a converted function. We give
    438         # the function a weak reference to itself to avoid a reference cycle.
--> 439         return weak_wrapped_fn().__wrapped__(*args, **kwds)
    440     weak_wrapped_fn = weakref.ref(wrapped_fn)
    441 

~/.local/lib/python3.6/site-packages/tensorflow_core/python/saved_model/function_deserialization.py in restored_function_body(*args, **kwargs)
    260         .format(_pretty_format_positional(args), kwargs,
    261                 len(saved_function.concrete_functions),
--> 262                 "\n\n".join(signature_descriptions)))
    263 
    264   concrete_function_objects = []

ValueError: Could not find matching function to call loaded from the SavedModel. Got:
  Positional arguments (2 total):
    * {'age': <tf.Tensor 'features:0' shape=(None, 1) dtype=int64>, 'ca': <tf.Tensor 'features_1:0' shape=(None, 1) dtype=int64>, 'chol': <tf.Tensor 'features_2:0' shape=(None, 1) dtype=int64>, 'cp': <tf.Tensor 'features_3:0' shape=(None, 1) dtype=int64>, 'exang': <tf.Tensor 'features_4:0' shape=(None, 1) dtype=int64>, 'fbs': <tf.Tensor 'features_5:0' shape=(None, 1) dtype=int64>, 'oldpeak': <tf.Tensor 'features_6:0' shape=(None, 1) dtype=int64>, 'restecg': <tf.Tensor 'features_7:0' shape=(None, 1) dtype=int64>, 'sex': <tf.Tensor 'features_8:0' shape=(None, 1) dtype=int64>, 'slope': <tf.Tensor 'features_9:0' shape=(None, 1) dtype=int64>, 'thal': <tf.Tensor 'features_10:0' shape=(None, 1) dtype=string>, 'thalach': <tf.Tensor 'features_11:0' shape=(None, 1) dtype=int64>, 'trestbps': <tf.Tensor 'features_12:0' shape=(None, 1) dtype=int64>}
    * None
  Keyword arguments: {}

Expected these arguments to match one of the following 1 option(s):

Option 1:
  Positional arguments (2 total):
    * {'ca': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/ca'), 'exang': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/exang'), 'fbs': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/fbs'), 'chol': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/chol'), 'thalach': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/thalach'), 'thal': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/thal'), 'cp': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/cp'), 'age': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/age'), 'oldpeak': TensorSpec(shape=(None, 1), dtype=tf.float32, name='features/oldpeak'), 'restecg': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/restecg'), 'trestbps': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/trestbps'), 'slope': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/slope'), 'sex': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/sex')}
    * None
  Keyword arguments: {}

1 Answer 1

1

Try this :

pip uninstall tensorflow
pip uninstall tensorboard
pip install -q tf-nightly
pip install --ignore-installed tf-nightly

Should fix the issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.