{"id":13801692,"url":"https://github.com/SciScala/NDScala","last_synced_at":"2025-05-13T11:31:40.820Z","repository":{"id":46277025,"uuid":"272212766","full_name":"SciScala/NDScala","owner":"SciScala","description":"N-dimensional / multi-dimensional arrays (tensors) in Scala 3. Think NumPy ndarray / PyTorch Tensor but type-safe over shapes, array/axis labels \u0026 numeric data types","archived":false,"fork":false,"pushed_at":"2022-12-22T18:02:46.000Z","size":186,"stargazers_count":47,"open_issues_count":0,"forks_count":6,"subscribers_count":7,"default_branch":"main","last_synced_at":"2024-11-18T16:57:41.597Z","etag":null,"topics":["array-programming","dotty","machine-learning","matrix","multi-dimensional-array","n-dimensional-array","named-tensor","ndarray","neural-networks","numerical-computing","numpy","scala","scala-3","scala3","shape-safety","tensor"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SciScala.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-14T13:57:45.000Z","updated_at":"2024-08-24T14:25:09.000Z","dependencies_parsed_at":"2023-01-30T15:46:06.233Z","dependency_job_id":null,"html_url":"https://github.com/SciScala/NDScala","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/SciScala%2FNDScala","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SciScala%2FNDScala/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SciScala%2FNDScala/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SciScala%2FNDScala/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SciScala","download_url":"https://codeload.github.com/SciScala/NDScala/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253932923,"owners_count":21986478,"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":["array-programming","dotty","machine-learning","matrix","multi-dimensional-array","n-dimensional-array","named-tensor","ndarray","neural-networks","numerical-computing","numpy","scala","scala-3","scala3","shape-safety","tensor"],"created_at":"2024-08-04T00:01:25.979Z","updated_at":"2025-05-13T11:31:36.201Z","avatar_url":"https://github.com/SciScala.png","language":"Scala","funding_links":[],"categories":["Table of Contents","Scala"],"sub_categories":["Science and Data Analysis","General-Purpose Machine Learning"],"readme":"Training a (shape-safe) neural network in 10 lines:\n\nIn NDScala:\n```scala\n//After some setup\n//Declaring types and their corresponding values\ntype Mat10kX10k = 10000 #: 10000 #:SNil\ntype AxisLabels = \"AxisLabel\" ##: \"AxisLabel\" ##: TSNil\nval mat10kX10k = shapeOf[Mat10kX10k]\nval axisLabels = tensorShapeDenotationOf[AxisLabels]\n\nval ones = Tensor(Array.fill(100000000)(1.0f),\"TensorLabel\",axisLabels, mat10kX10k)\n\ndef train(x: Tensor[Float, (\"TensorLabel\", AxisLabels, Mat10kX10k)],\n          y: Tensor[Float, (\"TensorLabel\", AxisLabels, Mat10kX10k)],\n          w0: Tensor[Float, (\"TensorLabel\", AxisLabels, Mat10kX10k)],\n          w1: Tensor[Float, (\"TensorLabel\", AxisLabels, Mat10kX10k)],\n          iter: Int): Tuple2[Tensor[Float, (\"TensorLabel\", AxisLabels, Mat10kX10k)],\n                             Tensor[Float, (\"TensorLabel\", AxisLabels, Mat10kX10k)]] =\n    if iter == 0 then (w0, w1)\n    else\n        val l1 =  (x.matmul(w0)).sigmoid()\n        val l2 = (l1.matmul(w1)).sigmoid()\n        val error = y - l2\n        val l2Delta = (error) * (l2 * (ones - l2))\n        val l1Delta =  (l2Delta.matmul(w1.transpose))\n        val w1New = w1 + (((l1.transpose).matmul(l2Delta)))\n        val w0New = w0 + (((x.transpose).matmul(l1Delta)))\n        train(x,y,w0New,w1New,iter-1)\n```\n\nAnd for reference, in NumPy, in 10 lines:\n\n```python\ndef train(X,Y,iter): \n    syn0 = 2*np.random.random((10000,10000)).astype('float32') - 1\n    syn1 = 2*np.random.random((10000,1000)).astype('float32') - 1\n    for j in range(iter): \n        l1 = 1/(1+np.exp(-(np.dot(X,syn0))))  \n        l2 = 1/(1+np.exp(-(np.dot(l1,syn1)))) \n        error = y - l2\n        l2_delta = (error)*(l2*(1-l2))\n        l1_delta = l2_delta.dot(syn1.T) * (l1 * (1-l1))\n        syn1 += l1.T.dot(l2_delta)\n        syn0 += X.T.dot(l1_delta) \n```\n\nThe run time of the NDScala version is ~80% of that of NumPy w/MKL\n\nThe PyTorch equivalent is slightly faster, at ~85% of the NDScala version run time.\nThis can be accounted for by the copy overhead of passing data between the JVM and native memory.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSciScala%2FNDScala","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSciScala%2FNDScala","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSciScala%2FNDScala/lists"}