Guest User

Untitled

a guest
Jun 25th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. /**
  2. original: the tensor to modify
  3. axis: the axis to splice in
  4. start: the position in the given axis to begin splicing from
  5. deleteCount: how many frames to remove from the tensor
  6. toInsert: the tensor to insert at "start" (all axes must match except for "axis")
  7. */
  8. function spliceTensor(original, axis, start, deleteCount, toInsert)
  9. {
  10. var preStart = original.shape.map((e) => 0);
  11. var preEnd = original.shape.map((e, i) => i == axis ? start : e);
  12. var postStart = original.shape.map((e, i) => i == axis ? (start + deleteCount) : 0);
  13. var postEnd = original.shape.map((e, i) => i == axis ? (e - (start + deleteCount)) : e);
  14.  
  15. var pre = preEnd[axis] == 0 ? null : original.slice(preStart, preEnd);
  16. var post = postStart[axis] == original.shape[axis] ? null : original.slice(postStart, postEnd);
  17.  
  18. var toConcat = [];
  19. if(pre != null) { toConcat.push(pre); }
  20. if(toInsert != null) { toConcat.push(toInsert); }
  21. if(post != null) { toConcat.push(post); }
  22.  
  23. return tf.concat(toConcat, axis);
  24. }
  25.  
  26.  
  27.  
  28. /* usage */
  29.  
  30. var tf = require("@tensorflow/tfjs-core")
  31.  
  32. var orig = tf.ones([4, 4]);
  33. var vertical = tf.zeros([4, 2]);
  34. var horizontal = tf.zeros([2, 4]);
  35.  
  36. orig.print();
  37. vertical.print();
  38. horizontal.print();
  39.  
  40. var spliced = spliceTensor(orig, 1, 2, 0, vertical);
  41. spliced.print();
  42. /**
  43. Tensor
  44. [[1, 1, 0, 0, 1, 1],
  45. [1, 1, 0, 0, 1, 1],
  46. [1, 1, 0, 0, 1, 1],
  47. [1, 1, 0, 0, 1, 1]]
  48. */
  49.  
  50. spliced = spliceTensor(orig, 0, 2, 1, horizontal);
  51. spliced.print();
  52. /**
  53. Tensor
  54. [[1, 1, 1, 1],
  55. [1, 1, 1, 1],
  56. [0, 0, 0, 0],
  57. [0, 0, 0, 0],
  58. [1, 1, 1, 1]]
  59. */
  60.  
  61. spliced = spliceTensor(orig, 0, 2, 1);
  62. spliced.print();
  63. /*
  64. Tensor
  65. [[1, 1, 1, 1],
  66. [1, 1, 1, 1],
  67. [1, 1, 1, 1]]
  68. */
Add Comment
Please, Sign In to add comment