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

TensorFlow训练模型报错:无法解析优化器标识符求助

TensorFlow Adam优化器 ValueError 解决方法

这个错误的核心原因是TensorFlow与Keras的导入路径不兼容——你混合使用了独立导入的Adam类和tf.keras的模型接口,在TensorFlow 2.10+版本(Keras独立拆分后)容易出现实例识别失效的问题。

以下是几种可行的解决方式:

  • 统一使用TensorFlow封装的Keras接口
    放弃单独导入Adam类,直接通过tf.keras.optimizers实例化优化器,确保整个流程的接口一致性:

    import tensorflow as tf
    
    # 直接用tf.keras的优化器实例化
    optimizer = tf.keras.optimizers.Adam(learning_rate=2e-5)
    
    model.compile(optimizer=optimizer,
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                  metrics=['accuracy'])
    
    model.fit(train_inputs, train_labels, epochs=epochs, batch_size=batch_size)
    
  • 传递优化器配置字典替代实例
    如果实例化方式还是报错,可以直接传入优化器的配置字典,让TensorFlow内部自行处理实例化:

    import tensorflow as tf
    
    model.compile(optimizer={'class_name': 'Adam', 'config': {'learning_rate': 2e-5}},
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                  metrics=['accuracy'])
    
    model.fit(train_inputs, train_labels, epochs=epochs, batch_size=batch_size)
    
  • 清理版本冲突
    若你同时安装了独立的keras包和TensorFlow,会导致接口混淆。执行以下命令卸载独立Keras:

    pip uninstall -y keras
    

内容的提问来源于stack exchange,提问作者mahdi rasoulzade

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 01:14:55