Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keras SpectralNormalization and stop_gradient #20309

Open
dryglicki opened this issue Oct 1, 2024 · 1 comment
Open

Keras SpectralNormalization and stop_gradient #20309

dryglicki opened this issue Oct 1, 2024 · 1 comment
Assignees
Labels
keras-team-review-pending Pending review by a Keras team member. type:Bug

Comments

@dryglicki
Copy link

dryglicki commented Oct 1, 2024

Keras Version: 3.5.0

Hello.

A while ago, I had some issues with PyTorch and SpectralNormalization in an RNN layer: #19527

It looks like some things have changed with SpecNorm, but there's something in the code where I'm seeing degraded performance. So degraded, that I'm getting exploding gradients. The difference comes down to some commented-out code in the SpectralNormalization wrapper:

        """Generate spectral normalized weights.

        This method returns the updated value for `self.kernel` with the
        spectral normalized value, so that the layer is ready for `call()`.
        """

        weights = ops.reshape(self.kernel, [-1, self.kernel_shape[-1]])
        vector_u = self.vector_u.value

        for _ in range(self.power_iterations):
            vector_v = normalize(
                ops.matmul(vector_u, ops.transpose(weights)), axis=None
            )
            vector_u = normalize(ops.matmul(vector_v, weights), axis=None)
->    # vector_u = tf.stop_gradient(vector_u)
->    # vector_v = tf.stop_gradient(vector_v)
        sigma = ops.matmul(
            ops.matmul(vector_v, weights), ops.transpose(vector_u)
        )
        kernel = ops.reshape(ops.divide(self.kernel, sigma), self.kernel_shape)
        return ops.cast(vector_u, self.vector_u.dtype), ops.cast(
            kernel, self.kernel.dtype
        )

The stop_gradient() call is used in both the TF Addons implementation (https://github.com/tensorflow/addons/blob/v0.20.0/tensorflow_addons/layers/spectral_normalization.py#L120-L121) and the official PyTorch implementation (https://pytorch.org/docs/stable/_modules/torch/nn/utils/spectral_norm.html)

I've made the change myself by implementing ops.stop_gradient at both commented-out spots, and expected (stable) functionality returned.

@mehtamansi29 mehtamansi29 added keras-team-review-pending Pending review by a Keras team member. type:Bug labels Oct 4, 2024
@hertschuh
Copy link
Contributor

@dryglicki ,

I believe this omission came from the fact that SpectralNormalization was ported to Keras 3 before ops.stop_gradient was available.

I've made the change myself by implementing ops.stop_gradient at both commented-out spots, and expected (stable) functionality returned

Great! Are you able to open a PR with this fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keras-team-review-pending Pending review by a Keras team member. type:Bug
Projects
None yet
Development

No branches or pull requests

4 participants