예제 #1
0
    def __init__(self,
                 name,
                 type,
                 value,
                 strict,
                 allow_downcast=None,
                 container=None):
        super(SharedVariable, self).__init__(type=type,
                                             name=name,
                                             owner=None,
                                             index=None)

        if container is not None:
            self.container = container
            if (value is not None) or (strict is not None):
                raise TypeError('value and strict are ignored if you pass '
                                'a container here')
        else:
            self.container = Container(self,
                                       storage=[
                                           type.filter(
                                               value,
                                               strict=strict,
                                               allow_downcast=allow_downcast)
                                       ],
                                       readonly=False,
                                       strict=strict,
                                       allow_downcast=allow_downcast)
예제 #2
0
 def allocate(self, memo):
     """override `Component.allocate` """
     for old_r, new_r in self.random_state_variables:
         if old_r in memo:
             assert memo[old_r].update is new_r
         else:
             memo[old_r] = In(old_r,
                              value=Container(old_r, storage=[None]),
                              update=new_r,
                              mutable=True)
예제 #3
0
    def __init__(self,
                 name,
                 type,
                 value,
                 strict,
                 allow_downcast=None,
                 container=None):
        """
        :param name: The name for this variable (see `Variable`).

        :param type: The type for this variable (see `Variable`).

        :param value: A value to associate with this variable (a new
        container will be created).

        :param strict: True -> assignments to .value will not be cast
        or copied, so they must have the correct type.

        :param allow_downcast: Only applies if `strict` is False.
        True -> allow assigned value to lose precision when cast during assignment.
        False -> never allow precision loss.
        None -> only allow downcasting of a Python float to a scalar floatX.

        :param container: The container to use for this
        variable. Illegal to pass this as well as a value.

        :note: For more user-friendly constructor, see `shared`

        """
        super(SharedVariable, self).__init__(type=type,
                                             name=name,
                                             owner=None,
                                             index=None)

        if container is not None:
            self.container = container
            if (value is not None) or (strict is not None):
                raise TypeError(
                    'value and strict are ignored if you pass a container here'
                )
        else:
            if container is not None:
                raise TypeError('Error to specify both value and container')
            self.container = Container(self,
                                       storage=[
                                           type.filter(
                                               value,
                                               strict=strict,
                                               allow_downcast=allow_downcast)
                                       ],
                                       readonly=False,
                                       strict=strict,
                                       allow_downcast=allow_downcast)
예제 #4
0
 def allocate(self, memo):
     """
     Allocates a Container for each input in the kit. Sets a key in
     the memo that maps the SymbolicInputKit to the list of
     Containers.
     """
     for input in self.kit.sinputs:
         r = input.variable
         if r not in memo:
             input = copy(input)
             input.value = Container(r, storage=[None])
             memo[r] = input
예제 #5
0
def clone_get_equiv(i, o, replaced_inputs=[]):
    """ WRITEME

    :type i: list
    :param i: input L{Variable}s
    :type o: list
    :param o: output L{Variable}s
    :type copy_inputs_and_orphans: bool
    :param copy_inputs_and_orphans:
        if True, the inputs and the orphans will be replaced in the cloned graph by copies
        available in the equiv dictionary returned by the function (copy_inputs_and_orphans
        defaults to True)

    :rtype: a dictionary
    :return:
        equiv mapping each L{Variable} and L{Op} in the graph delimited by i and o to a copy
        (akin to deepcopy's memo).
    """

    from theano.gof.graph import io_toposort
    from theano.gof import Container
    from copy import deepcopy

    copy_inputs_and_orphans = True
    d = {}
    for input in i:
        if input in replaced_inputs:
            cpy = input.clone()
            # deep-copying the container, otherwise the copied input's container will point to the same place
            cont = input.container
            cpy.container = Container(
                cpy,
                storage=[
                    input.type.filter(deepcopy(cpy.value),
                                      strict=cont.strict,
                                      allow_downcast=cont.allow_downcast)
                ],
                readonly=cont.readonly,
                strict=cont.strict,
                allow_downcast=cont.allow_downcast)
            cpy.owner = None
            cpy.index = None
            d[input] = cpy
        else:
            d[input] = input

    for apply in io_toposort(i, o):
        for input in apply.inputs:
            if input not in d:
                if copy_inputs_and_orphans and input in replaced_inputs:
                    # TODO: not quite sure what to do here
                    cpy = input.clone()
                    d[input] = cpy
                else:
                    d[input] = input

        new_apply = apply.clone_with_new_inputs([d[i] for i in apply.inputs])
        d[apply] = new_apply
        for output, new_output in zip(apply.outputs, new_apply.outputs):
            d[output] = new_output

    for output in o:
        if output not in d:
            d[output] = output.clone()

    return d