def show_labels(args):
    """Show the labels on a graph"""
    pld.setGlobalFontSize(16)
    fig, ax = plt.subplots(2, 2, figsize=(8, 6))
    keys = ['MoE Cost', 'MoE Gate', 'k-Means-3', 'k-Means-5']
    titles = ['MoE I', 'MoE II', '$k$-Means-3', '$k$-Means-5']
    moelbl = np.load('data/pen/moe_label.npy')
    momlbl = np.load('data/pen/mom_label.npy')
    gatelbl = np.load('data/pen/gate_expert_label.npy')
    kmeanlbl = np.load('data/pen/pca_kmean_label.npz')
    k3lbl = kmeanlbl['3']
    k5lbl = kmeanlbl['5']

    data = npload(cfg['file_path'], cfg['uniqueid'])
    x0 = data[cfg['x_name']]

    markers = ['s', 'o', 'x', 'd', '*']
    colors = ['b', 'g', 'r', 'c', 'k']
    cm = plt.get_cmap('jet')
    norm = mpl.colors.Normalize(0, 5)

    def show_label_on_axis(ax, x, lbl):
        nlbl = np.amax(lbl) + 1
        ax.imshow(np.reshape(lbl, (61, 21)).T,
                  cmap=cm,
                  origin='lower',
                  norm=norm,
                  extent=[0, 2 * np.pi, -2.0, 2.0])
        # for i in range(nlbl):
        #     mask = lbl == i
        #     ax.scatter(x[mask, 0], x[mask, 1], s=3, marker=markers[i], color=colors[i])

    show_label_on_axis(ax[0][0], x0, moelbl)
    ax[0][0].set_title(titles[0])
    show_label_on_axis(ax[0][1], x0, gatelbl)
    ax[0][1].set_title(titles[1])
    show_label_on_axis(ax[1][0], x0, k3lbl)
    ax[1][0].set_title(titles[2])
    show_label_on_axis(ax[1][1], x0, k5lbl)
    ax[1][1].set_title(titles[3])
    ax[1][0].set_xlabel(r'$\theta$')
    ax[1][1].set_xlabel(r'$\theta$')
    ax[0][0].set_xticklabels([])
    ax[0][1].set_xticklabels([])
    ax[0][0].set_ylabel(r'$\omega$')
    ax[1][0].set_ylabel(r'$\omega$')
    ax[0][1].set_yticklabels([])
    ax[1][1].set_yticklabels([])
    fig.tight_layout()
    fig.savefig('gallery/pen/pen_label_assign.pdf')
    plt.show()
def showThetaf(data):
    """Show thetaf of each model prediction"""
    pld.setGlobalFontSize(16)
    # load validation set
    valid_data = np.load(cfg['valid_path'])
    x0 = valid_data[cfg['x_name']]
    Sol = valid_data[cfg['y_name']]

    usetitle = True
    cm = plt.get_cmap('jet')
    xi = np.linspace(0, 2 * np.pi, 100)
    yi = np.linspace(-2.0, 2.0, 50)

    # calculate those angle error
    vthetaf = []
    keys = [
        'SNN', 'MoE Cost', 'MoE Gate', 'k-Means-3', 'k-Means-5', 'k-Means-10'
    ]
    titles = [
        'SNN', 'MoE I', 'MoE II', '$k$-Means-3', '$k$-Means-5', '$k$-Means-10'
    ]
    for key in keys:
        vthetaf.append(data[key][:, 48] - Sol[:, 48])

    # get figures for this
    nmdl = len(vthetaf)
    num = int(np.ceil(np.sqrt(nmdl)))
    nrow = nmdl // num
    if nmdl % num != 0:
        nrow += 1
    fig, axes = plt.subplots(nrow, num, figsize=(7, 3.5))
    # get val
    minval = np.amin([np.amin(thetaf) for thetaf in vthetaf])
    maxval = np.amax([np.amax(thetaf) for thetaf in vthetaf])
    norm = mpl.colors.Normalize(-1, 7)
    levels = np.linspace(minval, maxval, 10)
    for i in range(nmdl):
        row = i // num
        col = i % num
        if nrow > 1:
            ax = axes[row][col]
        else:
            ax = axes[col]
        zi = griddata((x0[:, 0], x0[:, 1]),
                      np.abs(vthetaf[i]), (xi[None, :], yi[:, None]),
                      method='nearest')
        cs = ax.imshow(np.reshape(zi, (50, 100)),
                       cmap=cm,
                       origin='lower',
                       norm=norm,
                       extent=[0, 2 * np.pi, -2.0, 2.0])
        # cs = ax.tricontourf(x0[:, 0], x0[:, 1], np.abs(vthetaf[i]), cmap=cm, origin='lower', norm=norm)
        if row == nrow - 1:
            ax.set_xlabel(r'$\theta$')
        else:
            ax.set_xticklabels([])
        if col == 0:
            ax.set_ylabel(r'$\omega$')
        else:
            ax.set_yticklabels([])
        if usetitle:
            ax.set_title(titles[i])
    divider = make_axes_locatable(ax)
    fig.tight_layout()
    fig.colorbar(cs, ax=axes.ravel().tolist())
    fig.savefig('gallery/pen/penPredictionThetaf.pdf')
    plt.show()
For vehicle benchmark problem, show examples that SNN fails badly.
"""
from __future__ import print_function, division
import sys, os, time
import numpy as np
import matplotlib.pyplot as plt

from pyLib.io import getArgs, npload
from pyLib.train import modelLoader, MoMNet
from pyLib.math import Query
import pyLib.plot as pld

import util


pld.setGlobalFontSize(16)


def parseX(sol, *args):
    """Parse a sol into plotable piece"""
    tX = np.reshape(sol[:25*4], (25, 4))
    tU = np.reshape(sol[100: 100 + 2*24], (24, 2))
    tf = sol[-1]
    return tX, tU, tf


def main():
    args = util.get_args('show')
    cfg, lbl_name = util.get_label_cfg_by_args(args)
    show_picky_states(cfg, lbl_name, args)
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
from collections import OrderedDict
from scipy.interpolate import griddata
import cPickle as pkl

from pyLib.io import getArgs, npload
import pyLib.plot as pld
from pyLib.math import l1loss

import datanames

pld.setGlobalFontSize(14)
cfg = datanames.PENDULUM
KEYS = [
    'SNN', 'MoE Cost', 'MoE', 'MoE Gate', 'k-Means-3', 'k-Means-5',
    'k-Means-10'
]


def main():
    # theta draws final prediction of theta
    # label draws final labels from each model
    # error shows error on validation set
    # success shows success on rollout
    args = getArgs('theta', 'hist', 'label', 'error', 'success')
    if args.theta:
        data = load_pred_y()
示例#5
0
"""
show_dataset.py

To write the paper, I need figures and numbers of the dataset.
"""
from __future__ import print_function, division
import sys, os, time
import numpy as np
import matplotlib.pyplot as plt

from pyLib.io import getArgs
import pyLib.plot as pld
from pyLib.math import getPCA
import util

pld.setGlobalFontSize(mode='half')


def main():
    # num shows dataset size
    # drawpca draws pca results for datasets
    # umap draws scatter plot of umap projection
    # penscatter draws scatter plot on pendulum dataset, we draw scatter plot of x0
    args = util.get_args('num', 'drawpca', 'umap', 'penscatter')
    cfg, lbl = util.get_label_cfg_by_args(args)
    if args.num:
        show_dataset_size(cfg)
    if args.drawpca:
        drawPCA(cfg)
    if args.umap:
        drawUMap(cfg)