{"id":21883024,"url":"https://github.com/lemonzi/tensorify","last_synced_at":"2025-10-19T22:17:04.287Z","repository":{"id":151142868,"uuid":"80069487","full_name":"lemonzi/tensorify","owner":"lemonzi","description":"A small Python utility module that provides TensorFlow decorators","archived":false,"fork":false,"pushed_at":"2017-03-27T21:50:04.000Z","size":10,"stargazers_count":7,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-22T07:14:18.819Z","etag":null,"topics":["decorators","deep-learning","python","tensorflow"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lemonzi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-25T23:45:36.000Z","updated_at":"2022-11-21T13:13:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"19fb09e2-602d-4251-b58c-bf8bae19433c","html_url":"https://github.com/lemonzi/tensorify","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/lemonzi/tensorify","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemonzi%2Ftensorify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemonzi%2Ftensorify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemonzi%2Ftensorify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemonzi%2Ftensorify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lemonzi","download_url":"https://codeload.github.com/lemonzi/tensorify/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemonzi%2Ftensorify/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279926895,"owners_count":26245501,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-19T02:00:07.647Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["decorators","deep-learning","python","tensorflow"],"created_at":"2024-11-28T09:38:44.050Z","updated_at":"2025-10-19T22:17:04.244Z","avatar_url":"https://github.com/lemonzi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tensorify\n\nPrototyping with TensorFlow can often be very tideous because it provides a\nlimited number of possible operations on data. Pre-processing steps can be\nimplemented in pure Python, but if an intermediate parsing is required (or if\nyou want to take more advantage of TensorFlow's multi-threading capabilities)\nthe solution is to either write it yourself in C++ or use the `tf.py_func`\nwrapper. Using the wrapper, however, requires passing a lot of arguments\nto it that make software maintenance harder -- especially if you are re-using\nparsing functions or classes for other pursposes.\n\nThis Python package provides `tensorflow_op`, a Python decorator that converts\nregular functions into TensorFlow ops, and `tensorify`, which is a\nconvenience wrapper that applies the decorator to all the functions in the\ngiven module, either in-place or in a copy. \n\nIn short, it lets you do this:\n\n```python\n@tensorflow_op(tf.int32)\ndef add(a_numpy_array, another_numpy_array, extra_one=False):\n    extra = 1 if extra_one else 0\n    return a_numpy_array + another_numpy_array + extra\n\nresult = add(a_tf_tensor, another_tf_tensor, extra_one=True)\n```\n\nWith the advantage of using any class or library you want inside the \nfunction. This makes wrapping existing numerical libraries for use with\nTensorflow extremely easy.\n\n**WARNING**: All functions wrapped with tensorify will be executed in the\nsame machine as the main script, so this is not a very efficient solution.\nThis is intended for prototyping and for pre-processing or parsing code only.\nDo not implement heavy deep learning layers with this!\n\n## Installation\n\nThe tensorify package is not yet on the PyPI archive; you can install it with:\n\n```bash\npip install git+https://github.com/lemonzi/tensorify\n```\n\nAnd then import it as usual.\n\n## Usage\n\n### `tensorflow_op`\n\n\u003e A decorator that takes a function and turns it into a TensorFlow op.\n\nA call to the decorated function will create a node with input tensors\nset to the function arguments, and the supplied key-word arguments will\nbe passed to the function directly using a partial invocation.\n\nThe decorator takes as optional arguments a list with output types, a\nname for the op, and whether the function is stateful (`False` by default).\n\nIf no name is supplied, the CamelCased name of the function will be used.\nThe name can also be modified at call time using the `with_name()` method:\n\n```python\n@tensorflow_op(tf.int32)\ndef add(x, y, extra_one=False):\n    return x + y + (1 if extra_one else 0)\n\ninput_one = tf.constant([1])\ninput_two = tf.constant([2])\nanswer.with_name(\"AddPlusOne\")(input_one, input_two, extra_one=True)\nanswer.with_name(\"AddAndThatsIt\")(input_one, input_two)\n```\n\nSimilarly, the op can be made stateful using `stateful()`. In this example,\nwe need to supply `is_method` so that the `self` argument doesn't get\nsent to the TensorFlow engine and stays in the closure instead.\nActually, when `stateful` is not provided as argument and `is_method` is\ntrue the op is stateful by default; this is only an example.\n\n```python\nclass Accumulator:\n    def __init__(self):\n        self.state = 0\n    @tensorify.tensorflow_op(tf.int64, is_method=True)\n    def accumulate(self, x):\n        self.state = self.state + x\n        return self.state\n\nx = tf.constant([1])\ny = tf.constant([2])\nmy_accumulator = Accumulator()\nafter_accumulating_x = my_accumulator.stateful()(x)\nafter_accumulating_y = my_accumulator.stateful()(y)\n```\n\nNotice that, in this example, all ops that come form the same accumulator\nwill accumulate to the same buffer, so the accumulator acts as a global\ncounter. This is not very efficient, though; it would be better to use a\n`tf.Variable` for state storage and have the operation be stateless.\n\nThe same applies for the outputs:\n\n```python\n@tensorflow_op()\ndef replicate(value, how_many_times):\n    return [value] * how_many_times\n\nx = tf.constant([1])\nthree_x = replicate.with_outputs([tf.int32] * 3)(x, 3)\n```\n\n### `tensorify`\n\n\u003e Converts all functions in a module into TensorFlow ops.\n\nExample usage:\n\nIn `fancy_module.py`:\n\n```python\ndef add(x, y):\n    return x + y\n```\n\nIn `app.py`:\n\n```python\nimport tensorflow as tf\nfrom tensorify import tensorify\n\nimport fancy_module\ntensorify(fancy_module, tf.int32)\n\nx_tensor = tf.constant([1])\ny_tensor = tf.constant([2])\nresult = fancy_module.add(x_tensor, y_tensor)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemonzi%2Ftensorify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flemonzi%2Ftensorify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemonzi%2Ftensorify/lists"}