tensorflow - Most efficient way to reshape tensor into sequences -
i working audio in tensorflow, , obtain series of sequences obtained sliding window on data, speak. examples illustrate situation:
current data format:
shape = [batch_size, num_features]
example = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15] ] what want:
shape = [batch_size - window_length + 1, window_length, num_features]
example = [ [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ], [ [4, 5, 6], [7, 8, 9], [10, 11, 12] ], [ [7, 8, 9], [10, 11, 12], [13, 14, 15] ], ] my current solution this:
list_of_windows_of_data = [] x in range(batch_size - window_length + 1): list_of_windows_of_data.append(tf.slice(data, [x, 0], [window_length, num_features])) windowed_data = tf.squeeze(tf.stack(list_of_windows_of_data, axis=0)) and transform. however, creates 20,000 operations slows tensorflow down lot when creating graph. if else has fun , more efficient way this, please share.
you can using tf.map_fn follows:
example = tf.constant([ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15] ] ) res = tf.map_fn(lambda i: example[i:i+3], tf.range(example.shape[0]-2), dtype=tf.int32) sess=tf.interactivesession() res.eval() this prints
array([[[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]], [[ 4, 5, 6], [ 7, 8, 9], [10, 11, 12]], [[ 7, 8, 9], [10, 11, 12], [13, 14, 15]]])
Comments
Post a Comment