0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ラズパイでkeras その3

Posted at

概要

ラズパイで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)
    

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?