概要
ラズパイでkeras使って、gymのPong-v0やってるけど、全然学習が進まない。
乱数では、勝てないので、勝ったデータが積み上がらない。
くやしいので、自分でやってみたら、真剣に勝てなかった話。
写真
自分でキー入力するサンプルコード
aキー 下
dキー 上
import gym
import pygame
from pygame.locals import HWSURFACE, DOUBLEBUF, RESIZABLE, VIDEORESIZE
def display_arr(screen, arr, video_size, transpose):
arr_min, arr_max = arr.min(), arr.max()
arr = 255.0 * (arr - arr_min) / (arr_max - arr_min)
pyg_img = pygame.surfarray.make_surface(arr.swapaxes(0, 1) if transpose else arr)
pyg_img = pygame.transform.scale(pyg_img, video_size)
screen.blit(pyg_img, (0, 0))
def play(env, transpose = True, fps = 30, zoom = None, callback = None, keys_to_action = None):
obs_s = env.observation_space
keys_to_action = env.unwrapped.get_keys_to_action()
relevant_keys = set(sum(map(list, keys_to_action.keys()), []))
video_size = env.observation_space.shape[1], env.observation_space.shape[0]
pressed_keys = []
running = True
env_done = True
screen = pygame.display.set_mode(video_size)
clock = pygame.time.Clock()
while running:
if env_done:
env_done = False
obs = env.reset()
else:
action = keys_to_action[tuple(sorted(pressed_keys))]
prev_obs = obs
obs, rew, env_done, info = env.step(action)
if obs is not None:
display_arr(screen, obs, transpose = transpose, video_size = video_size)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key in relevant_keys:
pressed_keys.append(event.key)
elif event.key == 27:
running = False
elif event.type == pygame.KEYUP:
if event.key in relevant_keys:
pressed_keys.remove(event.key)
elif event.type == pygame.QUIT:
running = False
pygame.display.flip()
clock.tick(fps)
pygame.quit()
if __name__ == '__main__':
env = gym.make("Pong-v0")
play(env, zoom = 1, fps = 10)
以上。