tf.greater(v1,v2)和tf.where(p1,p2,p3)

目录

tf.greater(v1,v2)

tf.where(p1,p2,p3)


tf.greater(v1,v2)

功能:比较两个输入张量的每一个元素的大小,并返回比较结果。


import tensorflow as tf

v1 = tf.constant([1,2,3,4])
v2 = tf.constant([4,3,2,1])

with tf.Session() as sess:
    print(sess.run(tf.greater(v1,v2)))   
    sess.close()

输出结果:[False False  True  True]


 


tf.where(p1,p2,p3)

参数p1是一个bool变量,也可以是一个表达式,返回值是True或者False。这个变量可以是一个也可以是一个列表,就是很多个True或者False组成的列表。

如果是True,返回p2,反之返回p3


#coding:utf-8

import tensorflow as tf

A = 100
B = tf.constant([1, 2, 3, 4])
C = tf.constant([1, 1, 1, 1])
D = tf.constant([0, 0, 0, 0])

with tf.Session() as sess:
    print(sess.run(tf.where(A > 1, 'A', 'B')))
    print(sess.run(tf.where(False, 'A', 'B')))
    print(sess.run(tf.where(B > 2, C, D)))

输出:b'A'
            b'B'
            [0 0 1 1]

天上我才必有用,千金散尽还复来!
原文地址:https://www.cnblogs.com/lutaishi/p/13436331.html