mirror of
https://github.com/wassname/pyrobolearn.git
synced 2026-09-09 11:31:38 +08:00
add 2 examples in models
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python
|
||||
"""Provide some examples using GMMs.
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.mixture import GaussianMixture
|
||||
|
||||
from pyrobolearn.models.gmm import Gaussian, GMM, plot_gmm, plot_gmm_sklearn
|
||||
|
||||
|
||||
# create manually a GMM
|
||||
dim, num_components = 2, 5
|
||||
gmm = GMM(gaussians=[Gaussian(mean=np.random.uniform(-1., 1., size=dim),
|
||||
covariance=0.1*np.identity(dim)) for _ in range(num_components)])
|
||||
gmm_sklearn = GaussianMixture(n_components=num_components)
|
||||
|
||||
|
||||
# plot initial GMM
|
||||
plot_gmm(gmm, title='Initial GMM')
|
||||
plt.show()
|
||||
|
||||
|
||||
# create data: Generate random sample following a sine curve
|
||||
# Ref: https://scikit-learn.org/stable/auto_examples/mixture/plot_gmm_sin.html#sphx-glr-auto-examples-mixture-\
|
||||
# plot-gmm-sin-py
|
||||
n_samples = 100
|
||||
np.random.seed(0)
|
||||
X = np.zeros((n_samples, 2))
|
||||
step = 4. * np.pi / n_samples
|
||||
|
||||
for i in range(X.shape[0]):
|
||||
x = i * step - 6.
|
||||
X[i, 0] = x + np.random.normal(0, 0.1)
|
||||
X[i, 1] = 3. * (np.sin(x) + np.random.normal(0, .2))
|
||||
|
||||
xlim, ylim = [-8, 8], [-8, 8]
|
||||
|
||||
# plot data
|
||||
plt.title('Training data')
|
||||
plt.scatter(X[:, 0], X[:, 1])
|
||||
plt.show()
|
||||
|
||||
|
||||
# init GMM
|
||||
init_method = 'k-means' # 'random', 'k-means', 'uniform', 'sklearn', 'curvature'
|
||||
gmm.init(X, method=init_method)
|
||||
fig, ax = plt.subplots(1, 1)
|
||||
plot_gmm(gmm, X=X, ax=ax, title='GMM after ' + init_method.capitalize(), xlim=xlim, ylim=ylim)
|
||||
plt.show()
|
||||
|
||||
|
||||
# fit a GMM using EM
|
||||
result = gmm.fit(X, init=None)
|
||||
gmm_sklearn.fit(X)
|
||||
|
||||
# plot EM optimization
|
||||
plt.plot(result['losses'])
|
||||
plt.title('EM per iteration')
|
||||
plt.show()
|
||||
|
||||
# plot trained GMM
|
||||
fig, ax = plt.subplots(1, 2)
|
||||
plot_gmm(gmm, X=X, label=True, ax=ax[0], title='Our Trained GMM', option=1, xlim=xlim, ylim=ylim)
|
||||
plot_gmm_sklearn(gmm_sklearn, X, label=True, ax=ax[1], title="Sklearn's Trained GMM", xlim=xlim, ylim=ylim)
|
||||
plt.show()
|
||||
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python
|
||||
"""Provide some examples using ProMPs.
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from pyrobolearn.models.promp.promp import DiscreteProMP, plot_state, plot_proba_state, plot_weighted_basis
|
||||
|
||||
|
||||
# create data and plot it
|
||||
N = 8
|
||||
t = np.linspace(0., 1., 100)
|
||||
# eps = 0.1
|
||||
# y = np.array([np.sin(2*np.pi*t) + eps * np.random.rand(len(t)) for _ in range(N)]) # shape: NxT
|
||||
# dy = np.array([2*np.pi*np.cos(2*np.pi*t) + eps * np.random.rand(len(t)) for _ in range(N)]) # shape: NxT
|
||||
phi = np.random.uniform(low=-1., high=1., size=N)
|
||||
y = np.array([np.sin(2 * np.pi * t + phi[i]) for i in range(int(N/2))]) # shape: NxT
|
||||
y1 = np.array([np.cos(2 * np.pi * t + phi[i]) for i in range(int(N/2))])
|
||||
y = np.vstack((y, y1))
|
||||
dy = np.array([2 * np.pi * np.cos(2 * np.pi * t + phi[i]) for i in range(int(N/2))]) # shape: NxT
|
||||
dy1 = np.array([2 * np.pi * np.sin(2 * np.pi * t + phi[i]) for i in range(int(N/2))])
|
||||
dy = np.vstack((dy, dy1))
|
||||
Y = np.dstack((y, dy)) # N,T,2D --> why not N,2D,T
|
||||
plot_state(Y, title='Training data')
|
||||
plt.show()
|
||||
|
||||
# create discrete and rhythmic ProMP
|
||||
promp = DiscreteProMP(num_dofs=1, num_basis=10, basis_width=1./20)
|
||||
|
||||
# plot the basis function activations
|
||||
plt.plot(promp.Phi(t)[:, :, 0].T)
|
||||
plt.title('basis functions')
|
||||
plt.show()
|
||||
|
||||
# plot ProMPs
|
||||
y_pred = promp.rollout()
|
||||
fig, ax = plt.subplots(1, 2)
|
||||
plot_state(y_pred[None], ax=ax, title='ProMP prediction before learning', linewidth=2.) # shape: N,T,2D
|
||||
plot_weighted_basis(t, promp, ax=ax)
|
||||
plt.show()
|
||||
|
||||
# learn from demonstrations
|
||||
promp.imitate(Y)
|
||||
y_pred = promp.rollout()
|
||||
fig, ax = plt.subplots(1, 2)
|
||||
plot_state(y_pred[None], ax=ax, title='ProMP prediction after learning', linewidth=3.) # N,T,2D
|
||||
plot_weighted_basis(t, promp, ax=ax)
|
||||
plt.show()
|
||||
|
||||
method = 'marginal'
|
||||
means, covariances = promp.rollout_proba(method=method, return_gaussian=False)
|
||||
fig, ax = plt.subplots(1, 2)
|
||||
# plot_state(Y, ax=ax, title='Training data')
|
||||
plot_proba_state(means, covariances, ax=ax, title='ProMP prediction after learning', linewidth=3.)
|
||||
plt.show()
|
||||
Reference in New Issue
Block a user