Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Jakob Sultan Ericsson
    @jakeri
    tensorflow/java#320 issue created.
    It might be a threading issue because it actually succeeded ~1 out of 10 times. Or depending on how many cores the unit test runs on.
    1 reply
    Gili Tzabari
    @cowwoc
    Hey guys, is there any documentation for https://github.com/tensorflow/java/tree/master/tensorflow-framework beyond the tiny readme file? For example, is there any public Javadoc for it equivalent to what https://javadoc.io/doc/org.tensorflow/tensorflow-core-api/latest/index.html does for the core API?
    Also javadoc.io will auto generate the javadoc for an artifact if you try to load it (which I just did for tensorflow-framework)
    Gili Tzabari
    @cowwoc
    Oh, very cool. I did not know that. Thank you!
    Out of curiosity, how do I know which version of the API https://www.tensorflow.org/jvm/api_docs/java/org/tensorflow/package-summary is written against? I don't see any versioning information.
    The API changes enough that it would be helpful to know if I'm looking at outdated documentation.
    Adam Pocock
    @Craigacp
    It's the docs from 0.3 at the moment because that was when we got the docs generation working. I believe in the future we'll have the version drop down that Python has, but I'm not sure if that's been set yet.
    Gili Tzabari
    @cowwoc

    Are there any examples that show the usage of the Java framework API for basic workflow like this?

    training_images = training_images/255.0
    test_images = test_images/255.0
    
    model = tf.keras.models.Sequential([#tf.keras.layers.Flatten(),
                                        tf.keras.layers.Dense(64, activation=tf.nn.relu),
                                        tf.keras.layers.Dense(10, activation=tf.nn.softmax)])
    
    model.compile(optimizer = 'adam',
                  loss = 'sparse_categorical_crossentropy')
    
    model.fit(training_images, training_labels, epochs=5)
    
    model.evaluate(test_images, test_labels)
    
    classifications = model.predict(test_images)

    Specifically, I don't see the equivalent of models.Sequential, model.compile, model.fit etc.

    Adam Pocock
    @Craigacp
    We haven't got an implementation of Model yet. It's being worked on. Ditto layers.
    Gili Tzabari
    @cowwoc
    Okay, let's go with the flow then... Say I want to train in python and model.predict() in Java.
    1. What is the easiest way to build a dataset in Java and pass it into python for training? I want to leverage existing Java code to retrieve the input dataset from a database. I don't want to reimplement it in python.
    2. Is there an easy way for me to load a graph in Java that was constructed + trained in python? Can you point me at any sample/test code?
    Adam Pocock
    @Craigacp
    If you want to train an MLP and specify it purely in Java you can do that.
    It's just lower level than Keras.
    For 1, you could emit it as TFRecords, but I'm not sure we've got saving support lined up for that. For 2, yes you can load in a SavedModel, see https://github.com/tensorflow/java-models
    Gili Tzabari
    @cowwoc
    Thank you. If one of my placeholders is a matrix of TFloat32, what's the best way to populate the matrix column by column? I assume I can't push values directly into the Placeholder. Do I invoke Ops.tensorArray() of the same size as the placeholder, then populate that, then eventually invoke Runner.feed(placeholder, array)? Or is there a better way?
    Adam Pocock
    @Craigacp
    Create a TFloat32 either an empty one, or by copying from some source. Then feed it to the placeholder. Note the empty one returns memory which has not been zeroed yet tensorflow/java#271.
    Gili Tzabari
    @cowwoc
    Thanks. How about creating the model in python, but running model.fit() and model.predict() from Java? Do you have any example code for doing that?
    Adam Pocock
    @Craigacp
    I had code for doing it for models emitted by TF 1. Keras's models look different and have different entry points, so while it's probably possible to pull the graph out of a function such that you could train it, it's probably quite tricky with the tools we have at the moment.
    Gili Tzabari
    @cowwoc
    I assume you guys haven't added many of the debugging tools that are present on the Python side, so I think it's best if I construct the model from python, debug model.fit() until I get something working.... then I can move model.fit() to Java and model.predict() would always sit in Java.
    Adam Pocock
    @Craigacp

    So to some extent it depends how complicated your model is. Tribuo's next release exposes TF models but wraps up all the fitting, evaluation and prediction in it's interface to make it a lot simpler. It's not the same as Keras, it's a little bit more like scikit-learn as we don't have callbacks in Tribuo.

    However TF-Java will have this in the future, it's just a lot of stuff to build with a much smaller team than the Keras team.

    Gili Tzabari
    @cowwoc
    Does it really matter how complex the model is? Don't I just need to access the inputs and outputs? I don't need to touch the hidden layers.
    Adam Pocock
    @Craigacp
    Well writing a complex model in Java is more painful than doing it in Python at the moment as we don't have Keras style layers.
    Gili Tzabari
    @cowwoc
    Right, but remember I said that for now I plan to write the model in python and just do model.fit() and model.predict() in Java.
    Adam Pocock
    @Craigacp
    Yes, but figuring out how to save out a Keras model such that you can fit it in Java is very difficult.
    Running predictions in Java is significantly simpler as you can save it out from Python as a Saved Model - https://www.tensorflow.org/guide/keras/save_and_serialize, and then just load that into Java.
    Gili Tzabari
    @cowwoc
    Maybe I'm missing something. Isn't Python model.fit() equivalent to Java session.runner().feed(...).run()?
    Adam Pocock
    @Craigacp
    Unfortunately not. It was closer in TF 1, but in TF 2 Keras owns the data pipeline and iterates it itself. So you'll need to implement the full training loop. Plus the saved model format doesn't (as far as I know) expose the training ops as functions you can call.
    So you'll need to poke at Keras until you can emit a graph, figure out the right names for all the gradient ops in the graph, and then load that graph into java and poke it through session.runner().
    Adam Pocock
    @Craigacp
    Yep.
    There they have a custom training loop outside Keras which is annotated with a function that can be exported in the model so you can see it in Java.
    Gili Tzabari
    @cowwoc
    Okay. Another question: is the Java implementation as efficient as the Python one for model training and predictions?
    Or is there some massive performance difference I should know about up-front? And also I've seen hints of some threading bugs in the github issue tracker.
    Because another approach I could try is loading the dataset in Java, save it somehow... then load it up in python.. do the training... then go back to Java for predictions. The saving/loading data bit would obviously be slower which is why I'm keen to avoid it if possible. But if you're going to tell me that Java training is slower than Python anyway then I'll probably go with this approach instead.
    Adam Pocock
    @Craigacp
    Approximately. In Java we don't have the ability to use XLA to compile the function, we use Graph mode which has some optimizations but not as many things as XLA. Then there are data transfer issues, which should be minimal but as they are still under development we might have bugs. We also can't easily distribute computation across multiple compute nodes (e.g. multi-GPU, or across a cluster), though parallelising things on a CPU works just fine.
    Gili Tzabari
    @cowwoc
    Okay. Is there a standard way to save/load data (similar to how we save/load models)?
    For example, I notice that MNIST embeds both models and data. Is there a way for me to only save/load the data? I'd like to save the data from Java and load it from python.
    Adam Pocock
    @Craigacp
    TFRecords are the preferred input/output format for training data in TF. We can load them in Java, but I don't know if anyone has tried to write them in Java (they are just protobufs).
    Gili Tzabari
    @cowwoc
    Right. There is a TfRecordReader but not corresponding TfRecordWriter.
    Gili Tzabari
    @cowwoc
    Is there a TensorFlow for Python-specific gitter channel I could join to ask questions? Specifically, I'm wondering whether it's possible for me to use the TF 2.x high-level API to create a Sequential() with a mixed input type. Some of the input is a int32. Other parts are float64. Any ideas?
    Jason Zaman
    @perfinion
    specifically Squential()? its pretty trivial to have several inputs if you use the functional API yeah
    Gili Tzabari
    @cowwoc
    Okay, what do I lose if I move over to the functional API? Ease-of-use? Would I lose much of it?
    Jason Zaman
    @perfinion
    lose nothing, the functional API is better than Sequential anyway, for a trivial model with only a couple layers its really just a difference between if you put your layers in a list or just call them in order, for multi-input or multi-output you cant do that with sequential
    Gili Tzabari
    @cowwoc
    Okay, thank you.
    Jason Zaman
    @perfinion
    there are some nice examples on the keras docs, it should be pretty simple to change a Sequential model to use the functional API then from there you can add other stuff easily enough
    Gili Tzabari
    @cowwoc
    Okay. I was under the impression that TF 2.x took over all future Keras API development. Are you saying that the latter is still undergoing independent development and I should look at their docs instead of the TensorFlow ones?
    Jason Zaman
    @perfinion
    the docs on keras.io are still relevant