Closed git4sun closed 5 years ago
Are you using Scala 2.11 or 2.12?
2.12
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
?
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)))
updating to 0.4.1-SNAPSHOT doesn't change anything
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?
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
It still didn't solve the problem. Don't know why.
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?
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.
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. :)
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 calledimport org.platanios.tensorflow.api.implicits.Implicits._
but it is not accessible from outside the package. How to properly import the implicits? Thanks.