1
0
mirror of https://github.com/gryf/coach.git synced 2025-12-17 19:20:19 +01:00

fixing the dropout rate code (#72)

addresses issue #53
This commit is contained in:
Itai Caspi
2018-11-08 16:53:47 +02:00
committed by GitHub
parent 389c65cbbe
commit 3a0a1159e9
11 changed files with 33 additions and 33 deletions

View File

@@ -21,13 +21,13 @@ from rl_coach.base_parameters import EmbedderScheme, NetworkComponentParameters
class InputEmbedderParameters(NetworkComponentParameters): class InputEmbedderParameters(NetworkComponentParameters):
def __init__(self, activation_function: str='relu', scheme: Union[List, EmbedderScheme]=EmbedderScheme.Medium, def __init__(self, activation_function: str='relu', scheme: Union[List, EmbedderScheme]=EmbedderScheme.Medium,
batchnorm: bool=False, dropout=False, name: str='embedder', input_rescaling=None, input_offset=None, batchnorm: bool=False, dropout_rate: float=0.0, name: str='embedder', input_rescaling=None,
input_clipping=None, dense_layer=None, is_training=False): input_offset=None, input_clipping=None, dense_layer=None, is_training=False):
super().__init__(dense_layer=dense_layer) super().__init__(dense_layer=dense_layer)
self.activation_function = activation_function self.activation_function = activation_function
self.scheme = scheme self.scheme = scheme
self.batchnorm = batchnorm self.batchnorm = batchnorm
self.dropout = dropout self.dropout_rate = dropout_rate
if input_rescaling is None: if input_rescaling is None:
input_rescaling = {'image': 255.0, 'vector': 1.0} input_rescaling = {'image': 255.0, 'vector': 1.0}

View File

@@ -22,12 +22,12 @@ from rl_coach.base_parameters import MiddlewareScheme, NetworkComponentParameter
class MiddlewareParameters(NetworkComponentParameters): class MiddlewareParameters(NetworkComponentParameters):
def __init__(self, parameterized_class_name: str, def __init__(self, parameterized_class_name: str,
activation_function: str='relu', scheme: Union[List, MiddlewareScheme]=MiddlewareScheme.Medium, activation_function: str='relu', scheme: Union[List, MiddlewareScheme]=MiddlewareScheme.Medium,
batchnorm: bool=False, dropout: bool=False, name='middleware', dense_layer=None, is_training=False): batchnorm: bool=False, dropout_rate: float=0.0, name='middleware', dense_layer=None, is_training=False):
super().__init__(dense_layer=dense_layer) super().__init__(dense_layer=dense_layer)
self.activation_function = activation_function self.activation_function = activation_function
self.scheme = scheme self.scheme = scheme
self.batchnorm = batchnorm self.batchnorm = batchnorm
self.dropout = dropout self.dropout_rate = dropout_rate
self.name = name self.name = name
self.is_training = is_training self.is_training = is_training
self.parameterized_class_name = parameterized_class_name self.parameterized_class_name = parameterized_class_name
@@ -36,19 +36,19 @@ class MiddlewareParameters(NetworkComponentParameters):
class FCMiddlewareParameters(MiddlewareParameters): class FCMiddlewareParameters(MiddlewareParameters):
def __init__(self, activation_function='relu', def __init__(self, activation_function='relu',
scheme: Union[List, MiddlewareScheme] = MiddlewareScheme.Medium, scheme: Union[List, MiddlewareScheme] = MiddlewareScheme.Medium,
batchnorm: bool = False, dropout: bool = False, batchnorm: bool = False, dropout_rate: float = 0.0,
name="middleware_fc_embedder", dense_layer=None, is_training=False): name="middleware_fc_embedder", dense_layer=None, is_training=False):
super().__init__(parameterized_class_name="FCMiddleware", activation_function=activation_function, super().__init__(parameterized_class_name="FCMiddleware", activation_function=activation_function,
scheme=scheme, batchnorm=batchnorm, dropout=dropout, name=name, dense_layer=dense_layer, scheme=scheme, batchnorm=batchnorm, dropout_rate=dropout_rate, name=name, dense_layer=dense_layer,
is_training=is_training) is_training=is_training)
class LSTMMiddlewareParameters(MiddlewareParameters): class LSTMMiddlewareParameters(MiddlewareParameters):
def __init__(self, activation_function='relu', number_of_lstm_cells=256, def __init__(self, activation_function='relu', number_of_lstm_cells=256,
scheme: MiddlewareScheme = MiddlewareScheme.Medium, scheme: MiddlewareScheme = MiddlewareScheme.Medium,
batchnorm: bool = False, dropout: bool = False, batchnorm: bool = False, dropout_rate: float = 0.0,
name="middleware_lstm_embedder", dense_layer=None, is_training=False): name="middleware_lstm_embedder", dense_layer=None, is_training=False):
super().__init__(parameterized_class_name="LSTMMiddleware", activation_function=activation_function, super().__init__(parameterized_class_name="LSTMMiddleware", activation_function=activation_function,
scheme=scheme, batchnorm=batchnorm, dropout=dropout, name=name, dense_layer=dense_layer, scheme=scheme, batchnorm=batchnorm, dropout_rate=dropout_rate, name=name, dense_layer=dense_layer,
is_training=is_training) is_training=is_training)
self.number_of_lstm_cells = number_of_lstm_cells self.number_of_lstm_cells = number_of_lstm_cells

View File

@@ -39,8 +39,8 @@ class InputEmbedder(nn.HybridBlock):
self.net.add(nn.BatchNorm()) self.net.add(nn.BatchNorm())
if params.activation_function: if params.activation_function:
self.net.add(nn.Activation(params.activation_function)) self.net.add(nn.Activation(params.activation_function))
if params.dropout: if params.dropout_rate:
self.net.add(nn.Dropout(rate=params.dropout)) self.net.add(nn.Dropout(rate=params.dropout_rate))
@property @property
def schemes(self) -> dict: def schemes(self) -> dict:

View File

@@ -36,8 +36,8 @@ class Middleware(nn.HybridBlock):
self.net.add(nn.BatchNorm()) self.net.add(nn.BatchNorm())
if params.activation_function: if params.activation_function:
self.net.add(nn.Activation(params.activation_function)) self.net.add(nn.Activation(params.activation_function))
if params.dropout: if params.dropout_rate:
self.net.add(nn.Dropout(rate=params.dropout)) self.net.add(nn.Dropout(rate=params.dropout_rate))
@property @property
def schemes(self) -> dict: def schemes(self) -> dict:

View File

@@ -34,15 +34,14 @@ class InputEmbedder(object):
can be multiple embedders in a single network can be multiple embedders in a single network
""" """
def __init__(self, input_size: List[int], activation_function=tf.nn.relu, def __init__(self, input_size: List[int], activation_function=tf.nn.relu,
scheme: EmbedderScheme=None, batchnorm: bool=False, dropout: bool=False, scheme: EmbedderScheme=None, batchnorm: bool=False, dropout_rate: float=0.0,
name: str= "embedder", input_rescaling=1.0, input_offset=0.0, input_clipping=None, dense_layer=Dense, name: str= "embedder", input_rescaling=1.0, input_offset=0.0, input_clipping=None, dense_layer=Dense,
is_training=False): is_training=False):
self.name = name self.name = name
self.input_size = input_size self.input_size = input_size
self.activation_function = activation_function self.activation_function = activation_function
self.batchnorm = batchnorm self.batchnorm = batchnorm
self.dropout = dropout self.dropout_rate = dropout_rate
self.dropout_rate = 0
self.input = None self.input = None
self.output = None self.output = None
self.scheme = scheme self.scheme = scheme
@@ -68,7 +67,7 @@ class InputEmbedder(object):
# we allow adding batchnorm, dropout or activation functions after each layer. # we allow adding batchnorm, dropout or activation functions after each layer.
# The motivation is to simplify the transition between a network with batchnorm and a network without # The motivation is to simplify the transition between a network with batchnorm and a network without
# batchnorm to a single flag (the same applies to activation function and dropout) # batchnorm to a single flag (the same applies to activation function and dropout)
if self.batchnorm or self.activation_function or self.dropout: if self.batchnorm or self.activation_function or self.dropout_rate > 0:
for layer_idx in reversed(range(len(self.layers_params))): for layer_idx in reversed(range(len(self.layers_params))):
self.layers_params.insert(layer_idx+1, self.layers_params.insert(layer_idx+1,
BatchnormActivationDropout(batchnorm=self.batchnorm, BatchnormActivationDropout(batchnorm=self.batchnorm,

View File

@@ -32,10 +32,10 @@ class ImageEmbedder(InputEmbedder):
""" """
def __init__(self, input_size: List[int], activation_function=tf.nn.relu, def __init__(self, input_size: List[int], activation_function=tf.nn.relu,
scheme: EmbedderScheme=EmbedderScheme.Medium, batchnorm: bool=False, dropout: bool=False, scheme: EmbedderScheme=EmbedderScheme.Medium, batchnorm: bool=False, dropout_rate: float=0.0,
name: str= "embedder", input_rescaling: float=255.0, input_offset: float=0.0, input_clipping=None, name: str= "embedder", input_rescaling: float=255.0, input_offset: float=0.0, input_clipping=None,
dense_layer=Dense, is_training=False): dense_layer=Dense, is_training=False):
super().__init__(input_size, activation_function, scheme, batchnorm, dropout, name, input_rescaling, super().__init__(input_size, activation_function, scheme, batchnorm, dropout_rate, name, input_rescaling,
input_offset, input_clipping, dense_layer=dense_layer, is_training=is_training) input_offset, input_clipping, dense_layer=dense_layer, is_training=is_training)
self.return_type = InputImageEmbedding self.return_type = InputImageEmbedding
if len(input_size) != 3 and scheme != EmbedderScheme.Empty: if len(input_size) != 3 and scheme != EmbedderScheme.Empty:

View File

@@ -31,10 +31,10 @@ class VectorEmbedder(InputEmbedder):
""" """
def __init__(self, input_size: List[int], activation_function=tf.nn.relu, def __init__(self, input_size: List[int], activation_function=tf.nn.relu,
scheme: EmbedderScheme=EmbedderScheme.Medium, batchnorm: bool=False, dropout: bool=False, scheme: EmbedderScheme=EmbedderScheme.Medium, batchnorm: bool=False, dropout_rate: float=0.0,
name: str= "embedder", input_rescaling: float=1.0, input_offset:float=0.0, input_clipping=None, name: str= "embedder", input_rescaling: float=1.0, input_offset: float=0.0, input_clipping=None,
dense_layer=Dense, is_training=False): dense_layer=Dense, is_training=False):
super().__init__(input_size, activation_function, scheme, batchnorm, dropout, name, super().__init__(input_size, activation_function, scheme, batchnorm, dropout_rate, name,
input_rescaling, input_offset, input_clipping, dense_layer=dense_layer, input_rescaling, input_offset, input_clipping, dense_layer=dense_layer,
is_training=is_training) is_training=is_training)

View File

@@ -8,7 +8,7 @@ from rl_coach.architectures import layers
from rl_coach.architectures.tensorflow_components import utils from rl_coach.architectures.tensorflow_components import utils
def batchnorm_activation_dropout(input_layer, batchnorm, activation_function, dropout, dropout_rate, is_training, name): def batchnorm_activation_dropout(input_layer, batchnorm, activation_function, dropout_rate, is_training, name):
layers = [input_layer] layers = [input_layer]
# batchnorm # batchnorm
@@ -26,7 +26,7 @@ def batchnorm_activation_dropout(input_layer, batchnorm, activation_function, dr
) )
# dropout # dropout
if dropout: if dropout_rate > 0:
layers.append( layers.append(
tf.layers.dropout(layers[-1], dropout_rate, name="{}_dropout".format(name), training=is_training) tf.layers.dropout(layers[-1], dropout_rate, name="{}_dropout".format(name), training=is_training)
) )
@@ -100,7 +100,7 @@ class BatchnormActivationDropout(layers.BatchnormActivationDropout):
""" """
return batchnorm_activation_dropout(input_layer, batchnorm=self.batchnorm, return batchnorm_activation_dropout(input_layer, batchnorm=self.batchnorm,
activation_function=self.activation_function, activation_function=self.activation_function,
dropout=self.dropout_rate > 0, dropout_rate=self.dropout_rate, dropout_rate=self.dropout_rate,
is_training=is_training, name=name) is_training=is_training, name=name)
@staticmethod @staticmethod

View File

@@ -27,10 +27,11 @@ from rl_coach.utils import force_list
class FCMiddleware(Middleware): class FCMiddleware(Middleware):
def __init__(self, activation_function=tf.nn.relu, def __init__(self, activation_function=tf.nn.relu,
scheme: MiddlewareScheme = MiddlewareScheme.Medium, scheme: MiddlewareScheme = MiddlewareScheme.Medium,
batchnorm: bool = False, dropout: bool = False, batchnorm: bool = False, dropout_rate: float = 0.0,
name="middleware_fc_embedder", dense_layer=Dense, is_training=False): name="middleware_fc_embedder", dense_layer=Dense, is_training=False):
super().__init__(activation_function=activation_function, batchnorm=batchnorm, super().__init__(activation_function=activation_function, batchnorm=batchnorm,
dropout=dropout, scheme=scheme, name=name, dense_layer=dense_layer, is_training=is_training) dropout_rate=dropout_rate, scheme=scheme, name=name, dense_layer=dense_layer,
is_training=is_training)
self.return_type = Middleware_FC_Embedding self.return_type = Middleware_FC_Embedding
self.layers = [] self.layers = []

View File

@@ -28,10 +28,11 @@ from rl_coach.utils import force_list
class LSTMMiddleware(Middleware): class LSTMMiddleware(Middleware):
def __init__(self, activation_function=tf.nn.relu, number_of_lstm_cells: int=256, def __init__(self, activation_function=tf.nn.relu, number_of_lstm_cells: int=256,
scheme: MiddlewareScheme = MiddlewareScheme.Medium, scheme: MiddlewareScheme = MiddlewareScheme.Medium,
batchnorm: bool = False, dropout: bool = False, batchnorm: bool = False, dropout_rate: float = 0.0,
name="middleware_lstm_embedder", dense_layer=Dense, is_training=False): name="middleware_lstm_embedder", dense_layer=Dense, is_training=False):
super().__init__(activation_function=activation_function, batchnorm=batchnorm, super().__init__(activation_function=activation_function, batchnorm=batchnorm,
dropout=dropout, scheme=scheme, name=name, dense_layer=dense_layer, is_training=is_training) dropout_rate=dropout_rate, scheme=scheme, name=name, dense_layer=dense_layer,
is_training=is_training)
self.return_type = Middleware_LSTM_Embedding self.return_type = Middleware_LSTM_Embedding
self.number_of_lstm_cells = number_of_lstm_cells self.number_of_lstm_cells = number_of_lstm_cells
self.layers = [] self.layers = []

View File

@@ -31,15 +31,14 @@ class Middleware(object):
""" """
def __init__(self, activation_function=tf.nn.relu, def __init__(self, activation_function=tf.nn.relu,
scheme: MiddlewareScheme = MiddlewareScheme.Medium, scheme: MiddlewareScheme = MiddlewareScheme.Medium,
batchnorm: bool = False, dropout: bool = False, name="middleware_embedder", dense_layer=Dense, batchnorm: bool = False, dropout_rate: float = 0.0, name="middleware_embedder", dense_layer=Dense,
is_training=False): is_training=False):
self.name = name self.name = name
self.input = None self.input = None
self.output = None self.output = None
self.activation_function = activation_function self.activation_function = activation_function
self.batchnorm = batchnorm self.batchnorm = batchnorm
self.dropout = dropout self.dropout_rate = dropout_rate
self.dropout_rate = 0
self.scheme = scheme self.scheme = scheme
self.return_type = MiddlewareEmbedding self.return_type = MiddlewareEmbedding
self.dense_layer = dense_layer self.dense_layer = dense_layer
@@ -58,7 +57,7 @@ class Middleware(object):
# we allow adding batchnorm, dropout or activation functions after each layer. # we allow adding batchnorm, dropout or activation functions after each layer.
# The motivation is to simplify the transition between a network with batchnorm and a network without # The motivation is to simplify the transition between a network with batchnorm and a network without
# batchnorm to a single flag (the same applies to activation function and dropout) # batchnorm to a single flag (the same applies to activation function and dropout)
if self.batchnorm or self.activation_function or self.dropout: if self.batchnorm or self.activation_function or self.dropout_rate > 0:
for layer_idx in reversed(range(len(self.layers_params))): for layer_idx in reversed(range(len(self.layers_params))):
self.layers_params.insert(layer_idx+1, self.layers_params.insert(layer_idx+1,
BatchnormActivationDropout(batchnorm=self.batchnorm, BatchnormActivationDropout(batchnorm=self.batchnorm,