You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sequence.cs 3.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using NumSharp;
  14. using NumSharp.Utilities;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. namespace Tensorflow.Keras
  19. {
  20. public class Sequence
  21. {
  22. /// <summary>
  23. /// Pads sequences to the same length.
  24. /// https://keras.io/preprocessing/sequence/
  25. /// https://faroit.github.io/keras-docs/1.2.0/preprocessing/sequence/
  26. /// </summary>
  27. /// <param name="sequences">List of lists, where each element is a sequence.</param>
  28. /// <param name="maxlen">Int, maximum length of all sequences.</param>
  29. /// <param name="dtype">Type of the output sequences.</param>
  30. /// <param name="padding">String, 'pre' or 'post':</param>
  31. /// <param name="truncating">String, 'pre' or 'post'</param>
  32. /// <param name="value">Float or String, padding value.</param>
  33. /// <returns></returns>
  34. public NDArray pad_sequences(IEnumerable<int[]> sequences,
  35. int? maxlen = null,
  36. string dtype = "int32",
  37. string padding = "pre",
  38. string truncating = "pre",
  39. object value = null)
  40. {
  41. if (value != null) throw new NotImplementedException("padding with a specific value.");
  42. if (padding != "pre" && padding != "post") throw new InvalidArgumentError("padding must be 'pre' or 'post'.");
  43. if (truncating != "pre" && truncating != "post") throw new InvalidArgumentError("truncating must be 'pre' or 'post'.");
  44. var length = sequences.Select(s => s.Length);
  45. if (maxlen == null)
  46. maxlen = length.Max();
  47. if (value == null)
  48. value = 0f;
  49. var type = getNPType(dtype);
  50. var nd = new NDArray(type, new Shape(length.Count(), maxlen.Value), true);
  51. for (int i = 0; i < nd.shape[0]; i++)
  52. {
  53. var s = sequences.ElementAt(i);
  54. if (s.Length > maxlen.Value)
  55. {
  56. s = (truncating == "pre") ? s.Slice(s.Length - maxlen.Value, s.Length) : s.Slice(0, maxlen.Value);
  57. }
  58. var sliceString = (padding == "pre") ? $"{i},{maxlen - s.Length}:" : $"{i},:{s.Length}";
  59. nd[sliceString] = np.array(s);
  60. }
  61. return nd;
  62. }
  63. private Type getNPType(string typeName)
  64. {
  65. return System.Type.GetType("NumSharp.np,NumSharp").GetField(typeName).GetValue(null) as Type;
  66. }
  67. }
  68. }