Parameters ---------- inputs_shape : tuple the shape of inputs tensor
(self, inputs_shape)
| 228 | return s.format(classname=self.__class__.__name__, **self.__dict__) |
| 229 | |
| 230 | def build(self, inputs_shape): |
| 231 | """ |
| 232 | Parameters |
| 233 | ---------- |
| 234 | inputs_shape : tuple |
| 235 | the shape of inputs tensor |
| 236 | """ |
| 237 | # Look up embeddings for inputs. |
| 238 | # Note: a row of 'embeddings' is the vector representation of a word. |
| 239 | # for the sake of speed, it is better to slice the embedding matrix |
| 240 | # instead of transferring a word id to one-hot-format vector and then |
| 241 | # multiply by the embedding matrix. |
| 242 | # embed is the outputs of the hidden layer (embedding layer), it is a |
| 243 | # row vector with 'embedding_size' values. |
| 244 | |
| 245 | self.embeddings = self._get_weights( |
| 246 | "embeddings", |
| 247 | shape=(self.vocabulary_size, self.embedding_size), |
| 248 | init=self.E_init, |
| 249 | ) |
| 250 | |
| 251 | self.normalized_embeddings = tf.nn.l2_normalize(self.embeddings, 1) |
| 252 | |
| 253 | if self.activate_nce_loss: |
| 254 | # Construct the variables for the NCE loss (i.e. negative sampling) |
| 255 | self.nce_weights = self._get_weights( |
| 256 | "nce_weights", |
| 257 | shape=(self.vocabulary_size, self.embedding_size), |
| 258 | init=self.nce_W_init, |
| 259 | ) |
| 260 | |
| 261 | self.nce_biases = self._get_weights( |
| 262 | "nce_biases", |
| 263 | shape=(self.vocabulary_size, ), |
| 264 | init=self.nce_b_init, |
| 265 | ) |
| 266 | |
| 267 | # @tf.function |
| 268 | def forward(self, inputs, use_nce_loss=None): |