def save_data(network, games_played, scores, description): network.model.save_weights(config.get_full_path("weights.hdf5")) config.save_pickle(games_played, "games_played") config.save_pickle(scores, "scores") config.save_pickle(network.memory_states, "memory_states") config.save_pickle(network.outcomes, "outcomes") plot_seaborn(range(1, games_played), scores, description)
def load_data(description, network): games_played = config.load_pickle("games_played") scores = config.load_pickle("scores") if games_played is None or scores is None: print("Starting from scratch") games_played = 1 scores = [] return games_played, scores, network weights_file = config.get_full_path("weights.hdf5") if config.exists(weights_file): network.model.load_weights(weights_file) print("Weights loaded") memory_file = config.get_full_path("memory_states") if config.exists(memory_file): network.memory = config.load_pickle("memory_states") print("Memory loaded") memory_file = config.get_full_path("outcomes") if config.exists(memory_file): network.memory = config.load_pickle("outcomes") print("Outcomes loaded") return games_played, scores, network
def __init__(self): crf_model_file = get_full_path(config['crf']['cell_classifier_model_file']) with open(crf_model_file, 'rb') as infile: self.model = pickle.load(infile, encoding='latin1') # latin1 encoding since we are reading a python2 pickle file
import config DEBUG = True TEMPLATE_DEBUG = DEBUG ADMINS = ( # ('Your Name', '*****@*****.**'), ) MANAGERS = ADMINS DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 'NAME': config.get_full_path('data/db/learnenglish.db'), # Or path to database file if using sqlite3. 'USER': '', # Not used with sqlite3. 'PASSWORD': '', # Not used with sqlite3. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } } # Local time zone for this installation. Choices can be found here: # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name # although not all choices may be available on all operating systems. # On Unix systems, a value of None will cause Django to use the same # timezone as the operating system. # If running in a Windows environment this must be set to the same as your # system time zone. TIME_ZONE = 'Europe/Amsterdam'
import config DEBUG = True TEMPLATE_DEBUG = DEBUG ADMINS = ( # ('Your Name', '*****@*****.**'), ) MANAGERS = ADMINS DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 'NAME': config.get_full_path('data/db/learndutch.db'), # Or path to database file if using sqlite3. 'USER': '', # Not used with sqlite3. 'PASSWORD': '', # Not used with sqlite3. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } } # Local time zone for this installation. Choices can be found here: # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name # although not all choices may be available on all operating systems. # On Unix systems, a value of None will cause Django to use the same # timezone as the operating system. # If running in a Windows environment this must be set to the same as your # system time zone. TIME_ZONE = 'Europe/Amsterdam'
def main(description): global snake, food death_states = [] if gm.auto_movement is False: gm.display = True if gm.display: window = pygame.display.set_mode((gm.window_width, gm.window_width)) snake = Snake((192, 0, 0), (gm.rows // 2, gm.rows // 2)) food = Cube(generate_food(snake.body), color=(0, 220, 0)) if gm.display: clock = pygame.time.Clock() network = NeuralNet(gm.rows) games_played, scores, network = load_data(description, network) # epsilon = 10 - games_played # init_game(snake, food, network) game_over = False while True: if game_over: snake.reset(((gm.rows // 2, gm.rows // 2))) game_over = False score = 0 if gm.auto_movement: # Network is playing while not game_over: if gm.display: pygame.event.get() # pygame.time.delay(0) if gm.display: clock.tick(gm.speed) food_reached = False state_old = network.get_state(snake, food) #perform random actions based on network.epsilon, or choose the action # if random.randint(0, 200) < epsilon: # move_vector = to_categorical(random.randint(0, 2), num_classes=3) # # move_vector = np.array([0.0, 1.0, 0.0]) # else: # # predict action based on the old state prediction = network.model.predict( state_old.reshape((1, network.n_states)))[0] move_vector = to_categorical(np.argmax(prediction), num_classes=3) food_dist_pre_move = manhatten_dist(food, snake) snake.move(move_vector) food_dist_post_move = manhatten_dist(food, snake) food_dist_chng = food_dist_post_move - food_dist_pre_move if snake.has_eaten(food.pos): food_reached = True snake.grow() food = Cube(generate_food(snake.body), color=(0, 220, 0)) if snake.is_dead: game_over = True score = snake.body_length state_new = network.get_state(snake, food) if snake.is_dead: death_states.append( list( np.concatenate([state_old, move_vector, state_new]))) network.update_reward(food_reached, food_dist_chng, game_over) network.train_short_memory(state_old, move_vector, state_new, game_over) network.remember(state_old, move_vector, state_new, game_over) if gm.display: redraw_window(window) if games_played % 100 == 0: network.replay_new() games_played += 1 print(f"game {games_played}, score {score}") scores.append(score) if games_played % 50 == 0: save_data(network, games_played, scores, description) if games_played % 50 == 0: pd.DataFrame.from_records(death_states).to_csv( config.get_full_path("death_states.txt"), mode="a") death_states = [] else: # Manual Play while not game_over: clock.tick(gm.speed) food_reached = False snake.move() if snake.has_eaten(food.pos): food_reached = True snake.grow() food = Cube(generate_food(snake.body), color=(0, 220, 0)) if snake.is_dead: game_over = True score = snake.body_length # snake.reset((10, 10)) redraw_window(window) # print(f"x:{snake.pos_x}, y:{snake.pos_y}") save_data(network, games_played, scores, description)
""" Create logger to handle events. Important messages and errors are recorded in the corresponding logs. Other messages are printed to the console """ import logging import sys from config import get_full_path FORMATTER = logging.Formatter( "%(asctime)s — %(name)s — %(levelname)s — %(message)s") SHORT_FORMATTER = logging.Formatter("%(levelname)s — %(message)s") EVENT_LOG_FILE = get_full_path("logs/events.log") ERROR_LOG_FILE = get_full_path("logs/error.log") def _get_debug_handler(): """Messages are printed to console""" console_handler = logging.StreamHandler(stream=sys.stdout) console_handler.setFormatter(SHORT_FORMATTER) console_handler.setLevel(logging.DEBUG) return console_handler def _get_event_handler(): """Event messages are written to file""" file_handler = logging.FileHandler(filename=EVENT_LOG_FILE) file_handler.setFormatter(FORMATTER) file_handler.setLevel(logging.INFO) return file_handler
def __init__(self): layout_model_file = get_full_path(config['crf']['layout_detector_model_file']) with open(layout_model_file, 'rb') as infile: self.model = pickle.load(infile)
DEBUG = True TEMPLATE_DEBUG = DEBUG ADMINS = ( # ('Your Name', '*****@*****.**'), ) MANAGERS = ADMINS DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 'NAME': config.get_full_path('data/db/learndutch.db' ), # Or path to database file if using sqlite3. 'USER': '', # Not used with sqlite3. 'PASSWORD': '', # Not used with sqlite3. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } } # Local time zone for this installation. Choices can be found here: # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name # although not all choices may be available on all operating systems. # On Unix systems, a value of None will cause Django to use the same # timezone as the operating system. # If running in a Windows environment this must be set to the same as your # system time zone.