본문 바로가기
ML

[Tensorflow 2.x] 기초

by 나른한 사람 2021. 8. 13.

tensor

  • ~= Numpy.arrays
  • 일반적인 축 순서: tf.zeros([batch, width, height, features])

variable

tensor = tf.constant([[1.0, 2.0], [3.0, 4.0]])
variable = tf.Variable(tensor, name='my_tensor')

variable.assign([[1,2], [3,4]]) # same as initial variable (keep dtype)

variable.assign_add([[1,1], [1,1]]) # [[2.,3.], [4.,5.,]]

print(tf.Variable(tensor+1) == tf.Variable(tensor)) # [[False, False], [False, False]]: scalar add = broadcast

gradient tape

x = tf.ones((2,2)) # [[1,1], [1,1]]

with tf.GradientTape() as tape:
    tape.watch(x)    # ensures that tensor is being traced by this tape ( if x is variable, this line can be deleted )
    y = tf.reduce_sum(x)    # [4.]
    z = tf.multiply(y, y)    # [16.]

dz_dy = tape.gradient(z, y)    # [8.] ( z = y^2 -> z` = 2y and y=4 )
dz_dx = tape.gradient(z, x) # [[8.,8.], [8.,8.]] ( dy_dx = [[1.,1.,],[1.,1.,]], dz_dy * dy_dx)

GradientTape.gradient() method 호출되면 tape 리소스 해제,
여러 gradient를 계산하려면, persistent 속성을 True로 설정. tape객체가 gc될 때 리소스 해제됨.

with tf.GradientTape(persistent=True) as tape:
    y = x*x
    z = y*y
dz_dx = tape.gradient(z, x)
dy_dx = tape.gradient(y, x)
del tape

layer

class Dense(tf.Module):
    def __init__(self, in_features, out_features, name=None):
        super().__init__(name=name)
        self.is_built = False
        self.out_features = out_features
    def __call__(self, x):
        if not self.is_built:
            self.w = tf.Variable(tf.random.normal([in_features, out_features]), name='w')
            self.b = tf.Variable(tf.zeros([out_features]), name='b')
               self.is_built = True

        y = tf.matmul(x, self.w) + self.b
        return tf.nn.relu(y)

class Sequential(tf.Module):
    def __init__(self, name=None):
        super().__init__(name=name)

        self.dense_1 = Dense(out_features=3)
        self.dense_2 = Dense(out_features=2)

    def __call__(self, x):
        x = self.dense_1(x)
        return self.dense_2(x)

model = Sequential()

model(tf.constant([[2., 2., 2.,]]))
# tf.Tensor([[0.95288247 6.590308  ]], shape=(1, 2), dtype=float32)


model.submodules
# (<__main__.Dense object at 0x000001DCA4DF3FA0>, <__main__.Dense object at 0x000001DCA4DF39D0>)


model.variables
'''
(<tf.Variable 'b:0' shape=(3,) dtype=float32, numpy=array([0., 0., 0.], dtype=float32)>, <tf.Variable 'w:0' shape=(3, 3) dtype=float32, numpy=
array([[ 0.04039047, -0.78243726,  0.23027515],
       [ 0.8556069 ,  2.5516295 , -0.9199117 ],
       [ 0.49421707, -0.7252276 ,  0.49171472]], dtype=float32)>, <tf.Variable 'b:0' shape=(2,) dtype=float32, numpy=array([0., 0.], dtype=float32)>, <tf.Variable 'w:0' shape=(3, 2) dtype=float32, numpy=
array([[ 0.78440386,  1.8548651 ],
       [-0.5881889 ,  0.68632   ],
       [-0.20760429, -0.8521825 ]], dtype=float32)>)
'''

Summary

class mModel(tf.Module):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.w = tf.Variable(5.0)
        self.b = tf.Variable(0.0)

    def __call__(self, x):
        return self.w * x + self.b

def loss(y, y_pred):
    return tf.reduce_mean(tf.square(y-y_pred))

def train(model, x, y, learning_rate):
    with tf.GradientTape() as tape:
        _loss = loss(y, model(x))
    dw, db = tape.gradient(_loss, [model.w, model.b])

    model.w.assign_sub(learning_rate * dw)
    model.b.assign_sub(learning_rate * db)

model = mModel()

epochs = range(10)

def training_loop(model, x, y):
    for epoch in epochs:
        train(model, x, y, learning_rate=0.1)

        _loss = loss(y, model(x))
        print("Epoch %2d: W=%1.2f b=%1.2f, loss=%2.5f" %
          (epoch, Ws[-1], bs[-1], current_loss))

'ML' 카테고리의 다른 글

Basic RNN/LSTM cell implementation  (0) 2021.09.10
seq2seq + attention 이란?  (0) 2021.08.17
RNN, LSTM ?  (0) 2021.08.16
AutoEncoder  (0) 2021.07.28
[정리] DeepFM: A Factorization-Machine based Neural Network for CTR Prediction  (0) 2021.04.17

댓글