[API] Tensorflow Control Flow

(텐서플로우 제어 연산자)

Note: Functions taking  Tensor  arguments can also take anything accepted by  tf.convert_to_tensor .


Tensorflow Logical Operators

TensorFlow provides several operations that you can use to add logical operators to your graph.


tf.logical_and(x, y, name=None)


Returns the truth value of x AND y element-wise.

Args:
  • x: A Tensor of type bool.
  • y: A Tensor of type bool.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


출처: Control Flow > Logical Operators


Tensorflow tf.logical_and 결과

In [1]: import tensorflow as tf

In [2]: import tfutil
In [3]: for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
   ...:     const1, const2 = tf.constant(xs[0]), tf.constant(xs[1])
   ...:     y = tfutil.get_operation_value(tf.logical_and(tf.cast(const1, tf.bool), tf.cast(const2, tf.bool)))
   ...:     print(str(xs) + " -> " + str(tfutil.get_operation_value(tf.cast(y, tf.int32))))
(0, 0) -> 0
(1, 0) -> 0
(0, 1) -> 0
(1, 1) -> 1


tf.logical_not(x, name=None)


Returns the truth value of NOT x element-wise.

Args:
  • x: A Tensor of type bool.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


출처: Control Flow > Logical Operators


Tensorflow tf.logical_not 결과

In [1]: import tensorflow as tf

In [2]: import tfutil
In [3]: for xs in [0, 1]:
   ...:     const = tf.constant(xs)
   ...:     y = tfutil.get_operation_value(tf.logical_not(tf.cast(const, tf.bool)))
   ...:     print(str(xs) + " -> " + str(tfutil.get_operation_value(tf.cast(y, tf.int32))))
0 -> 1
1 -> 0

tf.logical_or(x, y, name=None)


Returns the truth value of x OR y element-wise.

Args:
  • x: A Tensor of type bool.
  • y: A Tensor of type bool.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


출처: Control Flow > Logical Operators


Tensorflow tf.logical_or 결과

In [1]: import tensorflow as tf

In [2]: import tfutil
In [3]: for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
   ...:     const1, const2 = tf.constant(xs[0]), tf.constant(xs[1])
   ...:     y = tfutil.get_operation_value(tf.logical_or(tf.cast(const1, tf.bool), tf.cast(const2, tf.bool)))
   ...:     print(str(xs) + " -> " + str(tfutil.get_operation_value(tf.cast(y, tf.int32))))
(0, 0) -> 0
(1, 0) -> 1
(0, 1) -> 1
(1, 1) -> 1

tf.logical_xor(x, y, name='LogicalXor')


x ^ y = (x | y) & ~(x & y).


출처: Control Flow > Logical Operators


Tensorflow tf.logical_xor 결과

In [1]: import tensorflow as tf

In [2]: import tfutil
In [3]: for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
   ...:     const1, const2 = tf.constant(xs[0]), tf.constant(xs[1])
   ...:     y = tfutil.get_operation_value(tf.logical_xor(tf.cast(const1, tf.bool), tf.cast(const2, tf.bool)))
   ...:     print(str(xs) + " -> " + str(tfutil.get_operation_value(tf.cast(y, tf.int32))))
(0, 0) -> 0
(1, 0) -> 1
(0, 1) -> 1
(1, 1) -> 0


Tensorflow Comparison Operators

TensorFlow provides several operations that you can use to add comparison operators to your graph.


tf.equal(x, y, name=None)


Returns the truth value of (x == y) element-wise.

Args:
  • x: A Tensor. Must be one of the following types: halffloat32float64uint8int8int16int32int64complex64quint8qint8qint32stringboolcomplex128.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


출처: Control Flow > Comparison Operators


Tensorflow tf.equal 결과

In [1]: import tensorflow as tf

In [2]: import tfutil
In [3]: for xs in [(0, 0), (1, 0)]:
   ...:     const1, const2 = tf.constant(xs[0]), tf.constant(xs[1])
   ...:     y = tfutil.get_operation_value(tf.equal(const1, const2))
   ...:     print(str(xs) + " -> " + str(y))
(0, 0) -> True
(1, 0) -> False

tf.not_equal(x, y, name=None)


Returns the truth value of (x != y) element-wise.

Args:
  • x: A Tensor. Must be one of the following types: halffloat32float64uint8int8int16int32int64complex64quint8qint8qint32stringboolcomplex128.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


출처: Control Flow > Comparison Operators


Tensorflow tf.not_equal 결과

In [1]: import tensorflow as tf

In [2]: import tfutil
In [3]: for xs in [(0, 0), (1, 0)]:
   ...:     const1, const2 = tf.constant(xs[0]), tf.constant(xs[1])
   ...:     y = tfutil.get_operation_value(tf.not_equal(const1, const2))
   ...:     print(str(xs) + " -> " + str(y))
(0, 0) -> False
(1, 0) -> False

tf.less(x, y, name=None) 


Returns the truth value of (x < y) element-wise.


Args:

• x : A  Tensor . Must be one of the following types:  float32 ,  float64 ,  int32 ,  int64 ,  uint8 ,  int16 ,  int8 ,  uint16 ,  half .

• y : A  Tensor . Must have the same type as  x .

• name : A name for the operation (optional).


Returns:


A  Tensor  of type  bool .


출처: Control Flow > Comparison Operators


Tensorflow tf.less 결과

In [1]: import tensorflow as tf

In [2]: import tfutil
In [3]: for xs in [(0, 0), (0, 1), (1, 0)]:
   ...:     const1, const2 = tf.constant(xs[0]), tf.constant(xs[1])
   ...:     y = tfutil.get_operation_value(tf.less(const1, const2))
   ...:     print(str(xs[0]) + " < " + str(xs[1]) + " -> " + str(y))
0 < 0 -> False
0 < 1 -> True
1 < 0 -> False

tf.less_equal(x, y, name=None)


Returns the truth value of (x <= y) element-wise.

Args:
  • x: A Tensor. Must be one of the following types: float32float64int32int64uint8int16int8uint16half.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


출처: Control Flow > Comparison Operators


Tensorflow tf.less_equal 결과

In [1]: import tensorflow as tf

In [2]: import tfutil
In [3]: for xs in [(0, 0), (0, 1), (1, 0)]:
   ...:     const1, const2 = tf.constant(xs[0]), tf.constant(xs[1])
   ...:     y = tfutil.get_operation_value(tf.less_equal(const1, const2))
   ...:     print(str(xs[0]) + " <= " + str(xs[1]) + " -> " + str(y))
0 <= 0 -> True
0 <= 1 -> True
1 <= 0 -> False


tf.greater(x, y, name=None)


Returns the truth value of (x > y) element-wise.

Args:
  • x: A Tensor. Must be one of the following types: float32float64int32int64uint8int16int8uint16half.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


출처: Control Flow > Comparison Operators


Tensorflow tf.greater 결과

In [1]: import tensorflow as tf

In [2]: import tfutil
In [3]: for xs in [(0, 0), (0, 1), (1, 0)]:
   ...:     const1, const2 = tf.constant(xs[0]), tf.constant(xs[1])
   ...:     y = tfutil.get_operation_value(tf.greater(const1, const2))
   ...:     print(str(xs[0]) + " > " + str(xs[1]) + " -> " + str(y))
0 > 0 -> False
0 > 1 -> False
1 > 0 -> True


tf.greater_equal(x, y, name=None)


Returns the truth value of (x >= y) element-wise.


Args:
  • x: A Tensor. Must be one of the following types: float32float64int32int64uint8int16int8uint16half.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


출처: Control Flow > Comparison Operators


Tensorflow tf.greater_equal 결과

In [1]: import tensorflow as tf

In [2]: import tfutil
In [3]: for xs in [(0, 0), (0, 1), (1, 0)]:
   ...:     const1, const2 = tf.constant(xs[0]), tf.constant(xs[1])
   ...:     y = tfutil.get_operation_value(tf.greater_equal(const1, const2))
   ...:     print(str(xs[0]) + " >= " + str(xs[1]) + " -> " + str(y))
0 >= 0 -> True
0 >= 1 -> False
1 >= 0 -> True



이 포스팅은 머신러닝/딥러닝 오픈소스 Tensorflow 개발을 위한 선행학습으로 Tensorflow API Document의 Python API 대한 학습노트입니다.

Posted by 이성윤