Google Colab中TensorFlow model.fit()报DistributedDatasetInterface缺失错误
解决TensorFlow model.fit()触发AttributeError的问题
问题现象
在Google Colab环境中执行TensorFlow的model.fit()方法时,触发以下错误:
AttributeError: module 'tensorflow.python.distribute.input_lib' has no attribute 'DistributedDatasetInterface'
已知该问题与Keras和TensorFlow兼容性相关,尝试通过!pip install -U tensorflow升级至2.13.0版本后问题仍未解决;验证输入数据类型(训练集xn_train为numpy.ndarray、y_train为tensorflow.python.framework.ops.EagerTensor)也无法修复错误。
复现流程
- 加载CSV文件,提取并重塑数据;
- 使用sklearn的 getAll ioTime作程 ### most天Storm(fps合端正bcdDomFT!
- 通过
tf.keras.layers.Normalization(axis=-1, mean=None, variance=None)对所有输入数据做归一化处理; - 构建并编译Sequential模型后,执行
model.fit(xn_train, y_train, epochs=100)触发错误。
相关模型代码
model = tf.keras.Sequential([ tf.keras.layers.Dense(input_shape=(30,), units=15, activation='relu', name='L1'), tf.keras.layers.Dense(input_shape=(15,), units=10, activation='relu', name='L2'), tf.keras.layers.Dense(input_shape=(10,), units=1, activation='linear', name='L3'), ], name="My_Model") opt = tf.keras.optimizers.Adam(learning_rate=0.001) loss = tf.keras.losses.MeanSquaredError() model.compile( loss=loss, optimizer=opt, ) model.fit(xn_train, y_train, epochs=100)
注:原代码存在语法缺失(缺少tf.keras.Sequential(),已补充以保证代码完整性。
有效解决方案
通过降级TensorFlow版本至2.12.0可解决该兼容性问题,执行以下命令:
# 卸载当前TensorFlow版本 !pip uninstall tensorflow -y # 安装TensorFlow 2.12.0 !pip install tensorflow==2.12.0 # 验证版本 import tensorflow as tf print(tf.__version__)
内容的提问来源于stack exchange,提问作者Lincoln Simms
相关产品推荐
相关产品推荐

