【问题标题】:Printing tensor in strides大步打印张量
【发布时间】:2019-09-30 04:56:20
【问题描述】:

我正在从通过 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 one-hot-encoding


    【解决方案1】:

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

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

    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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-14
      • 2019-08-13
      • 1970-01-01
      • 2021-12-16
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      • 2023-01-11
      相关资源
      最近更新 更多