1. 모델 구성 (layers)
2. 모델 컴파일 (model.compile)
3. 모델 적용(model.fit)
🔸 상수텐서와 변수
x_ones = tf.ones(shape=(2,1))
x_zeros = tf.zeros(shape=(2,1))
x_normal = tf.random.normal(shape=(3,1),mean=0,stddev=1.)
x_uniform = tf.random.uniform(shape=(3,1), minval=0, maxval=1)
print('ones:\n', x_ones)
print('\nzeros:\n', x_zeros)
print('\nnormal(정규분포랜덤):\n', x_normal)
print('\nuniform(균등분포랜덤):\n', x_uniform)
🔸 넘파이와 차이점
🔸 텐서플로로 변수 만들기: Variable
v= tf.Variable(tf.random.normal((3,1)))
v
v.assign(tf.ones((3,1)))
v.assign_add(np.array([[2.],[0],[0]]))
🔸 초깃값 0으로 스칼라변수 생성
x = tf.Variable(0.)
with tf.GradientTape() as tape:
y = 2*x + 3
grad_y_x = tape.gradient(y,x)
print(grad_y_x)
tf.Tensor(2.0, shape=(), dtype=float32)
🔸 크기(2,2) 값 0
x = tf.Variable(tf.zeros((2,2)))
with tf.GradientTape() as tape:
y= 2*x + 3
grad_y_x = tape.gradient(y,x)
print(grad_y_x)
tf.Tensor( [[2. 2.] [2. 2.]], shape=(2, 2), dtype=float32)
🔸 watch(): 추적한다는 것을 수동으로 알려주기
input_const = tf.constant(3.)
with tf.GradientTape() as tape:
tape.watch(input_const)
result=tf.square(input_const)
gradient = tape.gradient(result, input_const)
gradient
<tf.Tensor: shape=(), dtype=float32, numpy=6.0>
🔸 이계도 그래이디언트(그레이디언트의 그레이디언트 계산)
time = tf.Variable(5.)
with tf.GradientTape() as outer_tape:
with tf.GradientTape() as inner_tape:
position = 4.9 * time**2
speed = inner_tape.gradient(position, time) #4.9*2*time
acceleration = outer_tape.gradient(speed, time) #4.9*2
print('speed:', speed)
print('acceleration:', acceleration)
🔸 변수 리스트 계산
W = tf.Variable(tf.random.uniform((2,2))) #가중치
b = tf.Variable(tf.zeros((2,))) #편향
x = tf.random.uniform((2,2)) #데이터
with tf.GradientTape() as tape:
y = tf.matmul(x,W) + b
grad_y_W_and_b = tape.gradient(y,[W,b])
print(grad_y_W_and_b)
🔸 inputs
import numpy as np
num_samples_per_class = 1000
negative_samples = np.random.multivariate_normal(
mean=[0, 3],
cov=[[1, 0.5],[0.5, 1]],
size=num_samples_per_class)
positive_samples = np.random.multivariate_normal(
mean=[3, 0],
cov=[[1, 0.5],[0.5, 1]],
size=num_samples_per_class)
inputs = np.vstack((negative_samples, positive_samples)).astype(np.float32)
🔸 targets: 0 혹은 1
targets = np.vstack((np.zeros((num_samples_per_class, 1), dtype="float32"),
np.ones((num_samples_per_class, 1), dtype="float32")))
input_dim = 2
output_dim = 1
W = tf.Variable(initial_value=tf.random.uniform(shape=(input_dim, output_dim)))
b = tf.Variable(initial_value=tf.zeros(shape=(output_dim,)))
🔸 정방향 패스 함수: model
def model(inputs):
return tf.matmul(inputs, W) + b
# 평균제곱오차
def square_loss(targets, predictions):
per_sample_losses = tf.square(targets - predictions)
return tf.reduce_mean(per_sample_losses)
learning_rate = 0.1
def training_step(inputs, targets):
with tf.GradientTape() as tape:
predictions = model(inputs)
loss = square_loss(targets, predictions)
grad_loss_W, grad_loss_b = tape.gradient(loss, [W, b])
W.assign_sub(grad_loss_W * learning_rate)
b.assign_sub(grad_loss_b * learning_rate)
return loss
for step in range(40):
loss = training_step(inputs, targets)
print(f"Loss at step {step}: {loss:.4f}")
import matplotlib.pyplot as plt
plt.scatter(inputs[:, 0], inputs[:, 1], c=targets[:, 0])
predictions = model(inputs)
#타깃이 0또는 1이므로 예측값이 0.5보다 작으면 0, 크면 1
plt.scatter(inputs[:, 0], inputs[:, 1], c=predictions[:, 0] > 0.5)