-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
ValueShim.cs
897 lines (845 loc) · 42.2 KB
/
ValueShim.cs
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
//
// ValueShim.cs -- C# Api for CNTK Value class
//
using System;
using System.Collections.Generic;
using System.Linq;
namespace CNTK
{
public partial class Value
{
/// <summary>
/// Property Device
/// </summary>
public DeviceDescriptor Device
{
get { return _Device(); }
}
/// <summary>
/// Property DataType
/// </summary>
public DataType DataType
{
get { return _GetDataType(); }
}
/// <summary>
/// Property StorageFormat
/// </summary>
public StorageFormat StorageFormat
{
get { return _GetStorageFormat(); }
}
/// <summary>
/// Property Shape
/// </summary>
public NDShape Shape
{
get { return _Shape(); }
}
/// <summary>
/// Property IsValid
/// </summary>
public bool IsValid
{
get { return _IsValid(); }
}
/// <summary>
/// Property IsSparse
/// </summary>
public bool IsSparse
{
get { return _IsSparse(); }
}
/// <summary>
/// Property IsReadOnly
/// </summary>
public bool IsReadOnly
{
get { return _IsReadOnly(); }
}
/// <summary>
/// Property MaskedCount
/// </summary>
public int MaskedCount
{
get { return (int)_MaskedCount(); }
}
/// <summary>
/// Property Data
/// </summary>
public NDArrayView Data
{
get { return _Data(); }
}
/// <summary>
/// Property Mask
/// </summary>
public NDMask Mask
{
get { return _Mask(); }
}
/// <summary>
/// Create Value object from dense input as batch data.
/// </summary>
/// <typeparam name="T">float or double</typeparam>
/// <param name="sampleShape">shape of the Value</param>
/// <param name="batch">batch of data</param>
/// <param name="device">device</param>
/// <param name="readOnly">readonly value</param>
/// <returns>the value</returns>
public static Value CreateBatch<T>(NDShape sampleShape, IEnumerable<T> batch, DeviceDescriptor device, bool readOnly = false)
{
T[] batchAsArray = batch.ToArray();
return CreateBatch<T>(sampleShape, batchAsArray, 0, batchAsArray.Count(), device, readOnly);
}
/// <summary>
/// Create batch data as a Value object from a dense source buffer.
/// Batch data of the specified dataSize are copied from the source buffer
/// starting at the specified starting position
/// </summary>
/// <typeparam name="T">float or double</typeparam>
/// <param name="sampleShape">shape of the Value</param>
/// <param name="dataBuffer">source data buffer</param>
/// <param name="dataStart">index to the source data buffer where batch data start</param>
/// <param name="dataSize">size of the data</param>
/// <param name="device">device where data are allocated</param>
/// <param name="readOnly">whether data are mutable</param>
/// <returns>the batch data value</returns>
public static Value CreateBatch<T>(NDShape sampleShape, T[] dataBuffer, int dataStart, int dataSize,
DeviceDescriptor device, bool readOnly = false)
{
if (typeof(T).Equals(typeof(float)))
{
return Value._CreateBatchFloat(sampleShape, dataBuffer as float[], dataStart, dataSize, device, readOnly);
}
else if (typeof(T).Equals(typeof(double)))
{
return Value._CreateBatchDouble(sampleShape, dataBuffer as double[], dataStart, dataSize, device, readOnly);
}
else
{
throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
}
}
/// <summary>
/// Create Value object from dense input as sequence data.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sampleShape">the shape fo the value</param>
/// <param name="sequence">daat sequence</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it is a readonly value</param>
/// <returns></returns>
public static Value CreateSequence<T>(NDShape sampleShape,
IEnumerable<T> sequence,
DeviceDescriptor device,
bool readOnly = false)
{
return CreateSequence<T>(sampleShape, sequence, true, device, readOnly);
}
/// <summary>
/// Create Value object from dense input as sequence data with sequenceStartFlag.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="sampleShape">data shape</param>
/// <param name="sequence">data sequence</param>
/// <param name="sequenceStartFlag">true indicates that it is a new sequence. false means a continuation of a previous sequence.</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value CreateSequence<T>(NDShape sampleShape,
IEnumerable<T> sequence,
bool sequenceStartFlag,
DeviceDescriptor device,
bool readOnly = false)
{
if (typeof(T).Equals(typeof(float)))
{
float[] sequenceBuffer = sequence.ToArray() as float[];
return Value._CreateSequenceFloat(sampleShape, sequenceBuffer, sequenceBuffer.Length, sequenceStartFlag, device, readOnly);
}
else if (typeof(T).Equals(typeof(double)))
{
double[] sequenceBuffer = sequence.ToArray() as double[];
return Value._CreateSequenceDouble(sampleShape, sequenceBuffer, sequenceBuffer.Length, sequenceStartFlag, device, readOnly);
}
else
{
throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
}
}
/// <summary>
/// Create Value object from dense input as batch of sequences data.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="sampleShape">data shape</param>
/// <param name="batchOfSequences">the data to be stored in the Value.
/// The outer vector represents a collection of sequences with variable length,
/// and the inner vector represents each individual sequence.</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value CreateBatchOfSequences<T>(NDShape sampleShape,
IEnumerable<IEnumerable<T>> batchOfSequences,
DeviceDescriptor device,
bool readOnly = false)
{
return Create(sampleShape, batchOfSequences, new List<bool>(0), device, readOnly);
}
/// <summary>
/// Create Value object from dense input as batch of sequences data with sequenceStartFlags.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="sampleShape">data shape</param>
/// <param name="batchOfSequences">the data to be stored in the Value.
/// The outer vector represents a collection of sequences with variable length,
/// and the inner vector represents each individual sequence.</param>
/// <param name="sequenceStartFlag">true indicates that it is a new sequence.
/// false means a continuation of a previous sequence.</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value CreateBatchOfSequences<T>(NDShape sampleShape,
IEnumerable<IEnumerable<T>> batchOfSequences,
IEnumerable<bool> sequenceStartFlags,
DeviceDescriptor device,
bool readOnly = false)
{
return Create(sampleShape, batchOfSequences, sequenceStartFlags, device, readOnly);
}
/// <summary>
/// Create Value object from dense input as batch of sequences data with sequenceStartFlags.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="sampleShape">data shape</param>
/// <param name="sequences">data sequence</param>
/// <param name="sequenceStartFlag">true indicates that it is a new sequence.
/// false means a continuation of a previous sequence.</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value Create<T>(NDShape sampleShape,
IEnumerable<IEnumerable<T>> sequences,
IEnumerable<bool> sequenceStartFlags,
DeviceDescriptor device,
bool readOnly = false)
{
var seqFlags = Helper.AsBoolVector(sequenceStartFlags);
if (typeof(T).Equals(typeof(float)))
{
var inputAsSequencesVector = new FloatVectorVector();
foreach (var seq in sequences)
{
var seqVector = Helper.AsFloatVector(seq);
// The seqVector is copied when adding to inputAsSequencesVector.
inputAsSequencesVector.Add(seqVector);
}
return Value._CreateDenseFloat(sampleShape, inputAsSequencesVector, seqFlags, device, readOnly);
}
else if (typeof(T).Equals(typeof(double)))
{
var inputAsSequencesVector = new DoubleVectorVector();
foreach (var seq in sequences)
{
var seqVector = Helper.AsDoubleVector(seq);
inputAsSequencesVector.Add(seqVector);
}
return Value._CreateDenseDouble(sampleShape, inputAsSequencesVector, seqFlags, device, readOnly);
}
else
{
throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
}
}
/// <summary>
/// Create Value object from OneHotVector input, for N-dimenstional tensor. Only Create() method for now.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="sampleShape">data shape</param>
/// <param name="sequences">data sequence</param>
/// <param name="sequenceStartFlag">true indicates that it is a new sequence.
/// false means a continuation of a previous sequence.</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value Create<T>(NDShape sampleShape,
IEnumerable<IEnumerable<int>> sequences,
IEnumerable<bool> sequenceStartFlags,
DeviceDescriptor device,
bool readOnly = false)
{
var seqFlags = Helper.AsBoolVector(sequenceStartFlags);
var inputSeqVector = new SizeTVectorVector();
foreach (var seq in sequences)
{
var s = Helper.AsSizeTVector(seq);
inputSeqVector.Add(s);
}
if (typeof(T).Equals(typeof(float)))
{
return Value._CreateOneHotFloat(sampleShape, inputSeqVector, seqFlags, device, readOnly);
}
else if (typeof(T).Equals(typeof(double)))
{
return Value._CreateOneHotDouble(sampleShape, inputSeqVector, seqFlags, device, readOnly);
}
else
{
throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
}
}
/// <summary>
/// Create Value object from OneHotVector input as batch data, for 1D tensor only.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="dimension">data dimension</param>
/// <param name="batch">data batches</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value CreateBatch<T>(int dimension, IEnumerable<int> batch, DeviceDescriptor device, bool readOnly = false)
{
var inputVector = Helper.AsSizeTVector(batch);
if (typeof(T).Equals(typeof(float)))
{
return Value._CreateBatchFloat((uint)dimension, inputVector, device, readOnly);
}
else if (typeof(T).Equals(typeof(double)))
{
return Value._CreateBatchDouble((uint)dimension, inputVector, device, readOnly);
}
else
{
throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
}
}
/// <summary>
/// Create Value object from OneHotVector input as sequence data, for 1D tensor only.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="dimension">data dimension</param>
/// <param name="sequence">data sequence</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value CreateSequence<T>(int dimension,
IEnumerable<int> sequence,
DeviceDescriptor device,
bool readOnly = false)
{
return CreateSequence<T>(dimension, sequence, true, device, readOnly);
}
/// <summary>
/// Create Value object from OneHotVector input as sequence data with sequenceStartFlag, for 1D tensor only.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="dimension">data dimension</param>
/// <param name="sequence">data sequence</param>
/// <param name="sequenceStartFlag">true indicates that it is a new sequence.
/// false means a continuation of a previous sequence.</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value CreateSequence<T>(int dimension,
IEnumerable<int> sequence,
bool sequenceStartFlag,
DeviceDescriptor device,
bool readOnly = false)
{
var inputVector = Helper.AsSizeTVector(sequence);
if (typeof(T).Equals(typeof(float)))
{
return Value._CreateSequenceFloat((uint)dimension, inputVector, sequenceStartFlag, device, readOnly);
}
else if (typeof(T).Equals(typeof(double)))
{
return Value._CreateSequenceDouble((uint)dimension, inputVector, sequenceStartFlag, device, readOnly);
}
else
{
throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
}
}
/// <summary>
/// Create Value object from OneHotVector input as batch of sequences data, for 1D tensor only.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="dimension">data dimension</param>
/// <param name="batchOfSequences">data sequence batches</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value CreateBatchOfSequences<T>(int dimension,
IEnumerable<IEnumerable<int>> batchOfSequences,
DeviceDescriptor device,
bool readOnly = false)
{
return Create<T>(dimension, batchOfSequences, new List<bool>(0), device, readOnly);
}
/// <summary>
/// Create Value object from OneHotVector input as batch of sequences data with sequenceStratFlags, for 1D tensor only.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="dimension">data dimension</param>
/// <param name="batchOfSequences">data sequence batches</param>
/// <param name="sequenceStartFlag">true indicates that it is a new sequence.
/// false means a continuation of a previous sequence.</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value CreateBatchOfSequences<T>(int dimension,
IEnumerable<IEnumerable<int>> batchOfSequences,
IEnumerable<bool> sequenceStartFlags,
DeviceDescriptor device,
bool readOnly = false)
{
return Create<T>(dimension, batchOfSequences, sequenceStartFlags, device, readOnly);
}
/// <summary>
/// Create Value object from OneHotVector input as batch of sequences data with sequenceStratFlags, for 1D tensor only.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="dimension">data dimension</param>
/// <param name="sequences">data sequences</param>
/// <param name="sequenceStartFlag">true indicates that it is a new sequence.
/// false means a continuation of a previous sequence.</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value Create<T>(int dimension,
IEnumerable<IEnumerable<int>> sequences,
IEnumerable<bool> sequenceStartFlags,
DeviceDescriptor device,
bool readOnly = false)
{
var seqFlags = Helper.AsBoolVector(sequenceStartFlags);
var inputSeqVector = new SizeTVectorVector();
foreach (var seq in sequences)
{
var s = Helper.AsSizeTVector(seq);
inputSeqVector.Add(s);
}
if (typeof(T).Equals(typeof(float)))
{
return Value._CreateOneHotFloat((uint)dimension, inputSeqVector, seqFlags, device, readOnly);
}
else if (typeof(T).Equals(typeof(double)))
{
return Value._CreateOneHotDouble((uint)dimension, inputSeqVector, seqFlags, device, readOnly);
}
else
{
throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
}
}
/// <summary>
/// Create Value object from sparse input as sequence data with sequenceStartFlag, for N-dimensional tensor. Only CreateSequence() for now.
/// </summary>
/// <typeparam name="T">data type of the created Value object.Currently, float and double are supported.</typeparam>
/// <param name="sampleShape">the tensor shape. For sparse input, the tensor shape leading dimensionality must be the same as the total size of the tensor shape.</param>
/// <param name="sequenceLength">the sequence length.</param>
/// <param name="colStarts">column start indices</param>
/// <param name="rowIndices">row indices</param>
/// <param name="nonZeroValues">sparse values</param>
/// <param name="sequenceStartFlag">true indicates that it is a new sequence.
/// false means a continuation of a previous sequence.</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value CreateSequence<T>(NDShape sampleShape, int sequenceLength,
int[] colStarts, int[] rowIndices, T[] nonZeroValues,
bool sequenceStartFlag,
DeviceDescriptor device,
bool readOnly = false)
{
if (nonZeroValues.Length != rowIndices.Length)
{
throw new ArgumentException("The length of rowIndicies must be same as the length of nonZeroValues.");
}
if (colStarts.Length != sequenceLength + 1)
{
throw new ArgumentException("The length of colStarts must be equal to (sequenceLength + 1)");
}
uint numNonZeroValues = (uint)nonZeroValues.Length;
if (typeof(T).Equals(typeof(float)))
{
return Value._CreateSequenceFloat(sampleShape, (uint)sequenceLength, colStarts, rowIndices, nonZeroValues as float[], numNonZeroValues, sequenceStartFlag, device, readOnly);
}
else if (typeof(T).Equals(typeof(double)))
{
return Value._CreateSequenceDouble(sampleShape, (uint)sequenceLength, colStarts, rowIndices, nonZeroValues as double[], numNonZeroValues, sequenceStartFlag, device, readOnly);
}
else
{
throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
}
}
/// <summary>
/// Create Value object from sparse input as sequence data, for N-dimensional tensor. Only CreateSequence() for now.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="sampleShape">the tensor shape. For sparse input, the tensor shape leading dimensionality must be the same as the total size of the tensor shape.</param>
/// <param name="sequenceLength">the sequence length.</param>
/// <param name="colStarts">column start indices</param>
/// <param name="rowIndices">row indices</param>
/// <param name="nonZeroValues">sparse values</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value CreateSequence<T>(NDShape sampleShape, int sequenceLength,
int[] colStarts, int[] rowIndices, T[] nonZeroValues,
DeviceDescriptor device,
bool readOnly = false)
{
return Value.CreateSequence<T>(sampleShape, sequenceLength, colStarts, rowIndices, nonZeroValues, true, device, readOnly);
}
/// <summary>
/// Create Value object from sparse input as sequence data with sequenceStartFlag, for 1D tensor. Only CreateSequence() for now.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dimension"></param>
/// <param name="sequenceLength">the sequence length.</param>
/// <param name="colStarts">column start indices</param>
/// <param name="rowIndices">row indices</param>
/// <param name="nonZeroValues">sparse values</param>
/// <param name="sequenceStartFlag">true indicates that it is a new sequence.
/// false means a continuation of a previous sequence.</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value CreateSequence<T>(int dimension, int sequenceLength,
int[] colStarts, int[] rowIndices, T[] nonZeroValues,
bool sequenceStartFlag,
DeviceDescriptor device,
bool readOnly = false)
{
if (nonZeroValues.Length != rowIndices.Length)
{
throw new ArgumentException("The length of rowIndicies must be same as the length of nonZeroValues.");
}
if (colStarts.Length != sequenceLength + 1)
{
throw new ArgumentException("The length of colStarts must be equal to (sequenceLength + 1)");
}
uint numNonZeroValues = (uint)nonZeroValues.Length;
if (typeof(T).Equals(typeof(float)))
{
return Value._CreateSequenceFloat((uint)dimension, (uint)sequenceLength, colStarts, rowIndices, nonZeroValues as float[], numNonZeroValues, sequenceStartFlag, device, readOnly);
}
else if (typeof(T).Equals(typeof(double)))
{
return Value._CreateSequenceDouble((uint)dimension, (uint)sequenceLength, colStarts, rowIndices, nonZeroValues as double[], numNonZeroValues, sequenceStartFlag, device, readOnly);
}
else
{
throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
}
}
/// <summary>
/// Create Value object from sparse input as sequence data, for 1D tensor. Only CreateSequence() for now.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="dimension">data dimension</param>
/// <param name="sequenceLength">the sequence length.</param>
/// <param name="colStarts">column start indices</param>
/// <param name="rowIndices">row indices</param>
/// <param name="nonZeroValues">sparse values</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value CreateSequence<T>(int dimension, int sequenceLength,
int[] colStarts, int[] rowIndices, T[] nonZeroValues,
DeviceDescriptor device,
bool readOnly = false)
{
return Value.CreateSequence<T>(dimension, sequenceLength, colStarts, rowIndices, nonZeroValues, true, device, readOnly);
}
/// <summary>
/// Create Value object from NDArrayViews.
/// </summary>
/// <param name="sampleShape">data shape</param>
/// <param name="sequences">data sequence</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value Create(NDShape sampleShape,
IEnumerable<NDArrayView> sequences,
DeviceDescriptor device,
bool readOnly = false)
{
return Create(sampleShape, sequences, new List<bool>(0), device, readOnly);
}
/// <summary>
/// Create Value object from NDArrayViews with sequenceStartFlags
/// </summary>
/// <param name="sampleShape">data shape</param>
/// <param name="sequences">data sequences</param>
/// <param name="sequenceStartFlag">true indicates that it is a new sequence.
/// false means a continuation of a previous sequence.</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <returns></returns>
public static Value Create(NDShape sampleShape,
IEnumerable<NDArrayView> sequences,
IEnumerable<bool> sequenceStartFlags,
DeviceDescriptor device,
bool readOnly = false)
{
return Create(sampleShape, sequences, sequenceStartFlags, device, readOnly, /*createNewCopy = */ false);
}
/// <summary>
/// Create Value object from NDArrayViews with sequenceStartFlags
/// </summary>
/// <param name="sampleShape">data shape</param>
/// <param name="sequences">data sequence</param>
/// <param name="sequenceStartFlag">true indicates that it is a new sequence.
/// false means a continuation of a previous sequence.</param>
/// <param name="device">device</param>
/// <param name="readOnly">whether it a readonly value</param>
/// <param name="createNewCopy"></param>
/// <returns></returns>
public static Value Create(NDShape sampleShape,
IEnumerable<NDArrayView> sequences,
IEnumerable<bool> sequenceStartFlags,
DeviceDescriptor device,
bool readOnly,
bool createNewCopy)
{
var seqVector = new NDArrayViewPtrVector();
foreach (var element in sequences)
{
seqVector.Add(element);
}
var startFlags = Helper.AsBoolVector(sequenceStartFlags);
return _Create(sampleShape, seqVector, startFlags, device, readOnly, createNewCopy);
}
/// <summary>
/// Return the data of the Value object as a list of sequences with variable length.
/// This method returns an IList<IList<T>>. Each element of the outer list represents a sequence.
/// Each sequence, represented by IList<T>, contains a variable number of samples.
/// Each sample consits of a fixed number of elements with type of 'T'. The number of elements is determined by the variable shape.
/// The number of samples = (the count of elements in IList<T>)/(the count of elements of the sample)
/// The shape of the variable should match the shape of the Value object.
/// </summary>
/// <typeparam name="T">data type</typeparam>
/// <param name="outputVariable">the source variable</param>
/// <returns></returns>
public IList<IList<T>> GetDenseData<T>(Variable outputVariable)
{
var sequences = new List<IList<T>>();
if (typeof(T).Equals(typeof(float)))
{
if (_GetDataType() != DataType.Float)
{
throw new ArgumentException("The value type does not match the list type.");
}
var seqVec = new FloatVectorVector();
_CopyVariableValueToFloat(outputVariable, seqVec);
foreach (var seq in seqVec)
{
var seqList = seq as IList<T>;
if (seqList == null)
throw new TypeAccessException("Cannot convert to the value type.");
// It is required to create a new List from seq, since seq is dependent on the life cycle of seqVec.
sequences.Add(new List<T>(seqList));
}
}
else if (typeof(T).Equals(typeof(double)))
{
if (_GetDataType() != DataType.Double)
{
throw new ArgumentException("The value type does not match the list type.");
}
var seqVec = new DoubleVectorVector();
_CopyVariableValueToDouble(outputVariable, seqVec);
foreach (var seq in seqVec)
{
var seqList = seq as IList<T>;
if (seqList == null)
throw new TypeAccessException("Cannot convert to the value type.");
// It is required to create a new List from seq, since seq is dependent on the life cycle of seqVec.
sequences.Add(new List<T>(seqList));
}
}
else
{
throw new ArgumentException("The value type does not match the list type.");
}
return sequences;
}
/// <summary>
/// Return the data of the Value object as a list of sequences with variable length.
/// This method returns an IList<IList<T>>. Each element of the outer list represents a sequence.
/// Each sequence, represented by List<int>, contains a variable number of samples.
/// Each sample is represented by an index of the OneHot vector. The size of the OneHot vector should match that defined in the variable.
/// The number of samples = the count of elements in List<int>.
/// </summary>
/// <param name="outputVariable">the source variable</param>
/// <returns></returns>
public IList<IList<int>> GetOneHotData(Variable outputVariable)
{
var sequences = new List<IList<int>>();
var seqVec = new SizeTVectorVector();
_CopyVariableValueTo(outputVariable, seqVec);
foreach (var seq in seqVec)
{
var seqList = new List<int>(seq.Count);
foreach (var element in seq)
{
seqList.Add((int)element);
}
sequences.Add(seqList);
}
return sequences;
}
/// <summary>
/// Copy the data of the Value object into the buffer provided by 'sequences'.
/// The 'sequences' is a list of sequences with variable length.
/// The number of items contained in the outer list of 'sequences' is the number of sequences in the Value object.
/// Each element of the outer list represents a sequence.
/// Each sequence, represented by List<T>, contains a variable number of samples.
/// Each sample consits of a fixed number of elements with type of 'T'. The number of elements is determined by the variable shape.
/// The number of samples = the count of elements in List<T> / the count of elements of the sample
/// The shape of the variable should match the shape of the Value object.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="outputVariable"></param>
/// <param name="sequences"></param>
[Obsolete("CopyVariableValueTo() will be deprecated soon. Please use GetDenseData() instead.")]
public void CopyVariableValueTo<T>(Variable outputVariable, List<List<T>> sequences)
{
sequences.Clear();
if (typeof(T).Equals(typeof(float)))
{
if (_GetDataType() != DataType.Float)
{
throw new ArgumentException("The value type does not match the list type.");
}
var seqVec = new FloatVectorVector();
_CopyVariableValueToFloat(outputVariable, seqVec);
foreach (var seq in seqVec)
{
var seqList = seq as IList<T>;
if (seqList == null)
throw new TypeAccessException("Cannot convert to the value type.");
sequences.Add(new List<T>(seqList));
}
}
else if (typeof(T).Equals(typeof(double)))
{
if (_GetDataType() != DataType.Double)
{
throw new ArgumentException("The value type does not match the list type.");
}
var seqVec = new DoubleVectorVector();
_CopyVariableValueToDouble(outputVariable, seqVec);
foreach (var seq in seqVec)
{
var seqList = seq as IList<T>;
if (seqList == null)
throw new TypeAccessException("Cannot convert to the value type.");
sequences.Add(new List<T>(seqList));
}
}
else
{
throw new ArgumentException("The value type does not match the list type.");
}
}
//
// Copy the data of the Value object into the buffer provided by 'sequences'.
// The 'sequences' is a list of sequences with variable length.
// The number of items contained in the outer list of 'sequences' is the number of sequences in the Value object.
// Each element of the outer list represents a sequence.
// Each sequence, represented by List<int>, contains a variable number of samples.
// Each sample is represented by an index of the OneHot vector. The size of the OneHot vector should match that defined in the variable.
// The number of samples = the count of elements in List<int>.
//
[Obsolete("CopyVariableValueTo() will be deprecated soon. Please use GetOneHotData() instead.")]
public void CopyVariableValueTo(Variable outputVariable, List<List<int>> sequences)
{
var seqVec = new SizeTVectorVector();
_CopyVariableValueTo(outputVariable, seqVec);
sequences.Clear();
foreach (var seq in seqVec)
{
var seqList = new List<int>(seq.Count);
foreach (var element in seq)
{
seqList.Add((int)element);
}
sequences.Add(seqList);
}
return;
}
/// <summary>
/// Copy the data stored in 'this' Value object to the buffers representing a sequence in CSC sparse format.
/// The sequence buffer will be resized if necessary.
/// The Value should have the same tensor shape as outputVariable.
/// On return, the sequenceLength is set to the length of the sequence stored in 'this' Value,
/// and the colStarts, rowIndices and nonZeroValues contain the data of column indexes, row indexes and non-zero values,
/// and the numNonZeroValues is set to number of non-zero values contained in 'this' Value.
/// </summary>
/// <typeparam name="T">dat type</typeparam>
/// <param name="outputVariable">source variable</param>
/// <param name="sequenceLength">legnth of the sequence</param>
/// <param name="colStarts">column start indices</param>
/// <param name="rowIndices">row indices</param>
/// <param name="nonZeroValues">sparse values</param>
/// <param name="numNonZeroValues">number of sparse values</param>
public void GetSparseData<T>(Variable outputVariable,
out int sequenceLength,
out IList<int> colStarts,
out IList<int> rowIndices,
out IList<T> nonZeroValues,
out int numNonZeroValues)
{
var colStartVec = new IntVector();
var rowIndicesVec = new IntVector();
int[] n1 = new int[1];
int[] n2 = new int[1];
if (typeof(T).Equals(typeof(float)))
{
if (_GetDataType() != DataType.Float)
{
throw new ArgumentException("The value type does not match the list type.");
}
var nonZeroValuesVec = new FloatVector();
_CopyVariableValueToFloat(outputVariable, n1, colStartVec,
rowIndicesVec, nonZeroValuesVec, n2);
nonZeroValues = nonZeroValuesVec as IList<T>;
}
else if (typeof(T).Equals(typeof(double)))
{
if (_GetDataType() != DataType.Double)
{
throw new ArgumentException("The value type does not match the list type.");
}
var nonZeroValuesVec = new DoubleVector();
_CopyVariableValueToDouble(outputVariable, n1, colStartVec,
rowIndicesVec, nonZeroValuesVec, n2);
nonZeroValues = nonZeroValuesVec as IList<T>;
}
else
{
throw new ArgumentException("The value type does not match the list type.");
}
sequenceLength = n1[0];
numNonZeroValues = n2[0];
colStarts = colStartVec;
rowIndices = rowIndicesVec;
}
/// <summary>
/// Creates a new Value which is an alias of this Value.
/// </summary>
/// <param name="readOnly"></param>
/// <returns></returns>
public Value Alias(bool readOnly = false)
{
return _Alias(readOnly);
}
}
}