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 |
