mirror of
https://github.com/gryf/coach.git
synced 2025-12-17 19:20:19 +01:00
Till now, most of the modules were importing all of the module objects (variables, classes, functions, other imports) into module namespace, which potentially could (and was) cause of unintentional use of class or methods, which was indirect imported. With this patch, all the star imports were substituted with top-level module, which provides desired class or function. Besides, all imports where sorted (where possible) in a way pep8[1] suggests - first are imports from standard library, than goes third party imports (like numpy, tensorflow etc) and finally coach modules. All of those sections are separated by one empty line. [1] https://www.python.org/dev/peps/pep-0008/#imports
70 lines
2.7 KiB
Python
70 lines
2.7 KiB
Python
#
|
|
# Copyright (c) 2017 Intel Corporation
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
#
|
|
import collections
|
|
|
|
from agents import agent
|
|
from architectures import network_wrapper as nw
|
|
import utils
|
|
import logging
|
|
|
|
|
|
# Imitation Agent
|
|
class ImitationAgent(agent.Agent):
|
|
def __init__(self, env, tuning_parameters, replicated_device=None, thread_id=0):
|
|
agent.Agent.__init__(self, env, tuning_parameters, replicated_device, thread_id)
|
|
self.main_network = nw.NetworkWrapper(tuning_parameters, False, self.has_global, 'main',
|
|
self.replicated_device, self.worker_device)
|
|
self.networks.append(self.main_network)
|
|
self.imitation = True
|
|
|
|
def extract_action_values(self, prediction):
|
|
return prediction.squeeze()
|
|
|
|
def choose_action(self, curr_state, phase=utils.RunPhase.TRAIN):
|
|
# convert to batch so we can run it through the network
|
|
prediction = self.main_network.online_network.predict(self.tf_input_state(curr_state))
|
|
|
|
# get action values and extract the best action from it
|
|
action_values = self.extract_action_values(prediction)
|
|
if self.env.discrete_controls:
|
|
# DISCRETE
|
|
# action = np.argmax(action_values)
|
|
action = self.evaluation_exploration_policy.get_action(action_values)
|
|
action_value = {"action_probability": action_values[action]}
|
|
else:
|
|
# CONTINUOUS
|
|
action = action_values
|
|
action_value = {}
|
|
|
|
return action, action_value
|
|
|
|
def log_to_screen(self, phase):
|
|
# log to screen
|
|
if phase == utils.RunPhase.TRAIN:
|
|
# for the training phase - we log during the episode to visualize the progress in training
|
|
logging.screen.log_dict(
|
|
collections.OrderedDict([
|
|
("Worker", self.task_id),
|
|
("Episode", self.current_episode),
|
|
("Loss", self.loss.values[-1]),
|
|
("Training iteration", self.training_iteration)
|
|
]),
|
|
prefix="Training"
|
|
)
|
|
else:
|
|
# for the evaluation phase - logging as in regular RL
|
|
agent.Agent.log_to_screen(self, phase)
|