TensorFlow | 상수, 시퀀스, 변수 텐서

텐서플로에서 텐서(Tensor) 데이터를 생성하는 기본 방법

import tensorflow as tf import os os.environ[‘TF_CPP_MIN_LOG_LEVEL’] = ‘2’

Tensor 생성하기

Constant Tensor

t1 = tf.constant(5, dtype=tf.int8, shape=(1, 1))
'''
[[5]]
'''

t2 = tf.zeros((3, 5), dtype=tf.int16)
'''
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
'''

t3 = tf.ones((4, 3), dtype=tf.int8)
'''
[[1 1 1]
 [1 1 1]
 [1 1 1]
 [1 1 1]]
'''

Sequence Tensor

seq_t1 = tf.range(1.5, 11, 4.5)
'''
[ 1.5  6.  10.5]
'''

seq_t2 = tf.range(2.5, 21, 4.5)
'''
[ 2.5  7.  11.5 16.  20.5]
'''

Variable Tensor

var_tensor = tf.Variable(100)
'''
100
'''

W = tf.Variable(tf.ones((2, 2)), name='W')
'''
[[1. 1.]
 [1. 1.]]
'''

b = tf.Variable(tf.zeros(2,), name='b')
'''
[0. 0.]
'''

Tensor 연산

a = tf.constant(10, dtype = tf.int32)
b = tf.constant(3, dtype = tf.int32)

add = tf.add(a, b) # tensor 덧셈
sub = tf.subtract(a, b) # tensor 뺄셈
mul = tf.multiply(a, b) # tensor 곱
div = tf.truediv(a, b) # tensor 나눗셈 (a/b)