Skip to content

GradientTape.gradient error if tf.slice is used #800

Closed

Description

@MPnoy

// Sample data
var X = np.array(3.3f, 4.4f, 5.5f, 6.71f, 6.93f, 4.168f, 9.779f, 6.182f, 7.59f, 2.167f,
7.042f, 10.791f, 5.313f, 7.997f, 5.654f, 9.27f, 3.1f);
var Y = np.array(1.7f, 2.76f, 2.09f, 3.19f, 1.694f, 1.573f, 3.366f, 2.596f, 2.53f, 1.221f,
2.827f, 3.465f, 1.65f, 2.904f, 2.42f, 2.94f, 1.3f);
var n_samples = X.shape[0];

// We can set a fixed init value in order to demo
var W = tf.Variable(-0.06f, name: "weight");
var b = tf.Variable(-0.73f, name: "bias");

// Wrap computation inside a GradientTape for automatic differentiation.
using var g = tf.GradientTape();
// Linear regression (Wx + b).
var pred = W * X + b;

// ------ Do nothing ------
var test = tf.slice(pred, new[] { 0 }, pred.shape);

// Mean square error.
var loss = tf.reduce_sum(tf.pow(test - Y, 2)) / (2 * n_samples);
// should stop recording
// Compute gradients.

// Error
var gradients = g.gradient(loss, (W, b));

Activity

  1. changed the title [-]GradientTape.gradients error if tf.slice is used[/-] [+]GradientTape.gradient error if tf.slice is used[/+] on Apr 22, 2021
  2. MPnoy commented on Apr 22, 2021

    @MPnoy
    ContributorAuthor

    Done

  3. MPnoy commented on Apr 22, 2021

    @MPnoy
    ContributorAuthor

    @Oceania2018 Also I found that GradientTape.gradient after tf.concat always returns (null, null)

  4. Oceania2018 commented on Apr 22, 2021

    @Oceania2018
    Member

    @MPnoy I got (None, None) in python:
    image

  5. added a commit that references this issue on Apr 22, 2021
  6. MPnoy commented on Apr 23, 2021

    @MPnoy
    ContributorAuthor

    @Oceania2018 Thanks for the fix, it works.
    About the concat: there is a issue in the tensorflow itself. tensorflow/tensorflow#37726
    Except for the rest, there is one working code there: tensorflow/tensorflow#37726 (comment)

    I rewrote this into unit test and PR if necessary
    83bd764

  7. Oceania2018 commented on Apr 23, 2021

    @Oceania2018
    Member

    @MPnoy Can you PR the runnable one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions