{"id":23449151,"url":"https://github.com/oeway/torchpydo","last_synced_at":"2025-04-10T02:37:59.325Z","repository":{"id":138962554,"uuid":"53419313","full_name":"oeway/torchpydo","owner":"oeway","description":"using torch(nn, rnn etc.) with numpy inside python","archived":false,"fork":false,"pushed_at":"2016-03-09T10:43:59.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-02-15T18:32:57.272Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oeway.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":"2016-03-08T14:38:42.000Z","updated_at":"2016-03-15T13:03:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"63af9e7c-9616-424a-b617-1efb447137f6","html_url":"https://github.com/oeway/torchpydo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oeway%2Ftorchpydo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oeway%2Ftorchpydo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oeway%2Ftorchpydo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oeway%2Ftorchpydo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oeway","download_url":"https://codeload.github.com/oeway/torchpydo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248144669,"owners_count":21054968,"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","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":[],"created_at":"2024-12-23T22:20:22.843Z","updated_at":"2025-04-10T02:37:59.317Z","avatar_url":"https://github.com/oeway.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Torchpydo\n\nTorchpydo is a two-way bridge between Python/Numpy and Lua/Torch, allowing use using Torch packages(nn, rnn etc.) with numpy inside python.\n\nThis is a project inspired by lunatic-python and based on lunatic-python.\n\n# Installation\n``` bash\n\ngit clone https://github.com/imodpasteur/torchpydo.git\ncd torchpydo\npython setup.py install     # use sudo if needed\n\n```\n\n\n\n# Getting Start\n\n\n## import torchpydo and bootstrap globals\n``` python\nimport torchpydo as lua\n# set the python globals() and __builtins__ to lua, so all the lua global variables can be seen in python globals()\nlua.set_globals(globals(), __builtins__)\n```\n## hello world\n\n``` python\nlua.execute(' greeting = \"hello world\" ')\nprint(greeting)\n```\n\n### Note: alternative way to access lua globals\nif you don't want to mess the python global variables, you can skip the previous line, but you need to access lua global variables through lua.globals(). \n\nNote that if you do this, all the following code should change acorrdingly.\n\n``` python\nimport lutorpy as lua\nlg = lua.globals()\nlua.execute(' greeting = \"hello world\" ')\nprint(lg.greeting)\n\n```\n\n## execute lua code\n\n``` python\na = lua.eval(' {11, 22} ') # define a lua table with two elements\nprint(a[1])\n\nlua.execute(' b={33, 44} ') # define another lua table with two elements\nprint(b[0]) # will get None, because lua used 1 as first index\nprint(b[1])\n\n```\n\n## use torch\n``` python\nlua.require(\"torch\")\nz = torch.Tensor(4,5,6,2)\nprint(torch.isTensor(z))\n\ns = torch.LongStorage(6)\nprint(torch.type(s))\n```\n\n## load image and use nn module\n``` python\nlua.require(\"image\")\nimg_rgb = image.lena()\nprint(img_rgb.size(img_rgb))\nimg = image.rgb2y(img_rgb)\nprint(img.size(img))\n\n# use SpatialConvolution from nn to process the image\nlua.require(\"nn\")\nn = nn.SpatialConvolution(1,16,12,12)\nres = n.forward(n, img)\nprint(res.size(res))\n\n```\n\n## build a simple model\n\n``` python\nmlp = nn.Sequential()\n\nmodule = nn.Linear(10, 5)\nmlp.add(mlp, module)\n\nprint(module.weight)\nprint(module.bias)\n\nprint(module.gradWeight)\nprint(module.gradBias)\n\nx = torch.Tensor(10) #10 inputs\n\n# pass self to the function\ny = mlp.forward(mlp, x)\nprint(y)\n\n```\nOr, you can use lua.bs, to bootstrap the add function.\n\n``` python\n# bootstrap the add function\nlua.bs(mlp,'add')\n# now we can use add without passing self as the first arugment\nmlp.add(module)\n```\n\n## build another model and training it\n\nTrain a model to perform XOR operation.\n\n``` python\nlua.require(\"nn\")\nmlp = nn.Sequential()\nmlp.add(mlp, nn.Linear(2, 20)) # 2 input nodes, 20 hidden nodes\nmlp.add(mlp, nn.Tanh())\nmlp.add(mlp, nn.Linear(20, 1)) # 1 output nodes\n\ncriterion = nn.MSECriterion() \n\nfor i in range(2500):\n    # random sample\n    input= torch.randn(2)    # normally distributed example in 2d\n    output= torch.Tensor(1)\n    if input[1]*input[2] \u003e 0:  # calculate label for XOR function\n        output.fill(output,-1) # output[0] = -1\n    else:\n        output.fill(output,1) # output[0] = -1\n    \n    # feed it to the neural network and the criterion\n    criterion.forward(criterion, mlp.forward(mlp, input), output)\n\n    # train over this example in 3 steps\n    # (1) zero the accumulation of the gradients\n    mlp.zeroGradParameters(mlp)\n    # (2) accumulate gradients\n    mlp.backward(mlp, input, criterion.backward(criterion, mlp.output, output))\n    # (3) update parameters with a 0.01 learning rate\n    mlp.updateParameters(mlp, 0.01)\n\n```\n## test the model\n\n``` python\nx = torch.randn(2)\nprint(x)\nyhat = mlp.forward(mlp,x)\nprint(yhat)\n```\n\n# Acknowledge\n\nThis is a project inspired by [lunatic-python](https://github.com/bastibe/lunatic-python) and based on lunatic-python.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foeway%2Ftorchpydo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foeway%2Ftorchpydo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foeway%2Ftorchpydo/lists"}