forked from instillai/TensorFlow-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
103 lines (76 loc) · 3.18 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
"""models.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ibfKtpxC_hIhZlPbefCoqpAS7jTdyiFw
## Models in TensorFlow
In TensorFlow, you always need to define models to train a machine learning model. A model consists of layers that conduct operations and can be reused in the model's structure. Let's get started.
"""
# Loading necessary libraries
import tensorflow as tf
import numpy as np
"""### Layer
In TensorFlow, we can implement layers using the high-level [tf.Module](https://www.tensorflow.org/api_docs/python/tf/Module) class.
"""
class SampleLayer(tf.Module):
"""
We define the layer with a class that inherited the structure of tf.Module class.
"""
def __init__(self, name=None):
super().__init__(name=name)
# Define a trainable variable
self.x = tf.Variable([[1.0, 3.0]], name="x_trainable")
# Define a non-trainable variable
self.y = tf.Variable(2.0, trainable=False, name="y_non_trainable")
def __call__(self, input):
return self.x * input + self.y
# Initialize the layer
# Here, __call__ function will not be called
simple_layer = SampleLayer(name="my_layer")
# Call the layer and extract some information
output = simple_layer(tf.constant(1.0))
print("Output:", output)
print("Layer name:", simple_layer.name)
print("Trainable variables:", simple_layer.trainable_variables)
"""### Model
Now. let's define a model. A model consists of multiple layers.
"""
class Model(tf.Module):
def __init__(self, name=None):
super().__init__(name=name)
self.layer_1 = SampleLayer('layer_1')
self.layer_2 = SampleLayer('layer_2')
def __call__(self, x):
x = self.layer_1(x)
output = self.layer_2(x)
return output
# Initialize the model
custom_model = Model(name="model_name")
# Call the model
# Call the layer and extract some information
output = custom_model(tf.constant(1.0))
print("Output:", output)
print("Model name:", custom_model.name)
print("Trainable variables:", custom_model.trainable_variables)
"""### Keras Models
Keras is a high-level API that is part of TensorFlow now. You can use [tf.keras.Model](https://www.tensorflow.org/api_docs/python/tf/keras/Model) to define a model. You can also use the collection of [tf.keras.layers](https://www.tensorflow.org/api_docs/python/tf/keras/layers) for your convenience. It's straightforward as below to define a model that has two fully-connected layers:
"""
class CustomModel(tf.keras.Model):
def __init__(self):
super(CustomModel, self).__init__()
self.layer_1 = tf.keras.layers.Dense(16, activation=tf.nn.relu)
self.layer_2 = tf.keras.layers.Dense(32, activation=None)
def call(self, inputs):
x = self.layer_1(inputs)
out = self.layer_2(inputs)
return out
# Create model
custom_model = CustomModel()
# Call the model
# Call the layer and extract some information
output = custom_model(tf.constant([[1.0, 2.0, 3.0]]))
print("Output shape:", output.shape)
print("Model name:", custom_model.name)
# Count total trainable variables
total_trainable_var = np.sum([tf.size(var).numpy() for var in custom_model.trainable_variables])
print("Number of trainable variables:", total_trainable_var)