Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    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
    but yeah tf.keras is where the majority of the dev is done
    Gili Tzabari
    @cowwoc
    Are the docs on keras.io fully up-to-date with the tf.keras API? Or are they older?
    Jason Zaman
    @perfinion
    not sure about all of them, but i have definitely seen plenty of updates recently yeah
    Gili Tzabari
    @cowwoc

    Okay, thank you.

    Another question: My dataset is dynamically generated from a database. All online tutorials I saw focus on pulling in an existing dataset (e.g. MNIST) or from CSV files on disk. Neither seems like a good fit for my case. Should I be "streaming" data from the database to the model for training somehow? Or am I expected to construct a fixed-sized tensor and populate it column by column based on the database resultset?

    Jason Zaman
    @perfinion
    oh heh. yeah i hate that part of all the intro docs, for real things your dataset doesnt fit in memory so yeah absolutely should be streamed in
    use hte tf.data.Dataset api and it should read in data then you .map() to preprocess how your model wants it (eg resize the images or whatever)
    SIG-IO has lots of other extensions for pulling data from other places if the builtin things arent good enough
    Gili Tzabari
    @cowwoc
    Thank you. That's very helpful. And I assume that if I want to shuffle the streamed dataset I'll have to shuffle on the DB end, right?
    Somehow... I'm not even sure how to do that to be honest :)