eaplatanios / tensorflow_scala

TensorFlow API for the Scala Programming Language
http://platanios.org/tensorflow_scala/
Apache License 2.0
936 stars 96 forks source link

Need `apply[T](T*)` method in Tensor.scala v0.4.0-SNAPSHOT #134

Closed mandar2812 closed 5 years ago

mandar2812 commented 5 years ago

Currently the Tensor object has the following methods

  def apply[T: TF](): Tensor[T] = {
    empty[T]
  }

  def apply[T: TF](tensors: Tensor[T]*): Tensor[T] = {
    Basic.stack(tensors, axis = 0)
  }

Its weird that there is no simple apply method.

def apply[T: TF](elements: T*): Tensor[T] = {
    ...
  }
mandar2812 commented 5 years ago

I see that the 0.4.0-SNAPSHOT has not been updated since 14 October, can you update it please @eaplatanios

eaplatanios commented 5 years ago

@mandar2812 This apply should not be needed if you do import org.platanios.tensorflow.api._ because it is handled through implicit conversions of the form implicit def fromSupportedType[T: TF](value: T): Tensor[T]. Does that not work for you?

Also, with respect to 0.4.0-SNAPSHOT yes I have only updated it for Scala 2.12 because I'm trying to fix an issue. I hope this will be resolved by end-of-day today and I will update the artifacts for both Scala 2.11 and 2.12.

mandar2812 commented 5 years ago

@eaplatanios I see, unfortunately the scala compiler complains about this. So in my source file I import the following

import java.nio.ByteBuffer

import io.github.mandar2812.dynaml.probability.RandomVariable
import org.platanios.tensorflow.api.core.types.{Half, TF}
import org.platanios.tensorflow.api._

and the following kind of code throws problems.

def tensor_i64(shape: Int*)(buffer: Long*): Tensor[Long] =
    Tensor(buffer.head, buffer.tail:_*).reshape(Shape(shape:_*))
eaplatanios commented 5 years ago

Could you change the last line to?

Tensor(buffer:_*).reshape(Shape(shape:_*))
mandar2812 commented 5 years ago

So this works

def tensor_i32(shape: Int*)(buffer: Int*): Tensor[Int] =
    Tensor[Int](buffer:_*).reshape(Shape(shape:_*))

So the implicit is being found! But Intellij Idea presentation compiler seems to disagree. But that is not an issue as the code is compiling. Thanks!

eaplatanios commented 5 years ago

@mandar2812 Thanks for verifying it works! :) And yeah the IntelliJ presentation compiler does fail sometimes.

mandar2812 commented 5 years ago

@eaplatanios Sorry for the wrong verification, but I was making a lot of changes to DynaML to make it compatible with tf-0.4.0, and now after all my refactors, I see the following errors.

def tensor_from[D: core.types.TF](shape: Shape)(buffer: D*): Tensor[D] = {
    val t: Tensor[D] = Tensor(buffer:_*)
    t.reshape(shape.toTensor)
  }
error] /Users/mandar/Development/DynaML/dynaml-core/src/main/scala-2.11/io/github/mandar2812/dynaml/tensorflow/api/Api.scala:31: type mismatch;
[error]  found   : Seq[D]
[error]  required: Seq[org.platanios.tensorflow.api.tensors.Tensor[D]]
[error]     val t: Tensor[D] = Tensor(buffer:_*)

This is from command line sbt, so its not just the presentation compiler, but even the standard scala compiler does not pick up the needed implicits, the imports in the beginning of the file are

import java.nio.ByteBuffer

import io.github.mandar2812.dynaml.probability.RandomVariable
//import org.platanios.tensorflow.api.core.types.{Half, core.types.TF}
import org.platanios.tensorflow.api._
mandar2812 commented 5 years ago

@eaplatanios Its also a bit weird that I have to explicitly specify the type for t before reshaping it.

mandar2812 commented 5 years ago

@eaplatanios But the following works!

def tensor_f32(shape: Int*)(buffer: Float*): Tensor[Float] =
    Tensor[Float](buffer).reshape(Shape(shape:_*))

testing it in the DynaML REPL

DynaML>val t = dtf.tensor_f32(2, 2)((1 to 4).map(_.toFloat).toSeq:_*) 
t: org.platanios.tensorflow.api.package.Tensor[Float] = Tensor[Float, [2, 2]]

DynaML>t.summarize() 
res4: String = """Tensor[Float, [2, 2]]
[[1.0, 2.0],
 [3.0, 4.0]]"""