forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackendImpl.cs
More file actions
1005 lines (876 loc) · 41.5 KB
/
BackendImpl.cs
File metadata and controls
1005 lines (876 loc) · 41.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
using Tensorflow.NumPy;
using System;
using System.Linq;
using System.Collections.Generic;
using Tensorflow.Functions;
using Tensorflow.Graphs;
using Tensorflow.Common.Extensions;
using static Tensorflow.Binding;
using static Tensorflow.Graphs.SubGraphUtility;
using Tensorflow.Util;
using Tensorflow.Common.Types;
using System.Diagnostics;
namespace Tensorflow.Keras
{
public class BackendImpl : BackendBase
{
/* ---------------------------------------- KERAS BACKEND NATIVE OBJECTS ---------------------------------------- */
public Func<Array, double> py_sum = sum;
public Func<Array, bool> py_all = all;
//Func<Array, bool> py_any = any;
//Func<double, double, double, IEnumerable<double>> py_slice = slice;
public Session _SESSION => ops.get_default_session();
public Graph _GRAPH;
FuncGraph _CURRENT_SCRATCH_GRAPH;
public Dictionary<Graph, GraphLearningPhase> _GRAPH_LEARNING_PHASES;
//Dictionary<Graph, Dictionary<string, int>> PER_GRAPH_LAYER_NAME_UIDS;
public bool _MANUAL_VAR_INIT = false;
public List<string> _LOCAL_DEVICES = null;
/* -------------------------------------- KERAS BACKEND NATIVE OBJECTS END -------------------------------------- */
/// <summary>
/// A global dictionary mapping graph objects to an index of counters used
/// for various layer names in each graph.
/// Allows to give unique autogenerated names to layers, in a graph-specific way.
/// </summary>
public Dictionary<Graph, Dictionary<string, int>> PER_GRAPH_LAYER_NAME_UIDS = new Dictionary<Graph, Dictionary<string, int>>();
public Dictionary<string, IVariableV1> _GRAPH_VARIABLES = new Dictionary<string, IVariableV1>();
public Dictionary<string, Optimizer> _GRAPH_TF_OPTIMIZERS = new Dictionary<string, Optimizer>();
public _DummyEagerGraph _DUMMY_EAGER_GRAPH = new _DummyEagerGraph();
public BackendImpl()
{
}
public void track_variable(IVariableV1 v)
{
if (tf.Context.executing_eagerly())
{
return;
}
var graph = v.Graph;
if(graph is null)
{
graph = get_graph();
}
_GRAPH_VARIABLES[graph.graph_key] = v;
}
public KerasTensor placeholder(Shape shape = null,
int ndim = -1,
TF_DataType dtype = TF_DataType.DtInvalid,
bool sparse = false,
string name = null,
bool ragged = false)
{
if (sparse)
{
throw new NotImplementedException("placeholder sparse is true");
}
else
{
return array_ops.placeholder(dtype: dtype, shape: shape, name: name);
}
}
public Graph get_graph()
{
if (tf.Context.executing_eagerly())
{
if (_GRAPH == null)
_GRAPH = new FuncGraph("keras_graph");
return _GRAPH;
}
return ops.get_default_graph();
}
FuncGraph _scratch_graph()
{
if (_CURRENT_SCRATCH_GRAPH == null)
_CURRENT_SCRATCH_GRAPH = new FuncGraph("keras_scratch_graph");
return _CURRENT_SCRATCH_GRAPH;
}
public int get_uid(string prefix)
{
var graph = tf.get_default_graph();
if (!PER_GRAPH_LAYER_NAME_UIDS.ContainsKey(graph))
PER_GRAPH_LAYER_NAME_UIDS.Add(graph, new defaultdict<string, int>());
if (!PER_GRAPH_LAYER_NAME_UIDS[graph].ContainsKey(prefix))
PER_GRAPH_LAYER_NAME_UIDS[graph][prefix] = 0;
PER_GRAPH_LAYER_NAME_UIDS[graph][prefix] += 1;
return PER_GRAPH_LAYER_NAME_UIDS[graph][prefix];
}
public void reset_uids() => PER_GRAPH_LAYER_NAME_UIDS = new Dictionary<Graph, Dictionary<string, int>>();
public void clear_session()
{
tf.Context.reset_context();
reset_uids();
// var phase = tf.placeholder_with_default(false, new int[] { }, name: "keras_learning_phase");
if (_GRAPH_LEARNING_PHASES != null)
_GRAPH_LEARNING_PHASES.Clear();
if (_GRAPH_LEARNING_PHASES != null)
_GRAPH_LEARNING_PHASES.Clear();
PER_GRAPH_LAYER_NAME_UIDS.Clear();
_CURRENT_SCRATCH_GRAPH = null;
_GRAPH = null;
ops.set_default_session(tf.Session(ops.get_default_graph()));
tf.enable_eager_execution();
tf.Runner.ClearEagerOperationMap();
GC.Collect();
GC.WaitForPendingFinalizers();
}
public void manual_variable_initialization(bool value)
{
_MANUAL_VAR_INIT = value;
}
public Tensor mean(Tensor x, int axis = -1, bool keepdims = false)
{
if (x.dtype.as_base_dtype() == TF_DataType.TF_BOOL)
x = math_ops.cast(x, TF_DataType.TF_FLOAT);
return math_ops.reduce_mean(x, axis: axis, keepdims: false);
}
public GraphLearningPhase learning_phase()
{
var graph = tf.get_default_graph();
if (_GRAPH_LEARNING_PHASES.ContainsKey(graph))
{
var phase = tf.placeholder_with_default(false, shape: new int[] { }, name: "keras_learning_phase");
_GRAPH_LEARNING_PHASES[graph] = 0;
}
return _GRAPH_LEARNING_PHASES[graph];
}
public void set_learning_phase(bool value)
{
_GRAPH_LEARNING_PHASES[tf.get_default_graph()] = (GraphLearningPhase)((value) ? 1 : 0);
}
public void set_value(IVariableV1 x, object value)
{
// TODO(Rinne): check the implementation.
x.assign(value);
}
public void batch_set_value(List<(IVariableV1, NDArray)> tuples)
{
if (ops.executing_eagerly_outside_functions())
{
foreach (var (x, value) in tuples)
x.assign(value, read_value: false);
}
else
{
throw new NotImplementedException("");
}
}
/// <summary>
/// Pads the 2nd and 3rd dimensions of a 4D tensor.
/// </summary>
/// <param name="x"></param>
/// <param name="padding"></param>
/// <param name="data_format"></param>
/// <returns></returns>
public Tensor spatial_2d_padding(Tensor x, NDArray padding = null, string data_format = null)
{
if (padding == null)
padding = new[,] { { 1, 1 }, { 1, 1 } };
NDArray pattern;
if (data_format == "channels_first")
pattern = new int[,]
{
{ 0, 0 },
{ 0, 0 },
{ padding[0][0], padding[0][1] },
{ padding[1][0], padding[1][1] }
};
else
pattern = new int[,]
{
{ 0, 0 },
{ padding[0][0], padding[0][1] },
{ padding[1][0], padding[1][1] },
{ 0, 0 }
};
return array_ops.pad(x, pattern);
}
/// <summary>
/// Method to evaluate a tensor in eager or in a tf.function.
/// </summary>
/// <param name="outputs"></param>
/// <returns></returns>
public NDArray eval_in_eager_or_function(Tensors outputs)
{
if (outputs[0].op.type == "Const")
return tensor_util.constant_value(outputs);
var source_graph = outputs.graph;
var exec_graph = _scratch_graph();
var global_graph = get_graph();
if (source_graph == global_graph && exec_graph != global_graph)
{
var lifted_map = lift_to_graph(outputs, exec_graph,
new List<Tensor>(),
add_sources: true,
handle_captures: true,
base_graph: source_graph);
}
if (outputs[0].op.type == "Placeholder"
|| outputs[0].op.type == "StridedSlice")
return exec_graph.external_captures.Last().numpy();
// Consolidate updates
exec_graph.as_default();
exec_graph.Inputs = exec_graph.internal_captures;
exec_graph.Outputs = outputs;
var graph_fn = new ConcreteFunction(exec_graph);
_CURRENT_SCRATCH_GRAPH = null;
tf.Context.restore_mode();
// return outputs.eval();
throw new NotImplementedException("");
}
public class _DummyEagerGraph
{ }
/// <summary>
/// Categorical crossentropy between an output tensor and a target tensor.
/// </summary>
/// <param name="target"></param>
/// <param name="output"></param>
/// <param name="from_logits"></param>
/// <param name="axis"></param>
/// <returns></returns>
public Tensor categorical_crossentropy(Tensor target, Tensor output, bool from_logits = false, int axis = -1)
{
if (from_logits)
return tf.nn.softmax_cross_entropy_with_logits_v2(labels: target, logits: output, axis: axis);
if (output.op != null && output.op.type == "Softmax")
{
if (output.op.inputs.Length != 1) throw new ApplicationException();
var o = output.op.inputs[0];
return tf.nn.softmax_cross_entropy_with_logits_v2(labels: target, logits: o, axis: axis);
}
// scale preds so that the class probas of each sample sum to 1
output = output / math_ops.reduce_sum(output, new Axis(axis), true);
// Compute cross entropy from probabilities.
var epsilon_ = constant_op.constant(epsilon(), output.dtype.as_base_dtype());
output = clip_ops.clip_by_value(output, epsilon_, 1.0f - epsilon_);
return -math_ops.reduce_sum(target * math_ops.log(output), new Axis(axis));
}
public Tensor sparse_categorical_crossentropy(Tensor target, Tensor output, bool from_logits = false, int axis = -1, int? ignore_class = null)
{
target = tf.cast(target, tf.int64);
if (!from_logits)
{
var epsilon_ = constant_op.constant(epsilon(), output.dtype.as_base_dtype());
output = tf.clip_by_value(output, epsilon_, 1 - epsilon_);
output = tf.math.log(output);
}
var output_rank = output.shape.ndim;
if (output_rank > -1)
{
axis = Math.Abs(axis) % output_rank;
if (axis != output_rank - 1)
{
/*var permutation = list(
itertools.chain(
range(axis), range(axis + 1, output_rank), [axis]
)
);
output = tf.transpose(output, perm: permutation);*/
throw new NotImplementedException("");
}
}
var output_shape = tf.shape(output);
var target_rank = target.shape.ndim;
var update_shape = target_rank > -1 && output_rank > -1 && target_rank != output_rank - 1;
if (update_shape)
{
target = tf.reshape(target, -1);
output = tf.reshape(output, (-1, output.shape[-1]));
}
if (ignore_class.HasValue)
{
throw new NotImplementedException("");
}
var res = tf.nn.sparse_softmax_cross_entropy_with_logits(labels: target, logits: output);
if (ignore_class.HasValue)
{
throw new NotImplementedException("");
}
if (update_shape && output_rank >= 3)
{
// If our output includes timesteps or
// spatial dimensions we need to reshape
res = tf.reshape(res, output_shape[":-1"]);
}
return res;
}
public Tensor binary_crossentropy(Tensor target, Tensor output, bool from_logits = false)
{
if (from_logits)
return tf.nn.sigmoid_cross_entropy_with_logits(labels: target, logits: output);
var epsilon_ = constant_op.constant(epsilon(), dtype: output.dtype.as_base_dtype());
output = tf.clip_by_value(output, epsilon_, 1.0f - epsilon_);
// Compute cross entropy from probabilities.
var bce = target * tf.math.log(output + epsilon());
bce += (1 - target) * tf.math.log(1 - output + epsilon());
return -bce;
}
/// <summary>
/// Resizes the images contained in a 4D tensor.
/// </summary>
/// <param name="x"></param>
/// <param name="height_factor"></param>
/// <param name="width_factor"></param>
/// <param name="data_format"></param>
/// <param name="interpolation"></param>
/// <returns></returns>
public Tensor resize_images(Tensor x, int height_factor, int width_factor,
string data_format, string interpolation = "nearest")
{
var (rows, cols) = (0, 0);
if (data_format == "channels_first")
(rows, cols) = (2, 3);
else if (data_format == "channels_last")
(rows, cols) = (1, 2);
else
throw new ValueError($"Invalid `data_format` argument: {data_format}");
var original_shape = x.shape;
var new_shape = array_ops.shape(x)[new Slice(rows, cols + 1)];
new_shape *= constant_op.constant(np.array(height_factor, width_factor));
if (data_format == "channels_first")
// x = permute_dimensions(x, [0, 2, 3, 1]);
throw new NotImplementedException("");
if (interpolation == "nearest")
x = tf.image.resize_images_v2(x, new_shape, method: ResizeMethod.NEAREST_NEIGHBOR);
if (data_format == "channels_first")
// x = permute_dimensions(x, [0, 3, 1, 2]);
throw new NotImplementedException("");
int new_height = original_shape[rows] < 0 ? -1 : (int)original_shape[rows] * height_factor;
int new_width = original_shape[cols] < 0 ? -1 : (int)original_shape[cols] * width_factor;
Shape output_shape = data_format == "channels_first" ?
(-1, -1, new_height, new_width) : (-1, new_height, new_width, -1);
x.shape = output_shape;
return x;
}
/// <summary>
/// Concatenates a list of tensors alongside the specified axis.
/// </summary>
/// <param name="tensors">list of tensors to concatenate.</param>
/// <param name="axis">concatenation axis.</param>
/// <returns></returns>
public Tensor concatenate(Tensors tensors, int axis = -1)
{
if(axis < 0)
{
var rank = tensors[0].ndim;
if (rank > -1)
axis += rank;
else
axis = 0;
}
return array_ops.concat(tensors, axis);
}
public Tensor conv2d_transpose(Tensor x,
IVariableV1 kernel,
Tensor output_shape,
Shape strides = null,
string padding = "valid",
string data_format = null,
Shape dilation_rate = null)
{
/*
var force_transpose = false;
if (data_format == "channels_first" && !dilation_rate.Equals(new[] { 1, 1 }))
force_transpose = true;
x, tf_data_format = _preprocess_conv2d_input(x, data_format, force_transpose)
*/
var tf_data_format = "NHWC";
padding = padding.ToUpper();
strides = new Shape(1, strides[0], strides[1], 1);
if (dilation_rate.Equals(new long[] { 1, 1 }))
x = nn_impl.conv2d_transpose(x, kernel, output_shape, strides,
padding: padding,
data_format: tf_data_format);
else
throw new NotImplementedException("dilation_rate other than [1,1] is not yet supported");
return x;
}
public (Tensors, Tensors, Tensors) rnn(
Func<Tensors, Tensors, (Tensors, Tensors)> step_function, // args:inputs, states, return:output, new_states
Tensors inputs, // inputs is a tuple of tensors (one per input sequence)
Tensors initial_states,
bool go_backwards = false,
Tensor? mask = null,
Tensors? constants = null,
bool unroll = false,
Tensors? input_length = null, // An integer or a 1-D Tensor,depending on whether the time dimension is fixed-length or not
bool time_major = false,
bool zero_output_for_mask = false,
bool return_all_outputs = true)
{
Tensor swap_batch_timestep(Tensor input_t)
{
var axes = Enumerable.Range(0, input_t.rank).ToArray();
axes[0] = 1;
axes[1] = 0;
return tf.transpose(input_t, axes);
}
if (!time_major)
{
inputs = Nest.MapStructure(swap_batch_timestep, inputs).ToTensors();
}
var flatted_inptus = Nest.Flatten(inputs).ToList();
var first_flatted_input = flatted_inptus[0];
var time_steps = first_flatted_input.shape[0];
var batch = first_flatted_input.shape[1];
var time_steps_t = tf.shape(first_flatted_input)[0];
foreach (var input_ in flatted_inptus)
{
input_.shape.with_rank_at_least(3);
}
if (mask != null)
{
if (mask.dtype != TF_DataType.TF_BOOL)
{
mask = tf.cast(mask, TF_DataType.TF_BOOL);
}
if (mask.rank == 2)
{
mask = tf.expand_dims(mask, -1);
}
if (!time_major)
{
mask = swap_batch_timestep(mask);
}
}
// tf.where needs its condition tensor to be the same shape as its two
// result tensors, but in our case the condition (mask) tensor is
// (nsamples, 1), and inputs are (nsamples, ndimensions) or even more.
// So we need to broadcast the mask to match the shape of inputs.
// That's what the tile call does, it just repeats the mask along its
// second dimension n times.
Tensors _expand_mask(Tensors mask_t, Tensors input_t, int fixed_dim = 1)
{
if (!mask_t.IsSingle())
{
throw new ValueError($"mask_t is expected to be tensor, but got {mask_t}");
}
if (!input_t.IsSingle())
{
throw new ValueError($"input_t is expected to be tensor, but got {input_t}");
}
var rank_diff = input_t.rank - mask_t.rank;
for (int i = 0; i < rank_diff; i++)
{
mask_t = tf.expand_dims(mask_t, -1);
}
var multiples = Enumerable.Repeat(1, fixed_dim).ToArray().concat(input_t.shape.as_int_list().Skip(fixed_dim).ToArray());
return tf.tile(mask_t, multiples);
}
Tensors outputs = new Tensors();
Tensors output_time_zero = new Tensors();
Tensors last_output = new Tensors();
Tensors new_states = new Tensors();
if (unroll)
{
if (time_steps == 0)
{
throw new ValueError("Unrolling requires a fixed number of timesteps.");
}
// Process the input tensors. The input tensor need to be split on the
// time_step dim, and reverse if go_backwards is True. In the case of
// nested input, the input is flattened and then transformed
// individually. The result of this will be a tuple of lists, each of
// the item in tuple is list of the tensor with shape (batch, feature)
// TODO(Wanglongzhi2001),step_func接受的第二个参数为List,但是最后却用的tuple
//var states = Tuple.Create(initial_states);
var states = initial_states;
var successive_states = new Tensors();
var successive_outputs = new Tensors();
// Process the input tensors. The input tensor need to be split on the
// time_step dim, and reverse if go_backwards is True. In the case of
// nested input, the input is flattened and then transformed
// individually. The result of this will be a tuple of lists, each of
// the item in tuple is list of the tensor with shape (batch, feature)
Tensors _process_single_input_t(Tensor input_t)
{
var unstaked_input_t = array_ops.unstack(input_t); // unstack for time_step dim
if (go_backwards)
{
unstaked_input_t = unstaked_input_t.Reverse().ToArray();
}
return unstaked_input_t;
}
// TODO(Wanglongzhi2001)
Tensors processed_input;
if (!inputs.IsSingle())
{
processed_input = inputs.MapStructure(_process_single_input_t).ReduceTo<Tensors, Tensor>().ToTensors();
}
else
{
processed_input = _process_single_input_t(inputs);
}
object _get_input_tensor(int time)
{
List<Tensor> inp = new List<Tensor>();
foreach (var t_ in processed_input)
{
inp.Add(t_[time]);
}
return Nest.PackSequenceAs(inputs, inp);
}
if (mask != null)
{
var mask_list = tf.unstack(mask);
if (go_backwards)
{
mask_list.Reverse().ToArray();
}
for (int i = 0; i < time_steps; i++)
{
// TODO(Wanglongzhi2001),deal with _get_input_tensor
var inp = _get_input_tensor(i);
var mask_t = mask_list[i];
// TODO
var (output, newStates) = step_function((Tensors)inp, states.MergeWith(constants));
var tiled_mask_t = _expand_mask(mask_t, output);
Tensors prev_output;
if (successive_outputs == null)
{
prev_output = tf.zeros_like(output);
}
else
{
prev_output = successive_outputs.Last();
}
// output could be a tensor
output = tf.where(tiled_mask_t, output, prev_output);
var flat_states = Nest.Flatten(states).ToList();
var flat_new_states = Nest.Flatten(newStates).ToList();
var tiledMaskT = flat_states
.Select(s => _expand_mask(mask_t, s))
.ToArray();
var tuple = Tuple.Create(tiledMaskT);
List<Tensor> flat_final_states = new List<Tensor>();
foreach (var (m, s, ps) in zip(tiled_mask_t.ToList(), flat_new_states, flat_states))
{
flat_final_states.Add(tf.where(m, s, ps));
}
states = Nest.PackSequenceAs(states, flat_final_states).ToTensors();
if (return_all_outputs)
{
successive_outputs = successive_outputs.MergeWith(output);
successive_outputs = successive_states.MergeWith(states);
}
else
{
successive_outputs = new Tensors(output);
successive_states = new Tensors(states);
}
}
last_output = successive_outputs.Last();
new_states = successive_states.Last();
outputs = tf.stack(successive_outputs);
if (zero_output_for_mask)
{
last_output = tf.where(_expand_mask(mask_list.Last(), last_output), last_output, tf.zeros_like(last_output));
outputs = tf.where(_expand_mask(mask, outputs, fixed_dim: 2), outputs, tf.zeros_like(outputs));
}
else // mask is null
{
for (int i = 0; i < time_steps; i++)
{
var inp = _get_input_tensor(i);
var (output, newStates) = step_function((Tensors)inp, states.MergeWith(constants));
states = newStates;
if (return_all_outputs)
{
successive_outputs.Add(output);
successive_states.Add(newStates);
}
else
{
successive_outputs = new Tensors { output };
successive_states = new Tensors { newStates };
}
}
last_output = successive_outputs.Last();
new_states = successive_states.Last();
outputs = tf.stack(successive_outputs);
}
}
}
else // unroll == false
{
var states = initial_states;
// Create input tensor array, if the inputs is nested tensors, then it
// will be flattened first, and tensor array will be created one per
// flattened tensor.
var input_ta = new List<TensorArray>();
for (int i = 0; i < flatted_inptus.Count; i++)
{
input_ta.Add(TensorArray.Create(dtype: flatted_inptus[i].dtype, size: time_steps_t));
}
foreach(var (ta, input_) in zip(input_ta, flatted_inptus))
{
if (!go_backwards)
{
ta.unstack(input_);
}
else
{
ta.unstack(reverse(input_, 0));
}
}
// Get the time(0) input and compute the output for that, the output will
// be used to determine the dtype of output tensor array. Don't read from
// input_ta due to TensorArray clear_after_read default to True.
var input_time_zero = Nest.PackSequenceAs(inputs, flatted_inptus.Select(x => x[0]).ToArray()).ToTensors();
// output_time_zero is used to determine the cell output shape and its
// dtype. the value is discarded.
(output_time_zero, _) = step_function(input_time_zero,
constants is null ? initial_states : initial_states.MergeWith(constants));
Tensor output_ta_size = return_all_outputs ? time_steps_t : constant_op.constant(1);
var output_ta = new List<TensorArray>();
foreach(var output in output_time_zero.Flatten())
{
output_ta.Add(TensorArray.Create(dtype: output.dtype, size: output_ta_size, element_shape: output.shape));
}
var time = tf.constant(0, dtype: TF_DataType.TF_INT32, name: "time");
Func<Tensor, Tensor>? masking_fn;
Func<Tensors, Tensors, Tensors, Tensors>? compute_masked_output = null;
if (mask != null)
{
if (go_backwards)
{
mask = tf.reverse(mask, axis: new[] { 0 });
}
var mask_ta = TensorArray.Create(dtype: TF_DataType.TF_BOOL, size: time_steps_t);
mask_ta = mask_ta.unstack(mask);
masking_fn = (time) =>
{
return mask_ta.read(time);
};
compute_masked_output = (mask_t, flat_out, flat_mask) =>
{
var tiled_mask_t = new Tensors();
foreach (var o in flat_out)
{
tiled_mask_t.Add(_expand_mask(mask_t, o, fixed_dim: mask_t.rank));
}
Tensors res = new Tensors();
foreach (var (m, o, fm) in zip(tiled_mask_t.ToList(), flat_out.ToList(), flat_mask.ToList()))
{
res.Add(tf.where(m, o, fm));
}
return res;
};
}
// TODO(Wanglongzhi2001), what the input_length's type should be(an integer or a single tensor), it could be an integer or tensor
else if (input_length is Tensor)
{
if (go_backwards)
{
var max_len = tf.reduce_max(input_length, axis: 0);
var rev_input_length = tf.subtract(max_len - 1, input_length);
masking_fn = (time) =>
{
return tf.less(rev_input_length, time);
};
}
else
{
masking_fn = (time) =>
{
return tf.greater(input_length, time);
};
}
compute_masked_output = (mask_t, flat_out, flat_mask) =>
{
var res = new List<Tensor>();
foreach (var (o, zo) in zip(flat_out, flat_mask))
{
res.Add(tf.where(mask_t, o, zo));
}
return res;
};
}
else
{
masking_fn = null;
}
Func<Tensors, Tensor> cond = (time) => (time[0] < time_steps_t);
int parallel_iterations = 32;
Tensors final_outputs;
if (masking_fn != null)
{
// Mask for the T output will be base on the output of T - 1. In the
// case T = 0, a zero filled tensor will be used.
var flat_zero_output = new Tensors();
foreach (var o in Nest.Flatten(output_time_zero))
{
flat_zero_output.Add(tf.zeros_like(o));
}
var prev_output = flat_zero_output;
var output_ta_t = output_ta;
Tensors _step(Tensors tensors)
{
/*
RNN step function.
Args:
time: Current timestep value.
output_ta_t: TensorArray.
prev_output: tuple of outputs from time - 1.
*states: List of states.
Returns:
Tuple(todo): `(time + 1, output_ta_t, output) + tuple(new_states)`
*/
Tensor time = tensors[0];
TensorArray output_ta_t = (tensors[1] as FakeTensorByTensorArray).TensorArray;
Tensors prev_output = tensors.GetShallow(2);
Tensors states = new Tensors(tensors.Skip(2 + prev_output.Length).ToArray());
var flat_current_input = input_ta.Select(x => x.read(time)).ToList();
// maybe set shape
// TODO(Wanglongzhi2001),deal with nest.pack_sequence_as's return type
var current_input = Nest.PackSequenceAs(inputs, flat_current_input).ToTensors();
var mask_t = masking_fn(time);
var (output, new_states) = step_function(current_input, states.MergeWith(constants));
// mask output
var flat_output = Nest.Flatten(output).ToList();
var flat_mask_output = zero_output_for_mask ? flat_zero_output : prev_output.Flatten().ToList();
// TODO(Wanglongzhi2001),deal with compute_masked_output's third parameter's type
var flat_new_output = compute_masked_output(mask_t, flat_output, flat_mask_output);
// mask states
var flat_state = states.Flatten().ToList();
var flat_new_state = new_states.Flatten().ToList();
foreach (var (state, new_state) in zip(flat_state, flat_new_state))
{
if (new_state is Tensor)
{
new_state.shape = state.shape;
}
}
var flat_final_state = compute_masked_output(mask_t, flat_new_state, flat_state);
new_states = Nest.PackSequenceAs(new_states, flat_final_state.ToArray()).ToTensors();
var ta_index_to_write = return_all_outputs ? time : tf.constant(0);
Debug.Assert(flat_output.Count() == 1);
output_ta_t = output_ta_t.write(ta_index_to_write, flat_new_output.First());
return new Tensor[] { time + 1, new FakeTensorByTensorArray(output_ta_t) }.Concat(flat_new_output).Concat(new_states)
.ToArray().ToTensors();
}
var loop_vars = new Tensor[] { time + 1, new FakeTensorByTensorArray(output_ta[0]) }
.Concat(flat_zero_output.Flatten()).Concat(states).ToArray().ToTensors();
final_outputs = control_flow_ops.while_loop(cond: cond, body: _step, loop_vars: loop_vars, parallel_iterations: parallel_iterations);
new_states = final_outputs.Skip(3).ToList();
}
else
{
var output_ta_t = output_ta;
new_states = states;
Tensors _step(Tensors tensors)
{
Tensor time = tensors[0];
TensorArray output_ta_t = (tensors[1] as FakeTensorByTensorArray).TensorArray;
Tensors states = new Tensors(tensors.Skip(2).ToArray());
var flat_current_input = input_ta.Select(x => x.read(time)).ToList();
// maybe set shape
// TODO(Wanglongzhi2001),deal with nest.pack_sequence_as's return type
var current_input = Nest.PackSequenceAs(inputs, flat_current_input).ToTensors();
var (output, new_states) = step_function(current_input, states.MergeWith(constants));
var flat_state = new_states.Flatten().ToList();
var flat_new_state = new_states.Flatten().ToList();
foreach (var (state, new_state) in zip(flat_state, flat_new_state))
{
if (new_state is Tensor)
{
new_state.shape = state.shape;
}
}
var flat_output = Nest.Flatten(output);
var ta_index_to_write = return_all_outputs ? time : tf.constant(0);
Debug.Assert(flat_output.Count() == 1);
output_ta_t = output_ta_t.write(ta_index_to_write, flat_output.First());
new_states = Nest.PackSequenceAs(initial_states, flat_new_state).ToTensors();
return new Tensor[] { time + 1, new FakeTensorByTensorArray(output_ta_t) }.Concat(new_states).ToArray().ToTensors();
}
Debug.Assert(output_ta.Count == 1);
var loop_vars = new Tensor[] { time + 1, new FakeTensorByTensorArray(output_ta[0]) }.Concat(states).ToArray().ToTensors();
final_outputs = control_flow_ops.while_loop(cond: cond, body: _step, loop_vars: loop_vars, parallel_iterations: parallel_iterations);
new_states = final_outputs.Skip(2).ToList();
}
output_ta = new List<TensorArray> { (final_outputs[1] as FakeTensorByTensorArray).TensorArray };
outputs = outputs.MergeWith(output_ta.Select(o => o.stack()).ToArray().ToTensors());
last_output = last_output.MergeWith(outputs.Select(o => o[-1]).ToArray().ToTensors());
outputs = Nest.PackSequenceAs(output_time_zero, (Tensor[])outputs).ToTensors();
last_output = Nest.PackSequenceAs(output_time_zero, (Tensor[])last_output).ToTensors();
}
Func<Tensor, Tensor> set_shape;
set_shape = (output_) =>
{
if (output_ is Tensor)
{
var shape = output_.shape.as_int_list();
if (return_all_outputs)
{
shape[0] = (int)time_steps;
}
else
{
shape[0] = 1;
}
shape[1] = (int)batch;
output_.shape = shape;
}
return output_;
};
outputs = Nest.MapStructure(set_shape, outputs).ToTensors();
if (!time_major)
{
outputs = Nest.MapStructure(swap_batch_timestep, outputs).ToTensors();
}
return (last_output, outputs, new_states);
}
/// <summary>
/// Repeats the elements of a tensor along an axis, like `np.repeat`.
/// </summary>
/// <param name="x"></param>
/// <param name="rep"></param>
/// <param name="axis"></param>
/// <returns></returns>
public Tensor repeat_elements(Tensor x, int rep, int axis)
{
var x_shape = x.shape.as_int_list();
if (x_shape[axis] != -1)
{
var splits = tf.split(x, x_shape[axis], axis:axis);
var x_rep = splits.SelectMany(s => Enumerable.Repeat(s, rep)).ToArray();
return concatenate(x_rep, axis);
}
//var auxiliary_axis = axis + 1;
//x_shape = x.shape;
//var x_rep = tf.expand_dims(x, auxiliary_axis);
//var reps = np.ones(x_shape.Length + 1);
//reps[auxiliary_axis] = rep;
//x_rep = tf.tile(x_rep, reps);
throw new NotImplementedException();
}
public Tensor reverse(Tensor input, int axis)
{
return reverse(input, new int[] { axis });
}
public Tensor reverse(Tensor input, int[] axes)
{
return tf.reverse(input, axes);
}
public Tensor maybe_convert_to_ragged(bool is_ragged_output, Tensor output, int nested_row_lengths, bool go_backwards = false)
{
if (!is_ragged_output)
{
return output;
}