Tensorflow踩坑笔记,记录各种报错和解决方法

不知道是不是错觉,tensorflow比pytorch容易报错,记录一下踩的坑。
随时更新。

0 当出现报错,首先把Jupyter notebook代码换成.py文件直接运行

当出现莫名其妙的报错时,如果你当前使用的是Jupyter notebook,先换成.py文件直接运行试试看。
尤其是gpu版本的tensorflow在Jupyter下容易出问题。

1 Loaded runtime CuDNN library: 7.4.1 but source was compiled with: 7.6.0

初始环境:tensorflow2.0(gpu)、CUDA 10.0、Cudnn 7.4.1、Windows 10

训练卷积神经网络(使用Conv2d类时)报错:

Loaded runtime CuDNN library: 7.4.1 but source was compiled with: 7.6.0.

然而官网的 tensorflow2.0(gpu)的环境要求却写了CUDA 10.0、Cudnn 7.4,误导作用明显。
报错直接提示了解决方案,把Cudnn升级到7.6.0即可,升级方法和安装Cudnn方法相同,去官网下载对应的Cudnn版本,覆盖源文件即可。也可以参考下面的参考资料。

参考资料:
版本不匹配||Loaded runtime CuDNN library: 7.4.2 but source was compiled with: 7.6.0. CuDNN library major

2 自定义的模型不能保存

环境:tensorflow2.0(gpu)、CUDA 10.0、Cudnn 7.6.0、Windows 10

使用tf.keras.models.save_model(model, 'path')保存模型时报错:

ValueError: Model <resnet.Resnet object at 0x000001A9E61DBC50> cannot be saved because the input shapes have not been set. Usually, input shapes are automatically determined from calling .fit() or .predict(). To manually set the shapes, call model._set_inputs(inputs).

参考资料:
1.stackoverflow
2.Github: Model cannot be saved because the input shapes have not been set. #39906

我的解决方案:
model初始化后,在build()中指定input_shape后,再写上model.compute_output_shape(input_shape=(None, 32, 32, 3))即可。
在这里插入图片描述
可以保存了:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值