worldearth 发表于 2021-5-2 16:42

TypeError: 'int' object is not iterable

最近在跟着视频学习tensorflow,照着视频做,发现老出现这个问题,检查了好多遍也没发现到底哪里跟人家的不一样,不知道哪位大神可以帮我看看啊。    TypeError: 'int' object is not iterable

import tensorflow as tf
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense,Dropout,Conv2D,Flatten,MaxPooling2D

model=Sequential()
model.add(Conv2D(10,(5,5),activation='relu',input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(20,(5,5),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Dropout(0,25))
model.add(Flatten())
model.add(Dense(100,activation="relu"))
model.add(Dense(10,activation="softmax"))
model.compile(optimizer="rmsprop",loss=tf.keras.losses.categorical_crossentropy,metrics=['accuracy'])

(x_train,y_train),(x_test,y_test)=tf.keras.datasets.mnist.load_data()
#normalize图片处理
normlized_x_train=tf.keras.utils.normalize(x_train)
normlized_x_test=tf.keras.utils.normalize(x_test)
#one_hot标签处理
one_hot_y_train=tf.one_hot(y_train,10)
one_hot_y_test=tf.one_hot(y_test,10)
reshaped_x_train=normlized_x_train.reshape(-1,28,28,1)
reshaped_x_test=normlized_x_test.reshape(-1,28,28,1)


train_result=model.fit(reshaped_x_train,one_hot_y_train,epochs=5,validation_data=(reshaped_x_test,one_hot_y_test))



nanaqilin 发表于 2021-5-2 16:44

麻烦发一下全的报错日志,感觉应该是需要加类型 转换

worldearth 发表于 2021-5-2 16:59

nanaqilin 发表于 2021-5-2 16:44
麻烦发一下全的报错日志,感觉应该是需要加类型 转换

C:\Users\Lenovo\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Lenovo/PycharmProjects/pythonProject/test/CNN.py
2021-05-02 16:58:46.488931: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2021-05-02 16:58:46.489042: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2021-05-02 16:58:48.718524: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-05-02 16:58:48.720356: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2021-05-02 16:58:48.720447: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)
2021-05-02 16:58:48.723941: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-8RSSFOO
2021-05-02 16:58:48.724065: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-8RSSFOO
2021-05-02 16:58:48.724288: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-05-02 16:58:48.724984: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-05-02 16:58:49.498120: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
Epoch 1/5
Traceback (most recent call last):
File "C:/Users/Lenovo/PycharmProjects/pythonProject/test/CNN.py", line 30, in <module>
    train_result=model.fit(reshaped_x_train,one_hot_y_train,epochs=5,validation_data=(reshaped_x_test,one_hot_y_test))
File "C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1100, in fit
    tmp_logs = self.train_function(iterator)
File "C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\eager\def_function.py", line 828, in __call__
    result = self._call(*args, **kwds)
File "C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\eager\def_function.py", line 871, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\eager\def_function.py", line 726, in _initialize
    *args, **kwds))
File "C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\eager\function.py", line 2969, in _get_concrete_function_internal_garbage_collected
    graph_function, _ = self._maybe_define_function(args, kwargs)
File "C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\eager\function.py", line 3361, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
File "C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\eager\function.py", line 3206, in _create_graph_function
    capture_by_value=self._capture_by_value),
File "C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\framework\func_graph.py", line 990, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\eager\def_function.py", line 634, in wrapped_fn
    out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\framework\func_graph.py", line 977, in wrapper
    raise e.ag_error_metadata.to_exception(e)
TypeError: in user code:

    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\engine\training.py:805 train_function*
      return step_function(self, iterator)
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\engine\training.py:795 step_function**
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1259 run
      return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2730 call_for_each_replica
      return self._call_for_each_replica(fn, args, kwargs)
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3417 _call_for_each_replica
      return fn(*args, **kwargs)
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\engine\training.py:788 run_step**
      outputs = model.train_step(data)
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\engine\training.py:754 train_step
      y_pred = self(x, training=True)
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1012 __call__
      outputs = call_fn(inputs, *args, **kwargs)
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\engine\sequential.py:375 call
      return super(Sequential, self).call(inputs, training=training, mask=mask)
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\engine\functional.py:425 call
      inputs, training=training, mask=mask)
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\engine\functional.py:560 _run_internal_graph
      outputs = node.layer(*args, **kwargs)
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1012 __call__
      outputs = call_fn(inputs, *args, **kwargs)
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\layers\core.py:231 call
      lambda: array_ops.identity(inputs))
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\utils\control_flow_util.py:115 smart_cond
      pred, true_fn=true_fn, false_fn=false_fn, name=name)
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\framework\smart_cond.py:54 smart_cond
      return true_fn()
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\layers\core.py:226 dropped_inputs
      noise_shape=self._get_noise_shape(inputs),
    C:\Users\Lenovo\PycharmProjects\pythonProject\venv\lib\site-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


Process finished with exit code 1

nanaqilin 发表于 2021-5-2 17:02

不是因为这个动库加载不成功的原因吗?Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found

worldearth 发表于 2021-5-2 17:06

nanaqilin 发表于 2021-5-2 17:02
不是因为这个动库加载不成功的原因吗?Could not load dynamic library 'cudart64_110.dll'; dlerror: cuda ...

Epoch 1/5
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-e0e72e64c4be> in <module>
   27
   28
---> 29 train_result=model.fit(reshaped_x_train,one_hot_y_train,epochs=5,validation_data=(reshaped_x_test,one_hot_y_test))

~\anaconda3\lib\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)
   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()

~\anaconda3\lib\site-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()

~\anaconda3\lib\site-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

~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in _initialize(self, args, kwds, add_initializers_to)
    723   self._graph_deleter = FunctionDeleter(self._lifted_initializer_graph)
    724   self._concrete_stateful_fn = (
--> 725         self._stateful_fn._get_concrete_function_internal_garbage_collected(# pylint: disable=protected-access
    726             *args, **kwds))
    727

~\anaconda3\lib\site-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

~\anaconda3\lib\site-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 = graph_function
   3363

~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
   3194   arg_names = base_arg_names + missing_arg_names
   3195   graph_function = ConcreteFunction(
-> 3196         func_graph_module.func_graph_from_py_func(
   3197             self._name,
   3198             self._python_function,

~\anaconda3\lib\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)
    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,

~\anaconda3\lib\site-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

~\anaconda3\lib\site-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               raise

TypeError: in user code:

    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:805 train_function*
      return step_function(self, iterator)
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:795 step_function**
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1259 run
      return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2730 call_for_each_replica
      return self._call_for_each_replica(fn, args, kwargs)
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3417 _call_for_each_replica
      return fn(*args, **kwargs)
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:788 run_step**
      outputs = model.train_step(data)
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:754 train_step
      y_pred = self(x, training=True)
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1012 __call__
      outputs = call_fn(inputs, *args, **kwargs)
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\keras\engine\sequential.py:375 call
      return super(Sequential, self).call(inputs, training=training, mask=mask)
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\keras\engine\functional.py:424 call
      return self._run_internal_graph(
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\keras\engine\functional.py:560 _run_internal_graph
      outputs = node.layer(*args, **kwargs)
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1012 __call__
      outputs = call_fn(inputs, *args, **kwargs)
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\keras\layers\core.py:230 call
      output = control_flow_util.smart_cond(training, dropped_inputs,
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\keras\utils\control_flow_util.py:114 smart_cond
      return smart_module.smart_cond(
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\framework\smart_cond.py:54 smart_cond
      return true_fn()
    C:\Users\Lenovo\anaconda3\lib\site-packages\tensorflow\python\keras\layers\core.py:226 dropped_inputs
      noise_shape=self._get_noise_shape(inputs),
    C:\Users\Lenovo\anaconda3\lib\site-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

nanaqilin 发表于 2021-5-2 17:10

加上类型转换试试呢

worldearth 发表于 2021-5-2 17:18

nanaqilin 发表于 2021-5-2 17:10
加上类型转换试试呢

{:1_907:}没系统学过,不懂。。。。我就想学会简单使用。。。结果照着视频里输入代码,都能出错。。。。{:1_893:}{:1_907:}{:301_980:}

nanaqilin 发表于 2021-5-2 17:18

没事,一点一点的来就好
页: [1]
查看完整版本: TypeError: 'int' object is not iterable