You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacBook M1 Pro本地Jupyter运行TensorFlow线性回归报错求助

MacBook M1 Pro上TensorFlow Metal运行线性回归报错的解决方法

问题背景

在MacBook M1 Pro上使用适配Apple Silicon的TensorFlow Metal运行线性回归模型,相同代码在Google Colab可正常执行,但本地Jupyter Notebook运行时抛出NotFoundError: Graph execution error,核心报错提示为could not find registered platform with id: 0x1129b0ef0。已尝试重装Jupyter、TensorFlow并更新所有依赖库,问题仍未解决。

运行代码

import tensorflow as tf
import pandas as pd

# Load data from Excel file
file_path = '/Users/ayushanand/Documents/Data Farming/New Data/Data_Final_Features.xlsx'
print(file_path)
data = pd.read_excel(file_path, sheet_name='Merge', index_col=0)

# Split data into features and target
X = data.iloc[:, :-1]
y = data.iloc[:, -1]

# Split data into training and testing sets
train_size = int(0.8 * len(data))
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]

# Define the linear regression model
model = tf.keras.Sequential([
  tf.keras.layers.Dense(1, input_shape=(X.shape[1],), activation='linear')
])

# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')

# Train the model
model.fit(X_train, y_train, epochs=100, verbose=0)

# Evaluate the model's accuracy on the training set
train_loss = model.evaluate(X_train, y_train, verbose=0)
print('Training loss:', train_loss)

# Evaluate the model's accuracy on the testing set
test_loss = model.evaluate(X_test, y_test, verbose=0)
print('Testing loss:', test_loss)

完整报错信息

/Users/ayushanand/Documents/Data Farming/New Data/Data_Final_Features.xlsx
2023-03-22 05:47:31.804968: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled.
2023-03-22 05:47:31.852556: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x1129b0ef0
2023-03-22 05:47:31.852588: W tensorflow/core/framework/op_kernel.cc:1830] OP_REQUIRES failed at xla_ops.cc:418 : NOT_FOUND: could not find registered platform with id: 0x1129b0ef0
---------------------------------------------------------------------------
NotFoundError                             Traceback (most recent call last)
Cell In[2], line 27
     24 model.compile(optimizer='adam', loss='mean_squared_error')
     26 # Train the model
---> 27 model.fit(X_train, y_train, epochs=100, verbose=0)
     29 # Evaluate the model's accuracy on the training set
     30 train_loss = model.evaluate(X_train, y_train, verbose=0)

File ~/miniforge3/lib/python3.9/site-packages/keras/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File ~/miniforge3/lib/python3.9/site-packages/tensorflow/python/eager/execute.py:52, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     50 try:
     51   ctx.ensure_initialized()
---> 52   tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     53                                       inputs, attrs, num_outputs)
     54 except core._NotOkStatusException as e:
     55   if name is not None:

NotFoundError: Graph execution error:

Detected at node 'StatefulPartitionedCall' defined at (most recent call last):
    File "/Users/ayushanand/miniforge3/lib/python3.9/runpy.py", line 197, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "/Users/ayushanand/miniforge3/lib/python3.9/runpy.py", line 87, in _run_code
      exec(code, run_globals)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/ipykernel_launcher.py", line 17, in <module>
      app.launch_new_instance()
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/traitlets/config/application.py", line 1043, in launch_instance
      app.start()
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/ipykernel/kernelapp.py", line 725, in start
      self.io_loop.start()
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/tornado/platform/asyncio.py", line 215, in start
      self.asyncio_loop.run_forever()
    File "/Users/ayushanand/miniforge3/lib/python3.9/asyncio/base_events.py", line 601, in run_forever
      self._run_once()
    File "/Users/ayushanand/miniforge3/lib/python3.9/asyncio/base_events.py", line 1905, in _run_once
      handle._run()
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/asyncio/events.py", line 80, in _run
      self._context.run(self._callback, *self._args)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 513, in dispatch_queue
      await self.process_one()
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 502, in process_one
      await dispatch(*args)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 409, in dispatch_shell
      await result
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/ipykernel/kernelbase.py", line 729, in execute_request
      reply_content = await reply_content
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/ipykernel/ipkernel.py", line 422, in do_execute
      res = shell.run_cell(
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/ipykernel/zmqshell.py", line 540, in run_cell
      return super().run_cell(*args, **kwargs)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 2961, in run_cell
      result = self._run_cell(
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3016, in _run_cell
      result = runner(coro)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
      coro.send(None)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3221, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3400, in run_ast_nodes
      if await self.run_code(code, result, async_=asy):
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3460, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "/var/folders/tb/pxp_kc851ts4tywxh3w8klgw0000gn/T/ipykernel_1905/1844586263.py", line 27, in <module>
      model.fit(X_train, y_train, epochs=100, verbose=0)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/keras/engine/training.py", line 1650, in fit
      tmp_logs = self.train_function(iterator)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/keras/engine/training.py", line 1249, in train_function
      return step_function(self, iterator)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/keras/engine/training.py", line 1233, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/keras/engine/training.py", line 1222, in run_step
      outputs = model.train_step(data)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/keras/engine/training.py", line 1027, in train_step
      self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 527, in minimize
      self.apply_gradients(grads_and_vars)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1140, in apply_gradients
      return super().apply_gradients(grads_and_vars, name=name)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 634, in apply_gradients
      iteration = self._internal_apply_gradients(grads_and_vars)
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1166, in _internal_apply_gradients
      return tf.__internal__.distribute.interim.maybe_merge_call(
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1216, in _distributed_apply_gradients_fn
      distribution.extended.update(
    File "/Users/ayushanand/miniforge3/lib/python3.9/site-packages/keras/optimizers/optimizer_experimental/optimizer.py", line 1211, in apply_grad_to_update_var
      return self._update_step_xla(grad, var, id(self._var_key(var)))
Node: 'StatefulPartitionedCall'
could not find registered platform with id: 0x1129b0ef0
     [[{{node StatefulPartitionedCall}}]] [Op:__inference_train_function_999]

解决方案

1. 禁用XLA加速

报错信息指向xla_ops.cc,说明是XLA(加速线性代数)模块与Metal插件的兼容性问题。在代码开头添加以下代码禁用XLA:

import tensorflow as tf
tf.config.optimizer.set_jit(False)  # 关闭XLA即时编译

2. 强制使用CPU运行

如果Metal插件的平台注册问题无法快速修复,可以强制TensorFlow使用CPU执行,避开GPU相关逻辑:

import tensorflow as tf
# 隐藏GPU设备,让TensorFlow默认使用CPU
tf.config.set_visible_devices([], 'GPU')

3. 匹配TensorFlow与Metal插件版本

确保tensorflow-macos和tensorflow-metal版本严格对应,版本不匹配会导致平台注册失败。执行以下命令查看当前版本:

pip list | grep tensorflow

例如,tensorflow-macos 2.12需要搭配tensorflow-metal 0.8,若版本不匹配,执行重新安装命令:

pip install tensorflow-macos==2.12 tensorflow-metal==0.8

4. 重启Jupyter内核与终端

环境变量或插件加载异常可能导致平台注册失效,重启Jupyter内核或终端后重新运行代码,可解决临时加载问题。

内容的提问来源于stack exchange,提问作者Ayush Anand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 05:37:01