You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
GradientTape.gradient error if tf.slice is used #800
// 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
[-]GradientTape.gradients error if tf.slice is used[/-][+]GradientTape.gradient error if tf.slice is used[/+]Oceania2018 commentedon Apr 22, 2021
@MPnoy Could you PR the Unit Test here? https://github.com/SciSharp/TensorFlow.NET/blob/master/test/TensorFlowNET.UnitTest/ManagedAPI/GradientTest.cs
MPnoy commentedon Apr 22, 2021
Done
MPnoy commentedon Apr 22, 2021
@Oceania2018 Also I found that GradientTape.gradient after tf.concat always returns (null, null)
Oceania2018 commentedon Apr 22, 2021
@MPnoy I got

(None, None)in python:Fix _SliceGrad #800
MPnoy commentedon Apr 23, 2021
@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
Oceania2018 commentedon Apr 23, 2021
@MPnoy Can you PR the runnable one?