python
是一个神经网络,
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))
是上述
encoder
函数。如何包括此功能并具有GPU并行化?任何想法/建议都将受到赞赏。
yyes,TensorFlow可以采用外部Python功能。
您正在寻找的是
decoder
。
https://www.tensorflow.org/api_docs/python/tf/py_function
请注意,如果您想使用外部Python代码,这是唯一的解决方案,可以包装到python
我最近也因这个问题而困扰,我需要处理神经网络的输出,因此我需要调用用纯numpy编写的函数来计算输出后的损失。使用TF时。 py_function在TensorFlow中,我发现对于未由TF操作的函数,尽管PY_FUNCTICT可以获得计算结果,但该结果不能用于将梯度保存用于反向传播。
tf.py_function()
如何在TensorFlow 2/keras自定义层中使用基于Numpy的外部库功能?
https://github.com/tensorflow/tensorflow/issues/32964