Skip to content

Commit 089fa11

Browse files
committed
TimeDistributedDense Speed up
Update core.py
1 parent 2c49115 commit 089fa11

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

keras/layers/core.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,25 @@ def output_shape(self):
11211121

11221122
def get_output(self, train=False):
11231123
X = self.get_input(train)
1124-
1124+
input_length = self.input_shape[1]
1125+
if input_length and K._BACKEND == 'theano':
1126+
import theano.tensor as T
1127+
#X: (nb_samples, timesteps, input_dim)
1128+
X = K.permute_dimensions(X, (1, 0, 2))
1129+
#X: (timesteps, nb_samples, input_dim)
1130+
W = [self.W] * input_length
1131+
W = T.stack(*W)
1132+
#W: (timesteps, input_dim, output_dim)
1133+
z = T.batched_tensordot(X, W, axes=[(2), (1)])
1134+
#z: (timesteps, nb_samples, output_dim)
1135+
z = K.permute_dimensions(z, (1, 0, 2))
1136+
#z: (nb_samples, timesteps, output_dim)
1137+
b = [self.b] * input_length
1138+
b = T.stack(*b)
1139+
#b: (timesteps, output_dim)
1140+
Y = self.activation(z + b)
1141+
return Y
1142+
11251143
def step(x, states):
11261144
output = K.dot(x, self.W) + self.b
11271145
return output, []

0 commit comments

Comments
 (0)