Calling `Model.fit` in graph mode is not supported when the `Model` instance was constructed with eager mode enabled. Please construct your `Model` instance in graph mode or call `Model.fit` with eager mode enabled.
时间: 2024-03-27 13:40:46 浏览: 38
这个错误通常是因为你在创建模型实例时启用了 Eager Execution(即 `tf.enable_eager_execution()`),但是在调用 `Model.fit` 方法时,却使用了 Graph Execution。这两种 Execution Mode 是不兼容的,你需要在创建模型实例时选择使用 Graph Execution,或者在调用 `Model.fit` 方法时启用 Eager Execution。
如果你使用的是 Keras API,你可以在创建模型实例时设置 `tf.compat.v1.disable_eager_execution()` 来禁用 Eager Execution,例如:
```
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
model = tf.keras.Sequential([...])
```
如果你使用的是 TensorFlow 2.0 版本以上的 API,你也可以在创建模型实例时使用 `tf.function` 来显式地指定使用 Graph Execution,例如:
```
import tensorflow as tf
@tf.function
def create_model():
model = tf.keras.Sequential([...])
return model
model = create_model()
```
希望这能帮到你解决问题!
相关问题
Starting CoreML export with coremltools 6.3.0... Model is not in eval mode. Consider calling '.eval()' on your model prior to conversion CoreML export failure: module 'torch.jit' has no attribute '_script'
这个错误提示信息意味着 PyTorch 的 TorchScript 模块无法正常使用。在导出 CoreML 模型时,coremltools 会调用 TorchScript 来序列化 PyTorch 模型,但是出现了 `_script` 属性不存在的错误。
这个问题的解决方法是更新 PyTorch 和 torch.jit 模块到最新版本。你可以使用以下命令来更新:
```
pip install torch -U
```
更新完成后,再次尝试导出 CoreML 模型,应该就可以成功了。如果还有其他问题,可以尝试升级 coremltools 模块和其他相关依赖。
Calling Family(..) with a link class is not allowed. Use an instance of a link class instead.
非常抱歉,我的前一条回答仍然存在错误。在使用 `sm.families.Binomial()` 拟合二项分布模型时,需要使用 `sm.families.links.Probit()` 指定Probit链接函数,而不是 `sm.families.links.probit`。因此,正确的写法应该是:
```
prob_fit = sm.GLM(y, x, family=sm.families.Binomial(link=sm.families.links.Probit())).fit()
```
请注意,这里的 `sm.families.links.Probit()` 实际上是创建了一个Probit链接函数的实例,而不是一个字符串。这样就应该可以正确地拟合一个基于Probit的二项分布模型了,非常抱歉之前给您造成的困扰。