> ## Content Index
> Fetch the complete content index at: https://yinguobing.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# TensorFlow 2冻结模型注意事项
- URL: https://yinguobing.com/blog/note-on-model-freezing-with-tensorflow/
- Published: 2021-07-01T08:14:18.000Z
- Updated: 2026-09-08T02:06:40.000Z
- Description: 冻结模型报错，可能是这个原因。
- Author: 尹国冰
- Tags: TensorFlow

今天在尝试冻结一个TensorFlow模型时遇到错误：

```bash
ValueError: Input 0 of node embedding_model/resnet50v2/conv2_block1_preact_bn/AssignNewValue/AssignVariableOp was passed float from embedding_model/resnet50v2/conv2_block1_preact_bn/FusedBatchNormV3/ReadVariableOp/resource:0 incompatible with expected resource.
```

冻结模型的代码来自这里：

[Save, Load and Inference From TensorFlow 2.x Frozen GraphHello Underworld.![](https://leimao.github.io/favicon/apple-touch-icon.png)Lei Mao's Log BookTwitter![](https://leimao.github.io/favicon/apple-touch-icon.png)](https://leimao.github.io/blog/Save-Load-Inference-From-TF2-Frozen-Graph/?ref=yinguobing.com)

模型本身是基于TensorFlow的 keras.app，所以模型出问题的可能性很小。多番尝试之后终于找到了问题所在。

keras模型可以自动判断训练与推演状态。但是我在模型推演命令中强制假如了 `training=True` 选项。导致冻结模型时，模型处于训练状态。

解决方案也很简单，将 `training=False` 即可。当然，你也可以在一开始就避免手动设置 `training` 参数。