MacBook M1 Pro运行TensorFlow出现NotFoundError: Graph execution error求助
M1 Max MacBook Pro上TensorFlow LSTM训练出现
NotFoundError: Graph execution error的问题 问题描述
我在MacBook Pro M1 Max上通过Anaconda安装依赖:
conda install -c apple tensorflow-deps
随后安装适配M1架构的TensorFlow及Metal GPU工具包:
pip install tensorflow-metal tensorflow-macos
编写了一个包含LSTM的简单网络,并用虚拟数据测试训练:
from tensorflow.keras.models import Sequential from tensorflow.keras.optimizers import Adam from tensorflow.keras import layers import numpy as np model = Sequential([layers.Input((3, 1)), layers.LSTM(64), layers.Dense(32, activation='relu'), layers.Dense(32, activation='relu'), layers.Dense(1)]) model.compile(loss='mse', optimizer=Adam(learning_rate=0.001), metrics=['mean_absolute_error']) X_train = np.random.rand(100,3) y_train = np.random.rand(100) X_val = np.random.rand(100,3) y_val = np.random.rand(100) model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=100)
执行时触发NotFoundError: Graph execution error,错误栈核心内容:
NotFoundError: Graph execution error: could not find registered platform with id: 0x1056be9e0 [[{{node StatefulPartitionedCall_7}}]] [Op:__inference_train_function_4146]
明明网络结构很简单,训练却无法执行,求解决办法。
问题原因及解决办法
1. 输入维度不匹配
模型输入定义为layers.Input((3, 1)),期望输入形状是(样本数, 时间步长, 特征数),但生成的X_train是(100,3),缺少特征维度。需要给输入数据扩充一个维度:
X_train = np.random.rand(100,3,1) # 修改为(100,3,1) y_train = np.random.rand(100) X_val = np.random.rand(100,3,1) # 验证集同样修改 y_val = np.random.rand(100)
2. Metal GPU版本兼容问题
错误提示中的平台ID找不到,大概率是tensorflow-metal和tensorflow-macos版本不兼容导致的:
- 先卸载现有包:
pip uninstall -y tensorflow-metal tensorflow-macos - 查看Apple官方提供的
tensorflow-deps与tensorflow-macos、tensorflow-metal的版本对应关系,安装匹配的版本,示例:pip install tensorflow-macos==2.15.0 tensorflow-metal==0.11.0
3. Anaconda环境依赖冲突
现有conda环境可能存在依赖冲突,建议创建全新环境:
conda create -n tf-m1 python=3.9 conda activate tf-m1 conda install -c apple tensorflow-deps pip install tensorflow-macos tensorflow-metal
内容的提问来源于stack exchange,提问作者rayryeng
相关产品推荐
相关产品推荐

