We propose a typesafe abstraction to tensors (i.e. multidimensional arrays) exploiting the type-level programming capabilities of Scala through heterogeneous lists (HList), and showcase typesafe abstractions of common tensor operations and various neural layers such as convolution or recurrent neural networks. This abstraction could lay the foundation of future typesafe deep learning frameworks that runs on Scala/JVM.
CCS Concepts • Software and its engineering → Software libraries and repositories;We propose a typesafe tensor abstraction, in which the axes are encoded by a heterogeneous list (HList) type parameter.
trait Tensor[D, A <: HList]D is the type of the elements this tensor holds (e.g. Float, Double, etc.), whereas types in the HList type parameter A are phantom types, i.e., they only serve as labels.Basic constructs such as scalars, vectors and matrices could be represented as follows (types A, B etc. are labels / names to axes).
type Scalar = Tensor[Float, HNil] type Vector[A] = Tensor[Float, A :: HNil] type Matrix[A, B] = Tensor[Float, A :: B :: HNil]Looking at more concrete examples from deep learning applications, images in computer vision, or sentences in which each word is mapped to a word embedding (i.e. vector representation of that word in R d space) in natural language processing could be encoded as follows. (See Fig. 1) type Image = Tensor[Float, Width :: Height :: Channel :: HNil] type EmbeddedSentence = Tensor[Float, Word :: Embedding :: HNil]By encoding the meaning of each axis into the type, our system guarantees that all operations on tensors are allowed 3 github.com/milessabin/shapeless. 4 github.com/ctongfei/nexus.