diff --git a/scheduled_sampling/random_schedule_generator.py b/scheduled_sampling/random_schedule_generator.py index 046dce63520aa961ace5b538305b6134ee042a79..7569eaffc29ab0abda3e44764f2ccf647b8b306e 100644 --- a/scheduled_sampling/random_schedule_generator.py +++ b/scheduled_sampling/random_schedule_generator.py @@ -1,21 +1,20 @@ import numpy as np import math -import pdb -''' -The random sampling rate for scheduled sampling algoithm, which uses devcayed -sampling rate. -''' class RandomScheduleGenerator: - ''' - schduled_type: is the type of the decay. It supports constant, linear, - exponential, and inverse_sigmoid right now. - a: parameter of the decay (MUST BE DOUBLE) - b: parameter of the decay (MUST BE DOUBLE) - ''' + """ + The random sampling rate for scheduled sampling algoithm, which uses devcayed + sampling rate. + """ def __init__(self, schedule_type, a, b): + """ + schduled_type: is the type of the decay. It supports constant, linear, + exponential, and inverse_sigmoid right now. + a: parameter of the decay (MUST BE DOUBLE) + b: parameter of the decay (MUST BE DOUBLE) + """ self.schedule_type = schedule_type self.a = a self.b = b @@ -24,33 +23,25 @@ class RandomScheduleGenerator: "constant": lambda a, b, d: a, "linear": lambda a, b, d: max(a, 1 - d / b), "exponential": lambda a, b, d: pow(a, d / b), - "inverse_sigmoid": lambda a, b, d: b / (b + exp(d * a / b)), + "inverse_sigmoid": lambda a, b, d: b / (b + math.exp(d * a / b)), } assert (self.schedule_type in self.schedule_computers) self.schedule_computer = self.schedule_computers[self.schedule_type] - ''' - Get the schedule sampling rate. Usually not needed to be called by the users - ''' - def getScheduleRate(self): + """ + Get the schedule sampling rate. Usually not needed to be called by the users + """ return self.schedule_computer(self.a, self.b, self.data_processed_) - ''' - Get a batch_size of sampled indexes. These indexes can be passed to a - MultiplexLayer to select from the grouth truth and generated samples - from the last time step. - ''' - def processBatch(self, batch_size): + """ + Get a batch_size of sampled indexes. These indexes can be passed to a + MultiplexLayer to select from the grouth truth and generated samples + from the last time step. + """ rate = self.getScheduleRate() numbers = np.random.rand(batch_size) indexes = (numbers >= rate).astype('int32').tolist() self.data_processed_ += batch_size return indexes - - -if __name__ == "__main__": - schedule_generator = RandomScheduleGenerator("linear", 0.1, 500000) - true_token_flag = schedule_generator.processBatch(5) - pdb.set_trace() diff --git a/scheduled_sampling/scheduled_sampling.py b/scheduled_sampling/scheduled_sampling.py index e641c44846d4a3ebb47190ef2a9293a9ecfd2f73..1dead8969a2f5579102731d62de241afe96e0996 100644 --- a/scheduled_sampling/scheduled_sampling.py +++ b/scheduled_sampling/scheduled_sampling.py @@ -74,7 +74,7 @@ def seqToseq_net(source_dict_dim, target_dict_dim, is_generating=False): decoder_state=decoder_mem) gru_out_memory = paddle.layer.memory( - name='gru_out', size=target_dict_dim) # , boot_with_const_id=0) + name='gru_out', size=target_dict_dim) generated_word = paddle.layer.max_id(input=gru_out_memory)