1

My goal is to use Keras to visualise the architecture of the model. No training, no inference.

For example if I just want to visualise the graph of the network of the classic VGG16-model with model = create_vgg_like_model() then Keras starts to pre-allocate the memory needed and the program crash (see image).

enter image description here

PC specs: CPU with 4 cores, 8GB RAM. Due to the "huge" amount of parameters of the model it uses even the swap partition.


I tried to play around with

cpu_devices = tf.config.list_physical_devices('CPU')
if cpu_devices:
 try:
 # avoid allocating all memory on the device
 tf.config.set_visible_devices(cpu_devices, 'CPU')
 tf.config.experimental.set_memory_growth(cpu_devices[0], True)
 model = create_vgg_like_model()
 dot = tf.keras.utils.model_to_dot(model) # example of display
 except ValueError as e:
 print(e)

but without success, it raises

ValueError: Cannot set memory growth on non-GPU and non-Pluggable devices.

Is there a way

  • to post-pone the memory allocation on CPU and/or on GPU
  • to implement a Model-like object containing only "shallow" information of the network such as layer name, input, output, number of parameters, ...?
asked Dec 3, 2025 at 15:53
11
  • it happens both with keras and tensorflow.keras Commented Dec 3, 2025 at 16:17
  • imho initialising the model with no weights should do the trick; so -> from keras.applications import VGG16 -> model = VGG16(weights=None) . You can get the model config with layer definitions from there like so: model.get_config(). Commented Dec 9, 2025 at 8:21
  • 1
    weights=None is accepted only by the pre-build models from keras.applications and what it does is a "random initialization" of the weights. Commented Dec 9, 2025 at 9:27
  • Correct, it didn't hit me you will want to go beyond predefined models. In that case - as far as I know, there is no way in keras to get precise architecture stats (i.e. input/output & num. of params details per layer) without building/compiling the model. There is a lazy module in pytorch that might be helpful here, but even there all params/weights are still initialised as soon as you do the first forward pass. Commented Dec 10, 2025 at 9:03
  • @mcvincekova but maybe it is possible to disable that behaviour by overwrite a method of the class Model / Sequential Commented Dec 10, 2025 at 10:57

1 Answer 1

-1

The problem is it didn't detect your gpu that's why it is throwing :ValueError: Cannot set memory growth on non-GPU and non-Pluggable devices. To avoid this issue try removing the line which is causing the issue :

tf.config.experimental.set_memory_growth(cpu_devices[0], True)

Coment it out and it might do the job just fine or if you want to use this try using a proper gpu.

answered Dec 12, 2025 at 12:36
Sign up to request clarification or add additional context in comments.

1 Comment

I mentioned the specs of the machine and no gpu is mentioned. The question is, indeed, how to avoid/postpone that memory allocation

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.