1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
import numpy as np |
5
|
|
|
import theano |
6
|
|
|
import theano.tensor as T |
7
|
|
|
from deepy.layers import NeuralLayer, Softmax3D, Softmax, Dense, Chain |
8
|
|
|
from deepy.core import CrossEntropyCost |
9
|
|
|
|
10
|
|
|
from cost import LMCost |
11
|
|
|
|
12
|
|
|
class FullOutputLayer(NeuralLayer): |
13
|
|
|
|
14
|
|
|
def __init__(self, vocab_size): |
15
|
|
|
super(FullOutputLayer, self).__init__("full_output") |
16
|
|
|
self.vocab_size = vocab_size |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def prepare(self): |
20
|
|
|
self.core = Chain(self.input_dim).stack(Dense(self.vocab_size), |
21
|
|
|
Softmax3D()) |
22
|
|
|
self.register_inner_layers(self.core) |
23
|
|
|
|
24
|
|
|
def compute_tensor(self, x): |
25
|
|
|
return self.core.compute_tensor(x) |
26
|
|
|
|
27
|
|
|
class ClassOutputLayer(NeuralLayer): |
28
|
|
|
|
29
|
|
|
def __init__(self, output_size, class_size): |
30
|
|
|
super(ClassOutputLayer, self).__init__("class_output") |
31
|
|
|
self.output_size = output_size |
32
|
|
|
self.class_size = class_size |
33
|
|
|
|
34
|
|
|
def prepare(self): |
35
|
|
|
# Output layers |
36
|
|
|
self.output_layer = Chain(self.input_dim).stack(Dense(self.output_size * self.class_size)) |
37
|
|
|
self.softmax_layer = Softmax().init(input_dim=self.output_size) |
38
|
|
|
|
39
|
|
|
self.class_layer = Chain(self.input_dim).stack(Dense(self.class_size), |
40
|
|
|
Softmax3D()) |
41
|
|
|
self.register_inner_layers(self.class_layer, self.output_layer) |
42
|
|
|
# Target tensor |
43
|
|
|
self.target_tensor = T.imatrix('target') |
44
|
|
|
self.register_external_targets(self.target_tensor) |
45
|
|
|
# arange cache |
46
|
|
|
self.arange_cache = theano.shared(np.arange(10*64), name="arange_cache") |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def compute_tensor(self, x): |
50
|
|
|
""" |
51
|
|
|
:param x: (batch, time, vec) |
52
|
|
|
""" |
53
|
|
|
# Target class |
54
|
|
|
class_matrix = self.target_tensor // self.output_size |
55
|
|
|
class_vector = class_matrix.reshape((-1,)) |
56
|
|
|
# Target index |
57
|
|
|
target_matrix = self.target_tensor % self.output_size |
58
|
|
|
target_vector = target_matrix.reshape((-1,)) |
59
|
|
|
# Input matrix |
60
|
|
|
input_matrix = x.reshape((-1, self.input_dim)) |
61
|
|
|
# Output matrix |
62
|
|
|
output_tensor3d = self.output_layer.compute_tensor(x) |
63
|
|
|
output_matrix = output_tensor3d.reshape((-1, self.class_size, self.output_size)) |
64
|
|
|
arange_vec = self.arange_cache[:output_matrix.shape[0]] |
65
|
|
|
sub_output_matrix = output_matrix[arange_vec, class_vector] |
66
|
|
|
# Softmax |
67
|
|
|
softmax_output_matrix = self.softmax_layer.compute_tensor(sub_output_matrix) |
68
|
|
|
# Class prediction |
69
|
|
|
class_output_matrix = self.class_layer.compute_tensor(x) |
70
|
|
|
# Costs |
71
|
|
|
output_cost = LMCost(softmax_output_matrix, target_vector).get() |
72
|
|
|
class_cost = LMCost(class_output_matrix, class_matrix).get() |
73
|
|
|
final_cost = output_cost + class_cost |
74
|
|
|
|
75
|
|
|
return final_cost |
76
|
|
|
|
77
|
|
|
|