Skip to content

Fix ConvTraspose2D gradient and register rsqrt gradient. #980

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

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/TensorFlowNET.Core/Gradients/Tape.ComputeGradient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Tensor[] ComputeGradient(Tensor[] target_tensor_ids,

in_gradients = trace.backward_function(out_gradients.ToArray(), unneeded_gradients.ToArray());

if (in_gradients.Count() != trace.input_tensor_id.Count())
if (in_gradients.Length != trace.input_tensor_id.Length && in_gradients.Length + unneeded_gradients.Count != trace.input_tensor_id.Length)
throw new RuntimeError($"Recorded operation '{trace.op_type}' returned too few gradients. Expected {trace.input_tensor_id.Length} but received {in_gradients.Count()}");
if (!_persistent)
{
Expand All @@ -103,9 +103,11 @@ public Tensor[] ComputeGradient(Tensor[] target_tensor_ids,
in_gradients = new Tensor[trace.input_tensor_id.Length];
}

for (int i = 0; i < in_gradients.Length; ++i)
bool skip_unneeded_id = trace.input_tensor_id.Length > in_gradients.Length;
for (int i = 0, k = 0; i < in_gradients.Length && k < trace.input_tensor_id.Count(); ++i, ++k)
{
var id = trace.input_tensor_id[i];
if (skip_unneeded_id && unneeded_gradients.Contains(k)) ++k;
var id = trace.input_tensor_id[k];
if (in_gradients[i] != null)
{
var unaggregated_grads = gradients[id];
Expand Down
14 changes: 14 additions & 0 deletions src/TensorFlowNET.Core/Gradients/math_grad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,20 @@ public static Tensor[] _SqrtGrad(Operation op, Tensor[] grads)
});
}

[RegisterGradient("Rsqrt")]
public static Tensor[] _RsqrtGrad(Operation op, Tensor[] grads)
{
var grad = grads[0];
var y = op.outputs[0];

return tf_with(ops.control_dependencies(grads), delegate
{
y = math_ops.conj(y);
var factor = constant_op.constant(-0.5f, dtype: y.dtype);
return new Tensor[] { grad * (factor * math_ops.square(y) * y) };
});
}

[RegisterGradient("Asin")]
public static Tensor[] _ASinGrad(Operation op, Tensor[] grads)
{
Expand Down
14 changes: 14 additions & 0 deletions src/TensorFlowNET.Core/Keras/Layers/ILayersApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ public ILayer Conv2D(int filters,
IRegularizer bias_regularizer = null,
IRegularizer activity_regularizer = null);

public ILayer Conv2DTranspose(int filters,
Shape kernel_size = null,
Shape strides = null,
string output_padding = "valid",
string data_format = null,
Shape dilation_rate = null,
string activation = null,
bool use_bias = true,
string kernel_initializer = null,
string bias_initializer = null,
string kernel_regularizer = null,
string bias_regularizer = null,
string activity_regularizer = null);

public ILayer Conv2D(int filters,
Shape kernel_size = null,
Shape strides = null,
Expand Down