Skip to content

Commit 9edada5

Browse files
committed
fix Tensor set_shape()
1 parent 58d714b commit 9edada5

5 files changed

Lines changed: 23 additions & 28 deletions

File tree

‎src/TensorFlowNET.Core/Gradients/array_grad.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ public static Tensor[] _PadGrad(Operation op, Tensor[] grads)
196196
var grad = grads[0];
197197
var x = op.inputs[0];
198198
var a = op.inputs[1];
199-
var pad_before = array_ops.slice(a, new[] { 0, 0 },
200-
new[] { array_ops.stack(new object[] { array_ops.rank(x), 1 }) });
199+
var size = array_ops.stack(new object[] { array_ops.rank(x), 1 });
200+
var pad_before = array_ops.slice(a, new[] { 0, 0 }, size);
201201

202202
// Make it a 1-D tensor.
203203
var begin = array_ops.reshape(pad_before, new[] { -1 });
204204
var sizes = array_ops.shape(x);
205-
var x_grad = array_ops.slice(grad, new[] { begin }, new[] { sizes });
205+
var x_grad = array_ops.slice(grad, begin, sizes);
206206

207207
if (len(op.inputs) == 3)
208208
return new Tensor[] { x_grad, null, null };

‎src/TensorFlowNET.Core/Gradients/gradients_util.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static Tensor[] _GradientsHelper(Tensor[] ys,
108108
{
109109
// generate gradient subgraph for op.
110110
var op = queue.Dequeue();
111-
if(tf.get_default_graph()._nodes_by_name.Count >= 20611)
111+
if(tf.get_default_graph()._nodes_by_name.Count >= 23868)
112112
{
113113

114114
}
@@ -216,7 +216,7 @@ public static Tensor[] _GradientsHelper(Tensor[] ys,
216216
in_grad.Tag == null && // maybe a IndexedSlice
217217
t_in.dtype != TF_DataType.TF_RESOURCE)
218218
{
219-
in_grad.shape = t_in.shape;
219+
in_grad.set_shape(t_in.TensorShape);
220220
}
221221

222222
_SetGrad(grads, t_in, in_grad);

‎src/TensorFlowNET.Core/Operations/array_ops.py.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ public static Tensor transpose<T1, T2>(T1 a, T2 perm, string name = "transpose",
611611
});
612612
}
613613

614-
public static Tensor slice<Tb, Ts>(Tensor input, Tb[] begin, Ts[] size, string name = null)
614+
public static Tensor slice<Tb, Ts>(Tensor input, Tb begin, Ts size, string name = null)
615615
=> gen_array_ops.slice(input, begin, size, name: name);
616616

617617
public static Tensor stack(object values, int axis = 0, string name = "stack")

‎src/TensorFlowNET.Core/Operations/gen_array_ops.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ public static Tensor strided_slice_grad(Tensor shape, Tensor begin, Tensor end,
475475
return op.output;
476476
}
477477

478-
public static Tensor slice<Tb, Ts>(Tensor input, Tb[] begin, Ts[] size, string name = null)
478+
public static Tensor slice<Tb, Ts>(Tensor input, Tb begin, Ts size, string name = null)
479479
{
480480
var _op = _op_def_lib._apply_op_helper("Slice", name, new { input, begin, size });
481481
return _op.outputs[0];

‎src/TensorFlowNET.Core/Tensors/Tensor.cs‎

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,13 @@ public int[] shape
105105

106106
if (_handle == IntPtr.Zero)
107107
{
108-
var status = new Status();
109-
c_api.TF_GraphGetTensorShape(op.graph, _as_tf_output(), dims, rank, status);
110-
status.Check();
111-
} else
108+
using (var status = new Status())
109+
{
110+
c_api.TF_GraphGetTensorShape(op.graph, _as_tf_output(), dims, rank, status);
111+
status.Check();
112+
}
113+
}
114+
else
112115
{
113116
for (int i = 0; i < rank; i++)
114117
dims[i] = c_api.TF_Dim(_handle, i);
@@ -119,14 +122,15 @@ public int[] shape
119122

120123
set
121124
{
122-
var status = new Status();
123-
124-
if (value == null)
125-
c_api.TF_GraphSetTensorShape(this.graph, this._as_tf_output(), null, -1, status);
126-
else
127-
c_api.TF_GraphSetTensorShape(this.graph, this._as_tf_output(), value.Select(Convert.ToInt64).ToArray(), value.Length, status);
125+
using (var status = new Status())
126+
{
127+
if (value == null)
128+
c_api.TF_GraphSetTensorShape(this.graph, this._as_tf_output(), null, -1, status);
129+
else
130+
c_api.TF_GraphSetTensorShape(this.graph, this._as_tf_output(), value.Select(Convert.ToInt64).ToArray(), value.Length, status);
128131

129-
status.Check(true);
132+
status.Check(true);
133+
}
130134
}
131135
}
132136

@@ -142,16 +146,7 @@ public int[] _shape_tuple()
142146
/// </summary>
143147
public void set_shape(TensorShape shape)
144148
{
145-
this.shape = (int[]) shape.dims.Clone();
146-
}
147-
148-
/// <summary>
149-
/// Updates the shape of this tensor.
150-
/// </summary>
151-
[Obsolete("Please use set_shape(TensorShape shape) instead.", false)]
152-
public void SetShape(TensorShape shape)
153-
{
154-
this.shape = (int[]) shape.dims.Clone();
149+
this.shape = shape.rank > 0 ? shape.dims : null;
155150
}
156151

157152
/// <summary>

0 commit comments

Comments
 (0)