252 lines
11 KiB
Plaintext
252 lines
11 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "filled-multiple",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"The root path: /Users/xuanyidong\n",
|
|
"The library path: /Users/xuanyidong/lib\n"
|
|
]
|
|
},
|
|
{
|
|
"ename": "AssertionError",
|
|
"evalue": "/Users/xuanyidong/lib does not exist",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
|
|
"\u001b[0;32m~/Desktop/AutoDL-Projects\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"The root path: {:}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mroot_dir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"The library path: {:}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlib_dir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mlib_dir\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexists\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"{:} does not exist\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlib_dir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlib_dir\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minsert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlib_dir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
"\u001b[0;31mAssertionError\u001b[0m: /Users/xuanyidong/lib does not exist"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import os, sys\n",
|
|
"import torch\n",
|
|
"from pathlib import Path\n",
|
|
"import numpy as np\n",
|
|
"import matplotlib\n",
|
|
"from matplotlib import cm\n",
|
|
"# matplotlib.use(\"agg\")\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"import matplotlib.ticker as ticker\n",
|
|
"\n",
|
|
"\n",
|
|
"__file__ = os.path.dirname(os.path.realpath(\"__file__\"))\n",
|
|
"root_dir = (Path(__file__).parent / \"..\").resolve()\n",
|
|
"lib_dir = (root_dir / \"lib\").resolve()\n",
|
|
"print(\"The root path: {:}\".format(root_dir))\n",
|
|
"print(\"The library path: {:}\".format(lib_dir))\n",
|
|
"assert lib_dir.exists(), \"{:} does not exist\".format(lib_dir)\n",
|
|
"if str(lib_dir) not in sys.path:\n",
|
|
" sys.path.insert(0, str(lib_dir))\n",
|
|
"\n",
|
|
"from datasets import ConstantGenerator, SinGenerator, SyntheticDEnv\n",
|
|
"from datasets import DynamicQuadraticFunc\n",
|
|
"from datasets.synthetic_example import create_example_v1"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "detected-second",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def visualize_env():\n",
|
|
" \n",
|
|
" dpi, width, height = 10, 800, 400\n",
|
|
" figsize = width / float(dpi), height / float(dpi)\n",
|
|
" LabelSize, LegendFontsize, font_gap = 40, 40, 5\n",
|
|
"\n",
|
|
" fig = plt.figure(figsize=figsize)\n",
|
|
"\n",
|
|
" dynamic_env, function = create_example_v1(100, num_per_task=250)\n",
|
|
" \n",
|
|
" timeaxis, xaxis, yaxis = [], [], []\n",
|
|
" for timestamp, dataset in dynamic_env:\n",
|
|
" num = dataset.shape[0]\n",
|
|
" timeaxis.append(torch.zeros(num) + timestamp)\n",
|
|
" xaxis.append(dataset[:,0])\n",
|
|
" # compute the ground truth\n",
|
|
" function.set_timestamp(timestamp)\n",
|
|
" yaxis.append(function(dataset[:,0]))\n",
|
|
" timeaxis = torch.cat(timeaxis).numpy()\n",
|
|
" xaxis = torch.cat(xaxis).numpy()\n",
|
|
" yaxis = torch.cat(yaxis).numpy()\n",
|
|
"\n",
|
|
" cur_ax = fig.add_subplot(2, 1, 1)\n",
|
|
" cur_ax.scatter(timeaxis, xaxis, color=\"k\", linestyle=\"-\", alpha=0.9, label=None)\n",
|
|
" cur_ax.set_xlabel(\"Time\", fontsize=LabelSize)\n",
|
|
" cur_ax.set_ylabel(\"X\", rotation=0, fontsize=LabelSize)\n",
|
|
" for tick in cur_ax.xaxis.get_major_ticks():\n",
|
|
" tick.label.set_fontsize(LabelSize - font_gap)\n",
|
|
" tick.label.set_rotation(10)\n",
|
|
" for tick in cur_ax.yaxis.get_major_ticks():\n",
|
|
" tick.label.set_fontsize(LabelSize - font_gap)\n",
|
|
" \n",
|
|
" cur_ax = fig.add_subplot(2, 1, 2)\n",
|
|
" cur_ax.scatter(timeaxis, yaxis, color=\"k\", linestyle=\"-\", alpha=0.9, label=None)\n",
|
|
" cur_ax.set_xlabel(\"Time\", fontsize=LabelSize)\n",
|
|
" cur_ax.set_ylabel(\"Y\", rotation=0, fontsize=LabelSize)\n",
|
|
" for tick in cur_ax.xaxis.get_major_ticks():\n",
|
|
" tick.label.set_fontsize(LabelSize - font_gap)\n",
|
|
" tick.label.set_rotation(10)\n",
|
|
" for tick in cur_ax.yaxis.get_major_ticks():\n",
|
|
" tick.label.set_fontsize(LabelSize - font_gap)\n",
|
|
" plt.show()\n",
|
|
"\n",
|
|
"visualize_env()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "supreme-basis",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# def optimize_fn(xs, ys, test_sets):\n",
|
|
"# xs = torch.FloatTensor(xs).view(-1, 1)\n",
|
|
"# ys = torch.FloatTensor(ys).view(-1, 1)\n",
|
|
" \n",
|
|
"# model = SuperSequential(\n",
|
|
"# SuperMLPv1(1, 10, 20, torch.nn.ReLU),\n",
|
|
"# SuperMLPv1(20, 10, 1, torch.nn.ReLU)\n",
|
|
"# )\n",
|
|
"# optimizer = torch.optim.Adam(\n",
|
|
"# model.parameters(),\n",
|
|
"# lr=0.01, weight_decay=1e-4, amsgrad=True\n",
|
|
"# )\n",
|
|
"# for _iter in range(100):\n",
|
|
"# preds = model(ys)\n",
|
|
"\n",
|
|
"# optimizer.zero_grad()\n",
|
|
"# loss = torch.nn.functional.mse_loss(preds, ys)\n",
|
|
"# loss.backward()\n",
|
|
"# optimizer.step()\n",
|
|
" \n",
|
|
"# with torch.no_grad():\n",
|
|
"# answers = []\n",
|
|
"# for test_set in test_sets:\n",
|
|
"# test_set = torch.FloatTensor(test_set).view(-1, 1)\n",
|
|
"# preds = model(test_set).view(-1).numpy()\n",
|
|
"# answers.append(preds.tolist())\n",
|
|
"# return answers\n",
|
|
"\n",
|
|
"# def f(x):\n",
|
|
"# return np.cos( 0.5 * x + x * x)\n",
|
|
"\n",
|
|
"# def get_data(mode):\n",
|
|
"# dataset = SynAdaptiveEnv(mode=mode)\n",
|
|
"# times, xs, ys = [], [], []\n",
|
|
"# for i, (_, t, x) in enumerate(dataset):\n",
|
|
"# times.append(t)\n",
|
|
"# xs.append(x)\n",
|
|
"# dataset.set_transform(f)\n",
|
|
"# for i, (_, _, y) in enumerate(dataset):\n",
|
|
"# ys.append(y)\n",
|
|
"# return times, xs, ys\n",
|
|
"\n",
|
|
"# def visualize_syn(save_path):\n",
|
|
"# save_dir = (save_path / '..').resolve()\n",
|
|
"# save_dir.mkdir(parents=True, exist_ok=True)\n",
|
|
" \n",
|
|
"# dpi, width, height = 40, 2000, 900\n",
|
|
"# figsize = width / float(dpi), height / float(dpi)\n",
|
|
"# LabelSize, LegendFontsize, font_gap = 40, 40, 5\n",
|
|
" \n",
|
|
"# fig = plt.figure(figsize=figsize)\n",
|
|
" \n",
|
|
"# times, xs, ys = get_data(None)\n",
|
|
" \n",
|
|
"# def draw_ax(cur_ax, xaxis, yaxis, xlabel, ylabel,\n",
|
|
"# alpha=0.1, color='k', linestyle='-', legend=None, plot_only=False):\n",
|
|
"# if legend is not None:\n",
|
|
"# cur_ax.plot(xaxis[:1], yaxis[:1], color=color, label=legend)\n",
|
|
"# cur_ax.plot(xaxis, yaxis, color=color, linestyle=linestyle, alpha=alpha, label=None)\n",
|
|
"# if not plot_only:\n",
|
|
"# cur_ax.set_xlabel(xlabel, fontsize=LabelSize)\n",
|
|
"# cur_ax.set_ylabel(ylabel, rotation=0, fontsize=LabelSize)\n",
|
|
"# for tick in cur_ax.xaxis.get_major_ticks():\n",
|
|
"# tick.label.set_fontsize(LabelSize - font_gap)\n",
|
|
"# tick.label.set_rotation(10)\n",
|
|
"# for tick in cur_ax.yaxis.get_major_ticks():\n",
|
|
"# tick.label.set_fontsize(LabelSize - font_gap)\n",
|
|
" \n",
|
|
"# cur_ax = fig.add_subplot(2, 1, 1)\n",
|
|
"# draw_ax(cur_ax, times, xs, \"time\", \"x\", alpha=1.0, legend=None)\n",
|
|
"\n",
|
|
"# cur_ax = fig.add_subplot(2, 1, 2)\n",
|
|
"# draw_ax(cur_ax, times, ys, \"time\", \"y\", alpha=0.1, legend=\"ground truth\")\n",
|
|
" \n",
|
|
"# train_times, train_xs, train_ys = get_data(\"train\")\n",
|
|
"# draw_ax(cur_ax, train_times, train_ys, None, None, alpha=1.0, color='r', legend=None, plot_only=True)\n",
|
|
" \n",
|
|
"# valid_times, valid_xs, valid_ys = get_data(\"valid\")\n",
|
|
"# draw_ax(cur_ax, valid_times, valid_ys, None, None, alpha=1.0, color='g', legend=None, plot_only=True)\n",
|
|
" \n",
|
|
"# test_times, test_xs, test_ys = get_data(\"test\")\n",
|
|
"# draw_ax(cur_ax, test_times, test_ys, None, None, alpha=1.0, color='b', legend=None, plot_only=True)\n",
|
|
" \n",
|
|
"# # optimize MLP models\n",
|
|
"# # [train_preds, valid_preds, test_preds] = optimize_fn(train_xs, train_ys, [train_xs, valid_xs, test_xs])\n",
|
|
"# # draw_ax(cur_ax, train_times, train_preds, None, None,\n",
|
|
"# # alpha=1.0, linestyle='--', color='r', legend=\"MLP\", plot_only=True)\n",
|
|
"# # import pdb; pdb.set_trace()\n",
|
|
"# # draw_ax(cur_ax, valid_times, valid_preds, None, None,\n",
|
|
"# # alpha=1.0, linestyle='--', color='g', legend=None, plot_only=True)\n",
|
|
"# # draw_ax(cur_ax, test_times, test_preds, None, None,\n",
|
|
"# # alpha=1.0, linestyle='--', color='b', legend=None, plot_only=True)\n",
|
|
"\n",
|
|
"# plt.legend(loc=1, fontsize=LegendFontsize)\n",
|
|
"\n",
|
|
"# fig.savefig(save_path, dpi=dpi, bbox_inches=\"tight\", format=\"pdf\")\n",
|
|
"# plt.close(\"all\")\n",
|
|
"# # plt.show()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "shared-envelope",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Visualization\n",
|
|
"# home_dir = Path.home()\n",
|
|
"# desktop_dir = home_dir / 'Desktop'\n",
|
|
"# print('The Desktop is at: {:}'.format(desktop_dir))\n",
|
|
"# visualize_syn(desktop_dir / 'tot-synthetic-v0.pdf')"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.8.8"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|