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 |
