如何在多个Jupyter Notebook中同时运行TensorFlow后端的Keras?
Hey there! That InternalError: Blas GEMV launch failed error you're hitting is super common when running multiple TensorFlow/Keras notebooks side by side—but rest assured, it's totally possible to run them simultaneously with a few tweaks to how TensorFlow uses GPU memory. Let's break down why this happens and how to fix it:
Why the Error Happens
By default, TensorFlow tries to grab all available GPU memory as soon as it initializes. When you fire up NotebookA first, it hogs most of the GPU's RAM, leaving almost nothing for NotebookB to use when it tries to start training. The BLAS GEMV operation (a core linear algebra routine for model training) can't get the memory it needs, hence the error.
Fixes to Enable Multiple Notebooks
Here are three reliable ways to let multiple Keras notebooks coexist peacefully:
1. Use GPU Memory Growth Mode (Recommended)
This tells TensorFlow to only allocate GPU memory as it needs it, instead of grabbing everything upfront. Add this code at the very start of each notebook:
import tensorflow as tf # Enable GPU memory growth gpus = tf.config.experimental.list_physical_devices('GPU') if gpus: try: for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) print(f"Detected {len(gpus)} physical GPUs; enabled memory growth for all.") except RuntimeError as e: # Memory growth must be set before any TensorFlow operations print(f"Error enabling memory growth: {e}")
This way, each notebook will only use the RAM it needs for its current training batch, freeing up space for other notebooks to run.
2. Limit GPU Memory Usage to a Fixed Percentage
If you prefer more control, you can cap how much of the GPU each notebook can use. For example, to limit each notebook to 40% of the GPU's total memory:
import tensorflow as tf gpus = tf.config.experimental.list_physical_devices('GPU') if gpus: try: # Set memory limit to 40% of the GPU's total RAM tf.config.experimental.set_virtual_device_configuration( gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=0.4)] ) print(f"Limited GPU memory usage to 40% for this notebook.") except RuntimeError as e: print(f"Error setting memory limit: {e}")
Note: If you pass an integer instead of a float, it will set the limit in megabytes (e.g., memory_limit=4096 for 4GB).
3. Assign Different GPUs to Each Notebook
If your machine has multiple physical GPUs, you can direct each notebook to use a separate GPU entirely.
In NotebookA, add this at the start:
import os # Tell TensorFlow to only use GPU 0 os.environ["CUDA_VISIBLE_DEVICES"] = "0"
In NotebookB, add this:
import os # Tell TensorFlow to only use GPU 1 os.environ["CUDA_VISIBLE_DEVICES"] = "1"
This way, the two notebooks won't compete for the same GPU resources at all.
Wrap-Up
You definitely don't have to close one notebook to run the other—these memory allocation tweaks will let you run multiple Keras/TensorFlow notebooks at the same time without hitting that BLAS error. Give the memory growth mode a try first; it's the most flexible and hands-off solution for most cases.
内容的提问来源于stack exchange,提问作者gf_tw

