【问题标题】:How to implement tf.gather with some other tf ops如何使用其他一些 tf 操作实现 tf.gather
【发布时间】:2022-10-09 09:21:42
【问题描述】:

我想用一些简单和常见的 tf 操作(如 concat、stack、reshape、slice 等)替换 tf.gather 以达到相同的结果,因为目前某些部署框架不支持 tf.gather(例如the operators that ncnn supports)。

测试代码:

import numpy as np
import tensorflow as tf 
params = np.random.rand(5, 2)
params = tf.constant(params)
print("==>> params: ", params)
indices = np.random.randint(low=0,high=5, size=(3,3))
indices = tf.constant(indices)
print("==>> indices: ", indices)
result = tf.gather(params, indices)  # to reimplement it
print("==>> result: ", result)
print("==>> result.shape: ", result.shape)

【问题讨论】:

    标签: tensorflow tensorflow2.0


    【解决方案1】:

    这很容易,他们可以创建两个带有视觉信息的不同图像,或者就像我们玩的视频游戏一样,以获得安全的可用带宽。 Tf.Gather 可以以许多有用的方式实现,包括选择性信息。

    [ 样本 ]:

    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Working
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    IMAGE_1 = plt.imread("C:\Users\Jirayu Kaewprateep\Pictures\Cats\206480490_10158968022281077_7829901115614154740_n_10158968022276077.jpg")
    IMAGE_1 = tf.image.resize(IMAGE_1, [log_scales_height, log_scales_width]).numpy()
    IMAGE_1 = tf.constant( IMAGE_1, dtype=tf.int32 ).numpy()
    
    params = plt.imread("C:\Users\Jirayu Kaewprateep\Pictures\Cats\206480490_10158968022281077_7829901115614154740_n_10158968022276077.jpg")
    params = tf.reshape( params, ( params.shape[0] * params.shape[1], 3 ))
    
    limit = tf.math.divide( params.shape[0] * params.shape[1], delta, name='Devide' ).numpy()
    limit = 414720
    limit = 691200
    indices = tf.range(start, limit, delta)
    result = tf.gather(params, indices)                 
    result = tf.reshape( result, (240, 288, 3) )        
    
    plt.figure(figsize=(1,2))
    plt.subplot(1,2,1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(IMAGE_1)
    plt.xlabel(" rescaling ")
    
    plt.subplot(1,2,2)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(result)
    plt.xlabel(" logarithms pictires ")
    
    plt.show()
    input('...')
    

    [ 输出 ]: 见附件图片。

    【讨论】:

    • 对不起,也许我没有把我的问题描述清楚,示例代码似乎不是我所期望的,我不知道 delta 的值是什么,以及为什么您仍然在示例代码中使用 tf.gather。我的目标是用其他一些 tf 操作替换 tf.gather 以达到相同的结果。
    • 我只是 tf.gather 上的一种方法的示例,它对选择性操作有其自身的好处,但也有矩阵示例。
    • 2. 我用 tf.gather 和 indices 矩阵做两个类似任务的例子。
    【解决方案2】:

    您是否找到了一种使用标准 tf ops 实现它的方法?我面临同样的问题。谢谢!

    【讨论】:

      猜你喜欢
      • 2014-03-30
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 2018-05-31
      • 2019-12-15
      • 2023-01-19
      相关资源
      最近更新 更多