Categories
Python Answers

How to prevent tensorflow from allocating all of a GPU’s memory with Python?

Sometimes, we want to prevent tensorflow from allocating all of a GPU’s memory with Python.

In this article, we’ll look at how to prevent tensorflow from allocating all of a GPU’s memory with Python.

How to prevent tensorflow from allocating all of a GPU’s memory with Python?

To prevent tensorflow from allocating all of a GPU’s memory with Python, we can create a GPUOptions object.

For instance, we write

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)

sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

to create a GPUOptions object with the per_process_gpu_memory_fraction argument set to 0.333 to make tensorflow use 33.3% of the GPU’s memory.

Then we start a session with gpu_options with

sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

Conclusion

To prevent tensorflow from allocating all of a GPU’s memory with Python, we can create a GPUOptions object.

Categories
Python Answers

How to get indices of N maximum values in a Python NumPy array?

Sometimes, we want to get indices of N maximum values in a Python NumPy array.

In this article, we’ll look at how to get indices of N maximum values in a Python NumPy array.

How to get indices of N maximum values in a Python NumPy array?

To get indices of N maximum values in a Python NumPy array, we can use the np.argpartition method.

For instance, we write

a = np.array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0])
ind = np.argpartition(a, -4)[-4:]

to create the a numpy array with

a = np.array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0])

Then we call argpartition with the array a and -4 to get the indices of the top 4 elements from sorted by value at the given index ascending with

np.argpartition(a, -4)[-4:]

Conclusion

To get indices of N maximum values in a Python NumPy array, we can use the np.argpartition method.

Categories
Python Answers

How to terminate a script with Python?

Sometimes, we want to terminate a script with Python

in this article, we’ll look at how to terminate a script with Python.

How to terminate a script with Python?

To terminate a script with Python, we can call sys.exit.

For instance, we write

import sys

sys.exit()

to call sys.exit with an optional exit code argument.

If we call it with anything other than 0, we exit the script with an abnormal termination.

Conclusion

To terminate a script with Python, we can call sys.exit.

Categories
Python Answers

How to search a list of dictionaries with Python?

Sometimes, we want to search a list of dictionaries with Python.

In this article, we’ll look at how to search a list of dictionaries with Python.

How to search a list of dictionaries with Python?

To search a list of dictionaries with Python, we can use the next function.

For instance, we write

dicts = [
    {"name": "jane", "age": 10},
    {"name": "mark", "age": 5},
    {"name": "john", "age": 7},
    {"name": "alex", "age": 12},
]

next(item for item in dicts if item["name"] == "jane")

to call next with an the items returned by the item for item in dicts if item["name"] == "jane" list to get the first item in dicts that has the dict entry with key name set to 'jane'.

Conclusion

To search a list of dictionaries with Python, we can use the next function.

Categories
Python Answers

How to save plot to image file instead of displaying it using Python Matplotlib?

Sometimes, we want to save plot to image file instead of displaying it using Python Matplotlib.

In this article, we’ll look at how to save plot to image file instead of displaying it using Python Matplotlib.

How to save plot to image file instead of displaying it using Python Matplotlib?

To save plot to image file instead of displaying it using Python Matplotlib, we can use the savefig method.

For instance, we write

from matplotlib import pyplot as plt

plt.savefig('foo.png')

to call savefig to save the plot to foo.png.

Conclusion

To save plot to image file instead of displaying it using Python Matplotlib, we can use the savefig method.