{
  "id": "tensorflow/type-error-tensorflow-dtype-mismatch",
  "signature": "TypeError: Cannot convert value of type 'numpy.ndarray' to TensorFlow DType 'int32'",
  "signature_zh": "类型错误：无法将类型为'numpy.ndarray'的值转换为TensorFlow DType 'int32'",
  "regex": "TypeError: Cannot convert value of type 'numpy\\.ndarray' to TensorFlow DType 'int32'",
  "domain": "tensorflow",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Passing a NumPy array where a scalar or tensor of a specific dtype is expected, often in tf.constant or model input.",
  "root_cause_type": "generic",
  "root_cause_zh": "在期望标量或特定dtype张量的地方传递了NumPy数组，通常出现在tf.constant或模型输入中。",
  "versions": [
    {
      "version": "tensorflow 2.11.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "numpy 1.24.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "tf.cast preserves shape; if shape is wrong, error persists.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "tf.constant expects a scalar or list, not a multi-dimensional array in this context.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure the input is a scalar or reshape it appropriately:\nimport numpy as np\nimport tensorflow as tf\n# Instead of passing array, pass scalar\nval = np.array([5])  # This may fail\n# Correct: pass scalar or use tf.constant with proper shape\nscalar_val = int(val[0])\ntf_constant = tf.constant(scalar_val, dtype=tf.int32)",
      "success_rate": 0.85,
      "how": "Ensure the input is a scalar or reshape it appropriately:\nimport numpy as np\nimport tensorflow as tf\n# Instead of passing array, pass scalar\nval = np.array([5])  # This may fail\n# Correct: pass scalar or use tf.constant with proper shape\nscalar_val = int(val[0])\ntf_constant = tf.constant(scalar_val, dtype=tf.int32)",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use tf.convert_to_tensor to explicitly convert the array before use:\ntensor = tf.convert_to_tensor(np.array([1,2,3]), dtype=tf.int32)\n# Then use tensor in place of the original array",
      "success_rate": 0.8,
      "how": "Use tf.convert_to_tensor to explicitly convert the array before use:\ntensor = tf.convert_to_tensor(np.array([1,2,3]), dtype=tf.int32)\n# Then use tensor in place of the original array",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Ensure the input is a scalar or reshape it appropriately:\nimport numpy as np\nimport tensorflow as tf\n# Instead of passing array, pass scalar\nval = np.array([5])  # This may fail\n# Correct: pass scalar or use tf.constant with proper shape\nscalar_val = int(val[0])\ntf_constant = tf.constant(scalar_val, dtype=tf.int32)",
    "Use tf.convert_to_tensor to explicitly convert the array before use:\ntensor = tf.convert_to_tensor(np.array([1,2,3]), dtype=tf.int32)\n# Then use tensor in place of the original array"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.tensorflow.org/api_docs/python/tf/convert_to_tensor",
  "official_doc_section": null,
  "error_code": "TCD",
  "verification_tier": "ai_generated",
  "confidence": 0.84,
  "fix_success_rate": 0.82,
  "resolvable": "true",
  "first_seen": "2024-05-12",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}