javascript - Memory Management about tf.Model in TensorFlow.js - Stack Overflow

I'm a newbie in TensorFlow.The "Memory Management: dispose and tf.tidy" section in .html

I'm a newbie in TensorFlow.

The "Memory Management: dispose and tf.tidy" section in .html says that we have to manage memories in the special way.

However, the classes in tfjs-layers (e.g. tf.Model and Layer) don't seem to have dispose and tf.tidy doesn't accept those as returned values.

So my questions are:

  • Does tf.Model manage memories automatically?
  • If no, how can I manage memories correctly?

Sample code:

function defineModel(
    regularizerRate: number,
    learningRate: number,
    stateSize: number,
    actionSize: number,
): tf.Model {
    return tf.tidy(() => { // Compile error here, I couldn't return model.
        const input = tf.input({
            name: "INPUT",
            shape: [stateSize],
            dtype: "int32" as any, // TODO(mysticatea): 
        })
        const temp = applyHiddenLayers(input, regularizerRate)
        const valueOutput = applyValueLayer(temp, regularizerRate)
        const policyOutput = applyPolicyLayer(temp, actionSize, regularizerRate)
        const model = tf.model({
            inputs: [input],
            outputs: [valueOutput, policyOutput],
        })

        // TODO(mysticatea): 
        modelpile({
            optimizer: tf.train.sgd(LEARNING_RATE),
            loss: ["meanSquaredError", "meanSquaredError"],
        })
        model.lossFunctions[1] = softmaxCrossEntropy

        return model
    })
}

I'm a newbie in TensorFlow.

The "Memory Management: dispose and tf.tidy" section in https://js.tensorflow/tutorials/core-concepts.html says that we have to manage memories in the special way.

However, the classes in tfjs-layers (e.g. tf.Model and Layer) don't seem to have dispose and tf.tidy doesn't accept those as returned values.

So my questions are:

  • Does tf.Model manage memories automatically?
  • If no, how can I manage memories correctly?

Sample code:

function defineModel(
    regularizerRate: number,
    learningRate: number,
    stateSize: number,
    actionSize: number,
): tf.Model {
    return tf.tidy(() => { // Compile error here, I couldn't return model.
        const input = tf.input({
            name: "INPUT",
            shape: [stateSize],
            dtype: "int32" as any, // TODO(mysticatea): https://github./tensorflow/tfjs/issues/120
        })
        const temp = applyHiddenLayers(input, regularizerRate)
        const valueOutput = applyValueLayer(temp, regularizerRate)
        const policyOutput = applyPolicyLayer(temp, actionSize, regularizerRate)
        const model = tf.model({
            inputs: [input],
            outputs: [valueOutput, policyOutput],
        })

        // TODO(mysticatea): https://github./tensorflow/tfjs/issues/98
        model.pile({
            optimizer: tf.train.sgd(LEARNING_RATE),
            loss: ["meanSquaredError", "meanSquaredError"],
        })
        model.lossFunctions[1] = softmaxCrossEntropy

        return model
    })
}
Share Improve this question edited Apr 8, 2018 at 10:16 mysticatea asked Apr 6, 2018 at 11:12 mysticateamysticatea 2,2371 gold badge14 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You should only use tf.tidy() when directly manipulating tensors.

When you are building a model, you are not yet directly manipulating tensors, rather you are setting up the structure of how layers fit together. This means you don't need to wrap your model creation in a tf.tidy().

Only when you call "predict()" or "fit()" do we deal with concrete Tensor values and need to deal with memory management.

When "predict()" is called, it returns a Tensor, which you must dispose, or surround with a "tidy()".

In the case of "fit()", internally we do all the memory management for you. The return value of "fit()" are plain numbers, so you do not need to wrap it in a "tidy()".

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745052187a4608455.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信