-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_model.py
51 lines (34 loc) · 1.22 KB
/
custom_model.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
import custom_layer
from tensorflow import keras
import tensorflow as tf
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
"""
KEYWORDS:
Defining Custom Models (task)
"""
# Defining custom model
# We are to create a cusom model. To do so, create a class like below. Note that it must inherit
# from keras.models.Model class
class CustomModel(keras.models.Model):
# For custom models, the layers (graph nodes) are defined in the constructor
def __init__(self, units):
super(CustomModel, self).__init__()
self.l1 = custom_layer.CustomSum(units)
self.l2 = custom_layer.CustomSum(units)
self.l3 = custom_layer.CustomSum(units)
self.l4 = keras.layers.Dense(units, activation="relu")
# For custom models, the inputs (graph connections) are defined in the 'call' function
def call(self, inputs):
x1 = self.l1(inputs)
x2 = self.l2(inputs)
x3 = self.l3(x1)
x3 = self.l3(x2)
output = self.l4(x3)
return output
# Such user-defined model, can be trained and used on a similar structure like other for models:
# model = CustomModel(2)
# model.compile(optimizer, loss, metrices)
# model.fit()
# model.evaluate()
# model.predict()