/
lds.py
182 lines (155 loc) · 6.56 KB
/
lds.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# coding=utf-8
# Copyright 2020 The Google Research Authors.
#
# 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.
"""Class and utils for linear dynamical systems."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import collections
import logging
import warnings
import numpy as np
from pylds.models import DefaultLDS
from scipy.stats import ortho_group
class LinearDynamicalSystem(object):
"""Class to represent a linear dynamical system."""
def __init__(self, transition_matrix, input_matrix, output_matrix):
"""Initializes a linear dynamical system object.
Args:
transition_matrix: The transition matrix of shape [hidden_state_dim,
hidden_state_dim].
input_matrix: The input matrix of shape [hidden_state_dim, input_dim].
output_matrix: The measurement matrix of shape [output_dim,
hidden_state_dim].
"""
self.hidden_state_dim = transition_matrix.shape[0]
self.input_dim = input_matrix.shape[1]
self.output_dim = output_matrix.shape[0]
if transition_matrix.shape != (self.hidden_state_dim,
self.hidden_state_dim):
raise ValueError('Dimension mismatch.')
if input_matrix.shape != (self.hidden_state_dim, self.input_dim):
raise ValueError('Dimension mismatch.')
if output_matrix.shape != (self.output_dim, self.hidden_state_dim):
raise ValueError('Dimension mismatch.')
self.transition_matrix = transition_matrix
self.input_matrix = input_matrix
self.output_matrix = output_matrix
def get_spectrum(self):
eigs = np.linalg.eig(self.transition_matrix)[0]
return eigs[np.argsort(eigs.real)[::-1]]
def get_expected_arparams(self):
return -np.poly(self.get_spectrum())[1:]
class LinearDynamicalSystemSequence(object):
"""Wrapper around input seq, hidden state seq, and output seq from LDS."""
def __init__(self, input_seq, hidden_state_seq, output_seq):
self.seq_len = np.shape(input_seq)[0]
if self.seq_len != np.shape(hidden_state_seq)[0]:
raise ValueError('Sequence length mismatch.')
if self.seq_len != np.shape(output_seq)[0]:
raise ValueError('Sequence length mismatch.')
self.inputs = input_seq
self.hidden_states = hidden_state_seq
self.outputs = output_seq
self.input_dim = np.shape(input_seq)[1]
self.output_dim = np.shape(output_seq)[1]
class SequenceGenerator(object):
"""Class for generating sequences according to linear dynamical systems."""
def __init__(self, output_noise_stddev):
"""Initializes SequenceGenerator.
Args:
output_noise_stddev: The stddev of the output noise distribution.
"""
self.output_noise_stddev = output_noise_stddev
def _random_normal(self, mean, stddev, dim):
return mean + stddev * np.random.randn(np.prod(dim)).reshape(dim)
def generate_seq(self, system, seq_len):
"""Generate seq with random initial state, inputs, and output noise.
Args:
system: A LinearDynamicalSystem instance.
seq_len: The desired length of the sequence.
Returns:
A LinearDynamicalSystemSequence object with:
- outputs: A numpy array of shape [seq_len, output_dim].
- hidden_states: A numpy array of shape [seq_len, hidden_state_dim].
- inputs: A numpy array of shape [seq_len, input_dim].
"""
inputs = self._random_normal(0., 1., [seq_len, system.input_dim])
outputs = np.zeros([seq_len, system.output_dim])
output_noises = self._random_normal(0., self.output_noise_stddev,
[seq_len, system.output_dim])
hidden_states = np.zeros([seq_len, system.hidden_state_dim])
# Initial state.
hidden_states[0, :] = self._random_normal(0., 1., system.hidden_state_dim)
for j in range(1, seq_len):
hidden_states[j, :] = (
np.matmul(system.transition_matrix, hidden_states[j - 1, :]) +
np.matmul(system.input_matrix, inputs[j, :]))
for j in range(seq_len):
outputs[j, :] = np.matmul(system.output_matrix,
hidden_states[j, :]) + output_noises[j, :]
return LinearDynamicalSystemSequence(inputs, hidden_states, outputs)
def generate_linear_dynamical_system(hidden_state_dim, input_dim=1,
output_dim=1):
"""Generates a LinearDynamicalSystem with given dimensions.
Args:
hidden_state_dim: Desired hidden state dim.
input_dim: The input dim.
output_dim: Desired output dim.
Returns:
A LinearDynamicalSystem object with
- A random stable symmetric transition matrx.
- Identity input matrix.
- A random output matrix.
"""
spectral_radius = np.inf
while spectral_radius > 1.0:
transition_matrix = np.random.rand(hidden_state_dim, hidden_state_dim)
spectral_radius = np.max(np.abs(np.linalg.eig(transition_matrix)[0]))
input_matrix = np.random.rand(hidden_state_dim, input_dim)
output_matrix = np.random.rand(output_dim, hidden_state_dim)
return LinearDynamicalSystem(transition_matrix, input_matrix, output_matrix)
def eig_dist(system1, system2):
"""Computes the eigenvalue distance between two LDS's.
Args:
system1: A LinearDynamicalSystem object.
system2: A LinearDynamicalSystem object.
Returns:
Frobenious norm between ordered eigenvalues.
"""
return np.linalg.norm(system1.get_spectrum() - system2.get_spectrum())
def fit_lds_pylds(seq, inputs, guessed_dim):
"""Fits LDS model via Gibbs sampling and EM. Returns fitted eigenvalues.
Args:
seq: A list of LinearDynamicalSystemSequence objects.
inputs: A numpy array.
guessed_dim: The hidden state dimension to fit.
Returns:
Eigenvalues in sorted order.
"""
if inputs is None:
model = DefaultLDS(D_obs=1, D_latent=guessed_dim, D_input=0)
else:
model = DefaultLDS(D_obs=1, D_latent=guessed_dim, D_input=1)
model.add_data(seq, inputs=inputs)
# Initialize with a few iterations of Gibbs.
for _ in range(10):
model.resample_model()
# Run EM
def update(model):
model.EM_step()
return model.log_likelihood()
ll = [update(model) for _ in range(100)]
eigs = np.linalg.eigvals(model.A)
return eigs[np.argsort(eigs.real)[::-1]]