张量已处置 [英] Tensor has disposed

查看:57
本文介绍了张量已处置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数TensorFlow JS示例使用浏览器中的预训练模型进行预测.

Most of the TensorFlow JS examples uses pre-trained model in the browser for prediction.

在这里,我试图在浏览器中创建自己的数据并使用该模型训练模型.

Here I am trying to create my own data in the browser and train the model with it.

最后,我认为我已经接近创建一个数据集,可以将其馈送到model.fit()来训练模型

Finally I have reached a point where I think I am close to creating a dataset which can be feed to a model.fit() to train the model

我有两个数组,一个包含图像,另一个包含标签.

I have two arrays, one containing images and other containing labels.

let images = []
let labels = []

我正在使用一种方法从画布捕获图像并将其推入images数组中.

I am using a method to capture images from the canvas and push it in the images array.

function getImage() {
        return tf.tidy(() => {
            const image = tf.browser.fromPixels($('#mycanvas')[0]);
            const batchedImage = image.expandDims(0);
            const norm = batchedImage.toFloat().div(tf.scalar(255)).sub(tf.scalar(1));
            return norm;
        });
    }

因此,每当我按下任意箭头键时,我都会将图像和标签推入数组

So I push an image and a label to the arrays whenever I press any of the arrow key

let collectData = (label) =>{
    tf.tidy(() => {
        const img = getImage();
        img.print() // check if it is a tensor
        //imges.concat(img)
        images.push(img)
        labels.push(label) // labels are 0,1,2
     })
 }

在创建带有训练数据集的数组之后,我将它们传递到model.fit()方法中以开始训练.

After creating the arrays with training dataset, I pass these into model.fit() method to start the training.

let fitModel = async () => { 
        let imageSet = tf.stack(images);
        let labelSet = tf.oneHot(tf.tensor1d(lables, 'int32'), 3); 

        if (currentModel == null) {
            currentModel = createModel();
            currentModel.summary();
        }

        await currentModel.fit(imageSet, labelSet, {
            batchSize: 4,
            epochs: 20,
            shuffle: true,
            validationSplit: 0.1,
            callbacks: {
                onTrainBegin: () => console.log("Training Start"),
                onTrainEnd: () => console.log("Traing End"),
                onBatchEnd: async (num, log) => {
                    await tf.nextFrame();
                    console.log(log)
                }
            }
        })
    }

此处 tf.stack(images); 引发错误说明- Tensor已处置.我不明白为什么会这样.从技术上讲,这应该按照官方文件中的说明进行操作.

Here tf.stack(images); throws error stating - Tensor has disposed. I don't understand why that's happening. Technically this should work as stated in the official document.

const a = tf.tensor1d([1, 2]);
const b = tf.tensor1d([3, 4]);
const c = tf.tensor1d([5, 6]);
let p = []
p.push(a)
p.push(b)
p.push(c)
console.log(p)
tf.stack(p).print();

所以我尝试了另一件事-tf.stack(tf.tensor(images)),但我得到了Tensor处理的错误.

So I tried another thing - tf.stack(tf.tensor(images)) and I get error Tensor has disposed.

我想尝试的另一件事是-tf.concat()也不起作用.任何人都知道如何创建这样的可训练数据集.

Another thing I though would give a try was - tf.concat() which also didn't work. Anyone has any idea how can I create trainable dataset like this.

我的第一个隐藏层具有与inputShape(150,300,3)的conv2d,输出层具有unit:3.

My first hidden layer has conv2d with inputShape(150,300,3) and output layer has unit:3.

任何帮助将不胜感激.请解释您的答案以更好地理解.

Any help would be really appreciated. Please explain your answers for better understanding.

推荐答案

处理张量是因为您在使用张量之前先调用 tf.tidy .要使用张量 img 并清除所有中间张量,必须从 tf.tidy

The tensors are disposed because you call tf.tidy before using them. To use the tensor img and clean all intermediates tensors, it has to be explicitly returned from the callback of tf.tidy

const img = tf.tidy(() => {
        const im = getImage();
        return im;
        // since im is returned, it will not be disposed;
        // but all unused intermediate tensors will be cleaned up
     })

images.push(img)
labels.push(label) // labels are 0,1,2

这篇关于张量已处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆