1-2. Artificial Nuerons 구현 (1)

2024. 3. 18. 23:18·Deep Learning/Deep learning Bacis Mathematics

 

 

 
 

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
'Deep Learning/Deep learning Bacis Mathematics' 카테고리의 다른 글
  • 2-2 Dense Layer 구현
  • 2-1 Dense Layer 이론
  • 1-3. Artificial Nuerons 구현 (2)
  • 1-1. Artificial Neurons 이론 (1)
Juson
Juson
  • Juson
    Juson의 데이터 공부
    Juson
  • 전체
    오늘
    어제
    • 분류 전체보기 (95)
      • RAG (2)
      • AI (2)
        • NLP (0)
        • Generative Model (0)
        • Deep Reinforcement Learning (2)
        • LLM (0)
      • Logistic Optimization (0)
      • Machine Learning (37)
        • Linear Regression (2)
        • Logistic Regression (2)
        • Decision Tree (5)
        • Naive Bayes (1)
        • KNN (2)
        • SVM (2)
        • Clustering (4)
        • Dimension Reduction (3)
        • Boosting (6)
        • Abnomaly Detection (2)
        • Recommendation (4)
        • Embedding & NLP (4)
      • Reinforcement Learning (5)
      • Deep Learning (10)
        • Deep learning Bacis Mathema.. (10)
      • Optimization (2)
        • OR Optimization (0)
        • Convex Optimization (0)
        • Integer Optimization (0)
      • SNA 분석 (0)
      • 포트폴리오 최적화 공부 (0)
        • 최적화 기법 (0)
        • 금융 베이스 (0)
      • Finanancial engineering (0)
      • 프로그래머스 데브코스(Boot camp) (15)
        • SQL (9)
        • Python (5)
        • Machine Learning (1)
      • Python (22)
      • Project (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
Juson
1-2. Artificial Nuerons 구현 (1)
상단으로

티스토리툴바