# 버전 확인
import tensorflow as td
print(tf.__version__)
딥러닝
# 필요한 두 텐서
t1 = tf.Variable([1, 2, 3])
t2 = tf.constant([1, 2, 3,])
print(t1)
print(t2)
print(type(t1)) # RescourceVariable
print(type(t2)) # EagerTensor
variable (변수)로 선언한 텐서는 RescourceVariable이라는 클래스 타입인 것을 알 수 있고,
constant (상수)로 선언한 텐서는 EagerTensor라는 것을 알 수 있다.
딥러닝 모델에서의 필요한 텐서는 2가지로 나뉜다.
tf.constant : 학습이 일어나지 않는 부분인 데이터셋(ds) 에서는 tf.constant 로 immutable (변할 수 없는) 텐서로 만들어 준다.
tf.variable : 학습이 일어나는 모델(model) 부분에는 tf.Variable 로 mutable (변할 수 있는) 텐서로 만들어주어 업데이트가 발생할 수 있도록 한다. 모델 파리미터의 경우 사용된다.
python list 와 numpy ndarray를 텐서플로우의 텐서로 바꿀 수 있다.
import numpy as np
# %% tf.constant
test_list = [1, 2, 3]
test_np = np.array([1, 2, 3])
print(type(test_list)) # list 타입
print(type(test_np)) # ndarray 타입
t1 = tf.constant(test_list)
t2 = tf.constant(test_np)
print(t1)
print(t2)
print(type(t1))
print(type(t2))
# %% tf.constant
test_list = [1, 2, 3]
test_np = np.array([1, 2, 3])
t1 = tf.variable(test_list)
t2 = tf.variable(test_np)
print(t1)
print(t2)
print(type(t1))
print(type(t2))
암기는 아니지만 알아두면 좋은 부분!
# %% 번외
t1 = tf.constant(test_list)
t2 = tf.Variable(test_list)
t3 = tf.constant(t2) # variable 텐서를 constant 텐서로 변경이 불가능하다.
t4 = tf.Variable(t1) # constant 텐서를 variable 텐서로 변경은 가능하다.
# %%
# variable -> constant 변경 가능하게 해보자
t1 = tf.convert_to_tensor(test_list)
t2 = tf.convert_to_tensor(test_np)
t3 = tf.Variable(test_list)
t4 = tf.convert_to_tensor(t3)
print(type(t3))
print(type(t4))
# %%
# constant 끼리의 합이나 variable 끼리의 합의 결과는 EagerTensor
# constant 와 variable 의 합의 결과도 EagerTensor
test_list1 = [10, 20, 30]
test_list2 = [1, 2, 3]
t1 = tf.constant(test_list1)
t2 = tf.Variable(test_list2)
t3 = t1 + t2
print(type(t3))
import tenserflow as tf
import numpy as np
test_list = [1, 1, 1, 1, 1, 1]
t1 = tf.constant(test_list)
print(t1)
# ones() 와 zeros() 함수
# 원하는 값들을 원하는 shape으로 만들어 줄 수 있다.
t2 = tf.ones(shape = (100, )) # 100, 의 의미는 길이가 100인 백터를 만든다는 의미이다.
t2 = tf.ones(shape = (100, 3)) # 100, 3으로 형태를 만들어준다.
print(t2)
t2 = tf.ones(shape = ( 128, 128, 3)) # 이미지의 경우
print(t2)
t3 = tf.zeros(shape = (128, 128, 3))
print(t3)
# %%
t4 = 3*tf.ones(shape = (128, 128, 3)) # 스칼라 * 벡터 의 형태로 값을 곱하는 것과 같은 의미히다.
print(t4)
test_list = [[1, 2, 3], [4, 5, 6]]
t1 = tf.Variable(teset_list)
print(t1)
t2 = 3 * tf.ones_like(t1) # 유사하게 만들고 싶은 형태의 텐서를 넣어 주면 된다.
print(t2)
t3 = tf.zeros_like(t1)
print(t3)
데이터사이언스에서 데이터 셋을 만들 때나 W, b를 initalization할 경우에는 random한 값을 자주 사용하게 된다.
np.random.seed(0) # 일정한 랜덤값 고정
tf.random.set_seed(0) # 텐서플로우의 경우 랜던값 고정
t1 = tf.random.normal(shape=(10,10))
print(t1)
# %%
# numpy에서의 형태
import matplotlib.pyplot as plt
t2 = tf.random.normal(mean=3, stddev=1, shape=(1000, )) # 평균이 3이고, 표준편차가 1인 값을 만들어낸다.
print(t2)
fig. ax = plt.subplots(figsize=(15, 15))
ax.hist(t2.numpy(), bins=30)
ax.tick_params(lablesize=20)
random.uniform()
# %%
# 텐서플로우에서의 형태
# random한 텐서 만들기
t2 = tf.random.uniform(shape=(1000, ), minval=-10, maxval=10)
print(t2)
fig. ax = plt.subplots(figsize=(15, 15))
ax.hist(t2.numpy(), bins=30)
ax.tick_params(lablesize=20)
poisson()
t2 = tf.random.poisson(shape=(1000, ), lam=5)
print(t2)
fig. ax = plt.subplots(figsize=(15, 15))
ax.hist(t2.numpy(), bins=30)
ax.tick_params(lablesize=20)
추가정보
만든 텐서에서 정보 추출하기!
t1 = tf.random.normal(shape=(128,128,3))
print("t1.shape : ", t1.shape)
print("t1.dtype : ", t1.dtype)
test_np = np.random.randint(-10,10, size=(100,))
print(test_np.dtype)
t1 = tf.constant(test_np, dtype=tf.float32) # 데이터 타입 정의 가능하다.
print(t1.dtype)