{"id":20272281,"url":"https://github.com/kubetrail/tfutil","last_synced_at":"2026-04-29T14:04:52.903Z","repository":{"id":41953462,"uuid":"476045640","full_name":"kubetrail/tfutil","owner":"kubetrail","description":"lib: Generics based Go (Golang) interface to TensorFlow tensors","archived":false,"fork":false,"pushed_at":"2025-01-12T23:59:46.000Z","size":139,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-23T19:45:53.489Z","etag":null,"topics":["generics","golang-library","tensorflow","tensors"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kubetrail.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,"zenodo":null}},"created_at":"2022-03-30T20:44:59.000Z","updated_at":"2025-01-12T23:57:20.000Z","dependencies_parsed_at":"2023-10-05T02:04:13.794Z","dependency_job_id":"ff274bd6-90b5-46d8-9b5f-5c3aaddd7d5b","html_url":"https://github.com/kubetrail/tfutil","commit_stats":{"total_commits":26,"total_committers":1,"mean_commits":26.0,"dds":0.0,"last_synced_commit":"2f1468efd623936ecdf023af2aa5e80122810721"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/kubetrail/tfutil","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubetrail%2Ftfutil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubetrail%2Ftfutil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubetrail%2Ftfutil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubetrail%2Ftfutil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kubetrail","download_url":"https://codeload.github.com/kubetrail/tfutil/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubetrail%2Ftfutil/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32428622,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-29T13:34:34.882Z","status":"ssl_error","status_checked_at":"2026-04-29T13:34:29.830Z","response_time":110,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["generics","golang-library","tensorflow","tensors"],"created_at":"2024-11-14T12:42:43.301Z","updated_at":"2026-04-29T14:04:52.890Z","avatar_url":"https://github.com/kubetrail.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tfutil\n`tfutil` is a generics based Go library on top of \n[TensorFlow](https://github.com/tensorflow/tensorflow) \n[Go interface](https://github.com/wamuir/graft) to make it\neasier to work with tensors. In particular, this package\nintends to enable easier use of operators on tensors and\nserialization of data to-from tensor objects.\n\n\u003e TF version v2.18.0\n\n# disclaimer\n\u003e The use of this library does not guarantee security or usability for any\n\u003e particular purpose. Please review the code and use at your own risk.\n\n\u003e Please also note that the API is not yet stable and can change.\n\n## installation\nThis step assumes you have [Go compiler toolchain](https://go.dev/dl/)\ninstalled on your system with version at least Go 1.21.1. The library\nmakes use of `go` generics.\n\nFirst make sure you have downloaded and installed `TensorFlow`\n[C-library](https://www.tensorflow.org/install/lang_c) and make \nsure you are able to build and run the \"hello-world\" as \ndescribed on that page.\n\n\u003e Please use TF version v2.18.0. Older versions may not work\n\nDownload this repo to a folder and cd to it.\n```bash\ngo test ./...\n```\n\u003e Please note that tests may fail on MacOS with the following error:\n```text\ndyld[1589]: Library not loaded: @rpath/libtensorflow.2.dylib\n  Reason: no LC_RPATH's found\nsignal: abort trap\n```\n\n## usage\nA type parameterized `Tensor` can be instantiated using one of these following\nways:\n```go\nfunc NewTensor[T PrimitiveTypes](value []T, shape ...int) (*Tensor[T], error) {...}\nfunc NewTensorFromFunc[T PrimitiveTypes](f func(int) T, shape ...int) (*Tensor[T], error) {...}\n```\n\nAnother way to create a tensor is to input any value such as a slice of slice,\nhowever, type parameter needs to be explicitly provided\n```go\nfunc NewTensorFromAny[T PrimitiveTypes](value any) (*Tensor[T], error) {...}\n```\n\nwhere, `PrimitiveTypes` are:\n```go\ntype PrimitiveTypes interface {\n\tbool |\n\t\tint8 | int16 | int32 | int64 |\n\t\tuint8 | uint16 | uint32 | uint64 |\n\t\tfloat32 | float64 |\n\t\tcomplex64 | complex128 |\n\t\tstring\n}\n```\n\nFor instance a random `5x5` matrix of data type `float64` can be created as follows:\n```go\nmatrix, err := tfutil.NewFromFunc(\n\t\tfunc(int) float64 { return rand.Float64() }, // generator function\n\t\t5, // rows\n\t\t5, // columns\n\t\t)\n```\n\nSimilarly, a random `5x5` matrix of data type `int64` can be created as follows:\n```go\nmatrixInt64, err := NewTensorFromFunc(\n        func(int) int64 { return int64(rand.Intn(100)) }, // generator function\n        5, // rows\n        5, // columns\n        )\n```\n\nMatrix can be printed as follows:\n```go\nfmt.Println(matrixInt64)\n```\n```bash\n[ # matrix shape: [5 5], dataType: int64\n      3   20   71   25   90     \n      3   34   48   14   71     \n     54   19   48   94   92     \n     22   75   69   50   54     \n     52    1   39    2    1     \n]\n```\n\nUse `Cast` to cast the matrix data type to a different data type.\nFor instance, passing a type parameter `float64` to the function\nwill cast input matrix, which is of data type `int64` to `float64`\n```go\nmatrixF64, err := Cast[float64](matrixInt64)\n```\n\n### operators\nOperations such as matrix inversion can be performed on tensors as follows.\n```go\nerr := matrixF64.Apply(MatrixInverseOp)\n```\n\n\u003e Please note that it is not possible to perform all checks at the compile\n\u003e time. So please apply operators with the knowledge of what they do. For\n\u003e instance, applying MatrixInverseOp on a tensor of data type int64 will\n\u003e cause runtime failures\n\n### serialization\n```go\n    input, err := NewTensor([]byte{1, 2, 3, 4, 5, 6}, 2, 3)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\tjb, err := json.Marshal(input)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\n\toutput := \u0026Tensor[byte]{}\n\tif err := json.Unmarshal(jb, output); err != nil {\n\t\tt.Fatal(err)\n\t}\n\t\n\tfmt.Println(string(jb))\n```\nThis results in JSON serialization as follows:\n```json\n{\"type\":\"tensor\",\"tfDataType\":\"Uint8\",\"goDataType\":\"uint8\",\"shape\":[2,3],\"value\":\"AQIDBAUG\"}\n```\n\nThe serialized form can be parsed back into a tensor parametrized by the corresponding data type.\n\n### json serialization of complex data\n`complex64` and `complex128` data types are not supported natively by JSON serialization.\nThese values, therefore, are separated out into real and imaginary parts as follows\n```go\n    input, _ := NewFromFunc(\n\t\tfunc(int) complex128 { return complex(rand.Float64(), rand.Float64()) }, 2, 3)\n\n\tb, _ := json.Marshal(input)\n\tfmt.Println(string(b))\n\n\toutput := \u0026Tensor[complex128]{}\n\t_ = json.Unmarshal(b, output)\n\n\tb, _ = output.PrettyPrint()\n\tfmt.Println(string(b))\n```\n\nThe JSON serialization looks as follows:\n```json\n{\n  \"type\": \"tensor\",\n  \"tfDataType\": \"Complex128\",\n  \"goDataType\": \"complex128\",\n  \"shape\": [\n    2,\n    3\n  ],\n  \"real64\": [\n    0.6046602879796196,\n    0.6645600532184904,\n    0.4246374970712657,\n    0.06563701921747622,\n    0.09696951891448456,\n    0.5152126285020654\n  ],\n  \"imag64\": [\n    0.9405090880450124,\n    0.4377141871869802,\n    0.6868230728671094,\n    0.15651925473279124,\n    0.30091186058528707,\n    0.8136399609900968\n  ]\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkubetrail%2Ftfutil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkubetrail%2Ftfutil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkubetrail%2Ftfutil/lists"}