《Python深度学习》图书代码修改笔记
1. module ‘keras.optimizers’ has no attribute 'RMSprop’ 书本原始代码清单 5-6 如下:
from keras import optimizers
model.compile(
loss = 'binary_crossentropy',
optimizer = optimizers.RMSprop(lr = 1e-4),
metrics = ['acc'])
运行后会出现module ‘keras.optimizers’ has no attribute ‘RMSprop’,可以将代码进行如下修改后再运行,对于书中其他相同的问题可采用相同的方法处理。
from tensorflow.keras.optimizers import RMSprop
model.compile(optimizer = RMSprop(lr = 1e-4),
loss = 'binary_crossentropy',
metrics = ['acc'])
2. cannot import name ‘VGG16’ from 'keras.applications’ 书本原始代码清单 5-16 如下:
from keras.applications import VGG16
conv_base = VGG16(
weights = 'imagenet',
include_top = False,
input_shape = (150, 150, 3))
运行后会出现cannot import name ‘VGG16’ from ‘keras.applications’,可以将代码进行如下修改后再运行,对于书中其他的相同问题可采用相同的方法处理。
from keras.applications import vgg16
conv_base = vgg16.VGG16(
weights = 'imagenet',
include_top = False,
input_shape = (150, 150, 3))
3. tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead 【reference】https://stackoverflow.com/questions/66221788/tf-gradients-is-not-supported-when-eager-execution-is-enabled-use-tf-gradientta 书本原始代码清单 5-38 中有一行如下:
grads = K.gradients(loss, model.input)[0]
针对这一行代码存在的问题,可以在文件的开始位置添加如下代码:
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
更新日期:2021-08-18
|