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)