[Python] how to easily modify the number of neurons of a certain layer in a neural network...

Discussão em 'Python' iniciado por Stack, Março 10, 2025.

  1. Stack

    Stack Membro Participativo

    In tensorflow, given a model h = Model(input_layer, output_layer) for example (its summary looks like below)

    Model: "functional_1"
    ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
    ┃ Layer (type) ┃ Output Shape ┃ Param # ┃
    ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
    │ input_layer (InputLayer) │ (None, 784) │ 0 │
    ├─────────────────────────────────┼────────────────────────┼───────────────┤
    │ dense (Dense) │ (None, 64) │ 50,240 │
    ├─────────────────────────────────┼────────────────────────┼───────────────┤
    │ dense_1 (Dense) │ (None, 32) │ 2,080 │
    ├─────────────────────────────────┼────────────────────────┼───────────────┤
    │ dense_2 (Dense) │ (None, 64) │ 2,112 │
    ├─────────────────────────────────┼────────────────────────┼───────────────┤
    │ dense_3 (Dense) │ (None, 784) │ 50,960 │
    └─────────────────────────────────┴────────────────────────┴───────────────┘
    Total params: 105,392 (411.69 KB)
    Trainable params: 105,392 (411.69 KB)
    Non-trainable params: 0 (0.00 B)


    If I want to change the number of neurons of dense_1 from 32 to 16, is there any easy and handy way to update the new configuration?

    I tried h.layers[2].units = 16, which unfortunately didn't change the model setup at all. I am wondering if there is kind of "assignment" operation to do that.

    Continue reading...

Compartilhe esta Página