Convert Keras model to SNN and load pre-trained weights

Hi everyone, I’m back on the forum.

I have this situations:
(1) - A classical network in Keras models implemented and with the synaptic weights of its training saved.

>>   model = Sequential([tf.keras.layers.Dense(128, input_dim = 3, activation = 'relu', 
                       kernel_constraint=maxnorm(weight_constraint)), 
                       tf.keras.layers.Dropout(dropout_rate), 
                       tf.keras.layers.Dense(64, activation = 'relu', 
                       kernel_constraint=maxnorm(weight_constraint)), 
                       tf.keras.layers.Dense(3, activation = activation)])

>>   model.save_weights('keras_to_snn_params(1).npz')

(2) - The classical neural network implemented above converted into SNN and with pre-trained weights:

>> from urllib.request import urlretrieve
>>
>> nengo_converter = nengo_dl.Converter(model)
>>
>> do_training = False
>> if do_training:   
>>          # To do training ...   
>> else:
>>    urlretrieve(
>>        "https://drive.google.com/drive/my-drive?hl=pt-BR",
>>        "keras_to_snn_params(1).npz",
>>    )
>>    print("Loaded pretrained weights")

I want to know how I can use the synaptic training weights of the classic model in the converted pulsed neural network, is there any strategy that I can adopt? So I won’t need to retrain the network, because I already have it validated in keras (tensorflow). Is it possible to do this?

My next step would be to implement:

 # build network, load in trained weights, run inference on test images
    with nengo_dl.Simulator(
        nengo_converter.net, minibatch_size=10, progress_bar=False
    ) as nengo_sim:
        nengo_sim.load_params("keras_to_snn_params(1)")
        [... ?????? ...]

Thank u, best regards.

Hii @xchoo and @Eric , can u help me??? :sweat_smile:

Can someone help me?

Can you load the weights into the Keras model before you convert it? That seems like the easiest solution to me.

I understood @Eric , thank u. Does loading the weights before the conversion guarantee me that it has the same synaptic weights as the classic model?

Yes, it should have all the same weights.

1 Like