Guest User

Untitled

a guest
Feb 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. import numpy as np
  2. import tensorflow as tf
  3.  
  4.  
  5. def _stack_past(x, steps):
  6. """Stack the past data for each step.
  7. Ex. x = [0, ..., 60]. steps = [10, 20]
  8. Result [x, x[:-10], x[:-20]] normalized to the same shape
  9. """
  10. # Sort the steps in ascending order [10, 20]
  11. sorted_steps = steps.copy()
  12. sorted_steps.sort()
  13.  
  14. largest_step = sorted_steps[-1]
  15.  
  16. # Include the original data
  17. stacks = [x[largest_step:]]
  18.  
  19. for step in sorted_steps:
  20. # Normalize the shapes by skipping the difference from the largest step
  21. # Which are rows without enough past data
  22. skip = largest_step - step
  23. stacks.append(x[skip:-step])
  24.  
  25. return stacks
  26.  
  27.  
  28. def np_stack_past(x, steps):
  29. stacks = _stack_past(x, steps)
  30. return np.stack(stacks, axis=-1)
  31.  
  32.  
  33. def tf_stack_past(tensor, steps):
  34. stacks = _stack_past(tensor, steps)
  35. return tf.stack(stacks, axis=-1)
Add Comment
Please, Sign In to add comment