Skip to content

Commit 318f991

Browse files
committed
gradient function Conv2DBackpropInput #895
1 parent 0e5f56e commit 318f991

File tree

7 files changed

+217
-178
lines changed

7 files changed

+217
-178
lines changed

src/TensorFlowNET.Core/APIs/tf.nn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public partial class tensorflow
2626

2727
public class nn_internal
2828
{
29-
public Tensor conv2d(Tensor input, IVariableV1 filter, int[] strides, string padding, bool use_cudnn_on_gpu = true,
29+
public Tensor conv2d(Tensor input, Tensor filter, int[] strides, string padding, bool use_cudnn_on_gpu = true,
3030
string data_format = "NHWC", int[] dilations = null, string name = null)
3131
{
3232
var parameters = new Conv2dParams

src/TensorFlowNET.Core/Gradients/nn_grad.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,46 @@ public static Tensor[] _SquaredDifferenceGrad(Operation op, Tensor[] grads)
133133
-x_grad
134134
};
135135
}
136+
137+
/// <summary>
138+
/// The derivatives for deconvolution.
139+
/// </summary>
140+
/// <param name="op">The Deconvolution op.</param>
141+
/// <param name="grads">The tensor representing the gradient w.r.t. the output</param>
142+
/// <returns>The gradients w.r.t. the input and the filter</returns>
143+
[RegisterGradient("Conv2DBackpropInput")]
144+
public static Tensor[] _Conv2DBackpropInputGrad(Operation op, Tensor[] grads)
145+
{
146+
var grad = grads[0];
147+
var dilations = op.get_attr_list<int>("dilations");
148+
var strides = op.get_attr_list<int>("strides");
149+
var padding = op.get_attr<string>("padding");
150+
var explicit_paddings = op.get_attr_list<int>("explicit_paddings");
151+
var use_cudnn_on_gpu = op.get_attr<bool>("use_cudnn_on_gpu");
152+
var data_format = op.get_attr<string>("data_format");
153+
154+
return new Tensor[]
155+
{
156+
gen_nn_ops.conv2d_backprop_filter(grad, array_ops.shape(op.inputs[1]), op.inputs[2],
157+
strides, padding,
158+
use_cudnn_on_gpu: use_cudnn_on_gpu,
159+
explicit_paddings: explicit_paddings,
160+
dilations: dilations,
161+
data_format: data_format),
162+
gen_nn_ops.conv2d(new Conv2dParams
163+
{
164+
Input = grad,
165+
Filter = op.inputs[1],
166+
Strides = strides,
167+
Padding = padding,
168+
DataFormat = data_format,
169+
Dilations = dilations,
170+
ExplicitPaddings = explicit_paddings,
171+
UseCudnnOnGpu = use_cudnn_on_gpu
172+
})
173+
};
174+
}
175+
136176
/// <summary>
137177
/// Gradient function for Conv2D.
138178
/// </summary>

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public virtual Operation create_op(string op_type, Tensor[] inputs, TF_DataType[
283283
// This was causing duplicate graph node name errors, when testing a conv2d autoencoder
284284
// https://keras.io/guides/functional_api/#:~:text=keras.,graph%20(DAG)%20of%20layers.
285285
// name = name.EndsWith("/") ? ops.name_from_scope_name(name) : unique_name(name);
286-
name = name.EndsWith("/") ? unique_name(ops.name_from_scope_name(name)) : unique_name(name);
286+
name = name.EndsWith("/") ? ops.name_from_scope_name(name) : unique_name(name);
287287
var node_def = ops._NodeDef(op_type, name, attrs: attrs);
288288

289289
var input_ops = inputs.Select(x => x.op).ToArray();
@@ -386,10 +386,6 @@ public string name_scope(string name)
386386
/// to name the operation being created.</returns>
387387
public string unique_name(string name, bool mark_as_used = true)
388388
{
389-
if (name.EndsWith("basic_r_n_n_cell"))
390-
{
391-
392-
}
393389
if (!String.IsNullOrEmpty(_name_stack))
394390
name = _name_stack + "/" + name;
395391
// For the sake of checking for names in use, we treat names as case

src/TensorFlowNET.Core/Operations/NnOps/Conv2dParams.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class Conv2dParams
4242
/// <summary>
4343
/// A 4-D tensor of shape
4444
/// </summary>
45-
public IVariableV1 Filter { get; set; }
45+
public Tensor Filter { get; set; }
4646

4747
/// <summary>
4848
/// An integer vector representing the tensor shape of `filter`

src/TensorFlowNET.Core/Operations/NnOps/ConvolutionInternal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public ConvolutionInternal(ConvolutionalArgs args)
3636
name = args.Name;
3737
}
3838

39-
public Tensor Apply(Tensors input, IVariableV1 filters)
39+
public Tensor Apply(Tensors input, Tensor filters)
4040
{
4141
var filters_rank = filters.shape.ndim;
4242
var inputs_rank = input.shape.ndim;

0 commit comments

Comments
 (0)