Commit 7792cdab authored by Emmanuel PIGNAT's avatar Emmanuel PIGNAT
Browse files

changing xi_dim, u_dim

parents b3d37495 4bad9ab5
......@@ -20,7 +20,7 @@ This requires v2.7 of Python.
Following these instructions install the library without copying the files, allowing to edit the sources files without reinstalling.
git clone git@gitlab.idiap.ch:epignat/pbdlib-python.git
git clone https://gitlab.idiap.ch/rli/pbdlib-python.git
cd pbdlib-python
pip install -e .
......@@ -44,6 +44,12 @@ Then navigate through folders and click on desired notebook.
## User interface for recording data with the mouse
### Installation
sudo apt-get intall python-tk
### Use
cd notebooks
python record_demo.py -p /path/to/folder -f filename
......@@ -55,6 +61,3 @@ To record demos with additional moving objects, run
By pressing, the number corresponding to the desired object on your keyboard, you will make it appear and be able to move it.
Rotate them by holding the key of the number and turning the scrollwheel of your mouse.
%% Cell type:code id: tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
import pbdlib as pbd
import os
%matplotlib inline
%load_ext autoreload
%autoreload 2
```
%% Output
The autoreload extension is already loaded. To reload it, use:
%reload_ext autoreload
%% Cell type:markdown id: tags:
### Read data from file
%% Cell type:code id: tags:
``` python
filename = os.path.dirname(pbd.__file__) + '/data/gui/' + 'test_conditioning_002.npy'
data = np.load(filename)[()]
x = np.concatenate(data['x'], axis=0)[::3]
```
%% Cell type:code id: tags:
``` python
plt.plot(x[:, 0], x[:, 1], 'kx')
plt.axes().set_aspect('equal')
```
%% Output
%% Cell type:markdown id: tags:
### Learning a joint distribution
%% Cell type:code id: tags:
``` python
gmm = pbd.GMM(nb_dim=2, nb_states=6)
gmm.em(x, reg=0.1);
# based on sklearn.mixture.BayesianGaussianMixture
bgmm = pbd.VBayesianGMM({'n_components':15, 'n_init':5, 'reg_covar': 0.1 ** 2,
'covariance_prior': 10. ** 2 * np.eye(2),'mean_precision_prior':1e-9})
bgmm.posterior(x);
```
%% Cell type:code id: tags:
``` python
plt.plot(x[:, 0], x[:, 1], 'kx')
gmm.plot(color='steelblue')
bgmm.plot(color='gold')
plt.axes().set_aspect('equal')
```
%% Output
%% Cell type:code id: tags:
``` python
x_in = np.linspace(-200, 200, 300)[:, None]
mu, sigma = gmm.condition(x_in, slice(0, 1), slice(1, 2))
bmu, bsigma = bgmm.condition(x_in, slice(0, 1), slice(1, 2))
```
%% Cell type:code id: tags:
``` python
plt.figure(figsize=(20, 5))
gen.plot(color='orangered', alpha=0.2)
gmm.plot(color='steelblue')
plt.plot(x_in, mu, label='predictive distribution - frequentist', color='steelblue')
plt.fill_between(x_in[:, 0],
mu[:, 0] - sigma[:, 0, 0]**0.5,
mu[:, 0] + sigma[:, 0, 0]**0.5,
alpha=0.3, color='steelblue')
plt.fill_between(x_in[:, 0],
bmu[:, 0] - bsigma[:, 0, 0]**0.5,
bmu[:, 0] + bsigma[:, 0, 0]**0.5,
alpha=0.3, color='gold')
plt.plot(x_in, bmu, label='posterior predictive distribution', color='gold')
plt.plot(x[:, 0], x[:, 1], 'kx')
plt.legend()
```
%% Output
<matplotlib.legend.Legend at 0x7fdb85579410>
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -8,10 +8,8 @@ from .model import Model
from .mvn import *
from .plot import *
from .pylqr import *
from .poglqr import PoGLQR, SparsePoGLQR
from .poglqr import PoGLQR
from .poglqr import PoGLQR, LQR
from .mtmm import MTMM, VBayesianGMM, VMBayesianGMM
from .lqr import LQR
try:
import gui
......
......@@ -2,9 +2,9 @@ import numpy as np
from model import *
from functions import multi_variate_normal
from scipy.linalg import block_diag
import scipy.sparse as ss
from termcolor import colored
from mvn import MVN, SparseMVN
from mvn import MVN
class GMM(Model):
......@@ -136,7 +136,7 @@ class GMM(Model):
return gmm
def concatenate_gaussian(self, q, get_mvn=True, sparse=False):
def concatenate_gaussian(self, q, get_mvn=True, reg=None):
"""
Get a concatenated-block-diagonal replication of the GMM with sequence of state
given by q.
......@@ -147,21 +147,29 @@ class GMM(Model):
:return:
"""
if not get_mvn:
return np.concatenate([self.mu[i] for i in q]), block_diag(*[self.sigma[i] for i in q])
else:
if not sparse:
if reg is None:
if not get_mvn:
return np.concatenate([self.mu[i] for i in q]), block_diag(*[self.sigma[i] for i in q])
else:
mvn = MVN()
mvn.mu = np.concatenate([self.mu[i] for i in q])
mvn._sigma = block_diag(*[self.sigma[i] for i in q])
mvn._lmbda = block_diag(*[self.lmbda[i] for i in q])
else:
mvn = SparseMVN()
return mvn
else:
if not get_mvn:
return np.concatenate([self.mu[i] for i in q]), block_diag(
*[self.sigma[i] + reg for i in q])
else:
mvn = MVN()
mvn.mu = np.concatenate([self.mu[i] for i in q])
mvn._sigma = ss.block_diag([self.sigma[i] for i in q])
mvn._lmbda = ss.block_diag([self.lmbda[i] for i in q])
return mvn
mvn._sigma = block_diag(*[self.sigma[i] + reg for i in q])
mvn._lmbda = block_diag(*[np.linalg.inv(self.sigma[i] + reg) for i in q])
return mvn
def compute_resp(self, demo=None, dep=None, table=None, marginal=None, norm=True):
sample_size = demo.shape[0]
......
......@@ -4,7 +4,14 @@ import os
import matplotlib.pyplot as plt
from pbdlib.gui import Interactive
from termcolor import colored
import tkinter as tk
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
except:
raise
from tkFileDialog import asksaveasfilename
from matplotlib import gridspec
import pbdlib as pbd
......
import numpy as np
from utils.utils import lifted_transfer_matrix
import pbdlib as pbd
import scipy.sparse as ss
class LQR(object):
def __init__(self, A=None, B=None, nb_dim=2, dt=0.01, horizon=50):
......@@ -133,9 +132,9 @@ class LQR(object):
return self._gmm_u.lmbda
elif isinstance(self._gmm_u, tuple):
gmm, seq = self._gmm_u
return gmm.lmbda[seq[t]]
return gmm.lmbda[seq[t]], gmm.mu[seq[t]]
elif isinstance(self._gmm_u, pbd.GMM):
return self._gmm_u.lmbda[t]
return self._gmm_u.lmbda[t], self._gmm_u.mu[t]
else:
raise ValueError, "Not supported gmm_u"
......@@ -177,14 +176,6 @@ class LQR(object):
self._K = _K
self._Kv = _Kv
def get_target(self):
ds = []
for t in range(0, self.horizon-1):
ds += [np.linalg.inv(self._S[t].dot(self.A)).dot(self._v[t])]
return np.array(ds)
def get_seq(self, xi0, return_target=False):
xis = [xi0]
us = [-self._K[0].dot(xi0) + self._Kv[0].dot(self._v[0])]
......@@ -221,7 +212,7 @@ class PoGLQR(LQR):
self.B = B
self.nb_dim = nb_dim
self.dt = dt
self._s_xi, self._s_u = None, None
self._x0 = None
......@@ -271,7 +262,6 @@ class PoGLQR(LQR):
else:
return self.nb_dim * self.horizon * 2
@property
def mvn_sol_u(self):
"""
......@@ -291,14 +281,14 @@ class PoGLQR(LQR):
@property
def seq_xi(self):
if self._seq_xi is None:
self._seq_xi = self.mvn_sol_xi.mu.reshape(self.horizon, self.xi_dim)
self._seq_xi = self.mvn_sol_xi.mu.reshape(self.horizon, self.nb_dim * 2)
return self._seq_xi
@property
def seq_u(self):
if self._seq_u is None:
self._seq_u = self.mvn_sol_u.mu.reshape(self.horizon, self.u_dim)
self._seq_u = self.mvn_sol_u.mu.reshape(self.horizon, self.nb_dim)
return self._seq_u
......@@ -395,42 +385,13 @@ class PoGLQR(LQR):
self._mvn_sol_xi, self._mvn_sol_u = None, None
self._seq_xi, self._seq_u = None, None
class SparsePoGLQR(PoGLQR):
@property
def mvn_u(self):
"""
Distribution of control input
:return:
"""
return self._mvn_u
@mvn_u.setter
def mvn_u(self, value):
"""
:param value [float] or [pbd.MVN]
"""
# resetting solution
self._mvn_sol_xi = None
self._mvn_sol_u = None
self._seq_u = None
self._seq_xi = None
def horizon(self):
return self._horizon
if isinstance(value, pbd.MVN):
self._mvn_u = value
else:
self._mvn_u = pbd.SparseMVN(
mu=np.zeros(self.u_dim), lmbda=10 ** value * ss.eye(self.u_dim))
@horizon.setter
def horizon(self, value):
self.reset_params()
@property
def s_u(self):
if self._s_u is None:
self._s_xi, self._s_u = lifted_transfer_matrix(self.A, self.B,
horizon=self.horizon, dt=self.dt, nb_dim=self.nb_dim, sparse=True)
return self._s_u
@property
def s_xi(self):
if self._s_xi is None:
self._s_xi, self._s_u = lifted_transfer_matrix(self.A, self.B,
horizon=self.horizon, dt=self.dt, nb_dim=self.nb_dim, sparse=True)
return self._s_xi
\ No newline at end of file
self._horizon = value
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment