[Python] Can TensorFlow take external Python functions when training a NN?

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

  1. Stack

    Stack Membro Participativo

    I am trying to train an autoencoder but having already known the decoder (which is a python function). My idea is to train the encoder and feed the latent space to an already known decoder, which is a function in python I already have. So, I am wondering if there is a way to train the encoder by adding the decoder function, but without losing parallelization over GPU. The part of code related to one training iteration would be like this:

    with tf.GradientTape() as tape:
    latent_space = encoder(x_batch, training=True) # NN model
    output = decoder(latent_space) # Python function
    loss_value = loss(y_batch, output)
    grads = tape.gradient(loss_value, encoder.trainable_variables)
    optimizer.apply_gradients(zip(grads, model.trainable_variables))


    where encoder is a neural network and decoder is the aforementioned python function. How can I include this function and have GPU parallelization? Any ideas/suggestions are appreciated.

    Continue reading...

Compartilhe esta Página