Ejemplo n.º 1
0
    def register_map_method(cls, fn, library_path, alias=None, doc=None):
        """
        This method enables you to register any function `fn` that takes a Tensor as its first argument and returns a Tensor as a method of the Builder class. The resulting method is created by *lifting* the function to work with a Builder.

        **Arguments**

        * `fn`: a function of type `Tensor -> Tensor`.
        * `library_path`: the route of the librar from which this function was taken, used for documentation purposes.
        * `alias`: allows you to specify the name of the method, it will take the name of the function if its `None`.
        * `doc`: the documentation for the method, if `None` a predefied documentation will be generated based on the documentation of `fn`.

        **Return**

        `None`

        **Examples**

        In this example we will register `tf.reshape` as a method of the Builder class

            import tensorflow as tf
            from tensorbuilder import tb

            tb.Builder.register_map_method(tf.reshape, "tf")
        """
        fn_signature = utils.get_method_sig(fn)
        fn_docs = inspect.getdoc(fn)
        original_name = fn.__name__
        name = alias if alias else original_name

        lifted = _lift(fn)
        lifted.__name__ = name
        lifted.__doc__ = doc if doc else _builder_register_map_method_docs(
            original_name, library_path, name, fn_signature, fn_docs)

        setattr(cls, name, lifted)
Ejemplo n.º 2
0
    def register_reduce_method(fn, library_path, alias=None):
        fn_signature = utils.get_method_sig(fn)
     	fn_docs = inspect.getdoc(fn)
        original_name = fn.__name__
        name = alias if alias else original_name

        _tree_method = _lift_tree_reduce(fn)

        _tree_method.__name__ = name
        _tree_method.__docs__ = """
THIS METHOD IS AUTOMATICALLY GENERATED

**@immutable**

This method is a lifted version the function `{1}.{0}`. `{1}.{0}` is expected to work with a list or iterable of Tensors, this method extracts the tensors and from the branches and applies the same function, and wraps the result inside a Builder.


** Original Documentation for `{1}.{0}`**

	def {3}

{4}
        """.format(original_name, library_path, name, fn_signature, fn_docs)

        exec("Builder.{0} = _tree_method".format(name))
Ejemplo n.º 3
0
    def register_map_method(cls, fn, library_path, alias=None, doc=None):
        """
        This method enables you to register any function `fn` that takes a Tensor as its first argument and returns a Tensor as a method of the Builder class. The resulting method is created by *lifting* the function to work with a Builder.

        **Arguments**

        * `fn`: a function of type `Tensor -> Tensor`.
        * `library_path`: the route of the librar from which this function was taken, used for documentation purposes.
        * `alias`: allows you to specify the name of the method, it will take the name of the function if its `None`.
        * `doc`: the documentation for the method, if `None` a predefied documentation will be generated based on the documentation of `fn`.

        **Return**

        `None`

        **Examples**

        In this example we will register `tf.reshape` as a method of the Builder class

            import tensorflow as tf
            from tensorbuilder import tb

            tb.Builder.register_map_method(tf.reshape, "tf")
        """
        fn_signature = utils.get_method_sig(fn)
     	fn_docs = inspect.getdoc(fn)
        original_name = fn.__name__
        name = alias if alias else original_name

        lifted = _lift(fn)
        lifted.__name__ = name
        lifted.__doc__ = doc if doc else _builder_register_map_method_docs(original_name, library_path, name, fn_signature, fn_docs)

        setattr(cls, name, lifted)
Ejemplo n.º 4
0
    def register_reduce_method(cls, fn, library_path, alias=None, doc=None):
        """
        This method enables you to register a function `fn` of type `(Tensor, Tensor) -> Tensor` as a method of the Builder class.

        **Arguments**

        * `fn`: a function of type `(Tensor, Tensor) -> Tensor`
        * `library_path`: the route of the librar from which this function was taken, used for documentation purposes.
        * `alias`: allows you to specify the name of the method, it will take the name of the function if its `None`.
        * `doc`: the documentation for the method, if `None` a predefied documentation will be generated based on the documentation of `fn`.

        **Return**

        `None`

        **Examples**

        In this example we will create the method `reduce_add` for the BuilderTree class

            import tensorflow as tf
            from tensorbuilder import tb

            tb.BuilderTree.register_reduce_method(tf.add, "tf", alias="reduce_add")
        """
        fn_signature = utils.get_method_sig(fn)
     	fn_docs = inspect.getdoc(fn)
        original_name = fn.__name__
        name = alias if alias else original_name

        _tree_method = _lift_tree_reduce(fn)

        _tree_method.__name__ = name
        _tree_method.__doc__ = doc if doc else _tree_register_reduce_method_docs(original_name, library_path, name, fn_signature, fn_docs)

        setattr(cls, name, _tree_method)
Ejemplo n.º 5
0
    def DoRegisterMethod(cls,
                         fn,
                         library_path,
                         alias=None,
                         original_name=None,
                         doc=None,
                         wrapped=None,
                         explanation="",
                         method_type=utils.identity,
                         explain=True):
        """
        This method enables you to register any function `fn` that takes an Applicative as its first argument as a method of the Builder class.

        **Arguments**

        * `fn`: a function that atleast takes an Applicative as its first argument.
        * `library_path`: the route of the librar from which this function was taken, used for documentation purposes.
        * `alias`: allows you to specify the name of the method, it will take the name of the function if its `None`.
        * `doc`: the documentation for the method, if `None` a predefied documentation will be generated based On the documentation of `fn`.

        **Return**

        `None`

        **Examples**

        """

        if wrapped:
            fn = functools.wraps(wrapped)(fn)

        fn_signature = utils.get_method_sig(fn)
        fn_docs = inspect.getdoc(fn)
        name = alias if alias else fn.__name__
        original_name = fn.__name__ if wrapped else original_name if original_name else name

        fn.__name__ = name
        fn.__doc__ = doc if doc else ("""
THIS METHOD IS AUTOMATICALLY GENERATED

    builder.{1}(*args, **kwargs)

It accepts the same arguments as `{3}.{0}`. """ + explanation + """

**{3}.{0}**

    {2}

        """).format(original_name, name, fn_docs,
                    library_path) if explain else fn_docs

        if name in cls.__core__:
            raise Exception(
                "Can't add method '{0}' because its On __core__".format(name))

        fn = method_type(fn)
        setattr(cls, name, fn)
Ejemplo n.º 6
0
    def register_method(cls, fn, library_path, alias=None, doc=None):
        """
        This method enables you to register any function `fn` that takes a BuilderTree as its first argument as a method of the Builder class.

        **Arguments**

        * `fn`: a function that atleast takes a BuilderTree as its first argument.
        * `library_path`: the route of the librar from which this function was taken, used for documentation purposes.
        * `alias`: allows you to specify the name of the method, it will take the name of the function if its `None`.
        * `doc`: the documentation for the method, if `None` a predefied documentation will be generated based on the documentation of `fn`.

        **Return**

        `None`

        **Examples**

        In this example we will create the method `fully_connected` for the BuilderTree class

            import tensorflow as tf
            from tensorbuilder import tb

            def _tree_fully_connected(tree, size, *args, **kwargs):
                activation_fn = None

                if "activation_fn" in kwargs:
                    activation_fn = kwargs["activation_fn"]
                    del kwargs["activation_fn"]

                builder = (
                    tree.map_each(tf.contrib.layers.fully_connected, size, *args, **kwargs)
                    .reduce(tf.add)
                )

                if activation_fn:
                    builder = builder.map(activation_fn)

                return builder

            tb.BuilderTree.register_method(_tree_fully_connected, "tensorbuilder.patches.tensorflow.fully_connected", alias="fully_connected")
        """
        fn_signature = utils.get_method_sig(fn)
        fn_docs = inspect.getdoc(fn)
        original_name = fn.__name__
        name = alias if alias else original_name

        fn.__name__ = name
        fn.__doc__ = doc if doc else _tree_register_method_docs(
            original_name, library_path, name, fn_signature, fn_docs)

        setattr(cls, name, fn)
Ejemplo n.º 7
0
    def register_method(fn, library_path, alias=None, doc=None):
        """
        This method enables you to register any function `fn` that takes a BuilderTree as its first argument as a method of the Builder class.

        **Arguments**

        * `fn`: a function that atleast takes a BuilderTree as its first argument.
        * `library_path`: the route of the librar from which this function was taken, used for documentation purposes.
        * `alias`: allows you to specify the name of the method, it will take the name of the function if its `None`.
        * `doc`: the documentation for the method, if `None` a predefied documentation will be generated based on the documentation of `fn`.

        **Return**

        `None`

        **Examples**

        In this example we will create the method `fully_connected` for the BuilderTree class

            import tensorflow as tf
            import tensorbuilder as tb


            def _tree_fully_connected(tree, size, *args, **kwargs):
                activation_fn = None

                if "activation_fn" in kwargs:
                    activation_fn = kwargs["activation_fn"]
                    del kwargs["activation_fn"]

                builder = (
                    tree.map_each(tf.contrib.layers.fully_connected, size, *args, **kwargs)
                    .reduce(tf.add)
                )

                if activation_fn:
                    builder = builder.map(activation_fn)

                return builder

            tb.BuilderTree.register_method(_tree_fully_connected, "tensorbuilder.patches.tensorflow.fully_connected", alias="fully_connected")
        """
        fn_signature = utils.get_method_sig(fn)
     	fn_docs = inspect.getdoc(fn)
        original_name = fn.__name__
        name = alias if alias else original_name

        fn.__name__ = name
        fn.__doc__ = doc if doc else _tree_register_method_docs(original_name, library_path, name, fn_signature, fn_docs)

        exec("BuilderTree.{0} = fn".format(name))
Ejemplo n.º 8
0
    def register_method(cls, fn, library_path, alias=None, doc=None):
        """
        This method enables you to register any function `fn` that takes a Builder as its first argument as a method of the Builder class.

        **Arguments**

        * `fn`: a function that atleast takes a Builder as its first argument.
        * `library_path`: the route of the librar from which this function was taken, used for documentation purposes.
        * `alias`: allows you to specify the name of the method, it will take the name of the function if its `None`.
        * `doc`: the documentation for the method, if `None` a predefied documentation will be generated based on the documentation of `fn`.

        **Return**

        `None`

        **Examples**

        In this example we will create a funtion and register it as a method called `relu_dropout_layer`

            import tensorflow as tf
            from tensorbuilder import tb


            def relu_dropout(builder, size, keep_prob):
                \"\"\"Fully connect to a relu layer of size `size` and apply dropout with `keep_prob`\"\"\"
                return (
                    builder.map(tf.contrib.layers.fully_connected, size)
                    .map(tf.nn.relu)
                    .map(tf.nn.dropout, keep_prob)
                )

            tb.Builder.register_method(relu_dropout_layer, "my.lib", alias="relu_dropout_layer")
        """

        fn_signature = utils.get_method_sig(fn)
        fn_docs = inspect.getdoc(fn)
        original_name = fn.__name__
        name = alias if alias else original_name

        fn.__name__ = name
        fn.__doc__ = doc if doc else _builder_register_method_docs(
            original_name, library_path, name, fn_signature, fn_docs)

        setattr(cls, name, fn)
Ejemplo n.º 9
0
    def register_method(cls, fn, library_path, alias=None, doc=None):
        """
        This method enables you to register any function `fn` that takes a Builder as its first argument as a method of the Builder class.

        **Arguments**

        * `fn`: a function that atleast takes a Builder as its first argument.
        * `library_path`: the route of the librar from which this function was taken, used for documentation purposes.
        * `alias`: allows you to specify the name of the method, it will take the name of the function if its `None`.
        * `doc`: the documentation for the method, if `None` a predefied documentation will be generated based on the documentation of `fn`.

        **Return**

        `None`

        **Examples**

        In this example we will create a funtion and register it as a method called `relu_dropout_layer`

            import tensorflow as tf
            from tensorbuilder import tb


            def relu_dropout(builder, size, keep_prob):
                \"\"\"Fully connect to a relu layer of size `size` and apply dropout with `keep_prob`\"\"\"
                return (
                    builder.map(tf.contrib.layers.fully_connected, size)
                    .map(tf.nn.relu)
                    .map(tf.nn.dropout, keep_prob)
                )

            tb.Builder.register_method(relu_dropout_layer, "my.lib", alias="relu_dropout_layer")
        """

        fn_signature = utils.get_method_sig(fn)
     	fn_docs = inspect.getdoc(fn)
        original_name = fn.__name__
        name = alias if alias else original_name

        fn.__name__ = name
        fn.__doc__ = doc if doc else _builder_register_method_docs(original_name, library_path, name, fn_signature, fn_docs)

        setattr(cls, name, fn)
Ejemplo n.º 10
0
    def register_method(fn, library_path, alias=None):
        fn_signature = utils.get_method_sig(fn)
     	fn_docs = inspect.getdoc(fn)
        original_name = fn.__name__
        name = alias if alias else original_name

        fn.__name__ = name
        fn.__doc__ = """
THIS METHOD IS AUTOMATICALLY GENERATED

**@immutable**

This method is a lifted version the function `{1}.{0}` to work with `tensorbuilder.tensorbuilder.BuilderTree`s. Instead of taking a Tensor as its first argument it takes a builder, the rest of the arguments are exactly the same.


** Original Documentation for `{1}.{0}`**

	def {3}

{4}
        """.format(original_name, library_path, name, fn_signature, fn_docs)

        exec("BuilderTree.{0} = fn".format(name))
Ejemplo n.º 11
0
    def register_method(cls, fn, library_path, alias=None, doc=None):
        """
        This method enables you to register any function `fn` that takes an Applicative as its first argument as a method of the Builder class.

        **Arguments**

        * `fn`: a function that atleast takes an Applicative as its first argument.
        * `library_path`: the route of the librar from which this function was taken, used for documentation purposes.
        * `alias`: allows you to specify the name of the method, it will take the name of the function if its `None`.
        * `doc`: the documentation for the method, if `None` a predefied documentation will be generated based on the documentation of `fn`.

        **Return**

        `None`

        **Examples**

        """
        fn_signature = utils.get_method_sig(fn)
     	fn_docs = inspect.getdoc(fn)
        original_name = fn.__name__
        name = alias if alias else original_name

        fn.__name__ = name
        fn.__doc__ = doc if doc else """
        THIS METHOD IS AUTOMATICALLY GENERATED

        This method accepts the same arguments as `{3}.{0}`

        ** Documentation from `{3}.{0}`**

            def {1}

        """.format(name, fn_signature, fn.__doc__, library_path)


        setattr(cls, name, fn)
Ejemplo n.º 12
0
    def register_reduce_method(cls, fn, library_path, alias=None, doc=None):
        """
        This method enables you to register a function `fn` of type `(Tensor, Tensor) -> Tensor` as a method of the Builder class.

        **Arguments**

        * `fn`: a function of type `(Tensor, Tensor) -> Tensor`
        * `library_path`: the route of the librar from which this function was taken, used for documentation purposes.
        * `alias`: allows you to specify the name of the method, it will take the name of the function if its `None`.
        * `doc`: the documentation for the method, if `None` a predefied documentation will be generated based on the documentation of `fn`.

        **Return**

        `None`

        **Examples**

        In this example we will create the method `reduce_add` for the BuilderTree class

            import tensorflow as tf
            from tensorbuilder import tb

            tb.BuilderTree.register_reduce_method(tf.add, "tf", alias="reduce_add")
        """
        fn_signature = utils.get_method_sig(fn)
        fn_docs = inspect.getdoc(fn)
        original_name = fn.__name__
        name = alias if alias else original_name

        _tree_method = _lift_tree_reduce(fn)

        _tree_method.__name__ = name
        _tree_method.__doc__ = doc if doc else _tree_register_reduce_method_docs(
            original_name, library_path, name, fn_signature, fn_docs)

        setattr(cls, name, _tree_method)
Ejemplo n.º 13
0
    def register_method(cls, fn, library_path, alias=None, doc=None):
        """
        This method enables you to register any function `fn` that takes an Applicative as its first argument as a method of the Builder class.

        **Arguments**

        * `fn`: a function that atleast takes an Applicative as its first argument.
        * `library_path`: the route of the librar from which this function was taken, used for documentation purposes.
        * `alias`: allows you to specify the name of the method, it will take the name of the function if its `None`.
        * `doc`: the documentation for the method, if `None` a predefied documentation will be generated based on the documentation of `fn`.

        **Return**

        `None`

        **Examples**

        """
        fn_signature = utils.get_method_sig(fn)
        fn_docs = inspect.getdoc(fn)
        original_name = fn.__name__
        name = alias if alias else original_name

        fn.__name__ = name
        fn.__doc__ = doc if doc else """
        THIS METHOD IS AUTOMATICALLY GENERATED

        This method accepts the same arguments as `{3}.{0}`

        ** Documentation from `{3}.{0}`**

            def {1}

        """.format(name, fn_signature, fn.__doc__, library_path)

        setattr(cls, name, fn)
Ejemplo n.º 14
0
_builder_excluded = ["copy"]
def _builder_methods():
    for _name, f in inspect.getmembers(tb.Builder, inspect.ismethod):
        if _name[0] != '_' and _name not in _builder_excluded:
            yield ("Builder", _name, f)

_tree_excluded = ["copy", "connect_layer" ] #, "tensors", "builders"]
def _builder_tree_methods():
    for _name, f in inspect.getmembers(tb.BuilderTree, inspect.ismethod):
        if _name[0] != '_' and _name not in _tree_excluded:
            yield ("BuilderTree", _name, f)


for _module_name, _name, f in itertools.chain(_builder_methods(), _builder_tree_methods()):
    _f_signature = utils.get_method_sig(f)
    _f_docs = inspect.getdoc(f)
    exec("""


def {0}(*args, **kwargs):
	\"\"\"
THIS FUNCTION IS AUTOMATICALLY GENERATED

This function accepts the same arguments as `{3}.{0}` but instead of getting the class instance as its first arguments, it returns a function that expects a builder and applies the builder plus all \*args and \*\*kwargs to `{3}.{0}`. The returned function is an `tensorbuilder.dsl.Applicative`, so you can use all the methods defined by this class.

** utils of `{3}.{0}`**

	def {1}