Affine Function¶
In [2]:
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Conv2D
x = tf.constant([[10.0]]) # input setting (Note : input은 matrix 형태 )
dense = Dense(units=1, activation="linear") # imp.an affine function
y_tf = dense(x) # forward propagation + p arams initialization
W, B = dense.get_weights() # get weight, bias
# print results
print("========Input/Weight/Bias=========")
print("x : {}\n{}\n".format(x.shape, x.numpy()))
print("W : {}\n{}\n".format(x.shape, W))
print("B : {}\n{}\n".format(x.shape, B))
y_man = tf.linalg.matmul(x, W) + B # forward propagation(manual) 행렬의 곱으로 증명
print("========Output/Weight/Bias=========")
print("y(Tensorflow):{}\n{}\n)".format(y_tf.shape, y_tf.numpy()))
print("y(Manual): {}\n{}\n".format(y_man.shape, y_man.numpy()))
========Input/Weight/Bias=========
x : (1, 1)
[[10.]]
W : (1, 1)
[[-0.7190732]]
B : (1, 1)
[0.]
========Output/Weight/Bias=========
y(Tensorflow):(1, 1)
[[-7.190732]]
)
y(Manual): (1, 1)
[[-7.190732]]
결과가 같음을 알수있음¶
Params Initalization¶
In [1]:
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.initializers import Constant
x = tf.constant([[10.]]) # input setting(Note : input -> matrix)
# weingt/bias setting
w, b = tf.constant(10.), tf.constant(20.) # float로 쓰는걸 습관으로 하기
w_init, b_init = Constant(w), Constant(b)
# target W, B설
dense = Dense(units=1,
activation='linear',
kernel_initializer=w_init,
bias_initializer=b_init)
# affine만들고 activation지정 , init으로 초기화 지정
y_tf = dense(x)
print(y_tf)
w, b = dense.get_weights()
print("W :{}\n{}\n".format(w.shape, w))
print("B :{}\n{}\n".format(b.shape, b))
tf.Tensor([[120.]], shape=(1, 1), dtype=float32)
W :(1, 1)
[[10.]]
B :(1,)
[20.]
10*10 +20 = 120 이 결과로 나온거
AFfine Function with n Features¶
In [4]:
import tensorflow as tf
from tensorflow.keras.layers import Dense
x = tf.random.uniform(shape=(1, 10), minval=0, maxval=10) # 0~10사이의 숫자
dense = Dense(units=1)
y_tf = dense(x) # Weight 가 10개로 적용
W, B = dense.get_weights()
# 알아서 초기화 됨
y_man = tf.linalg.matmul(x, W)+B
print('========Input/Weight/Bias=========')
print("x : {}\n{}\n".format(x.shape, x.numpy()))
print("W : {}\n{}\n".format(W.shape, W))
print("B : {}\n{}\n".format(B.shape, B))
print('========output/Weight/Bias=========')
print("y(Tensorflow):{}\n{}\n)".format(y_tf.shape, y_tf.numpy()))
print("y(Manual): {}\n{}\n".format(y_man.shape, y_man.numpy()))
========Input/Weight/Bias=========
x : (1, 10)
[[7.19821 2.8476775 4.774387 7.7870045 9.054894 3.8097656 6.4105678
2.5757134 2.0627534 5.32243 ]]
W : (10, 1)
[[ 0.46323365]
[ 0.06407285]
[ 0.7176154 ]
[ 0.50136095]
[-0.707779 ]
[ 0.10467267]
[-0.21442622]
[ 0.3111828 ]
[-0.21710354]
[-0.0219177 ]]
B : (1,)
[0.]
========output/Weight/Bias=========
y(Tensorflow):(1, 1)
[[3.6995368]]
)
y(Manual): (1, 1)
[[3.6995368]]
In [ ]:
'Deep Learning > Deep learning Bacis Mathematics' 카테고리의 다른 글
| 2-3 Cascaded_Dense_Layers 구현 (0) | 2024.03.28 |
|---|---|
| 2-2 Dense Layer 구현 (0) | 2024.03.28 |
| 2-1 Dense Layer 이론 (0) | 2024.03.27 |
| 1-3. Artificial Nuerons 구현 (2) (0) | 2024.03.18 |
| 1-1. Artificial Neurons 이론 (1) (0) | 2024.03.18 |
