NameError: name 'training_epochs' is not defined
时间: 2023-10-09 22:15:49 浏览: 143
This error occurs when you try to access the variable 'training_epochs' in your code, but it has not been defined or declared before. To fix this error, you need to declare or define the variable 'training_epochs' before using it in your code.
For example, you can define 'training_epochs' as follows:
training_epochs = 10
This assigns the value 10 to the variable 'training_epochs'. You can then use this variable in your code without getting the NameError.
相关问题
/tmp/ipykernel_1056/23656431.py in SGD(self, training_data, epochs, mini_batch_size, eta, test_data) 23 training_data[k:k+ mini_batch_size] 24 for k in range(0,n,mini_batch_size)] ---> 25 for mini.batch in mini_batches: 26 self.update_mini_batch(mini_batch, eta) 27 if test_data: NameError: name 'mini' is not defined
看起来你的代码中有一个笔误。在第25行,你写成了 `mini.batch` 而不是 `mini_batch`,所以 Python 不知道 `mini` 是什么。尝试将第25行改为以下代码:
```
for mini_batch in mini_batches:
```
这应该可以解决你的问题。
Epoch 1/10 2023-07-22 21:56:00.836220: W tensorflow/core/framework/op_kernel.cc:1807] OP_REQUIRES failed at cast_op.cc:121 : UNIMPLEMENTED: Cast string to int64 is not supported Traceback (most recent call last): File "d:\AI\1.py", line 37, in <module> model.fit(images, labels, epochs=10, validation_split=0.2) File "D:\AI\env\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "D:\AI\env\lib\site-packages\tensorflow\python\eager\execute.py", line 52, in quick_execute tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, tensorflow.python.framework.errors_impl.UnimplementedError: Graph execution error: Detected at node 'sparse_categorical_crossentropy/Cast' defined at (most recent call last): File "d:\AI\1.py", line 37, in <module> model.fit(images, labels, epochs=10, validation_split=0.2) File "D:\AI\env\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler return fn(*args, **kwargs) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1685, in fit tmp_logs = self.train_function(iterator) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1284, in train_function return step_function(self, iterator) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1268, in step_function outputs = model.distribute_strategy.run(run_step, args=(data,)) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1249, in run_step outputs = model.train_step(data) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1051, in train_step loss = self.compute_loss(x, y, y_pred, sample_weight) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1109, in compute_loss return self.compiled_loss( File "D:\AI\env\lib\site-packages\keras\engine\compile_utils.py", line 265, in __call__ loss_value = loss_obj(y_t, y_p, sample_weight=sw) File "D:\AI\env\lib\site-packages\keras\losses.py", line 142, in __call__ losses = call_fn(y_true, y_pred) File "D:\AI\env\lib\site-packages\keras\losses.py", line 268, in call return ag_fn(y_true, y_pred, **self._fn_kwargs) File "D:\AI\env\lib\site-packages\keras\losses.py", line 2078, in sparse_categorical_crossentropy return backend.sparse_categorical_crossentropy( File "D:\AI\env\lib\site-packages\keras\backend.py", line 5610, in sparse_categorical_crossentropy target = cast(target, "int64") File "D:\AI\env\lib\site-packages\keras\backend.py", line 2304, in cast return tf.cast(x, dtype) Node: 'sparse_categorical_crossentropy/Cast' Cast string to int64 is not supported [[{{node sparse_categorical_crossentropy/Cast}}]] [Op:__inference_train_function_1010]
这个错误是由于标签 `labels` 中的数据类型不正确导致的。`labels` 中的数据应该是整数类型,但是在您的代码中,它们被解析为字符串类型。为了解决这个问题,您需要将标签转换为整数类型。
请尝试在加载数据集时将标签转换为整数类型。修改以下部分的代码:
```python
for file in os.listdir(image_dir):
img_path = os.path.join(image_dir, file)
img = Image.open(img_path).convert('L') # 转换为灰度图像
img = img.resize((100, 100))
img = np.array(img, dtype=np.float32)
images.append(img)
labels.append(file.split('.')[0])
```
修改为:
```python
for file in os.listdir(image_dir):
img_path = os.path.join(image_dir, file)
img = Image.open(img_path).convert('L') # 转换为灰度图像
img = img.resize((100, 100))
img = np.array(img, dtype=np.float32)
images.append(img)
labels.append(int(file.split('.')[0]))
```
这样修改后,将标签转换为整数类型后,应该能够解决 `Cast string to int64 is not supported` 的问题。
阅读全文