### 2021.12.17
tf.constant
tf.constant 함수는 상수 텐서를 생성합니다.
예제 #1
tf.constant 함수는 파이썬 리스트 또는 NumPy 어레이로부터 상수 텐서를 생성합니다.
#In[]
import tensorflow as tf
import numpy as np
a = tf.constant([1, 2, 3, 4])
b = tf.constant(np.array([1, 2, 3, 4]))
print(a)
print(b)
#Out[]
tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)
tf.Tensor([1 2 3 4], shape=(4,), dtype=int64)
예제 #2
tf.constant 함수의 dtype은 생성될 값의 자료형을 지정합니다.
shape을 지정하면 입력값을 지정한 형태로 생성합니다.
#In[]
import tensorflow as tf
import numpy as np
a = tf.constant([1, 2, 3, 4], dtype=tf.float64)
b = tf.constant(0, shape=(2, 3))
print(a)
print(b)
#Out[]
tf.Tensor([1. 2. 3. 4.], shape=(4,), dtype=float64)
tf.Tensor(
[[0 0 0]
[0 0 0]], shape=(2, 3), dtype=int32)
tf. TenserShape
tf.TenseorShape은 텐서의 형태를 나타냅니다
예제 #1
#In[]
import tensorflow as tf
import numpy as np
a = tf.constant([1,2,3,4])
b = tf.constant(np.array([1,2,3,4]))
print(a)
print(b)
#Out[]
tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)
tf.Tensor([1 2 3 4], shape=(4,), dtype=int64)
tf.constant 함수는 파이썬 리스트 또는 NumPy 어레이로부터 상수 텐서를 생성합니다.
예제 #2
#In[]
a = tf.constant([1,2,3,4], dtype=tf.float64)
b = tf.constant(0, shape=(2,3))
print(a)
print(b)
#Out[]
tf.Tensor([1. 2. 3. 4.], shape=(4,), dtype=float64)
tf.Tensor(
[[0 0 0]
[0 0 0]], shape=(2, 3), dtype=int32)
tf.constant 함수의 dtype은 생성될 값의 자료형을 지정합니다.
shape을 지정하면 입력값을 지정한 형태로 생성합니다.
tf.rank
tf.rank 함수는 입력한 텐서의 rank를 반환합니다.
#In[]
scalar = tf.constant(1)
vector = tf.constant([1, 2, 3])
matrix = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
tensor = tf.constant([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]])
print(tf.rank(scalar))
print(tf.rank(vector))
print(tf.rank(matrix))
print(tf.rank(tensor))
#Out[]
tf.Tensor(0, shape=(), dtype=int32)
tf.Tensor(1, shape=(), dtype=int32)
tf.Tensor(2, shape=(), dtype=int32)
tf.Tensor(3, shape=(), dtype=int32)
텐서 (Tensor) 객체의 랭크 (Rank)는 차원의 수 (n-dimension)입니다.
tf.rank()는 텐서의 랭크를 반환합니다.
텐서 scalar, vector, matrix, tensor는 각각 랭크 0, 1, 2, 3를 가집니다.
tf.cast
tf.cast 함수는 텐서를 새로운 자료형으로 변환합니다.
예제 #1
#In[]
x = tf.constant([1.8,2.2], dtype=tf.float32)
y = tf.cast(x, tf.int32)
print(x)
print(y)
#Out[]
tf.Tensor([1.8 2.2], shape=(2,), dtype=float32)
tf.Tensor([1 2], shape=(2,), dtype=int32)
x는 (2,) 형태를 갖고, tf.float32 자료형을 갖는 텐서입니다.
tf.cast()를 사용해서 tf.int32 자료형을 갖도록 변환했습니다.
예제 #2
#In[]
x = tf.constant([1.8, 2.2], dtype=tf.float32)
y = tf.dtypes.cast(x, tf.int32)
print(x)
print(y)
#Out[]
tf.Tensor([1.8 2.2], shape=(2,), dtype=float32)
tf.Tensor([1 2], shape=(2,), dtype=int32)
tf.dtypes.cast()는 tf.cast()의 alias입니다.
tf.dtypes.cast()를 사용해서 tf.int32 자료형을 갖도록 변환했습니다.
tf.zeros
tf.zeros 함수는 모든 요소가 0인 텐서를 생성합니다.
#In[]
a = tf.zeros(1)
b = tf.zeros([2])
c = tf.zeros([2,3])
print(a)
print(b)
print(c)
#Out[]
tf.Tensor([0.], shape=(1,), dtype=float32)
tf.Tensor([0. 0.], shape=(2,), dtype=float32)
tf.Tensor(
[[0. 0. 0.]
[0. 0. 0.]], shape=(2, 3), dtype=float32)
tf.zeros()는 모든 요소가 0인 텐서를 만듭니다.
tf.zeros()에 만들어질 텐서의 형태 (shape)를 입력할 수 있습니다.
tf.ones
tf.ones 함수는 모든 요소가 1인 텐서를 생성합니다.
#In[]
a = tf.ones(3)
b = tf.ones([4])
c = tf.ones([2,2,2])
print(a)
print(b)
print(c)
#Out[]
tf.Tensor([1. 1. 1.], shape=(3,), dtype=float32)
tf.Tensor([1. 1. 1. 1.], shape=(4,), dtype=float32)
tf.Tensor(
[[[1. 1.]
[1. 1.]]
[[1. 1.]
[1. 1.]]], shape=(2, 2, 2), dtype=float32)
tf.ones()는 모든 요소가 1인 텐서를 만듭니다. tf.ones()에 만들어질 텐서의 형태 (shape)를 입력합니다.
tf.constant_initializer
tf.constant_initializer는 특정값을 갖는 텐서를 생성하는 Initializer입니다.
예제#1
#In[]
value = [0,1,2,3,4,5]
initializer = tf.constant_initializer(value)
const_tensor = initializer(shape=[2,3], dtype=tf.float32)
print(const_tensor)
#Out[]
tf.Tensor(
[[0. 1. 2.]
[3. 4. 5.]], shape=(2, 3), dtype=float32)
Initializer를 사용하면 텐서의 형태가 결정되지 않은 상태에서도 텐서를 어떻게 생성할지 결정할 수 있습니다.
tf.constant_initializer에 의해 미리 지정한 값 (value)을 갖는 텐서가 만들어졌습니다.
예제#2
#In[]
def make_variables(k, initializer):
return (tf.Variable(initializer(shape=[k], dtype=tf.float32)),
tf.Variable(initializer(shape=[k,k], dtype=tf.float32)))
v1, v2 = make_variables(2, tf.constant_initializer(2))
print(v1)
print(v2)
#Out[]
<tf.Variable 'Variable:0' shape=(2,) dtype=float32, numpy=array([2., 2.], dtype=float32)>
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[2., 2.],
[2., 2.]], dtype=float32)>
make_variables은 텐서의 크기와 Initializer를 입력받아서 tf.Variable 객체를 반환하는 함수입니다.
tf.constant_initializer를 사용해서 특정값을 갖는 두 개의 tf.Variable 객체를 생성했습니다.
tf.zeros_initializer
tf.zeros_initializer는 모두 0의 값을 갖는 텐서를 생성하는 Initializer입니다.
#In[]
initializer = tf.zeros_initializer()
zeros_tensor = initializer(shape=(2,2), dtype=tf.float32)
print(zeros_tensor)
#Out[]
tf.Tensor(
[[0. 0.]
[0. 0.]], shape=(2, 2), dtype=float32)
Initializer를 사용하면 텐서의 형태가 결정되지 않은 상태에서도 텐서를 어떻게 생성할지 결정할 수 있습니다.
tf.zeros_initializer를 사용해서 (2, 2) 형태를 갖고 모든 값이 0인 텐서를 생성했습니다.
tf.ones_initializer
tf.ones_initializer는 모두 1의 값을 갖는 텐서를 생성하는 Initializer입니다.
예제 #1
#In[]
initializer = tf.ones_initializer()
ones_tensor = initializer(shape=(2,2), dtype=tf.float32)
print(ones_tensor)
#Out[]
tf.Tensor(
[[1. 1.]
[1. 1.]], shape=(2, 2), dtype=float32)
Initializer를 사용하면 텐서의 형태가 결정되지 않은 상태에서도 텐서를 어떻게 생성할지 결정할 수 있습니다.
tf.ones_initializer를 사용해서 (2, 2) 형태를 갖고 모든 값이 1인 텐서를 생성했습니다.
예제 #2
#In[]
def make_variables(k, initializer):
return (tf.Variable(initializer(shape=[k], dtype=tf.float32)),
tf.Variable(initializer(shape=[k, k], dtype=tf.float32)))
v1, v2 = make_variables(2, tf.ones_initializer())
print(v1)
print(v2)
#Out[]
<tf.Variable 'Variable:0' shape=(2,) dtype=float32, numpy=array([1., 1.], dtype=float32)>
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[1., 1.],
[1., 1.]], dtype=float32)>
make_variables은 텐서의 크기와 Initializer를 입력받아서 tf.Variable 객체를 반환하는 함수입니다.
tf.ones_initializer를 사용해서 모두 1의 값을 갖는 두 개의 tf.Variable 객체를 생성했습니다.
tf.range
tf.range 함수는 주어진 범위와 간격을 갖는 숫자들의 시퀸스를 생성합니다.
#In[]
a = tf.range(0.3)
b = tf.range(1,5,2)
print(a)
print(b)
#Out[]
tf.Tensor([0.], shape=(1,), dtype=float32)
tf.Tensor([1 3], shape=(2,), dtype=int32)
tf.range()는 파이썬 range()와 비슷하게, 주어진 범위와 간격을 갖는 숫자들의 시퀀스를 만듭니다.
tf.linspace
tf.linsapce 함수는 주어진 범위를 균일한 간격으로 나누는 숫자의 시퀸스를 변환합니다.
#In[]
a = tf.linspace(0,1,3)
b = tf.linspace(0,3,10)
print(a)
print(b)
#Out[]
tf.Tensor([0. 0.5 1. ], shape=(3,), dtype=float64)
tf.Tensor(
[0. 0.33333333 0.66666667 1. 1.33333333 1.66666667
2. 2.33333333 2.66666667 3. ], shape=(10,), dtype=float64)
tf.linspace()는 numpy.linspace()와 비슷하게, 주어진 범위를 균일한 간격으로 나누는 숫자의 시퀀스를 반환합니다.