tensorflow output_data[output_id == i] = input_data[input_id == i]代码分析

该段代码等效于numpy操作,将input_data中对应output_id索引的元素赋值给output_data。在TensorFlow中,此操作用于根据索引进行数据选择和赋值。讨论了在学术和工业界使用低级API的重要性,以及在某些情况下使用成熟API的益处。同时提到了tf.searchsorted函数在找到输入索引在已排序tensor中的位置时的作用。
摘要由CSDN通过智能技术生成
import tensorflow as tf
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '1'

with tf.Graph().as_default(), tf.Session() as sess:
    input_data = tf.constant([0.1, 0.2, 0.3])
    input_ids = tf.constant([3, 1, 6])
    output_data = tf.constant([0., 0., 0., 0., 0.])
    output_ids = tf.constant([6, 3, 1, 3, 0])
    # From TF v1.13
    #s = tf.argsort(input_ids)
    # Before TF v1.13
    s = tf.contrib.framework.argsort(input_ids)
    input_ids_s = tf.gather(input_ids, s)
    n = tf.size(input_ids)
    output_idx_s = tf.minimum(tf.searchsorted(input_ids_s, output_ids), n - 1)
    searchsorted = tf.searchsorted(input_ids_s, output_ids), n - 1
    gather1 = tf.gather(input_ids_s, output_idx_s)
    equal = tf.equal(output_ids, tf.gather(input_ids_s, output_idx_s))
    gather2 = tf.gather(s, output_idx_s)
    gather3 = tf.gather(input_data, tf.gather(s, output_idx_s))
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值