Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear out/delete tensors in tensorflow?

As I understand, tf.reset_default_graph() only creates a new graph and sets it equal to the default graph. So, the previously created tensors would just be lying around occupying the memory. I have also read the unreferenced tensors are not garbage collected (like normal variables in Python are).

If I am running a cross-validation to search for a set of hyperparameters and thus creating the same graph, again and again, how do I get rid of the previously created tensors?

like image 285
MiloMinderbinder Avatar asked Jul 06 '18 17:07

MiloMinderbinder


People also ask

How to uninstall TensorFlow in Python?

Or, if you installed tensorflow-gpu: pip install tensorflow-gpu Once in a while, the pip in your current path is not the same pip you used to install it. In this case, find the Python environment where the TensorFlow install lives and run: /path/to/python -m pip uninstall tensorflow

What is TensorFlow JS?

TensorFlow.js is a framework to define and run computations using tensors in JavaScript. A tensor is a generalization of vectors and matrices to higher dimensions. The central unit of data in TensorFlow.js is the tf.Tensor: a set of values shaped into an array of one or more dimensions. tf.Tensor s are very similar to multidimensional arrays.

How do I reshape a tensor in TensorFlow?

# You can reshape a tensor to a new shape. The data maintains its layout in memory and a new tensor is created, with the requested shape, pointing to the same data. TensorFlow uses C-style "row-major" memory ordering, where incrementing the rightmost index corresponds to a single step in memory.

How to inspect a tensor's data type in TensorFlow?

To inspect a tf.Tensor 's data type use the Tensor.dtype property. When creating a tf.Tensor from a Python object you may optionally specify the datatype. If you don't, TensorFlow chooses a datatype that can represent your data. TensorFlow converts Python integers to tf.int32 and Python floating point numbers to tf.float32.


1 Answers

I had the same problem when designing experiments, after researching about this problem, the only solution that worked for me is this one. As you can read in that link, it seems to be a design flaw and the TF team doesn't seem to care about fixing.

The solution is to create a new process for each cross-validation iteration. So when the process finishes the system kills it and releases the resources automatically.

import multiprocessing

def evaluate(...):
    import tensorflow as tf
    # Your logic

for ... in cross_valiadtion_loop:
    process_eval = multiprocessing.Process(target=evaluate, args=(...))
    process_eval.start()
    process_eval.join()
like image 50
Pedrolarben Avatar answered Oct 30 '22 10:10

Pedrolarben