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

Example in Readme doesn't work with version 0.2.3 #124

Closed git4sun closed 5 years ago

git4sun commented 5 years ago

No class in the package is named: DatasetFromSlices, Estimator.train() method is missing, MNISTLoader is in data.image not data.loaders package. Do you have a working tutorial? Or detailed document?

eaplatanios commented 5 years ago

Sorry about the outdated readme. I've been working on some updates. Does the following work for you?

  import org.platanios.tensorflow.api._
  import org.platanios.tensorflow.api.tf.learn._
  import org.platanios.tensorflow.data.loaders.image.MNISTLoader

  // Load and batch data using pre-fetching.
  val dataSet = MNISTLoader.load(Paths.get("/tmp"))
  val trainImages = DatasetFromSlices(dataSet.trainImages)
  val trainLabels = DatasetFromSlices(dataSet.trainLabels)
  val trainData =
    trainImages.zip(trainLabels)
        .repeat()
        .shuffle(10000)
        .batch(256)
        .prefetch(10)

  // Create the MLP model.
  val input = Input(UINT8, Shape(-1, 28, 28))
  val trainInput = Input(UINT8, Shape(-1))
  val layer = Flatten() >> Cast(FLOAT32) >> 
      Linear(128, name = "Layer_0") >> ReLU(0.1f) >>
      Linear(64, name = "Layer_1") >> ReLU(0.1f) >>
      Linear(32, name = "Layer_2") >> ReLU(0.1f) >>
      Linear(10, name = "OutputLayer")
  val trainingInputLayer = Cast(INT64)
  val loss = SparseSoftmaxCrossEntropy() >> Mean()
  val optimizer = GradientDescent(1e-6)
  val model = Model(input, layer, trainInput, trainingInputLayer, loss, optimizer)

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

No.

  1. From the jar file, it doesn't seem to have data.loaders._. I do find org.platanios.tensorflow.data.image.MNISTLoader

  2. DatasetFromSlices is not recognized. I also didn't find the class in the source code. Do I suppose to create a class with that name by myself?

  3. org.platanios.tensorflow.api.ops.training.optimizers.GradientDescent should be imported. GradientDescent is not in other import path.

  4. Flatten() line complains about not enough argument for method apply. Are there any implicit that I need to import or create? Like tensorflow session?

BTW, I am using IntelliJ 2018.1. Using a sbt project.

eaplatanios commented 5 years ago

I'm sorry this is indeed very outdated and I can't test right now. Can you try the following:

  import org.platanios.tensorflow.api._
  import org.platanios.tensorflow.api.tf.learn._
  import org.platanios.tensorflow.api.ops.training.optimizers.GradientDescent
  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.TensorSlicesDataset(dataSet.trainImages)
  val trainLabels = tf.data.TensorSlicesDataset(dataSet.trainLabels)
  val trainData =
    trainImages.zip(trainLabels)
        .repeat()
        .shuffle(10000)
        .batch(256)
        .prefetch(10)

  // Create the MLP model.
  val input = Input(UINT8, Shape(-1, 28, 28))
  val trainInput = Input(UINT8, Shape(-1))
  val layer = Flatten("Input/Flatten") >> Cast(FLOAT32) >> 
      Linear("Layer0", 128) >> ReLU("Layer0/Activation", 0.1f) >>
      Linear("Layer1", 64) >> ReLU("Layer1/Activation", 0.1f) >>
      Linear("Layer2", 32) >> ReLU("Layer2/Activation", 0.1f) >>
      Linear("OutputLayer", 10)
  val trainingInputLayer = Cast("TrainInput/Cast", INT64)
  val loss = SparseSoftmaxCrossEntropy("Loss/CrossEntropy") >> Mean("Loss/Mean")
  val optimizer = GradientDescent(1e-6)
  val model = Model(input, layer, trainInput, trainingInputLayer, loss, optimizer)

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

Cast(FLOAT32) need a name parameter, estimator.train is not recognized. I checked the val types. It looks like Model(input, layer, trainInput, trainingInputLayer, loss, optimizer) and Estimator(model) didn't return Model and Estimator object. The compiler only sees Any. Not sure if it is related but I didn't find case class for Model and Estimator in the source code.

eaplatanios commented 5 years ago

Ok I think that until I get to a computer and can test this, the best solution for you is to look at an example that compiles and runs fine here: https://github.com/eaplatanios/tensorflow_scala/blob/master/examples/src/main/scala/org/platanios/tensorflow/examples/MNIST.scala

git4sun commented 5 years ago

The example doesn't work as well with version 0.2.4. It complains about Tensor[DataType]. I guess your latest changes are not in 0.2.4. I will just wait for you to update the docs then.

eaplatanios commented 5 years ago

Oh yes, I have updated everything to version 0.3.0-SNAPSHOT. Why do you want to use version 0.2.4?

git4sun commented 5 years ago

Now everything works with the new version. Thanks for the help.

git4sun commented 5 years ago

I got exception running your example:

Caused by: org.platanios.tensorflow.jni.NotFoundException: dlopen(/var/folders/17/53d8q3pd1qz8b3mpp5zzhyrm0000gn/T/tensorflow_scala_native_libraries7879828405513743896/libtensorflow_ops.so, 6): Symbol not found: __ZN10tensorflow22CheckNotInComputeAsyncEPNS_15OpKernelContextEPKc
  Referenced from: /var/folders/17/53d8q3pd1qz8b3mpp5zzhyrm0000gn/T/tensorflow_scala_native_libraries7879828405513743896/libtensorflow_ops.so (which was built for Mac OS X 10.14)
  Expected in: /private/var/folders/17/53d8q3pd1qz8b3mpp5zzhyrm0000gn/T/tensorflow_scala_native_libraries7879828405513743896/libtensorflow_framework.so
 in /var/folders/17/53d8q3pd1qz8b3mpp5zzhyrm0000gn/T/tensorflow_scala_native_libraries7879828405513743896/libtensorflow_ops.so
    at org.platanios.tensorflow.jni.TensorFlow$.loadOpLibrary(Native Method)
    at org.platanios.tensorflow.jni.TensorFlow$.$anonfun$load$6(TensorFlow.scala:107)
    at scala.Option.foreach(Option.scala:257)
    at org.platanios.tensorflow.jni.TensorFlow$.load(TensorFlow.scala:107)
    at org.platanios.tensorflow.jni.TensorFlow$.<init>(TensorFlow.scala:155)
    at org.platanios.tensorflow.jni.TensorFlow$.<clinit>(TensorFlow.scala)
    ... 7 more

Is it the installation issue? I am using the option of classifier "darwin-cpu-x86_64". Do I need to compile tensorflow and specify the path?

eaplatanios commented 5 years ago

The precompiled binaries that are currently distributed are broken due to an issue in the main TensorFlow repository. I'm waiting for an issue to be resolved there and then I can update ours. For now, you can get rid of this error by compiling TensorFlow yourself, from the master branch and adding the compiled shared libraries to LD_LIBRARY_PATH.

git4sun commented 5 years ago

I tried to compile tensorflow and the example code throws the following exception:

Exception in thread "main" java.lang.UnsatisfiedLinkError: /private/var/folders/17/53d8q3pd1qz8b3mpp5zzhyrm0000gn/T/tensorflow_scala_native_libraries6375800895764471554/libtensorflow_jni.so: dlopen(/private/var/folders/17/53d8q3pd1qz8b3mpp5zzhyrm0000gn/T/tensorflow_scala_native_libraries6375800895764471554/libtensorflow_jni.so, 1): Library not loaded: @rpath/libtensorflow.so
  Referenced from: /private/var/folders/17/53d8q3pd1qz8b3mpp5zzhyrm0000gn/T/tensorflow_scala_native_libraries6375800895764471554/libtensorflow_jni.so
  Reason: image not found

I am using IntelliJ and added the path to .so file in vm options like this: -Djava.library.path="/xxx/tf/bazel-bin/tensorflow". Is that the correct way of doing this? It seems the library is not loaded.

eaplatanios commented 5 years ago

No that is not the right way. You would need to add an environment variable in the IntelliJ run configuration that looks like this:

LD_LIBRARY_PATH="xxx/tf/bazel-bin/tensorflow:$LD_LIBRARY_PATH"

git4sun commented 5 years ago

Still can't get the example working.

Caused by: org.platanios.tensorflow.jni.NotFoundException: dlopen(/var/folders/17/53d8q3pd1qz8b3mpp5zzhyrm0000gn/T/tensorflow_scala_native_libraries1379345719323358294/libtensorflow_ops.so, 6): Symbol not found: __ZN10tensorflow10CopyTensor6ViaDMAENS_11StringPieceEPNS_13DeviceContextES3_PNS_6DeviceES5_NS_19AllocatorAttributesES6_PKNS_6TensorEPS7_iNSt3__18functionIFvRKNS_6StatusEEEE
  Referenced from: /var/folders/17/53d8q3pd1qz8b3mpp5zzhyrm0000gn/T/tensorflow_scala_native_libraries1379345719323358294/libtensorflow_ops.so (which was built for Mac OS X 10.14)
  Expected in: /Users/yang/tf/bazel-bin/tensorflow/libtensorflow_framework.so
 in /var/folders/17/53d8q3pd1qz8b3mpp5zzhyrm0000gn/T/tensorflow_scala_native_libraries1379345719323358294/libtensorflow_ops.so
    at org.platanios.tensorflow.jni.TensorFlow$.loadOpLibrary(Native Method)
    at org.platanios.tensorflow.jni.TensorFlow$.$anonfun$load$6(TensorFlow.scala:107)
    at scala.Option.foreach(Option.scala:257)
eaplatanios commented 5 years ago

@git4sun I'm finally back from traveling but unfortunately I'm still waiting on an issue from the TensorFlow team. Is this occurring on a Mac or a Linux machine for you?

git4sun commented 5 years ago

I’m on a Mac

On Tue, Sep 11, 2018 at 6:09 PM Anthony Platanios notifications@github.com wrote:

@git4sun https://github.com/git4sun I'm finally back from traveling but unfortunately I'm still waiting on an issue from the TensorFlow team. Is this occurring on a Mac or a Linux machine for you?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/eaplatanios/tensorflow_scala/issues/124#issuecomment-420477470, or mute the thread https://github.com/notifications/unsubscribe-auth/AFpew__dE9CTGoW040BgWozf-ifdnG8pks5uaF7ngaJpZM4V7j1V .

eaplatanios commented 5 years ago

Could you try again using the latest 0.3.0-SNAPSHOT and the precompiled binaries distributed with TF Scala? It currently works using the master branch of TensorFlow (i.e., the nightly builds). I finally got what we needed from the TF team. :)

git4sun commented 5 years ago

The demo example worked with the new updates. Thanks a lot! I can start to play around.

On Sat, Sep 15, 2018 at 11:04 AM Anthony Platanios notifications@github.com wrote:

Could you try again using the latest 0.3.0-SNAPSHOT and the precompiled binaries distributed with TF Scala? It currently works using the master branch of TensorFlow (i.e., the nightly builds). I finally got what we needed from the TF team. :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/eaplatanios/tensorflow_scala/issues/124#issuecomment-421608948, or mute the thread https://github.com/notifications/unsubscribe-auth/AFpew6GU-vqNPz1TwpxEgSRp43d7TT_iks5ubUE3gaJpZM4V7j1V .

eaplatanios commented 5 years ago

No problem. Thanks for verifying! :)