tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.44k stars 1.92k forks source link

String identifiers for losses, activations, etc. are not clearly documented in the API docs. #1315

Closed nsthorat closed 1 year ago

nsthorat commented 5 years ago

See https://stackoverflow.com/questions/54980045/tensorflow-js-is-there-a-list-of-all-the-identifiers

Model.compile should inline all the loss types for you: https://js.tensorflow.org/api/0.15.3/#tf.Model.compile

davidsoergel commented 5 years ago

This applies to activations too.

davidsoergel commented 5 years ago

Oh yeah: also metrics, constraints, initializers, and regularizers.

Search for type .*Identifier in the tfjs-layers code to find all the definitions.

emer7 commented 5 years ago

Hi, I'm new to Tensorflow but would like to learn something new! Is this issue available to be worked on?

nsthorat commented 5 years ago

This one unfortunately might be a little involved so I'm going to take off the good first issue tag :)

tafsiri commented 5 years ago

related to #680 #1171

sbrl commented 3 years ago

Just currently falling over this. Are the string names for the loss functions in Tensorflow.js documented anywhere? I'm having some serious trouble trying to train a model for my PhD, because I can't get the right loss function. I thought at first that the loss function names would match the documentation, but apparently not.

It's rather awkward to use Tensorflow.js at all without any loss functions.

Edit: I've offered a bounty on that Stackoverflow question.

davidsoergel commented 3 years ago

This is not exactly "documentation", and it's obviously not very easy to find, but at least it will solve your concrete problem now: https://github.com/tensorflow/tfjs/blob/f111dc03a87ab7664688011812beba4691bae455/tfjs-layers/src/keras_format/loss_config.ts#L16

Similarly, for other string identifiers (activations, metrics, constraints, initializers, and regularizers), see the neighboring files under https://github.com/tensorflow/tfjs/tree/master/tfjs-layers/src/keras_format.

In general these should match the string identifiers used for Python Keras. For instance, https://www.tensorflow.org/api_docs/python/tf/keras/losses/CategoricalCrossentropy shows name='categorical_crossentropy'. However, the TFJS implementations may not be up to date with the Python Keras ones (in particular, any new losses etc. added in the last year or two on the Python side are probably not implemented in TFJS yet).

sbrl commented 3 years ago

Thanks so much, @davidsoergel! That's very helpful. It is no substitute for proper documentation, but it will be helpful to solve my immediate issue. Thanks again!

sbrl commented 3 years ago

I've just tried the sparse_categorical_crossentropy one like this @davidsoergel:

model.compile({
    optimizer: this.tf.train.adam(),
    loss: 'sparse_categorical_crossentropy',
    // These are guesses - we don't actually know just yet what they are called
    metrics: [ "accuracy", "categoricalCrossentropy", "categoricalAccuracy" ]
});

....but it crashes with the following error:

ValueError: Unknown loss sparse_categorical_crossentropy
    at new ValueError (path/to/project/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:186:28)
    at get (path/to/project/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:5177:15)
    at LayersModel.compile (path/to/project/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:8966:34)
    at Sequential.compile (path/to/project/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:10894:20)
    at TemporalCNN.reset (file:///path/to/project/common/ai/temporal_cnn/TemporalCNN.mjs:353:14)
    ....

If I try specifying loss: this.tf.losses.sparseCategoricalCrossentropy directly, then I get this error:

ValueError: When passing an Array as loss, it should have one entry per model output. The model has 1 output(s), but you passed loss=.
    at new ValueError (path/to/project/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:186:28)
    at LayersModel.compile (path/to/project/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:8958:23)
    at Sequential.compile (path/to/project/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:10894:20)
    at TemporalCNN.reset (file://path/to/project/common/ai/temporal_cnn/TemporalCNN.mjs:353:14)

....so I'm out of ideas.

sbrl commented 3 years ago

It looks like sparseCategoricalCrossentropy is not actually exposed in tf.losses:

console.log(tf.losses);
{
  absoluteDifference: [Function: absoluteDifference__op],
  computeWeightedLoss: [Function: computeWeightedLoss__op],
  cosineDistance: [Function: cosineDistance__op],
  hingeLoss: [Function: hingeLoss__op],
  huberLoss: [Function: huberLoss__op],
  logLoss: [Function: logLoss__op],
  meanSquaredError: [Function: meanSquaredError__op],
  sigmoidCrossEntropy: [Function: sigmoidCrossEntropy__op],
  softmaxCrossEntropy: [Function: softmaxCrossEntropy__op]
}

....so I think there's another factor at play here and the file you linked to @davidsoergel doesn't accurately reflect the loss functions implemented?

Edit: Yet in the tests, I see this: https://github.com/tensorflow/tfjs/blob/f111dc03a87ab7664688011812beba4691bae455/tfjs-layers/src/losses_test.ts#L215-L241

And I also see sparseCategoricalAccuracy, yet it crashes with ValueError: Unknown metric sparseCategoricalAccuracy if I try and use it? Things seem broken to me.

gaikwadrahul8 commented 1 year ago

Hi, @nsthorat

Thank you for opening this issue for tracking purposes. Since this issue has been open for a long time, the code/debug information for this issue may not be relevant with the current state of the code base.

The TFJs team is constantly improving the framework by fixing bugs and adding new features. We suggest you try the latest TFJs version with the latest compatible hardware configuration which could potentially resolve the issue. We can keep the issue open if it is still relevant. Please confirm if we need to keep the issue open.

Thank you for your support and cooperation.

gaikwadrahul8 commented 1 year ago

Hi, @nsthorat

We have not received any confirmation from you, so we are closing this issue now. If the issue is still relevant, please let us know, either we'll re-open this issue or please feel free to create new issue after trying with latest version of Tensorflow.js. Thank you!