Set up the batch generator for a new epoch.
486655
def init_epoch(self):
        """"""

        if self._restored_from_state:
            self.random_shuffler.random_state = self._random_state_this_epoch
        else:
            self._random_state_this_epoch = self.random_shuffler.random_state

        self.create_batches()

        if self._restored_from_state:
            self._restored_from_state = False
        else:
            self._iterations_this_epoch = 0

        if not self.repeat:
            self.iterations = 0
1292090
def __batch_update(self, train_events, test_events, n_epoch):
        
        for epoch in range(n_epoch):
            # SGD requires us to shuffle events in each iteration
            # * if n_epoch == 1
            #   => shuffle is not required because it is a deterministic training (i.e. matrix sketching)
            if n_epoch != 1:
                np.random.shuffle(train_events)

            # train
            for e in train_events:
                self.rec.update(e, batch_train=True)

            # test
            MPR = self.__batch_evaluate(test_events)
            if self.debug:
                logger.debug('epoch %2d: MPR = %f' % (epoch + 1, MPR))
1366537
def set_generator_training_nb(self, number):
        

        self.samples_per_epoch = number
        diff_to_batch = number % self.get_batch_size()
        if diff_to_batch > 0:
            self.samples_per_epoch += self.get_batch_size() - diff_to_batch
94278
def _replace_batch(self):
        """"""
        if not self._head:
          self._head, self._buffer = self._record(self._buffer[0], 1, 0, None), self._buffer[1:]

        rank = 0.0
        current = self._head

        for b in self._buffer:
            if b < self._head._value:
              self._head = self._record(b, 1, 0, self._head)

            while current._successor and current._value < b:
                rank += current._rank
                current = current._successor

            if not current._successor:
                current._successor = self._record(b, 1, 0, None)

            current._successor = self._record(b, 1, self._invariant(rank, self._observations)-1, current._successor)
1279217
def execute_epoch(self, epoch_info, learner):
        
        for param_group in epoch_info.optimizer.param_groups:
            param_group['lr'] = self.lr

        epoch_result = learner.run_epoch(epoch_info, self._source)

        return epoch_result
683963
def set_state_batch(keys, values, namespace=None, ttl=3600*24*365):
    """"""

    keys, values = zip(*{k: v for k, v in zip(keys, values)}.items())

    if namespace:
        keys = ["{}:{}".format(namespace, key) for key in keys]

    return set_item_batch(keys, values, ttl)
1121370
def simple_generate_batch(klass, create, size, **kwargs):
    """"""
    return make_factory(klass, **kwargs).simple_generate_batch(create, size)
700529
def _create_epoch_data(self, streams: Optional[Iterable[str]]=None) -> EpochData:
        """"""
        if streams is None:
            streams = [self._train_stream_name] + self._extra_streams
        return OrderedDict([(stream_name, OrderedDict()) for stream_name in streams])
1067940
def increment(self, batch_size):
        """"""
        self.example_count += batch_size
        self.example_total += batch_size
        if self.log_unit == "seconds":
            self.unit_count = int(self.timer.elapsed())
            self.unit_total = int(self.timer.total_elapsed())
        elif self.log_unit == "examples":
            self.unit_count = self.example_count
            self.unit_total = self.example_total
        elif self.log_unit == "batches":
            self.unit_count += 1
            self.unit_total += 1
        elif self.log_unit == "epochs":
            # Track epoch by example count because otherwise we only know when
            # a new epoch starts, not when an epoch ends
            if self.example_count >= self.epoch_size:
                self.unit_count += 1
                self.unit_total += 1
        else:
            raise Exception(f"Unrecognized log_unit: {self.log_unit}")
595442
def initialize(self, params, repetition):
    
    self.name = params["name"]
    self.dataDir = params.get("datadir", "data")
    self.seed = params.get("seed", 42) + repetition

    torch.manual_seed(self.seed)
    np.random.seed(self.seed)
    random.seed(self.seed)

    # Training
    self.epochs = params.get("epochs", 1)
    self.batch_size = params.get("batch_size", 16)
    self.batches_in_epoch = params.get("batches_in_epoch", 60000)
    self.first_epoch_batch_size = params.get("first_epoch_batch_size", self.batch_size)
    self.batches_in_first_epoch = params.get("batches_in_first_epoch", self.batches_in_epoch)

    # Testing
    self.test_batch_size = params.get("test_batch_size", 1000)

    # Optimizer
    self.optimizer_class = eval(params.get("optimizer", "torch.optim.SGD"))
    self.optimizer_params = eval(params.get("optimizer_params", "{}"))
    self.lr_scheduler_class = eval(params.get("lr_scheduler", None))
    self.lr_scheduler_params = eval(params.get("lr_scheduler_params", "{}"))
    self.loss_function = eval(params.get("loss_function", "torch.nn.functional.nll_loss"))

    # CNN parameters
    c, h, w = map(int, params.get("input_shape", "1_28_28").split("_"))
    self.in_channels = c
    self.out_channels = map(int, params.get("out_channels", "30_30").split("_"))
    self.kernel_size = map(int, params.get("kernel_size", "5_5").split("_"))
    self.stride = map(int, params.get("stride", "1_1").split("_"))
    self.padding = map(int, params.get("padding", "0_0").split("_"))

    # Compute Flatten CNN output len
    self.maxpool = []
    self.maxpool.append(
      ((w + 2 * self.padding[0] - self.kernel_size[0]) // self.stride[0] + 1) // 2)
    self.maxpool.append(
      ((self.maxpool[0] + 2 * self.padding[1] - self.kernel_size[1]) // self.stride[1] + 1) // 2)

    self.cnn_output_len = [self.maxpool[i] * self.maxpool[i] * self.out_channels[i]
                           for i in range(len(self.maxpool))]

    # Linear parameteers
    self.n = params.get("n", 1000)
    self.output_size = params.get("output_size", 10)

    # Sparse parameters
    if "c1_k" in params:
      self.cnn_k = map(int, params["c1_k"].split("_"))
    else:
      self.cnn_k = self.cnn_output_len

    self.k = params.get("k", self.n)
    self.k_inference_factor = params.get("k_inference_factor", 1.0)
    self.boost_strength = params.get("boost_strength", 1.0)
    self.boost_strength_factor = params.get("boost_strength_factor", 1.0)
    self.weight_sparsity = params.get("weight_sparsity", 1.0)
    self.weight_sparsity_cnn = params.get("weight_sparsity_cnn", 1.0)
795771
def on_batch_begin(self, train, **kwargs:Any)->None:
        ""
        if train:
            self.lrs.append(self.opt.lr)
            self.moms.append(self.opt.mom)
1228671
def initialize(self, params, repetition):
    
    self.name = params["name"]
    self.dataDir = params.get("datadir", "data")
    self.seed = params.get("seed", 42) + repetition

    torch.manual_seed(self.seed)
    np.random.seed(self.seed)
    random.seed(self.seed)

    # Training
    self.epochs = params.get("epochs", 1)
    self.batch_size = params.get("batch_size", 128)
    self.batches_in_epoch = params.get("batches_in_epoch", 100000)
    self.first_epoch_batch_size = params.get("first_epoch_batch_size",
                                             self.batch_size)
    self.batches_in_first_epoch = params.get("batches_in_first_epoch",
                                             self.batches_in_epoch)

    # Testing
    self.test_batch_size = params.get("test_batch_size", 1000)
    self.test_batches_in_epoch = params.get("test_batches_in_epoch", 100000)
    self.noise_values = map(float,
                            params.get("noise_values", "0.0, 0.1").split(", "))
    self.best_noise_score = 0.0
    self.best_epoch = -1

    # Optimizer
    self.optimizer_class = eval(params.get("optimizer", "torch.optim.SGD"))
    self.lr = params.get("learning_rate", 0.05)
    self.momentum = params.get("momentum", 0.9)
    self.weight_decay = params.get("weight_decay", 0.0)
    self.optimizer_params = eval(params.get("optimizer_params", "{}"))
    self.lr_scheduler_gamma = params.get("lr_scheduler_gamma", 0.9)
    self.loss_function = eval(params.get("loss_function",
                                         "torch.nn.functional.cross_entropy"))

    # Network parameters
    self.conv1_sparsity = params.get("conv1_sparsity", 1.0)
    self.network_type = params.get("network_type", "sparse")
    self.growth_rate = params.get("growth_rate", 12)
    self.nblocks = map(int,
                       params.get("nblocks", "6, 12, 24, 16").split(", "))
    self.k_inference_factor = params.get("k_inference_factor", 1.5)

    self.dense_sparsities = map(float,
                                params.get("dense_sparsities",
                                           "1.0, 1.0, 1.0, 1.0").split(", "))
    self.transition_sparsities = map(float,
                                     params.get("transition_sparsities",
                                                "0.1, 0.1, 0.2").split(", "))
    self.linear_sparsity = params.get("linear_sparsity", 0.0)
    self.linear_weight_sparsity = params.get("linear_weight_sparsity", 0.3)
    self.linear_n = params.get("linear_n", 500)
    self.avg_pool_size = params.get("avg_pool_size", 2)
    self.dense_c1_out_planes = params.get("dense_c1_out_planes", 4*self.growth_rate)
664116
def new_run(self):
        """"""
        self.current_run += 1
        self.runs.append(RunData(self.current_run + 1))
636193
def _build(self):
    """"""
    q = tf.FIFOQueue(
        self._queue_capacity, [self._dtype, self._dtype],
        shapes=[[self._num_steps, self._batch_size, self._vocab_size]]*2)
    obs, target = tf.py_func(self._get_batch, [], [tf.int32, tf.int32])
    obs = self._one_hot(obs)
    target = self._one_hot(target)
    enqueue_op = q.enqueue([obs, target])
    obs, target = q.dequeue()
    tf.train.add_queue_runner(tf.train.QueueRunner(q, [enqueue_op]))
    return SequenceDataOpsNoMask(obs, target)
1190744
def init_rng(self):
        
        rng = torch.Generator()
        seed = self.seeds[self.epoch]
        logging.info(f'Sampler for epoch {self.epoch} uses seed {seed}')
        rng.manual_seed(seed)
        return rng
924975
def batch_iter(data, batch_size, num_epochs):
    """"""
    data = np.array(data)
    data_size = len(data)
    num_batches_per_epoch = int(len(data)/batch_size) + 1
    for epoch in range(num_epochs):
        # Shuffle the data at each epoch
        shuffle_indices = np.random.permutation(np.arange(data_size))
        shuffled_data = data[shuffle_indices]
        for batch_num in range(num_batches_per_epoch):
            start_index = batch_num * batch_size
            end_index = min((batch_num + 1) * batch_size, data_size)
            yield shuffled_data[start_index:end_index]
734111
def setup_run(self):
        
        super(ContainerState, self).setup_run()
        # reset the scoped data
        self._scoped_data = {}
        self._start_state_modified = False
        self.add_default_values_of_scoped_variables_to_scoped_data()
        self.add_input_data_to_scoped_data(self.input_data)