Colab – Keras – TypeError: ‘int’ 对象不可迭代

我在尝试使用keras的mnist数据集训练和测试一个识别数字的网络,在我的电脑上运行得很好,但在Google Colab上却无法工作。

笔记本设置:

  • 硬件加速器 GPU

笔记本代码:

%tensorflow_version 2.ximport tensorflow as tfimport kerasimport numpy as nptf.test.gpu_device_name()from tensorflow.python.client import device_libdevice_lib.list_local_devices()from keras.datasets import mnistfrom keras.models import Sequentialfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2Dfrom keras import backend as k from keras.callbacks import TensorBoardbatchsize=100num_clases=10epocas=10 #10*100 = 1000filas,columnas=28,28#Entrada y salida(xt,yt),(xtest, ytest) = mnist.load_data()xt=xt.reshape(xt.shape[0], filas, columnas, 1)xtest=xtest.reshape(xtest.shape[0], filas, columnas, 1)xt=xt.astype('float32')xtest=xtest.astype('float32')xt=xt/255xtest=xtest/255yt=keras.utils.to_categorical(yt,num_clases)ytest=keras.utils.to_categorical(ytest,num_clases)modelo=Sequential()modelo.add(Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=(28,28,1)))modelo.add(Conv2D(128, kernel_size=(3,3), activation='relu'))modelo.add(MaxPooling2D(pool_size=(2,3)))modelo.add(Flatten())modelo.add(Dense(70,activation='relu'))modelo.add(Dropout(0,25))modelo.add(Dense(num_clases, activation='softmax'))modelo.compile(loss=keras.losses.categorical_crossentropy, metrics=['categorical_accuracy'], optimizer=keras.optimizers.Adam())modelo.fit(xt, yt, batch_size=batchsize, epochs=epocas, validation_data=[xtest, ytest], verbose=1)puntuacion=modelo.evaluate(xtest, ytest, batch_size=batchsize)print(puntuacion)

这是Google Colab显示的完整错误日志:

TypeError                                 Traceback (most recent call last)<ipython-input-19-cac37ad8c603> in <module>()     45 modelo.compile(loss=keras.losses.categorical_crossentropy, metrics=['categorical_accuracy'], optimizer=keras.optimizers.Adam())     46 ---> 47 modelo.fit(xt, yt, batch_size=batchsize, epochs=epocas, validation_data=[xtest, ytest], verbose=1)     48      49 puntuacion=modelo.evaluate(xtest, ytest, batch_size=batchsize)/usr/local/lib/python3.7/dist-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)   1098                 _r=1):   1099               callbacks.on_train_batch_begin(step)-> 1100               tmp_logs = self.train_function(iterator)   1101               if data_handler.should_sync:   1102                 context.async_wait()/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)    826     tracing_count = self.experimental_get_tracing_count()    827     with trace.Trace(self._name) as tm:--> 828       result = self._call(*args, **kwds)    829       compiler = "xla" if self._experimental_compile else "nonXla"    830       new_tracing_count = self.experimental_get_tracing_count()/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)    869       # This is the first call of __call__, so we have to initialize.    870       initializers = []--> 871       self._initialize(args, kwds, add_initializers_to=initializers)    872     finally:    873       # At this point we know that the initialization is complete (or less/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)    724     self._concrete_stateful_fn = (    725         self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access--> 726             *args, **kwds))    727     728     def invalid_creator_scope(*unused_args, **unused_kwds):/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)   2967       args, kwargs = None, None   2968     with self._lock:-> 2969       graph_function, _ = self._maybe_define_function(args, kwargs)   2970     return graph_function   2971 /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)   3359    3360           self._function_cache.missed.add(call_context_key)-> 3361           graph_function = self._create_graph_function(args, kwargs)   3362           self._function_cache.primary[cache_key] = graph_function   3363 /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)   3204             arg_names=arg_names,   3205             override_flat_arg_shapes=override_flat_arg_shapes,-> 3206             capture_by_value=self._capture_by_value),   3207         self._function_attributes,   3208         function_spec=self.function_spec,/usr/local/lib/python3.7/dist-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)    988         _, original_func = tf_decorator.unwrap(python_func)    989 --> 990       func_outputs = python_func(*func_args, **func_kwargs)    991     992       # invariant: `func_outputs` contains only Tensors, CompositeTensors,/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)    632             xla_context.Exit()    633         else:--> 634           out = weak_wrapped_fn().__wrapped__(*args, **kwds)    635         return out    636 /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)    975           except Exception as e:  # pylint:disable=broad-except    976             if hasattr(e, "ag_error_metadata"):--> 977               raise e.ag_error_metadata.to_exception(e)    978             else:    979               raiseTypeError: in user code:    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function  *        return step_function(self, iterator)    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function  **        outputs = model.distribute_strategy.run(run_step, args=(data,))    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 run        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica        return self._call_for_each_replica(fn, args, kwargs)    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica        return fn(*args, **kwargs)    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step  **        outputs = model.train_step(data)    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:754 train_step        y_pred = self(x, training=True)    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:1012 __call__        outputs = call_fn(inputs, *args, **kwargs)    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/sequential.py:375 call        return super(Sequential, self).call(inputs, training=training, mask=mask)    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/functional.py:425 call        inputs, training=training, mask=mask)    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/functional.py:560 _run_internal_graph        outputs = node.layer(*args, **kwargs)    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:1012 __call__        outputs = call_fn(inputs, *args, **kwargs)    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/core.py:231 call        lambda: array_ops.identity(inputs))    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/control_flow_util.py:115 smart_cond        pred, true_fn=true_fn, false_fn=false_fn, name=name)    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/smart_cond.py:54 smart_cond        return true_fn()    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/core.py:226 dropped_inputs        noise_shape=self._get_noise_shape(inputs),    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/core.py:215 _get_noise_shape        for i, value in enumerate(self.noise_shape):    TypeError: 'int' object is not iterable

为什么我会得到这个错误?


回答:

问题出在Dropout层的输入上

modelo.add(Dropout(0,25))

第一个参数是输入单元的丢弃比例。第二个参数是一个表示将与输入相乘的二进制丢弃掩码形状的1D整数张量。我认为你只想设置第一个参数。如果你把它改成下面的行,它将运行。

modelo.add(Dropout(0.4))

另一个问题是验证数据的输入必须是元组而不是数组,所以也需要更改这一行:

modelo.fit(xt, y=yt, batch_size=batchsize, epochs=epocas, validation_data=(xtest, ytest), verbose=1)

训练和评估的输出:

Epoch 10/10600/600 [==============================] - 4s 7ms/step - loss: 0.0225 - categorical_accuracy: 0.9924 - val_loss: 0.0280 - val_categorical_accuracy: 0.9933100/100 [==============================] - 0s 3ms/step - loss: 0.0280 - categorical_accuracy: 0.9933[0.0280386321246624, 0.9933000206947327]

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中创建了一个多类分类项目。该项目可以对…

发表回复

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