# 类型错误：无法将类型为'numpy.ndarray'的值转换为TensorFlow DType 'int32'

- **ID:** `tensorflow/type-error-tensorflow-dtype-mismatch`
- **领域:** tensorflow
- **类别:** type_error
- **错误码:** `TCD`
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

在期望标量或特定dtype张量的地方传递了NumPy数组，通常出现在tf.constant或模型输入中。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| tensorflow 2.11.0 | active | — | — |
| numpy 1.24.0 | active | — | — |

## 解决方案

1. ```
   Ensure the input is a scalar or reshape it appropriately:
import numpy as np
import tensorflow as tf
# Instead of passing array, pass scalar
val = np.array([5])  # This may fail
# Correct: pass scalar or use tf.constant with proper shape
scalar_val = int(val[0])
tf_constant = tf.constant(scalar_val, dtype=tf.int32)
   ```
2. ```
   Use tf.convert_to_tensor to explicitly convert the array before use:
tensor = tf.convert_to_tensor(np.array([1,2,3]), dtype=tf.int32)
# Then use tensor in place of the original array
   ```

## 无效尝试

- **** — tf.cast preserves shape; if shape is wrong, error persists. (85% 失败率)
- **** — tf.constant expects a scalar or list, not a multi-dimensional array in this context. (90% 失败率)
