KerasのLearningRateScheduler(Callback)クラスを自作したいのですが,元のKerasのコードをみて,以下の様な問題があります.
Python
1class LearningRateScheduler(Callback): 2 """Learning rate scheduler. 3 # Arguments 4 schedule: a function that takes an epoch index as input 5 (integer, indexed from 0) and current learning rate 6 and returns a new learning rate as output (float). 7 verbose: int. 0: quiet, 1: update messages. 8 """ 9 10 11 def __init__(self, schedule, verbose=0): 12 super(LearningRateScheduler, self).__init__() 13 self.schedule = schedule 14 self.verbose = verbose 15 16 def on_epoch_begin(self, epoch, logs=None): 17 if not hasattr(self.model.optimizer, 'lr'): 18 raise ValueError('Optimizer must have a "lr" attribute.') 19 lr = float(K.get_value(self.model.optimizer.lr)) 20 try: # new API 21 lr = self.schedule(epoch, lr) 22 except TypeError: # old API for backward compatibility 23 lr = self.schedule(epoch) 24 if not isinstance(lr, (float, np.float32, np.float64)): 25 raise ValueError('The output of the "schedule" function ' 26 'should be float.') 27 K.set_value(self.model.optimizer.lr, lr) 28 if self.verbose > 0: 29 print('\nEpoch %05d: LearningRateScheduler setting learning ' 30 'rate to %s.' % (epoch + 1, lr)) 31 32 def on_epoch_end(self, epoch, logs=None): 33 logs = logs or {} 34 logs['lr'] = K.get_value(self.model.optimizer.lr)
**lr = float(K.get_value(self.model.optimizer.lr))**コードでは,
selfを用い,「他クラスのインスタンスの属性を得ている」様に見えますが,この様な理解で宜しいですか?
スーパークラスのinit()にself.model = Noneコードがありました.
結論として,『selfを用い,「他クラスのインスタンスの属性を得ている」』ではないです.
以下スーパークラスのCallback(object)となります.
**lr = self.schedule(epoch, lr)**コードでは,
selfを用い,得た属性の後ろに又属性を加えているのですが(関数の使用に似ているのですが),この様な使い方の意味は,どの様なものでしょうか?
宜しくお願い致します.
Python
1class Callback(object): 2 """Abstract base class used to build new callbacks. 3 4 # Properties 5 params: dict. Training parameters 6 (eg. verbosity, batch size, number of epochs...). 7 model: instance of `keras.models.Model`. 8 Reference of the model being trained. 9 10 The `logs` dictionary that callback methods 11 take as argument will contain keys for quantities relevant to 12 the current batch or epoch. 13 14 Currently, the `.fit()` method of the `Sequential` model class 15 will include the following quantities in the `logs` that 16 it passes to its callbacks: 17 18 on_epoch_end: logs include `acc` and `loss`, and 19 optionally include `val_loss` 20 (if validation is enabled in `fit`), and `val_acc` 21 (if validation and accuracy monitoring are enabled). 22 on_batch_begin: logs include `size`, 23 the number of samples in the current batch. 24 on_batch_end: logs include `loss`, and optionally `acc` 25 (if accuracy monitoring is enabled). 26 """ 27 28 def __init__(self): 29 self.validation_data = None 30 self.model = None 31 32 def set_params(self, params): 33 self.params = params 34 35 def set_model(self, model): 36 self.model = model 37 38 def on_epoch_begin(self, epoch, logs=None): 39 pass 40 41 def on_epoch_end(self, epoch, logs=None): 42 pass 43 44 def on_batch_begin(self, batch, logs=None): 45 pass 46 47 def on_batch_end(self, batch, logs=None): 48 pass 49 50 def on_train_begin(self, logs=None): 51 pass 52 53 def on_train_end(self, logs=None): 54 pass 55 56
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/07 11:55
2018/09/07 12:04
2018/09/07 12:06
2018/09/07 12:10
2018/09/07 12:53
2018/09/07 18:34
2018/09/08 01:08
2018/09/08 08:44