2-4 Model Implementation with Dense layer 구현

2024. 3. 28. 04:26·Deep Learning/Deep learning Bacis Mathematics
CH02_07_[구현강의]_Model_Implementation_with_Dense_Layers

Model Implementation with Sequential Method¶

In [ ]:
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

model = Sequential()
model.add(Dense(units=10, activation='sigmoid'))
model.add(Dense(units=20, activation='sigmoid'))

Model Implementation with Model-subclassing¶

In [ ]:
from tensorflow.keras.layers import Dense

from tensorflow.keras.models import Model


class TestModel(Model):
  def __init__(self):
      super(TestModel , self).__init__() # 무조건 써준다

      self.dense1 = Dense(units=10, activation='sigmoid')
      self.dense2 = Dense(units=20, activation='sigmoid')

model = TestModel()

Forward Propagation of Models¶

In [5]:
import tensorflow as tf

from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
from tensorflow.keras.models import Model

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

# sequential method
model = Sequential()
model.add(Dense(units=10, activation='sigmoid'))
model.add(Dense(units=20, activation='sigmoid'))

Y = model(X)
print(Y.numpy())

# Model-subclassing


class TestModel(Model):
    def __init__(self):
        super(TestModel, self).__init__()
        self.densel = Dense(units=10, activation='sigmoid')
        self.dense2 = Dense(units=20, activation='sigmoid')

    def call(self, x):
        x = self.dense1(x)
        x = self.dense2(x)
        return x


model = TestModel()
[[0.44034722 0.37609255 0.4359588  0.5492521  0.43932596 0.6292582
  0.52676266 0.42661175 0.5663988  0.40935916 0.5450262  0.55641127
  0.5205597  0.37022638 0.6054369  0.35700879 0.38535807 0.45880455
  0.61080605 0.4159431 ]
 [0.39387602 0.40782458 0.46422666 0.5331263  0.45014948 0.6550785
  0.5234818  0.41261548 0.5374     0.3968744  0.5426624  0.53718483
  0.5300889  0.4013928  0.6068768  0.3602311  0.42310882 0.4642651
  0.6051279  0.41305   ]
 [0.39640498 0.35755673 0.39175728 0.5044617  0.34511787 0.6933837
  0.55284184 0.36910236 0.509452   0.36285144 0.5693475  0.47053567
  0.47472352 0.35739046 0.54703695 0.3798083  0.3488087  0.43967214
  0.6649527  0.46420223]
 [0.39454338 0.41528487 0.47197795 0.5411958  0.47116384 0.6511402
  0.5202143  0.42434198 0.59252995 0.41855285 0.5132188  0.57328206
  0.5892586  0.4171354  0.6224916  0.31017452 0.39421713 0.49908176
  0.5878478  0.461307  ]]

Layers in Models¶

In [ ]:
import tensorflow as tf

from tensorflow.keras.layers import Dense

from tensorflow.keras.models import Sequential

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

model = Sequential()
model.add(Dense(units=10, activation='sigmoid'))
model.add(Dense(units=20, activation='sigmoid'))

Y = model(X)

print(type(model.layers))
print(model.layers)

for layer in model.layers:
  w, b = layer.get_weights()
  print(w.shape, b.shape)
<class 'list'>
[<keras.layers.core.dense.Dense object at 0x7fcba89a4a90>, <keras.layers.core.dense.Dense object at 0x7fcba89a4810>]
(10, 10) (10,)
(10, 20) (20,)

Trainable Variables in Models¶

In [12]:
import tensorflow as tf

from tensorflow.keras.layers import Dense

from tensorflow.keras.models import Sequential

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

model = Sequential()
model.add(Dense(units=10, activation="sigmoid"))
model.add(Dense(units=20, activation="sigmoid"))

Y = model(X)

print(type(model.trainable_variables))
print(len(model.trainable_variables))

for train_var in model.trainable_variables:
    print(train_var.shape) # weight와 bias 하나씩
<class 'list'>
4
(10, 10)
(10,)
(10, 20)
(20,)
In [ ]:
 

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

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

티스토리툴바