大步打印张量

Printing tensor in strides

我正在从通过 librosa 加载的音频文件创建一个 one_hot 编码的张量。张量很大,我不想全部打印出来。

事实上,这就是它向我展示的内容,然后当我尝试打印它时从未打印过:(或者可能会,但我不想等待)W tensorflow/core/framework/allocator.cc:124] Allocation of 1387692032 exceeds 10% of system memory.

如何只打印某些值?例如,我想在张量中每第 50 个打印一次热编码。

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()

tensorflow 中的张量支持类似于 numpy 的花式索引。您可以迭代张量的某个维度。

考虑以下形状为 (10000, 10) 的张量 (t)。现在您可以一次遍历第一维一个索引,并获得形状为 (10, ) 的数组 例如

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, )

您还可以通过指定坐标([行,列])值来访问张量内的值个体(单元格)位置。

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.