-
Notifications
You must be signed in to change notification settings - Fork 0
/
nwp.py
97 lines (77 loc) · 3.1 KB
/
nwp.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
import tensorflow as tf
import tensorflow_addons as tfa
import tensorflow_hub as hub
from tensorflow.keras import layers
class NWP_old(tf.keras.Model):
def __init__(self, n_classes, saved_finetune=None):
super(NWP_old, self).__init__()
self.USE = hub.KerasLayer("https://tfhub.dev/google/universal-sentence-encoder/4", trainable=False, dtype=tf.string)
self.dense2 = layers.Dense(512, activation=tfa.activations.gelu)
self.classes = layers.Dense(n_classes)
def __call__(self, x, training=True):
x = self.USE(x)
x = self.dense2(x)
x = self.classes(x)
return x
def inference(self, x, training=False):
x = self.USE(x, training=False)
x = self.dense2(x)
x = self.classes(x)
x = tf.nn.softmax(x)
return x
class NWP(tf.keras.Model):
def __init__(self, n_classes, saved_finetune=None):
super(NWP, self).__init__()
self.USE = hub.KerasLayer("https://tfhub.dev/google/universal-sentence-encoder/4", trainable=False, dtype=tf.string)
self.finetuned = FinetuneLayer(n_classes)
if saved_finetune is not None:
self.finetuned.load_weights(saved_finetune)
def __call__(self, x, training=True):
x = self.USE(x)
x = self.finetuned(x)
return x
def inference(self, x, training=False):
x = self.USE(x, training=False)
x = self.finetuned.inference(x)
return x
class FinetuneLayer(tf.keras.Model):
def __init__(self, n_classes):
super(FinetuneLayer, self).__init__()
self.dense2 = layers.Dense(512, activation=tfa.activations.gelu)
self.classes = layers.Dense(n_classes)
def __call__(self, x, training=True):
x = self.dense2(x)
x = self.classes(x)
return x
def inference(self, x, training=False):
x = self.dense2(x)
x = self.classes(x)
x = tf.nn.softmax(x)
return x
class NWPV2(tf.keras.Model):
def __init__(self, n_classes):
super(NWPV2, self).__init__()
self.USE = hub.KerasLayer("https://tfhub.dev/google/universal-sentence-encoder/4", trainable=False, dtype=tf.string)
self.dense1 = layers.Dense(512*4, activation=tfa.activations.gelu)
self.dropout = layers.Dropout(0.3)
self.dense2 = layers.Dense(512)
self.classes = layers.Dense(n_classes)
# self.curr_temp = 1.0
# self.temp_schedule = [1e-4, 1.0, 100000.0]
# def update_temp(self, step):
# self.curr_temp = min(self.temp_schedule[0] + (step * (self.temp_schedule[1]-self.temp_schedule[0])/self.temp_schedule[2]), self.temp_schedule[1])
def __call__(self, x, training=True):
x = self.USE(x, training=training)
x = self.dense1(x)
x = self.dropout(x, training=training)
x = self.dense2(x)
x = self.classes(x)
return x
def inference(self, x, training=False):
x = self.USE(x, training=training)
x = self.dense1(x)
x = self.dropout(x, training=training)
x = self.dense2(x)
x = self.classes(x)
x = tf.nn.softmax(x)
return x