Пример #1
0
class LastSelectorBlock(object):
    """
    TODO(igor).

    Parameters
    ----------
    x : Matrix (GpuMatrix or CpuMatrix)

    """
    def __init__(self, x):
        device_id = x[0].device_id
        learning = x[0].bpropagable
        self.context = Context(device_id)
        self.output = Matrix.empty_like(x[0])
        self.output = Connector(self.output, device_id if learning else None)
        if learning:
            self.x, self.dL_dx = izip(*x.register_usage(device_id, device_id))
        else:
            self.x = x.register_usage(device_id)
        self.last_idx = x.length - 1

    def fprop(self):
        self.output.assign(self.context, self.x[self.last_idx])
        self.output.fprop()

    def bprop(self):
        self.dL_dx[self.last_idx].add(self.context,
                                      self.output.backward_matrix)
Пример #2
0
class LastSelectorBlock(object):
    """
    TODO(igor).

    Parameters
    ----------
    x : Matrix (GpuMatrix or CpuMatrix)

    """
    def __init__(self, x):
        device_id = x[0].device_id
        learning = x[0].bpropagable
        self.context = Context(device_id)
        self.output = Matrix.empty_like(x[0])
        self.output = Connector(self.output, device_id if learning else None)
        if learning:
            self.x, self.dL_dx = izip(*x.register_usage(device_id, device_id))
        else:
            self.x = x.register_usage(device_id)
        self.last_idx = x.length - 1

    def fprop(self):
        self.output.assign(self.context, self.x[self.last_idx])
        self.output.fprop()

    def bprop(self):
        self.dL_dx[self.last_idx].add(self.context, self.output.backward_matrix)
Пример #3
0
class GaussianNoiseBlock(object):
    """
    Adds Gaussian noise to the block's input. Adding Gaussian noise can be
    viewed as a regularization.


    Parameters
    ----------
    mean : float
            Expected value of Gaussian noise
    std : float
            Standard deviation of added Gaussian noise
    x : matrix
            Block's input
    seed : int
            Seed for :func:`quagga.cuda.curand.create_generator`
    device_id: int
            Defines the device's id on which the computation will take place
    """
    def __init__(self, mean, std, x, seed=42, device_id=None):
        self.mean = mean
        self.std = std
        self.f_context = Context(device_id)
        device_id = self.f_context.device_id
        self.generator = Matrix.get_random_generator(seed)
        if x.bpropagable:
            self.b_context = Context(device_id)
            self.x, self.dL_dx = x.register_usage(device_id, device_id)
        else:
            self.x = x.register_usage(device_id)
        self.output = Matrix.empty_like(self.x)
        self.output = Connector(self.output,
                                device_id if x.bpropagable else None)
        self.training_mode = True

    def fprop(self):
        if self.training_mode:
            self.x.add_gaussian_noise(self.f_context, self.generator,
                                      self.mean, self.std, self.output)
        else:
            self.output.assign(self.f_context, self.x)
        self.output.fprop()

    def bprop(self):
        self.dL_dx.add(self.b_context, self.output.backward_matrix)

    def set_training_mode(self):
        self.training_mode = True

    def set_testing_mode(self):
        self.training_mode = False
Пример #4
0
class GaussianNoiseBlock(object):
    """
    Adds Gaussian noise to the block's input. Adding Gaussian noise can be
    viewed as a regularization.


    Parameters
    ----------
    mean : float
            Expected value of Gaussian noise
    std : float
            Standard deviation of added Gaussian noise
    x : matrix
            Block's input
    seed : int
            Seed for :func:`~quagga.cuda.curand.create_generator`
    device_id: int
            Defines the device's id on which the computation will take place
    """
    def __init__(self, mean, std, x, seed=42, device_id=None):
        self.mean = mean
        self.std = std
        self.f_context = Context(device_id)
        device_id = self.f_context.device_id
        self.generator = Matrix.get_random_generator(seed)
        if x.bpropagable:
            self.b_context = Context(device_id)
            self.x, self.dL_dx = x.register_usage(device_id, device_id)
        else:
            self.x = x.register_usage(device_id)
        self.output = Matrix.empty_like(self.x)
        self.output = Connector(self.output, device_id if x.bpropagable else None)
        self.training_mode = True

    def fprop(self):
        if self.training_mode:
            self.x.add_gaussian_noise(self.f_context, self.generator, self.mean, self.std, self.output)
        else:
            self.output.assign(self.f_context, self.x)
        self.output.fprop()

    def bprop(self):
        self.dL_dx.add(self.b_context, self.output.backward_matrix)

    def set_training_mode(self):
        self.training_mode = True

    def set_testing_mode(self):
        self.training_mode = False
Пример #5
0
class ScheduledSamplingBlock(object):
    def __init__(self, probs, true_labels, schedule, seed, device_id=None):
        self.schedule = schedule
        self.rnd = np.random.RandomState(seed)
        self.context = Context(device_id)
        device_id = self.context.device_id

        self.probs = probs.register_usage(device_id)
        self.true_labels = true_labels.register_usage(device_id)
        self.output = Connector(Matrix.empty_like(self.true_labels))

    def fprop(self):
        if self.rnd.binomial(1, self.schedule.value):
            self.output.assign(self.context, self.true_labels)
        else:
            self.probs.argmax(self.context, self.output, axis=1)
        self.output.fprop()