我开始研究神经网络。我正在复习《动手》一书第10章的练习10 使用 Scikit-Learn、Keras 和 TensorFlow 进行机器学习,第三版。我正在尝试运行 github 中显示的答案,但出现错误: istory = model.fit(X_train, y_train, epochs=1, 验证数据=(X_有效,y_有效), 回调=[expon_lr])
K = keras.backend
class ExponentialLearningRate(keras.callbacks.Callback):
def __init__(self, factor):
self.factor = factor
self.rates = []
self.losses = []
def on_batch_end(self, batch, logs):
self.rates.append(K.get_value(self.model.optimizer.learning_rate))
self.losses.append(logs["loss"])
K.set_value(self.model.optimizer.learning_rate, self.model.optimizer.learning_rate * self.factor)
keras.backend.clear_session()
np.random.seed(42)
tf.random.set_seed(42)
model = keras.models.Sequential([
keras.layers.Flatten(input_shape=[28, 28]), # Capa de entrada
keras.layers.Dense(300, activation="relu"), # Primera capa oculta
keras.layers.Dense(200, activation="relu"), # Segunda capa oculta
keras.layers.Dense(10, activation="softmax") # Capa de salida
])
model.compile(loss="sparse_categorical_crossentropy",
optimizer=keras.optimizers.SGD(learning_rate=1e-3),
metrics=["accuracy"])
expon_lr = ExponentialLearningRate(factor=1.005)
history = model.fit(X_train, y_train, epochs=1,
validation_data=(X_valid, y_valid),
callbacks=[expon_lr])
plt.plot(expon_lr.rates, expon_lr.losses)
plt.gca().set_xscale('log')
plt.hlines(min(expon_lr.losses), min(expon_lr.rates), max(expon_lr.rates))
plt.axis([min(expon_lr.rates), max(expon_lr.rates), 0, expon_lr.losses[0]])
plt.grid()
plt.xlabel("Learning rate")
plt.ylabel("Loss")
错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-14-a0cc0ada5ef0> in <cell line: 1>()
----> 1 history = model.fit(X_train, y_train, epochs=1,
2 validation_data=(X_valid, y_valid),
3 callbacks=[expon_lr])
1 frames
<ipython-input-6-f87513637cd3> in on_batch_end(self, batch, logs)
9 self.rates.append(K.get_value(self.model.optimizer.learning_rate))
10 self.losses.append(logs["loss"])
---> 11 K.set_value(self.model.optimizer.learning_rate, self.model.optimizer.learning_rate * self.factor)
AttributeError: 'str' object has no attribute 'name'
我希望它能像我从 github 上复制并粘贴代码一样工作。 ¿也许是因为tensorflow版本的原因?
聊天gpt解决了它:
import tensorflow as tf
from tensorflow import keras
# Callback personalizado
class ExponentialLearningRate(keras.callbacks.Callback):
def __init__(self, factor):
super().__init__()
self.factor = factor
self.rates = []
self.losses = []
def on_batch_end(self, batch, logs=None):
logs = logs or {}
self.losses.append(logs.get("loss", None))
# Obtener el valor de learning_rate y asegurarnos de que sea un número
lr = self.model.optimizer.learning_rate
if isinstance(lr, tf.Variable):
lr = lr.numpy() # Convertir a valor numérico si es un tf.Variable
# Registrar la tasa de aprendizaje
self.rates.append(lr)
# Actualizar el learning rate
if isinstance(lr, (float, int)):
new_lr = lr * self.factor
self.model.optimizer.learning_rate.assign(new_lr) # Modificar learning_rate
# Modelo simple
model = keras.models.Sequential([
keras.layers.Flatten(input_shape=[28, 28]),
keras.layers.Dense(300, activation="relu"),
keras.layers.Dense(100, activation="relu"),
keras.layers.Dense(10, activation="softmax")
])
# Compilar modelo
model.compile(
loss="sparse_categorical_crossentropy",
optimizer=keras.optimizers.SGD(learning_rate=1e-3),
metrics=["accuracy"]
)
# Crear el callback
expon_lr = ExponentialLearningRate(factor=1.005)
# Ajustar modelo
history = model.fit(
X_train, y_train,
epochs=1,
validation_data=(X_valid, y_valid),
callbacks=[expon_lr]
)