Python| TensorFlow logical_and() 方法
Tensorflow 是谷歌开发的开源机器学习库。它的应用之一是开发深度神经网络。
模块tensorflow.math
为许多基本的逻辑运算提供了支持。函数tf.logical_and()
[别名tf.math.logical_and
] 为 Tensorflow 中的逻辑 AND函数提供支持。它需要 bool 类型的输入。输入类型是张量,如果张量包含多个元素,则计算元素逻辑与, .
Syntax: tf.logical_and(x, y, name=None) or tf.math.logical_and(x, y, name=None)
Parameters:
x: A Tensor of type bool.
y: A Tensor of type bool.
name (optional): The name for the operation.
Return type: A Tensor of bool type with the same size as that of x or y.
代码:
# Importing the Tensorflow library
import tensorflow as tf
# A constant vector of size 4
a = tf.constant([True, False, True, False], dtype = tf.bool)
b = tf.constant([True, False, False, True], dtype = tf.bool)
# Applying the AND function and
# storing the result in 'c'
c = tf.logical_and(a, b, name ='logical_and')
# Initiating a Tensorflow session
with tf.Session() as sess:
print('Input type:', a)
print('Input a:', sess.run(a))
print('Input b:', sess.run(b))
print('Return type:', c)
print('Output:', sess.run(c))
输出:
Input type: Tensor("Const:0", shape=(4, ), dtype=bool)
Input a: [ True False True False]
Input b: [ True False False True]
Return type: Tensor("logical_and:0", shape=(4, ), dtype=bool)
Output: [ True False False False]