Skip to content

Commit 4c81923

Browse files
committed
tf.stack
1 parent dbfdddd commit 4c81923

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ public Tensor placeholder_with_default<T>(T input, int[] shape, string name = nu
140140
public Tensor shape(Tensor input, string name = null, TF_DataType out_type = TF_DataType.TF_INT32)
141141
=> array_ops.shape_internal(input, name, optimize: true, out_type: out_type);
142142

143+
/// <summary>
144+
/// Stacks a list of rank-`R` tensors into one rank-`(R+1)` tensor.
145+
/// </summary>
146+
/// <param name="values"></param>
147+
/// <param name="axis"></param>
148+
/// <param name="name"></param>
149+
/// <returns>A stacked `Tensor` with the same type as `values`.</returns>
150+
public Tensor stack(Tensor[] values, int axis = 0, string name = "stack")
151+
=> array_ops.stack(values, axis: axis, name: name);
152+
143153
/// <summary>
144154
/// Unpacks the given dimension of a rank-`R` tensor into rank-`(R-1)` tensors.
145155
/// </summary>

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,18 @@ public static Tensor one_hot(Tensor indices, int depth,
308308
public static (Tensor, Tensor) unique(Tensor x, TF_DataType out_idx = TF_DataType.TF_INT32, string name = null)
309309
=> gen_array_ops.unique(x, out_idx: out_idx, name: name);
310310

311+
public static Tensor stack(Tensor[] values, int axis = 0, string name = "stack")
312+
{
313+
if (axis == 0)
314+
{
315+
return ops.convert_to_tensor(values, name: name);
316+
}
317+
318+
var value_shape = ops.convert_to_tensor(values[0], name: name).TensorShape;
319+
320+
return gen_array_ops.pack(values, axis: axis, name: name);
321+
}
322+
311323
public static Tensor[] unstack(Tensor value, int? num = null, int axis = 0, string name = "unstack")
312324
{
313325
if(num == null)

0 commit comments

Comments
 (0)