eaplatanios / tensorflow_scala

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

No implicits found for parameter evOutputToDataType #138

Closed git4sun closed 5 years ago

git4sun commented 5 years ago

Just trying 0.4.0. Looks like there are some changes. The old MNISTLoader example doesn't work now. It is asking for the implicits. I found a package could have the implicits called import org.platanios.tensorflow.api.implicits.Implicits._ but it is not accessible from outside the package. How to properly import the implicits? Thanks.

eaplatanios commented 5 years ago

Are you using Scala 2.11 or 2.12?

git4sun commented 5 years ago

2.12

eaplatanios commented 5 years ago

Could you please send me the code sample that reproduces the compile-time error? I don't get any errors when running the MNIST example. Also, could you try using 0.4.1-SNAPSHOT?

git4sun commented 5 years ago

Here's the code:

import org.platanios.tensorflow.api._
import org.platanios.tensorflow.api.tf.learn._
import org.platanios.tensorflow.data.image.MNISTLoader
// Load and batch data using pre-fetching.
val dataSet = MNISTLoader.load(Paths.get("/tmp"))
val trainImages = tf.data.datasetFromTensorSlices(dataSet.trainImages)
val trainLabels = tf.data.datasetFromTensorSlices(dataSet.trainLabels)
val trainData =
  trainImages.zip(trainLabels)
    .repeat()
    .shuffle(10000)
    .batch(256)
    .prefetch(10)

// Create the MLP model.
val input = Input(FLOAT32, Shape(-1, 28, 28))
val trainInput = Input(INT64, Shape(-1))
val layer = Flatten[Float]("Input/Flatten") >>
  Linear[Float]("Layer_0", 128) >> ReLU[Float]("Layer_0/Activation", 0.1f) >>
  Linear[Float]("Layer_1", 64) >> ReLU[Float]("Layer_1/Activation", 0.1f) >>
  Linear[Float]("Layer_2", 32) >> ReLU[Float]("Layer_2/Activation", 0.1f) >>
  Linear[Float]("OutputLayer", 10)
val loss = SparseSoftmaxCrossEntropy[Float, Long, Float]("Loss") >>
  Mean("Loss/Mean")
val optimizer = tf.train.GradientDescent(1e-6f)
val model = Model.simpleSupervised(input, trainInput, layer, loss, optimizer)

// Create an estimator and train the model.
val estimator = InMemoryEstimator(model)
estimator.train(() => trainData, StopCriteria(maxSteps = Some(1000000)))
git4sun commented 5 years ago

updating to 0.4.1-SNAPSHOT doesn't change anything

eaplatanios commented 5 years ago

I think that you have a small typo in the first few lines. Specifically, the following two lines:

val trainImages = tf.data.datasetFromTensorSlices(dataSet.trainImages)
val trainLabels = tf.data.datasetFromTensorSlices(dataSet.trainLabels)

should be:


val trainImages = tf.data.datasetFromTensorSlices(dataSet.trainImages.toFloat)
val trainLabels = tf.data.datasetFromTensorSlices(dataSet.trainLabels.toLong)

I think the README on the repository is correct as the code sample you have is not exactly the same as the one posted there, right?

git4sun commented 5 years ago

Changing that .toFloat doesn't solve the problem. Still complains about the implicit. I found the implicit value in an example.

implicit val evOutputStructureFloatLong : OutputStructure[(Output[Float], Output[Long])]  = examples.evOutputStructureFloatLong
implicit val evOutputToDataTypeFloatLong: OutputToDataType[(Output[Float], Output[Long])] = examples.evOutputToDataTypeFloatLong
implicit val evOutputToShapeFloatLong   : OutputToShape[(Output[Float], Output[Long])]    = examples.evOutputToShapeFloatLong

https://github.com/eaplatanios/tensorflow_scala/blob/7e63725c3b605e03d62655f36062b8f1ff27dc55/modules/examples/src/main/scala/org/platanios/tensorflow/examples/MNIST.scala

It still didn't solve the problem. Don't know why.

eaplatanios commented 5 years ago

These implicits in the example that you found should only be needed for Scala 2.11. If you copy the example from here that should work even if you remove these helper implicits.

Note that there is a .toFloat and also a .toLong in the change I sent you. Did you change both?

git4sun commented 5 years ago

I got it running in command line. It's the IntelliJ that complains about the missing implicit vals. I guess it is the problem with the IDE. Sorry for the trouble.

eaplatanios commented 5 years ago

No worries at all. I’m glad it’s all good after all. It’s true that IntelliJ is not great sometimes when it comes to recursive implicits, but this should not be for much longer as better language server support for Scala should be coming soon. :)