2-3 Cascaded_Dense_Layers 구현

2024. 3. 28. 04:25·Deep Learning/Deep learning Bacis Mathematics
CH02_06_[구현강의]_Cascaded_Dense_Layers

Shapes of cacaded Dense Layers¶

In [1]:
import tensorflow as tf

from tensorflow.keras.layers import Dense

N, n_feature = 4, 10
X = tf.random.normal(shape=(N, n_feature))

n_neurons = [3, 5]
dense1 = Dense(units=n_neurons[0], activation="sigmoid")
dense2 = Dense(units=n_neurons[1], activation="sigmoid")

# forward propagation
A1 = dense1(X)
Y = dense2(A1)

# get weight/bias
W1, B1 = dense1.get_weights()
W2, B2 = dense2.get_weights()

print("X: {}\n".format(X.shape))

print("W1: ", W1.shape)
print("B1: ", B1.shape)
print("A1: {}\n".format(A1.shape))

print("W2: ", W2.shape)
print("B2: ", B2.shape)
print("Y: {}".format(Y.shape))
X: (4, 10)

W1:  (10, 3)
B1:  (3,)
A1: (4, 3)

W2:  (3, 5)
B2:  (5,)
Y: (4, 5)

Dense Layers with Python List¶

In [1]:
import tensorflow as tf

from tensorflow.keras.layers import Dense

N, n_feature = 4, 10

X = tf.random.normal(shape=(N, n_feature))

n_neurons = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

dense_layers = list()

for n_neuron in n_neurons:
    dense = Dense(units=n_neuron, activation="relu")
    dense_layers.append(dense)
print("Input: ”", X.shape)

for dense_idx, dense in enumerate(dense_layers):
    X = dense(X)
    print("After dense layer", dense_idx + 1)
    print(X.shape, "\n")
Y=X
Input: ” (4, 10)
After dense layer 1
(4, 10) 

After dense layer 2
(4, 20) 

After dense layer 3
(4, 30) 

After dense layer 4
(4, 40) 

After dense layer 5
(4, 50) 

After dense layer 6
(4, 60) 

After dense layer 7
(4, 70) 

After dense layer 8
(4, 80) 

After dense layer 9
(4, 90) 

After dense layer 10
(4, 100) 

Output Calculations¶

In [ ]:
import tensorflow as tf

from tensorflow.math import exp
from tensorflow.linalg import matmul

from tensorflow.keras.layers import Dense

N, n_feature = 4, 5
X = tf.random.normal(shape=(N, n_feature))
X_cp = tf.identity(X)

n_neurons = [3, 4, 5]

dense_layers = list()
for n_neuron in n_neurons:
    dense = Dense(units=n_neuron, activation='sigmoid')
    dense_layers.append(dense)
print(X_cp.shape)

# forward propagation(Tensorflow)
W, B = list(), list()
for dense_idx, dense in enumerate(dense_layers):
    x = dense(X)
    w, b = dense.get_weights()
    W.append(w)
    B.append(b)
    print("Y(Tensorflow): \n", X.numpy())


# forward propagation(Manual)
for layer_idx in range(len(n_neurons)):
    w, b = W[layer_idx], B[layer_idx]

    X_cp_ = matmul(X_cp, w) + b
    X_cp_ = 1/(1 + exp(-X_cp_))
    print("Y(Manual): \n", X_cp.numpy())
(4, 5)
Y(Tensorflow): 
 [[ 0.06675826  0.48867878  0.3436861   1.2327288  -0.92616004]
 [-0.78806245  0.05966637 -0.37663674  0.50513923 -1.5538013 ]
 [-2.0325694  -0.32740903  0.26999393  0.15857382  1.9611359 ]
 [-1.2176428  -1.0491334   1.6158277  -0.09503141  1.9127471 ]]
Y(Tensorflow): 
 [[ 0.06675826  0.48867878  0.3436861   1.2327288  -0.92616004]
 [-0.78806245  0.05966637 -0.37663674  0.50513923 -1.5538013 ]
 [-2.0325694  -0.32740903  0.26999393  0.15857382  1.9611359 ]
 [-1.2176428  -1.0491334   1.6158277  -0.09503141  1.9127471 ]]
Y(Tensorflow): 
 [[ 0.06675826  0.48867878  0.3436861   1.2327288  -0.92616004]
 [-0.78806245  0.05966637 -0.37663674  0.50513923 -1.5538013 ]
 [-2.0325694  -0.32740903  0.26999393  0.15857382  1.9611359 ]
 [-1.2176428  -1.0491334   1.6158277  -0.09503141  1.9127471 ]]
Y(Manual): 
 [[ 0.06675826  0.48867878  0.3436861   1.2327288  -0.92616004]
 [-0.78806245  0.05966637 -0.37663674  0.50513923 -1.5538013 ]
 [-2.0325694  -0.32740903  0.26999393  0.15857382  1.9611359 ]
 [-1.2176428  -1.0491334   1.6158277  -0.09503141  1.9127471 ]]
Y(Manual): 
 [[ 0.06675826  0.48867878  0.3436861   1.2327288  -0.92616004]
 [-0.78806245  0.05966637 -0.37663674  0.50513923 -1.5538013 ]
 [-2.0325694  -0.32740903  0.26999393  0.15857382  1.9611359 ]
 [-1.2176428  -1.0491334   1.6158277  -0.09503141  1.9127471 ]]
Y(Manual): 
 [[ 0.06675826  0.48867878  0.3436861   1.2327288  -0.92616004]
 [-0.78806245  0.05966637 -0.37663674  0.50513923 -1.5538013 ]
 [-2.0325694  -0.32740903  0.26999393  0.15857382  1.9611359 ]
 [-1.2176428  -1.0491334   1.6158277  -0.09503141  1.9127471 ]]
In [ ]:
 

'Deep Learning > Deep learning Bacis Mathematics' 카테고리의 다른 글

3-1 Sigmoid & Softmax 이론  (0) 2024.03.29
2-4 Model Implementation with Dense layer 구현  (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
'Deep Learning/Deep learning Bacis Mathematics' 카테고리의 다른 글
  • 3-1 Sigmoid & Softmax 이론
  • 2-4 Model Implementation with Dense layer 구현
  • 2-2 Dense Layer 구현
  • 2-1 Dense Layer 이론
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
2-3 Cascaded_Dense_Layers 구현
상단으로

티스토리툴바