mirror of
https://github.com/gryf/coach.git
synced 2025-12-18 11:40:18 +01:00
Cleanup imports.
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
This commit is contained in:
@@ -15,12 +15,11 @@
|
||||
#
|
||||
import time
|
||||
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
|
||||
from architectures.architecture import Architecture
|
||||
from utils import force_list, squeeze_list
|
||||
from configurations import Preset, MiddlewareTypes
|
||||
from architectures import architecture
|
||||
import configurations as conf
|
||||
import utils
|
||||
|
||||
def variable_summaries(var):
|
||||
"""Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
|
||||
@@ -37,14 +36,14 @@ def variable_summaries(var):
|
||||
tf.summary.scalar('min', tf.reduce_min(var))
|
||||
tf.summary.histogram('histogram', var)
|
||||
|
||||
class TensorFlowArchitecture(Architecture):
|
||||
class TensorFlowArchitecture(architecture.Architecture):
|
||||
def __init__(self, tuning_parameters, name="", global_network=None, network_is_local=True):
|
||||
"""
|
||||
:param tuning_parameters: The parameters used for running the algorithm
|
||||
:type tuning_parameters: Preset
|
||||
:param name: The name of the network
|
||||
"""
|
||||
Architecture.__init__(self, tuning_parameters, name)
|
||||
architecture.Architecture.__init__(self, tuning_parameters, name)
|
||||
self.middleware_embedder = None
|
||||
self.network_is_local = network_is_local
|
||||
assert tuning_parameters.agent.tensorflow_support, 'TensorFlow is not supported for this agent'
|
||||
@@ -174,7 +173,7 @@ class TensorFlowArchitecture(Architecture):
|
||||
feed_dict = self._feed_dict(inputs)
|
||||
|
||||
# feed targets
|
||||
targets = force_list(targets)
|
||||
targets = utils.force_list(targets)
|
||||
for placeholder_idx, target in enumerate(targets):
|
||||
feed_dict[self.targets[placeholder_idx]] = target
|
||||
|
||||
@@ -186,13 +185,13 @@ class TensorFlowArchitecture(Architecture):
|
||||
else:
|
||||
fetches.append(self.tensor_gradients)
|
||||
fetches += [self.total_loss, self.losses]
|
||||
if self.tp.agent.middleware_type == MiddlewareTypes.LSTM:
|
||||
if self.tp.agent.middleware_type == conf.MiddlewareTypes.LSTM:
|
||||
fetches.append(self.middleware_embedder.state_out)
|
||||
additional_fetches_start_idx = len(fetches)
|
||||
fetches += additional_fetches
|
||||
|
||||
# feed the lstm state if necessary
|
||||
if self.tp.agent.middleware_type == MiddlewareTypes.LSTM:
|
||||
if self.tp.agent.middleware_type == conf.MiddlewareTypes.LSTM:
|
||||
# we can't always assume that we are starting from scratch here can we?
|
||||
feed_dict[self.middleware_embedder.c_in] = self.middleware_embedder.c_init
|
||||
feed_dict[self.middleware_embedder.h_in] = self.middleware_embedder.h_init
|
||||
@@ -206,7 +205,7 @@ class TensorFlowArchitecture(Architecture):
|
||||
|
||||
# extract the fetches
|
||||
norm_unclipped_grads, grads, total_loss, losses = result[:4]
|
||||
if self.tp.agent.middleware_type == MiddlewareTypes.LSTM:
|
||||
if self.tp.agent.middleware_type == conf.MiddlewareTypes.LSTM:
|
||||
(self.curr_rnn_c_in, self.curr_rnn_h_in) = result[4]
|
||||
fetched_tensors = []
|
||||
if len(additional_fetches) > 0:
|
||||
@@ -308,7 +307,7 @@ class TensorFlowArchitecture(Architecture):
|
||||
if outputs is None:
|
||||
outputs = self.outputs
|
||||
|
||||
if self.tp.agent.middleware_type == MiddlewareTypes.LSTM:
|
||||
if self.tp.agent.middleware_type == conf.MiddlewareTypes.LSTM:
|
||||
feed_dict[self.middleware_embedder.c_in] = self.curr_rnn_c_in
|
||||
feed_dict[self.middleware_embedder.h_in] = self.curr_rnn_h_in
|
||||
|
||||
@@ -317,7 +316,7 @@ class TensorFlowArchitecture(Architecture):
|
||||
output = self.tp.sess.run(outputs, feed_dict)
|
||||
|
||||
if squeeze_output:
|
||||
output = squeeze_list(output)
|
||||
output = utils.squeeze_list(output)
|
||||
|
||||
return output
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#
|
||||
# Copyright (c) 2017 Intel Corporation
|
||||
# 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.
|
||||
@@ -13,8 +13,8 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
from configurations import EmbedderComplexity
|
||||
|
||||
|
||||
|
||||
@@ -13,15 +13,16 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
import tensorflow as tf
|
||||
|
||||
from architectures.tensorflow_components.embedders import *
|
||||
from architectures.tensorflow_components.heads import *
|
||||
from architectures.tensorflow_components.middleware import *
|
||||
from architectures.tensorflow_components.architecture import *
|
||||
from configurations import InputTypes, OutputTypes, MiddlewareTypes
|
||||
from architectures.tensorflow_components import architecture
|
||||
from architectures.tensorflow_components import embedders
|
||||
from architectures.tensorflow_components import middleware
|
||||
from architectures.tensorflow_components import heads
|
||||
import configurations as conf
|
||||
|
||||
|
||||
class GeneralTensorFlowNetwork(TensorFlowArchitecture):
|
||||
class GeneralTensorFlowNetwork(architecture.TensorFlowArchitecture):
|
||||
"""
|
||||
A generalized version of all possible networks implemented using tensorflow.
|
||||
"""
|
||||
@@ -37,7 +38,7 @@ class GeneralTensorFlowNetwork(TensorFlowArchitecture):
|
||||
self.activation_function = self.get_activation_function(
|
||||
tuning_parameters.agent.hidden_layers_activation_function)
|
||||
|
||||
TensorFlowArchitecture.__init__(self, tuning_parameters, name, global_network, network_is_local)
|
||||
architecture.TensorFlowArchitecture.__init__(self, tuning_parameters, name, global_network, network_is_local)
|
||||
|
||||
def get_activation_function(self, activation_function_string):
|
||||
activation_functions = {
|
||||
@@ -56,37 +57,37 @@ class GeneralTensorFlowNetwork(TensorFlowArchitecture):
|
||||
# the observation can be either an image or a vector
|
||||
def get_observation_embedding(with_timestep=False):
|
||||
if self.input_height > 1:
|
||||
return ImageEmbedder((self.input_height, self.input_width, self.input_depth), name="observation",
|
||||
input_rescaler=self.tp.agent.input_rescaler)
|
||||
return embedders.ImageEmbedder((self.input_height, self.input_width, self.input_depth), name="observation",
|
||||
input_rescaler=self.tp.agent.input_rescaler)
|
||||
else:
|
||||
return VectorEmbedder((self.input_width + int(with_timestep), self.input_depth), name="observation")
|
||||
return embedders.VectorEmbedder((self.input_width + int(with_timestep), self.input_depth), name="observation")
|
||||
|
||||
input_mapping = {
|
||||
InputTypes.Observation: get_observation_embedding(),
|
||||
InputTypes.Measurements: VectorEmbedder(self.measurements_size, name="measurements"),
|
||||
InputTypes.GoalVector: VectorEmbedder(self.measurements_size, name="goal_vector"),
|
||||
InputTypes.Action: VectorEmbedder((self.num_actions,), name="action"),
|
||||
InputTypes.TimedObservation: get_observation_embedding(with_timestep=True),
|
||||
conf.InputTypes.Observation: get_observation_embedding(),
|
||||
conf.InputTypes.Measurements: embedders.VectorEmbedder(self.measurements_size, name="measurements"),
|
||||
conf.InputTypes.GoalVector: embedders.VectorEmbedder(self.measurements_size, name="goal_vector"),
|
||||
conf.InputTypes.Action: embedders.VectorEmbedder((self.num_actions,), name="action"),
|
||||
conf.InputTypes.TimedObservation: get_observation_embedding(with_timestep=True),
|
||||
}
|
||||
return input_mapping[embedder_type]
|
||||
|
||||
def get_middleware_embedder(self, middleware_type):
|
||||
return {MiddlewareTypes.LSTM: LSTM_Embedder,
|
||||
MiddlewareTypes.FC: FC_Embedder}.get(middleware_type)(self.activation_function)
|
||||
return {conf.MiddlewareTypes.LSTM: middleware.LSTM_Embedder,
|
||||
conf.MiddlewareTypes.FC: middleware.FC_Embedder}.get(middleware_type)(self.activation_function)
|
||||
|
||||
def get_output_head(self, head_type, head_idx, loss_weight=1.):
|
||||
output_mapping = {
|
||||
OutputTypes.Q: QHead,
|
||||
OutputTypes.DuelingQ: DuelingQHead,
|
||||
OutputTypes.V: VHead,
|
||||
OutputTypes.Pi: PolicyHead,
|
||||
OutputTypes.MeasurementsPrediction: MeasurementsPredictionHead,
|
||||
OutputTypes.DNDQ: DNDQHead,
|
||||
OutputTypes.NAF: NAFHead,
|
||||
OutputTypes.PPO: PPOHead,
|
||||
OutputTypes.PPO_V: PPOVHead,
|
||||
OutputTypes.CategoricalQ: CategoricalQHead,
|
||||
OutputTypes.QuantileRegressionQ: QuantileRegressionQHead
|
||||
conf.OutputTypes.Q: heads.QHead,
|
||||
conf.OutputTypes.DuelingQ: heads.DuelingQHead,
|
||||
conf.OutputTypes.V: heads.VHead,
|
||||
conf.OutputTypes.Pi: heads.PolicyHead,
|
||||
conf.OutputTypes.MeasurementsPrediction: heads.MeasurementsPredictionHead,
|
||||
conf.OutputTypes.DNDQ: heads.DNDQHead,
|
||||
conf.OutputTypes.NAF: heads.NAFHead,
|
||||
conf.OutputTypes.PPO: heads.PPOHead,
|
||||
conf.OutputTypes.PPO_V: heads.PPOVHead,
|
||||
conf.OutputTypes.CategoricalQ: heads.CategoricalQHead,
|
||||
conf.OutputTypes.QuantileRegressionQ: heads.QuantileRegressionQHead
|
||||
}
|
||||
return output_mapping[head_type](self.tp, head_idx, loss_weight, self.network_is_local)
|
||||
|
||||
|
||||
@@ -13,10 +13,10 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
from utils import force_list
|
||||
|
||||
import utils
|
||||
|
||||
|
||||
# Used to initialize weights for policy and value output layers
|
||||
@@ -36,7 +36,7 @@ class Head(object):
|
||||
self.loss = []
|
||||
self.loss_type = []
|
||||
self.regularizations = []
|
||||
self.loss_weight = force_list(loss_weight)
|
||||
self.loss_weight = utils.force_list(loss_weight)
|
||||
self.target = []
|
||||
self.input = []
|
||||
self.is_local = is_local
|
||||
@@ -50,12 +50,12 @@ class Head(object):
|
||||
with tf.variable_scope(self.get_name(), initializer=tf.contrib.layers.xavier_initializer()):
|
||||
self._build_module(input_layer)
|
||||
|
||||
self.output = force_list(self.output)
|
||||
self.target = force_list(self.target)
|
||||
self.input = force_list(self.input)
|
||||
self.loss_type = force_list(self.loss_type)
|
||||
self.loss = force_list(self.loss)
|
||||
self.regularizations = force_list(self.regularizations)
|
||||
self.output = utils.force_list(self.output)
|
||||
self.target = utils.force_list(self.target)
|
||||
self.input = utils.force_list(self.input)
|
||||
self.loss_type = utils.force_list(self.loss_type)
|
||||
self.loss = utils.force_list(self.loss)
|
||||
self.regularizations = utils.force_list(self.regularizations)
|
||||
if self.is_local:
|
||||
self.set_loss()
|
||||
self._post_build()
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#
|
||||
# Copyright (c) 2017 Intel Corporation
|
||||
# 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.
|
||||
@@ -13,7 +13,6 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
|
||||
@@ -79,4 +78,4 @@ class SharedRunningStats(object):
|
||||
|
||||
@property
|
||||
def shape(self):
|
||||
return self._shape
|
||||
return self._shape
|
||||
|
||||
Reference in New Issue
Block a user