This repository was archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathrnn.py
More file actions
70 lines (58 loc) · 2.37 KB
/
Copy pathrnn.py
File metadata and controls
70 lines (58 loc) · 2.37 KB
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
# Copyright 2018 Google, Inc.,
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""We can view an RNN as a VAE with no latent variables:
Notation:
- d_1:T are the (deterministic) RNN outputs.
- x_1:T are the observed states.
- c_1:T are per-timestep inputs.
Generative model
=====================
x_1 x_t
^ ^
| |
d_1 ------------> d_t
^ ^
| |
c_1 c_t
"""
import tensorflow as tf
from tensorflow.contrib import distributions
from .. import util
from .. import vae_module
class RNN(vae_module.VAECore):
"""Implementation of an RNN as a sequential VAE where all latent
variables are deterministic."""
def __init__(self, hparams, obs_encoder, obs_decoder, name=None):
super(RNN, self).__init__(hparams, obs_encoder, obs_decoder, name)
with self._enter_variable_scope():
self._d_core = util.make_rnn(hparams, name="d_core")
@property
def state_size(self):
return self._d_core.state_size
def _next_state(self, d_state, event=None):
del event # Not used.
return d_state
def _initial_state(self, batch_size):
return self._d_core.initial_state(batch_size)
def _build(self, input_, d_state):
d_out, d_state = self._d_core(util.concat_features(input_), d_state)
return self._obs_decoder(d_out), d_state
def _infer_latents(self, inputs, observed):
"""Because the RNN latent state is fully deterministic, there's no
need to do two passes over the training data."""
del inputs # Not used.
batch_size = util.batch_size_from_nested_tensors(observed)
sequence_size = util.sequence_size_from_nested_tensors(observed)
divs = tf.zeros([batch_size, sequence_size], name="divergences")
return None, divs