Auto-Keras API详解(1)——安装Auto-Keras

(一)前 言

在这里插入图片描述
Auto-Keras是一款开源的,用于自动化机器学习的高级API,它能够在指定的时间内帮助我们寻找最优的网络结构,是一款非常棒的API!从这一节开始,我们将会开始介绍Auto-Keras的安装和使用方法,并且配以实践项目加以说明。

(二)安装PyTorch、Keras

Auto-Keras依赖于PyTorch、Keras组件,打开Anconda Prompt,输入以下命令:

pip install keras
conda install pytorch -c pytorch

出现以下界面:
在这里插入图片描述
等待安装完毕即可。

(三)安装graphviz

此依赖包的目的是为了绘制Auto-Keras生成的网络结构,同样的输入以下命令:

pip install graphviz

(四)安装Auto-Keras

最后来安装Auto-Keras,输入命令:

pip3 install --user autokeras

出现以下界面:
在这里插入图片描述

(五)测 试

打开jupyter编辑器,输入:

import autokeras
import pprint

pprint.pprint(autokeras)
# 输 出:
<module 'autokeras' from 'C:\\Users\\12394\\AppData\\Roaming\\Python\\Python36\\site-packages\\autokeras\\__init__.py'>

pprint.pprint()函数返回了Auto-Keras组件的安装位置,代表已经安装成功!

(六)总 结

在这一节中,我们介绍了Auto-Keras的安装方法,大家有任何的疑问请在评论区留言,我会尽快回复,谢谢支持!

已标记关键词 清除标记
刚配好autokeras环境准备跑一下mnist简单试一下, 但是在fit的时候就会报错 ``` from keras.datasets import mnist from autokeras import ImageClassifier #下载数据 (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train.reshape(x_train.shape + (1,)) x_test = x_test.reshape(x_test.shape + (1,)) ``` ``` # Initialize the ImageClassifier. clf = ak.ImageClassifier(max_trials=3) # Search for the best model. clf.fit(x_train, y_train, epochs=3, time_limit=10*60) ``` 报错信息为: ``` Epoch 1/3 --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-10-42cb2b350dcc> in <module> 1 # Search for the best model. ----> 2 clf.fit(x_train, y_train, epochs=3) /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/autokeras/tasks/image.py in fit(self, x, y, epochs, callbacks, validation_split, validation_data, **kwargs) 120 validation_split=validation_split, 121 validation_data=validation_data, --> 122 **kwargs) 123 124 /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/autokeras/auto_model.py in fit(self, x, y, batch_size, epochs, callbacks, validation_split, validation_data, **kwargs) 256 validation_data=validation_data, 257 fit_on_val_data=self._split_dataset, --> 258 **kwargs) 259 260 def _process_x(self, x, fit): /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/autokeras/engine/tuner.py in search(self, callbacks, fit_on_val_data, **fit_kwargs) 112 new_callbacks.append(tf.keras.callbacks.EarlyStopping(patience=10)) 113 --> 114 super().search(callbacks=new_callbacks, **fit_kwargs) 115 116 # Fully train the best model with original callbacks. /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/kerastuner/engine/base_tuner.py in search(self, *fit_args, **fit_kwargs) 128 129 self.on_trial_begin(trial) --> 130 self.run_trial(trial, *fit_args, **fit_kwargs) 131 self.on_trial_end(trial) 132 self.on_search_end() /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/autokeras/engine/tuner.py in run_trial(self, trial, x, *fit_args, **fit_kwargs) 69 model = self.hypermodel.build(trial.hyperparameters) 70 utils.adapt_model(model, x) ---> 71 history = model.fit(x, *fit_args, **copied_fit_kwargs) 72 for metric, epoch_values in history.history.items(): 73 if self.oracle.objective.direction == 'min': /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs) 64 def _method_wrapper(self, *args, **kwargs): 65 if not self._in_multi_worker_mode(): # pylint: disable=protected-access ---> 66 return method(self, *args, **kwargs) 67 68 # Running inside `run_distribute_coordinator` already. /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing) 846 batch_size=batch_size): 847 callbacks.on_train_batch_begin(step) --> 848 tmp_logs = train_function(iterator) 849 # Catch OutOfRangeError for Datasets of unknown size. 850 # This blocks until the batch has finished executing. /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds) 578 xla_context.Exit() 579 else: --> 580 result = self._call(*args, **kwds) 581 582 if tracing_count == self._get_tracing_count(): /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds) 625 # This is the first call of __call__, so we have to initialize. 626 initializers = [] --> 627 self._initialize(args, kwds, add_initializers_to=initializers) 628 finally: 629 # At this point we know that the initialization is complete (or less /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to) 504 self._concrete_stateful_fn = ( 505 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access --> 506 *args, **kwds)) 507 508 def invalid_creator_scope(*unused_args, **unused_kwds): /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs) 2444 args, kwargs = None, None 2445 with self._lock: -> 2446 graph_function, _, _ = self._maybe_define_function(args, kwargs) 2447 return graph_function 2448 /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs) 2775 2776 self._function_cache.missed.add(call_context_key) -> 2777 graph_function = self._create_graph_function(args, kwargs) 2778 self._function_cache.primary[cache_key] = graph_function 2779 return graph_function, args, kwargs /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes) 2665 arg_names=arg_names, 2666 override_flat_arg_shapes=override_flat_arg_shapes, -> 2667 capture_by_value=self._capture_by_value), 2668 self._function_attributes, 2669 # Tell the ConcreteFunction to clean up its graph once it goes out of /opt/anaconda3/envs/ak-env/lib/python3.6/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) 979 _, original_func = tf_decorator.unwrap(python_func) 980 --> 981 func_outputs = python_func(*func_args, **func_kwargs) 982 983 # invariant: `func_outputs` contains only Tensors, CompositeTensors, /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds) 439 # __wrapped__ allows AutoGraph to swap in a converted function. We give 440 # the function a weak reference to itself to avoid a reference cycle. --> 441 return weak_wrapped_fn().__wrapped__(*args, **kwds) 442 weak_wrapped_fn = weakref.ref(wrapped_fn) 443 /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs) 966 except Exception as e: # pylint:disable=broad-except 967 if hasattr(e, "ag_error_metadata"): --> 968 raise e.ag_error_metadata.to_exception(e) 969 else: 970 raise AttributeError: in user code: /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:571 train_function * outputs = self.distribute_strategy.run( /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:951 run ** return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica return self._call_for_each_replica(fn, args, kwargs) /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica return fn(*args, **kwargs) /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:543 train_step ** self.compiled_metrics.update_state(y, y_pred, sample_weight) /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/keras/engine/compile_utils.py:391 update_state self._build(y_pred, y_true) /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/keras/engine/compile_utils.py:322 _build self._metrics, y_true, y_pred) /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/util/nest.py:1118 map_structure_up_to **kwargs) /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/util/nest.py:1214 map_structure_with_tuple_paths_up_to *flat_value_lists)] /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/util/nest.py:1213 <listcomp> results = [func(*args, **kwargs) for args in zip(flat_path_list, /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/util/nest.py:1116 <lambda> lambda _, *values: func(*values), # Discards the path arg. /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/keras/engine/compile_utils.py:421 _get_metric_objects return [self._get_metric_object(m, y_t, y_p) for m in metrics] /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/keras/engine/compile_utils.py:421 <listcomp> return [self._get_metric_object(m, y_t, y_p) for m in metrics] /opt/anaconda3/envs/ak-env/lib/python3.6/site-packages/tensorflow/python/keras/engine/compile_utils.py:442 _get_metric_object y_t_rank = len(y_t.shape.as_list()) AttributeError: 'tuple' object has no attribute 'shape' ``` 在最后有说AttributeError: 'tuple' object has no attribute 'shape',但是我没有对示例代码或数据集做任何其他的修改,不懂这个错误是怎么出来的。 想过可能是keras版本过高,从2.4降到2.3还是不行。python也是autokeras唯一可用的3.6。tensorflow2.2。感觉这些应该都没问题啊? 求求大佬帮忙
©️2020 CSDN 皮肤主题: 数字50 设计师:CSDN官方博客 返回首页