Beispiel #1
0
def test_application_call():
    X = tensor.matrix('X')
    brick = TestBrick()
    Y = brick.access_application_call(X)
    (auxiliary_variable,) = get_application_call(Y).auxiliary_variables
    assert auxiliary_variable.name == 'test_val'
    assert get_brick(auxiliary_variable) == brick
    assert get_application_call(Y).auxiliary_variables[0].name == 'test_val'
Beispiel #2
0
def test_application_call():
    X = tensor.matrix('X')
    brick = TestBrick(0)
    Y = brick.access_application_call(X)
    (auxiliary_variable,) = get_application_call(Y).auxiliary_variables
    assert auxiliary_variable.name == 'test_val'
    assert get_brick(auxiliary_variable) == brick
    assert get_application_call(Y).auxiliary_variables[0].name == 'test_val'
Beispiel #3
0
    def apply(self, input_, **kwargs):
        output, u, s = self.do_apply(input_, **kwargs)

        if self.accumulate:
            if self.use_population:
                raise Exception("use_population is set to true as well as with"
                                "accumulation.",
                                "This is not possible as there is nothing to "
                                "take the population of.")

            self.updates[self.u] = self.u + u
            self.updates[self.s] = self.s + s
            self.updates[self.n] = self.n + 1

        if self.rolling_accumulate:
            if self.use_population:
                raise Exception("use_population is set to true as well as with"
                                "rolling_accumulation."
                                "This is not currently supported, "
                                " and might not make sense.")
            annotation = get_application_call(output)
            annotation.updates[self.u] = self.u * self.alpha + (1-self.alpha) * u
            annotation.updates[self.s] = self.s * self.alpha + (1-self.alpha) * s
            annotation.updates[self.n] = self.n*0 + 1

        return output
Beispiel #4
0
    def apply(self, input_, **kwargs):
        output, u, s = self.do_apply(input_, **kwargs)

        if self.accumulate:
            if self.use_population:
                raise Exception(
                    "use_population is set to true as well as with"
                    "accumulation.",
                    "This is not possible as there is nothing to "
                    "take the population of.")

            self.updates[self.u] = self.u + u
            self.updates[self.s] = self.s + s
            self.updates[self.n] = self.n + 1

        if self.rolling_accumulate:
            if self.use_population:
                raise Exception("use_population is set to true as well as with"
                                "rolling_accumulation."
                                "This is not currently supported, "
                                " and might not make sense.")
            annotation = get_application_call(output)
            annotation.updates[
                self.u] = self.u * self.alpha + (1 - self.alpha) * u
            annotation.updates[
                self.s] = self.s * self.alpha + (1 - self.alpha) * s
            annotation.updates[self.n] = self.n * 0 + 1

        return output
Beispiel #5
0
    def __init__(self, samples):
        # Extracting information from the sampling computation graph
        self.cg = ComputationGraph(samples)
        self.inputs = self.cg.inputs
        self.generator = get_brick(samples)
        if not isinstance(self.generator, BaseSequenceGenerator):
            raise ValueError
        self.generate_call = get_application_call(samples)
        if (not self.generate_call.application == self.generator.generate):
            raise ValueError
        self.inner_cg = ComputationGraph(self.generate_call.inner_outputs)

        # Fetching names from the sequence generator
        self.context_names = self.generator.generate.contexts
        self.state_names = self.generator.generate.states

        # Parsing the inner computation graph of sampling scan
        self.contexts = [
            VariableFilter(bricks=[self.generator], name=name,
                           roles=[INPUT])(self.inner_cg)[0]
            for name in self.context_names
        ]
        self.input_states = []
        # Includes only those state names that were actually used
        # in 'generate'
        self.input_state_names = []
        for name in self.generator.generate.states:
            var = VariableFilter(bricks=[self.generator],
                                 name=name,
                                 roles=[INPUT])(self.inner_cg)
            if var:
                self.input_state_names.append(name)
                self.input_states.append(var[0])

        self.compiled = False
Beispiel #6
0
    def __init__(self, beam_size, samples):
        self.beam_size = beam_size

        # Extracting information from the sampling computation graph
        cg = ComputationGraph(samples)
        self.inputs = cg.inputs
        self.generator = get_brick(samples)
        if not isinstance(self.generator, BaseSequenceGenerator):
            raise ValueError
        self.generate_call = get_application_call(samples)
        if not self.generate_call.application == self.generator.generate:
            raise ValueError
        self.inner_cg = ComputationGraph(self.generate_call.inner_outputs)

        # Fetching names from the sequence generator
        self.context_names = self.generator.generate.contexts
        self.state_names = self.generator.generate.states

        # Parsing the inner computation graph of sampling scan
        self.contexts = [
            VariableFilter(bricks=[self.generator], name=name, roles=[INPUT])(self.inner_cg)[0]
            for name in self.context_names
        ]
        self.input_states = []
        # Includes only those state names that were actually used
        # in 'generate'
        self.input_state_names = []
        for name in self.generator.generate.states:
            var = VariableFilter(bricks=[self.generator], name=name, roles=[INPUT])(self.inner_cg)
            if var:
                self.input_state_names.append(name)
                self.input_states.append(var[0])

        self.compiled = False
Beispiel #7
0
def test_saved_inner_graph():
    """Make sure that the original inner graph is saved."""
    x = tensor.tensor3()
    recurrent = SimpleRecurrent(dim=3, activation=Tanh())
    y = recurrent.apply(x)

    application_call = get_application_call(y)
    assert application_call.inner_inputs
    assert application_call.inner_outputs

    cg = ComputationGraph(application_call.inner_outputs)
    # Check that the inner scan graph is annotated
    # with `recurrent.apply`
    assert len(VariableFilter(applications=[recurrent.apply])(cg)) == 3
    # Check that the inner graph is equivalent to the one
    # produced by a stand-alone of `recurrent.apply`
    assert is_same_graph(application_call.inner_outputs[0],
                         recurrent.apply(*application_call.inner_inputs,
                                         iterate=False))
Beispiel #8
0
 def check_output_variable(o):
     assert get_application_call(o).application.brick is brick
     assert (get_application_call(o.owner.inputs[0]).application.brick
             is brick)
Beispiel #9
0
 def get_metadata(self, var):
     app_call = get_application_call(var)
     return app_call.metadata['offset'], get_application_call(var).metadata['divisor']
Beispiel #10
0
 def check_output_variable(o):
     assert get_application_call(o).brick is brick
     assert get_application_call(o.owner.inputs[0]).brick is brick
Beispiel #11
0
 def get_metadata(self, var):
     app_call = get_application_call(var)
     return app_call.metadata['offset'], get_application_call(
         var).metadata['divisor']