@application(outputs=['weighted_averages', 'weights', 'energies', 'step'])
    def take_glimpses(self, attended, preprocessed_attended=None,
                      attended_mask=None, weights=None, step=None, **states):
        # Cut the considered window.
        p = self.prior
        length = attended.shape[0]
        prior_type = p.get('type', 'expanding')
        if prior_type=='expanding':
            begin = p['initial_begin'] + step[0] * p['min_speed']
            end = p['initial_end'] + step[0] * p['max_speed']
            begin = tensor.maximum(0, tensor.minimum(length - 1, begin))
            end = tensor.maximum(0, tensor.minimum(length, end))
            additional_mask = None
        elif prior_type.startswith('window_around'):
            #check whether we want the mean or median!
            if prior_type == 'window_around_mean':
                position_in_attended = tensor.arange(length, dtype=floatX)[None, :]
                expected_last_source_pos = (weights * position_in_attended).sum(axis=1)
            elif prior_type == 'window_around_median':
                ali_to_05 = tensor.extra_ops.cumsum(weights, axis=1) - 0.5
                ali_to_05 = (ali_to_05>=0)
                ali_median_pos = ali_to_05[:,1:] - ali_to_05[:,:-1]
                expected_last_source_pos = tensor.argmax(ali_median_pos, axis=1)
                expected_last_source_pos = theano.gradient.disconnected_grad(
                    expected_last_source_pos)
            else:
                raise ValueError
            #the window taken around each element
            begins = tensor.floor(expected_last_source_pos - p['before'])
            ends = tensor.ceil(expected_last_source_pos + p['after'])
            #the global window to optimize computations
            begin = tensor.maximum(0, begins.min()).astype('int64')
            end = tensor.minimum(length, ends.max()).astype('int64')
            #the new mask, already cut to begin:end
            position_in_attended_cut = tensor.arange(
                begin * 1., end * 1., 1., dtype=floatX)[None, :]
            additional_mask = ((position_in_attended_cut > begins[:,None]) *
                               (position_in_attended_cut < ends[:,None]))
        else:
            raise Exception("Unknown prior type: %s", prior_type)
        begin = tensor.floor(begin).astype('int64')
        end = tensor.ceil(end).astype('int64')
        attended_cut = attended[begin:end]
        preprocessed_attended_cut = (preprocessed_attended[begin:end]
                                     if preprocessed_attended else None)
        attended_mask_cut = (
            (attended_mask[begin:end] if attended_mask else None)
            * (additional_mask.T if additional_mask else 1))
        weights_cut = weights[:, begin:end]

        # Call
        energies_cut = self.compute_energies(attended_cut, preprocessed_attended_cut,
                                             weights_cut, states)
        weights_cut = self.compute_weights(energies_cut, attended_mask_cut)
        weighted_averages = self.compute_weighted_averages(weights_cut, attended_cut)

        # Paste
        new_weights = new_energies = tensor.zeros_like(weights.T)
        new_weights = tensor.set_subtensor(new_weights[begin:end],
                                           weights_cut)
        new_energies = tensor.set_subtensor(new_energies[begin:end],
                                            energies_cut)

        return weighted_averages, new_weights.T, new_energies.T, step + 1
dataset/ETHPy150Open rizar/attention-lvcsr/lvsr/bricks/attention.py/SequenceContentAndConvAttention.take_glimpses/original