Skip to content

Instantly share code, notes, and snippets.

View ugo-nama-kun's full-sized avatar
🧠
@Homeostasis x Robots

naoto yoshida ugo-nama-kun

🧠
@Homeostasis x Robots
View GitHub Profile
@ugo-nama-kun
ugo-nama-kun / advent2021.md
Last active June 29, 2024 16:10
mujoco_py の marker の使い方

mujoco_py のマーカーの使い方

この記事は強化学習 Advent Calendar 2021の12/22の記事です。

はじめまして!東京大学の吉田です。

大学では身体を持って自律的に発達する人工知能をつくることに興味があって、それを研究しています。

今回の記事は強化学習というよりは強化学習の環境をmujoco-pyを使って作るときのtipsといった内容です。 すでに強化学習の環境を作ってみたり、mujoco-pyを使ってMujocoをつかった物理シミュレーションをしている・やろうとしている人向けの内容です。

@ugo-nama-kun
ugo-nama-kun / tf_gpu_resource.py
Created October 12, 2021 06:24
tensorflow で GPU のリソースを制限するやつ
import tensorflow as tf # chacked @ tensorflow==2.6.0
available_gpus = tf.config.experimental.list_physical_devices('GPU')
print("Num GPUs Available: ", len(available_gpus))
if available_gpus:
try:
tf.config.experimental.set_visible_devices(available_gpus[gpu_id], "GPU")
logical_gpus = tf.config.experimental.list_logical_devices('GPU')
print(len(available_gpus), "Physical GPUs,", len(logical_gpus), "Logical GPU")
except RuntimeError as e:
@ugo-nama-kun
ugo-nama-kun / rl_images.py
Last active October 12, 2021 03:50
強化学習の画像系のtips
from dm_control import suite
from collections import deque
# deepmind control suite を使う場合
env_dmc = suite.load("cartpole", "balance")
# dm_control で得た場合の画像の形式は (84, 84, 3).これは mujoco_py の場合も同じ
im = self.env_dmc.physics.render(camera_id=0, height=84, width=84)
# frame stack [(84, 84, 3), (84, 84, 3), (84, 84, 3)]
@ugo-nama-kun
ugo-nama-kun / visual_rl.md
Last active October 3, 2021 11:34
連続行動+視覚入力を使った深層強化学習まとめ

Deep RL + Continuous Control with Vision

  • 深層強化学習で 連続行動視覚入力 を使ったものをまとめる
  • 特に重要なテクニックが書かれていればそれも書き出す
  • マルチモーダルな強化学習もあれば書いておく

まとめた後の画像エージェントの構成パターン

  • SAC のような形で、完全に actor と critic でネットワークを分けて CNN を2つ利用する
  • actor と critic で CNN は共有するが、CNNの更新はcriticでのみしてactorはそれを利用する
@ugo-nama-kun
ugo-nama-kun / moving_average_numpy.py
Created September 15, 2021 15:53
Simple Moving Average
# From: https://stackoverflow.com/questions/14313510/how-to-calculate-rolling-moving-average-using-python-numpy-scipy
def moving_average(x: np.array, w: int):
return np.convolve(x, np.ones(w), 'valid') / w
@ugo-nama-kun
ugo-nama-kun / gym_rgb_image_to_torch_input.py
Last active September 15, 2021 07:37
gym の render の画像を 正しくpytorch のモデルに打っ込むヤツ
import torch
import numpy as np
import matplotlib.pyplot as plt
# Assuming some environment that render image...
im = env.render(mode="rgb_array",
height=84,
width=84,
camera_id=0) / 255.
im = torch.tensor(im.astype(np.float32))
@ugo-nama-kun
ugo-nama-kun / cohens_d.py
Last active September 15, 2021 07:37
【統計】Cohen's d の計算方法
from numpy import std, mean, sqrt
#correct if the population S.D. is expected to be equal for the two groups.
def cohen_d(x,y):
nx = len(x)
ny = len(y)
dof = nx + ny - 2
return (mean(x) - mean(y)) / sqrt(((nx-1)*std(x, ddof=1) ** 2 + (ny-1)*std(y, ddof=1) ** 2) / dof)
@ugo-nama-kun
ugo-nama-kun / example.py
Last active September 15, 2021 07:36
ものすごいシンプルな 4-th order Runge-Kutta
gamma = 0.05
def f(x):
# x = [y, v]^T
a = np.array(
[
[0, 1],
[-1, -2*gamma]
]
)
@ugo-nama-kun
ugo-nama-kun / upper_lower_bound_plot.py
Last active September 2, 2021 09:37
Simplest possible upper & lower bound plot using matplotlib
import matplotlib.pyplot as plt
import numpy as np
N = 100
x = np.linspace(0, 6*np.pi, N)
y = np.sin(x)
upper = y + 0.2
@ugo-nama-kun
ugo-nama-kun / sliceable_deque.py
Created August 18, 2021 07:12
Slicable Deque
# From : https://stackoverflow.com/questions/10003143/how-to-slice-a-deque
class sliceable_deque(collections.deque):
def __getitem__(self, index):
try:
return collections.deque.__getitem__(self, index)
except TypeError:
return type(self)(itertools.islice(self, index.start,
index.stop, index.step))