我已经创建了一个Keras顺序模型并使用了Adam优化器。我想在每个时代之后获得学习率。这stackoverflow question似乎回答了我的问题。但是,当我按照上面提到的解决方案时,我收到以下错误
set_model() missing 1 required positional argument: 'model'
这是我创建模型的代码:
model = Sequential()
model.add(Conv2D(64, (5, 5), input_shape=(IMG_HEIGHT, IMG_WIDTH, 3), activation='relu'))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(128, (5, 5), activation='relu'))
model.add(Conv2D(128, (5, 5), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(256, (5, 5), activation='relu'))
model.add(Conv2D(256, (5, 5), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(BatchNormalization(axis=3))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.4,
min_lr=0.0001)
csvlogger = CSVLogger("solution.csv", separator='\t')
checkpoint = ModelCheckpoint("models/best_model5.h5", monitor="val_acc", save_best_only=True, mode='max')
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.4,
min_lr=0.00001)
class MyCallback(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
lr = self.model.optimizer.lr
decay = self.model.optimizer.decay
iterations = self.model.optimizer.iterations
lr_with_decay = lr / (1. + decay * K.cast(iterations, K.dtype(decay)))
print(K.eval(lr_with_decay))
model.fit_generator(datagen.flow(x_train, y_train, batch_size=75),
epochs=10, validation_data=(x_validation, y_test),verbose=1,
steps_per_epoch=x_train.shape[0], callbacks=[csvlogger, checkpoint, MyCallback])
如何通过此错误“set_model()缺少1个必需的位置参数:'model' “ 下面是堆栈跟踪
TypeError Traceback (most recent call last)
<ipython-input-12-1826a19039cd> in <module>()
128 model.fit_generator(datagen.flow(x_train, y_train, batch_size=75),
129 epochs=10, validation_data=(x_validation, y_test),verbose=1,
--> 130 steps_per_epoch=x_train.shape[0], callbacks=[csvlogger, checkpoint, MyCallback])
131 model.save('trained_model5.h5')
132
/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name +
90 '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
/usr/local/lib/python3.6/dist-packages/keras/models.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1274 use_multiprocessing=use_multiprocessing,
1275 shuffle=shuffle,
-> 1276 initial_epoch=initial_epoch)
1277
1278 @interfaces.legacy_generator_methods_support
/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name +
90 '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
2131 else:
2132 callback_model = self
-> 2133 callbacks.set_model(callback_model)
2134 callbacks.set_params({
2135 'epochs': epochs,
/usr/local/lib/python3.6/dist-packages/keras/callbacks.py in set_model(self, model)
50 def set_model(self, model):
51 for callback in self.callbacks:
---> 52 callback.set_model(model)
53
54 def on_epoch_begin(self, epoch, logs=None):
TypeError: set_model() missing 1 required positional argument: 'model'
另外,我的另一个问题是,上述解决方案是否正确。This tensorflow link about Adam Optimizer建议将学习率计算为:
lr_t&lt; - learning_rate * sqrt(1 - beta2 ^ t)/(1 - beta1 ^ t)
这似乎与其他链接中提到的解决方案截然不同。我错过了什么吗?
答案 0 :(得分:8)
实际上,在model.fit_generator方法的callbacks参数中,您传递的是类而不是该类的对象。
应该是
my_calback_object = MyCallback() # create an object of the MyCallback class
model.fit_generator(datagen.flow(x_train, y_train, batch_size=75),
epochs=10, validation_data=(x_validation, y_test),
verbose=1, steps_per_epoch=x_train.shape[0],
callbacks=[csvlogger, checkpoint, my_callback_object])
答案 1 :(得分:0)
这更像是一个评论,它只是不合适。
很奇怪。 set_model
继承的MyCallback
的默认实现是:
def set_model(self, model):
self.model = model
这正是根据堆栈跟踪调用的方式:
/usr/local/lib/python3.6/dist-packages/keras/callbacks.py in set_model(self, model)
50 def set_model(self, model):
51 for callback in self.callbacks:
---> 52 callback.set_model(model)
我在这一点上唯一的猜测是你的系统上有一些版本不匹配。你也可能有一些旧的.pyc。我会尝试直接编辑/usr/local/lib/python3.6/dist-packages/keras/callbacks.py来调试它。例如,在第52行之前添加一个print语句,以确保此代码真正运行。然后,放入pdb
(添加import pdb; pdb.set_trace
)并检查它为什么抱怨。这是一个基本的python问题。
答案 2 :(得分:0)
该错误表示您未在位置1(从0开始)提供参数的值,该值称为“模型”。调用者是Tensorflow本身,因此故障很可能不存在。
此错误表明您正在调用静态方法而不是对象上的方法,因此仅传递1个参数而不是2个参数。这是因为在对象上调用方法时,该对象作为第一个参数传递和“模型”将作为第二个参数传递。
简而言之,您的错误是您的回调是“类”而不是“对象”。确保您提供的是回调类的实例,而不是类本身。
像这样(注意“ MyCallback”之后的括号):
model.fit_generator(datagen.flow(x_train, y_train, batch_size=75),
epochs=10, validation_data=(x_validation, y_test),verbose=1,
steps_per_epoch=x_train.shape[0], callbacks=[csvlogger,
checkpoint, MyCallback()])