|
| 1 | +using NumSharp.Core; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Text.RegularExpressions; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace Tensorflow.Keras |
| 10 | +{ |
| 11 | + public class Sequence |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Pads sequences to the same length. |
| 15 | + /// https://keras.io/preprocessing/sequence/ |
| 16 | + /// https://faroit.github.io/keras-docs/1.2.0/preprocessing/sequence/ |
| 17 | + /// </summary> |
| 18 | + /// <param name="sequences">List of lists, where each element is a sequence.</param> |
| 19 | + /// <param name="maxlen">Int, maximum length of all sequences.</param> |
| 20 | + /// <param name="dtype">Type of the output sequences.</param> |
| 21 | + /// <param name="padding">String, 'pre' or 'post':</param> |
| 22 | + /// <param name="truncating">String, 'pre' or 'post'</param> |
| 23 | + /// <param name="value">Float or String, padding value.</param> |
| 24 | + /// <returns></returns> |
| 25 | + public NDArray pad_sequences(NDArray sequences, |
| 26 | + int? maxlen = null, |
| 27 | + string dtype = "int32", |
| 28 | + string padding = "pre", |
| 29 | + string truncating = "pre", |
| 30 | + object value = null) |
| 31 | + { |
| 32 | + int[] length = new int[sequences.size]; |
| 33 | + switch (sequences.dtype.Name) |
| 34 | + { |
| 35 | + case "Object": |
| 36 | + for (int i = 0; i < sequences.size; i++) |
| 37 | + { |
| 38 | + switch (sequences.Data<object>(i)) |
| 39 | + { |
| 40 | + case string data: |
| 41 | + length[i] = Regex.Matches(data, ",").Count; |
| 42 | + break; |
| 43 | + } |
| 44 | + } |
| 45 | + break; |
| 46 | + case "Int32": |
| 47 | + for (int i = 0; i < sequences.size; i++) |
| 48 | + length[i] = Regex.Matches(sequences.Data<object>(i).ToString(), ",").Count; |
| 49 | + break; |
| 50 | + default: |
| 51 | + throw new NotImplementedException($"pad_sequences: {sequences.dtype.Name}"); |
| 52 | + } |
| 53 | + |
| 54 | + if (maxlen == null) |
| 55 | + maxlen = length.Max(); |
| 56 | + |
| 57 | + if (value == null) |
| 58 | + value = 0f; |
| 59 | + |
| 60 | + var nd = new NDArray(np.int32, new Shape(sequences.size, maxlen.Value)); |
| 61 | + for (int i = 0; i < nd.shape[0]; i++) |
| 62 | + { |
| 63 | + switch(sequences[i]) |
| 64 | + { |
| 65 | + case int[] data: |
| 66 | + for (int j = 0; j < nd.shape[1]; j++) |
| 67 | + nd[i, j] = j < data.Length ? data[j] : value; |
| 68 | + break; |
| 69 | + default: |
| 70 | + throw new NotImplementedException("pad_sequences"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return nd; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments