Search code examples
tensorflowone-hot-encoding

Printing tensor in strides


I am creating a tensor of one_hot encodings from an audio file loaded in through librosa. The tensor is massive, and I don't want to print all of it.

In fact this is what it shows me and then never prints when I try to print it: (or maybe it will but I don't want to wait) W tensorflow/core/framework/allocator.cc:124] Allocation of 1387692032 exceeds 10% of system memory.

How would I print only certain values? For example I would like to print every 50th one hot encoding in the tensor.

one_hot = _one_hot(load_audio()) # Tensor
sess = tf.InteractiveSession()

one_hot_prnt = tf.Print(one_hot, [one_hot], "One hot encoding:")
evaluator = tf.add(one_hot_prnt, one_hot_prnt)

evaluator.eval()

Solution

  • Tensors in tensorflow support fancy indexing similar to numpy. You can iterate over some dimension of the tensor.

    Consider the following tensor(t) with shape(10000, 10). Now you can iterate over the first dimension one index at a time, and get array with shape (10, ) e.g

    t = tf.random.uniform(shape=(10000, 10)
    print(t[0, :].eval(session=session)) # This prints first row of the tensor. The result is array with shape (10, )
    

    You can also access value individual (cell) position inside the tensor by specify the coordinate([row, col]) value.

    t = tf.random.uniform(shape=(10000, 10)
    print(t[0, 0].eval(session=session)) # This prints first element of first row. If the tensor has dimensions more than two, is this value would be a matrix or a tensor.