gorgonia / tensor

package tensor provides efficient and generic n-dimensional arrays in Go that are useful for machine learning and deep learning purposes
Apache License 2.0
359 stars 49 forks source link

Typechecked generic tensor? #137

Closed tholok97 closed 8 months ago

tholok97 commented 8 months ago

Ref this quote from https://github.com/gorgonia/tensor?tab=readme-ov-file#generic-features:

One could argue that this sidesteps the compiler's type checking system, deferring it to runtime (which a number of people consider dangerous). However, tools are being developed to type check these things, and until Go does support typechecked generics, unfortunately this will be the way it has to be. Currently, the tensor package supports limited type of genericity - limited to a tensor of any primitive type.

Go does have typechecked generics as of 1.18. I'm sure you've thought more about this than I have, so I'm very curious to hear your thoughts on templating the Tensor interface by T, so you'd write Tensor[float32] for example. It would allow writing functions operating only on float tensors like so: func dostuff[T Float](t Tensor[T]) Tensor[T]. Another consequence would be Apply taking a function of func (v T) T, rather than func (v interface{}) interface{}).

tholok97 commented 8 months ago

Closing this issue after a chat with @chewxy on the Gophers Slack. It is being worked on in the branch https://github.com/gorgonia/tensor/tree/v0.10.0-generics-working