加载保存的决策森林模型问题

enter code here我训练了一个RandomForestModel并保存了它。我可以用原始模型进行预测,但无法用加载后的模型进行预测。我该如何用加载后的模型进行预测?我没有找到任何关于加载保存的随机森林模型的示例。TFDF库中也没有加载函数。我认为我必须对model_2进行一些操作,但不知道具体是什么操作。(TensorFlow版本2.5.0,TF-DF版本0.1.5,Python 3.8.5)

model_1.save("saved_model")model_2 = tf.keras.models.load_model("saved_model")examples = tf.data.Dataset.from_tensor_slices(sample2)predictions = model_1.predict(examples)print("predictions:\n",predictions)predictions = model_2.predict(examples)print("predictions:\n",predictions)

出现错误:

INFO:tensorflow:Assets written to: saved_model/assetsINFO:tensorflow:Assets written to: saved_model/assetspredictions: [[0.99666584]]---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-36-bed461cbf29c> in <module>    206 predictions = model_1.predict(examples)    207 print("predictions:\n",predictions)--> 208 predictions = model_2.predict(examples)    209 print("predictions:\n",predictions)~/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)   1725           for step in data_handler.steps():   1726             callbacks.on_predict_batch_begin(step)-> 1727             tmp_batch_outputs = self.predict_function(iterator)   1728             if data_handler.should_sync:   1729               context.async_wait()~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)    887     888       with OptionalXlaContext(self._jit_compile):--> 889         result = self._call(*args, **kwds)    890     891       new_tracing_count = self.experimental_get_tracing_count()~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)    931       # This is the first call of __call__, so we have to initialize.    932       initializers = []--> 933       self._initialize(args, kwds, add_initializers_to=initializers)    934     finally:    935       # At this point we know that the initialization is complete (or less~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)    761     self._graph_deleter = FunctionDeleter(self._lifted_initializer_graph)    762     self._concrete_stateful_fn = (--> 763         self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access    764             *args, **kwds))    765 ~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)   3048       args, kwargs = None, None   3049     with self._lock:-> 3050       graph_function, _ = self._maybe_define_function(args, kwargs)   3051     return graph_function   3052 ~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)   3442    3443           self._function_cache.missed.add(call_context_key)-> 3444           graph_function = self._create_graph_function(args, kwargs)   3445           self._function_cache.primary[cache_key] = graph_function   3446 ~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)   3277     arg_names = base_arg_names + missing_arg_names   3278     graph_function = ConcreteFunction(-> 3279         func_graph_module.func_graph_from_py_func(   3280             self._name,   3281             self._python_function,~/.local/lib/python3.8/site-packages/tensorflow/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)    997         _, original_func = tf_decorator.unwrap(python_func)    998 --> 999       func_outputs = python_func(*func_args, **func_kwargs)   1000    1001       # invariant: `func_outputs` contains only Tensors, CompositeTensors,~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)    670         # the function a weak reference to itself to avoid a reference cycle.    671         with OptionalXlaContext(compile_with_xla):--> 672           out = weak_wrapped_fn().__wrapped__(*args, **kwds)    673         return out    674 ~/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)    984           except Exception as e:  # pylint:disable=broad-except    985             if hasattr(e, "ag_error_metadata"):--> 986               raise e.ag_error_metadata.to_exception(e)    987             else:    988               raiseValueError: in user code:    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:1569 predict_function  *        return step_function(self, iterator)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:1559 step_function  **        outputs = model.distribute_strategy.run(run_step, args=(data,))    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1285 run        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2833 call_for_each_replica        return self._call_for_each_replica(fn, args, kwargs)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:3608 _call_for_each_replica        return fn(*args, **kwargs)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:1552 run_step  **        outputs = model.predict_step(data)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:1525 predict_step        return self(x, training=False)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:1030 __call__        outputs = call_fn(inputs, *args, **kwargs)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py:69 return_outputs_and_add_losses        outputs, losses = fn(*args, **kwargs)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py:165 wrap_with_training_arg        return control_flow_util.smart_cond(    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/keras/utils/control_flow_util.py:109 smart_cond        return smart_module.smart_cond(    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/framework/smart_cond.py:56 smart_cond        return false_fn()    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py:167 <lambda>        lambda: replace_training_and_call(False))    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py:163 replace_training_and_call        return wrapped_call(*args, **kwargs)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py:889 __call__        result = self._call(*args, **kwds)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py:933 _call        self._initialize(args, kwds, add_initializers_to=initializers)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py:763 _initialize        self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py:3050 _get_concrete_function_internal_garbage_collected        graph_function, _ = self._maybe_define_function(args, kwargs)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py:3444 _maybe_define_function        graph_function = self._create_graph_function(args, kwargs)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py:3279 _create_graph_function        func_graph_module.func_graph_from_py_func(    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py:999 func_graph_from_py_func        func_outputs = python_func(*func_args, **func_kwargs)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py:672 wrapped_fn        out = weak_wrapped_fn().__wrapped__(*args, **kwds)    /home/shiba/.local/lib/python3.8/site-packages/tensorflow/python/saved_model/function_deserialization.py:285 restored_function_body        raise ValueError(    ValueError: Could not find matching function to call loaded from the SavedModel. Got:      Positional arguments (2 total)...* False      Keyword arguments: {}        Expected these arguments to match one of the following 4 option(s):        Option 1:      Positional arguments (2 total): ...* False      Keyword arguments: {}        Option 2:      Positional arguments (2 total): ... * True      Keyword arguments: {}        Option 3:      Positional arguments (2 total): ...* False      Keyword arguments: {}        Option 4:      Positional arguments (2 total): ... * True      Keyword arguments: {}

回答:

在你保存模型到磁盘或工作区之前:

import bz2import pickleimport _pickle as cPicklewith bz2.BZ2File('.../randfmodel' + '.pbz2', 'wb') as f: cPickle.dump(model, f)

然后加载保存的模型:

 model_directory='YOUR_DIRECTORY_PATH' pkl_file = open(r'{}/randfmodel.pbz2'.format(model_directory), 'rb') model = cPickle.load(pkl_file)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注