• 张量-序列比较与索引提取


    tf.argmin(input,axis,name = None)该函数用于返回input中最小值的索引index

    tf.argmax(input,axis,name = None)该函数用于返回input中最大值的索引index

    示例代码如下:

    1. import tensorflow.compat.v1 as tf
    2. tf.disable_v2_behavior()
    3. a = tf.constant([[1,8,3],[4,5,6]])
    4. with tf.Session() as sess:
    5. print(sess.run(tf.argmin(a)))
    6. print(sess.run(tf.argmax(a)))

    tf.setdiff1d(x,y,name = None)该函数用于返回x、y之间不同值的索引。返回的值共有两个:1、在x中出现,但是在y中没有出现的元素 2、这些元素在x中的索引,也就是下标或者位置。

    示例代码如下:

    1. import tensorflow.compat.v1 as tf
    2. tf.disable_v2_behavior()
    3. x = tf.constant([2,9,1,8,2,5,10])
    4. y = tf.constant([2,5,6,7,8,3,9])
    5. diff,index = tf.setdiff1d(x,y)
    6. with tf.Session() as sess:
    7. print(sess.run(diff))
    8. print(sess.run(index))

    tf.where(condition,x = None,y = None,name = None)该函数根据指定的条件,返回对应的值或坐标。如果x和y都为None,则返回condition中值为true的元素坐标,坐标以二维张量返回。如果x和y都不为None,则返回condition中值为true的坐标在x内的值以及condition中值为false的坐标在y内的值。这里,x和y必须具有相同的形状。

    示例代码如下:

    1. import tensorflow.compat.v1 as tf
    2. tf.disable_v2_behavior()
    3. cond = [True,False,True,False]
    4. x = [2,9,1,8]
    5. y = [4,5,6,7]
    6. with tf.Session() as sess:
    7. print(sess.run(tf.where(cond)))
    8. print(sess.run(tf.where(cond,x,y)))

    tf.unique(x,name = None)该函数返回一个张量,包含两个值:1、x中所有元素唯一化的数据列表y 2、x中的数据对应y元素的索引index。

    示例代码如下:

    1. import tensorflow.compat.v1 as tf
    2. tf.disable_v2_behavior()
    3. x = [2,2,3,5,5,7,9,8,8]
    4. y,index = tf.unique(x)
    5. with tf.Session() as sess:
    6. print(sess.run(y))
    7. print(sess.run(index))

    tf.invert_permutation(x,name = None)该函数用于将x中元素的值当作索引,返回新的张量。

    示例代码如下:

    1. import tensorflow.compat.v1 as tf
    2. tf.disable_v2_behavior()
    3. x = [2,1,3,0,4]
    4. y = tf.invert_permutation(x)
    5. with tf.Session() as sess:
    6. print(sess.run(y))

    tf.random_shuffle(value,seed = None,name = None)该函数沿着value的第一维将value随机重新排列。

    示例代码如下:

    1. import tensorflow.compat.v1 as tf
    2. tf.disable_v2_behavior()
    3. x = [[2,1,3],[0,4,3],[5,6,8]]
    4. y = tf.random_shuffle(x)
    5. with tf.Session() as sess:
    6. print(sess.run(y))
    7. print("--------------------------")
    8. print(sess.run(y))
    9. print("--------------------------")
    10. print(sess.run(y))

  • 相关阅读:
    Python tkinter--第13章数值调整控件(Spinbox)方法
    悲观锁和乐观锁
    开展高质量发展统计监测与评价的重要意义是什么
    大文件分片上传、断点续传、秒传
    Python函数进阶:实现自定义的装饰器
    总结的HTTP比较详细的知识
    Python面向对象 —— 类属性「二」(属性获取机制和陷阱、代码示例、执行流程分析)
    CENTOS上的网络安全工具(十一)走向Hadoop(3) MapReduce示例解析
    在哪里考华为认证更容易?
    神经网络模型应用实例,各种神经网络模型
  • 原文地址:https://blog.csdn.net/Victor_Li_/article/details/133652280