1

I have a function

def multivariate_data(dataset, target, start_index, end_index, history_size,
                      target_size, step, single_step=False):
  data = []
  labels = []
  start_index = start_index + history_size
  if end_index is None:
    end_index = len(dataset) - target_size
  #print(history_size)
  for i in range(start_index, end_index):
    indices = range(i-history_size, i, step)
    data.append(dataset[indices])
    if single_step:
      labels.append(target[i+target_size])
    else:
      labels.append(target[i:i+target_size])
  return np.array(data), np.array(labels)

I would like to make my calculations on GPU. But only tensor operations can be run on GPU. So I need to rewrite my function. for loops must be changed to tf.while_loop. All my numpy arrays mustbe changed into Tensors. How can i rewrite my function an for loops into tf.while_loop?

1 Answer 1

0

II have not tested using a GPU and I have assumed the input data is a rank 1 tensor and removed some parameters. I am not using the labels. There is also no exception handling but this can be refactored.

I am concatenating the tensors to 'append' to self._data but there are other efficient ways to 'append'.

            self._data = tf.concat([self._data,tf.gather(dataset, tf.range(1, 3, 1))],0)

This line just shows that a range can be used to pick values from a tensor to append to another. Exceptions are not handled as the data is fixed.

    import tensorflow as tf
    
    
class MultiVariate():
    def __init__(self):
        self._data = None
        self._labels = None

    def multivariate_data(self,
                          dataset,
                          start_index,
                          end_index,
                          history_size,
                          target_size,
                          single_step=False):
         start_index = start_index + history_size
         tf.print("end_index ", end_index)
         tf.print("start_index ", start_index)
         if self._data is None:
             self._data = tf.cast(tf.Variable(tf.reshape((), (0,))),dtype=tf.int32)
         if self._labels is None:
             self._labels = tf.cast(tf.Variable(tf.reshape((), (0,))),dtype=tf.int32)
         if end_index is None:
            end_index = len(dataset) - target_size

         def cond(i, j):
             return tf.less(i, j)

         def body(i, j):
             #A range of values are gathered
             self._data = tf.concat([self._data,[tf.gather(dataset, i)]],0)
             if ( i == start_index ): #Showing how A range of values are gathered and appended
                self._data = tf.concat([self._data,tf.gather(dataset, tf.range(1, 3, 1))],0)
             return tf.add( i , 1 ), j

         _,_ = tf.while_loop(cond, body, [start_index,end_index],shape_invariants=[start_index.get_shape(), end_index.get_shape()])

         return self._data

mv = MultiVariate()
d =    mv.multivariate_data(
                      tf.constant([1,88,99,4,5,6,7,8,9]),
                      tf.constant(2),
                      tf.constant(8),
                      tf.constant(1),
                      tf.constant(2),
                      tf.constant(2))
print("print ",d)

print tf.Tensor([ 4 88 99 5 6 7 8], shape=(7,), dtype=int32)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.