new default option initial_state;
layers.Recurrence() now accepts a scalar constant as the initial_state
This commit is contained in:
Родитель
d229d068bf
Коммит
c3a2effab4
|
@ -34,14 +34,18 @@ _INFERRED = (InferredDimension,) # as a tuple, makes life easier
|
|||
def UntestedBranchError(name):
|
||||
raise NotImplementedError("Untested code branch: " + name)
|
||||
|
||||
# This record contains the defaults for a number of optional parameters to layers.
|
||||
# These can be overwritten temporarily by saying
|
||||
# with default_options(init=..., ...):
|
||||
# # code block within which the changed defaults are active
|
||||
_current_default_options = Record(
|
||||
init=glorot_uniform(),
|
||||
activation=None, # Dense() and Convolution() have no activation by default
|
||||
activation=None, # Dense() and Convolution() have no activation by default
|
||||
pad=False,
|
||||
bias=True,
|
||||
init_bias=0,
|
||||
enable_self_stabilization=False # Stabilizer() and LSTM()
|
||||
# TODO: add initial_state?
|
||||
enable_self_stabilization=False, # Stabilizer() and LSTM()
|
||||
initial_state=None # Recurrence()
|
||||
)
|
||||
|
||||
_default_sentinel = Record() # This is a singleton sentinel value we recognize and replace in _initializer_for()
|
||||
|
@ -54,6 +58,7 @@ init_bias_default_or_0 = _default_sentinel_init_bias
|
|||
bias_default_or_True = _default_sentinel
|
||||
pad_default_or_False = _default_sentinel
|
||||
enable_self_stabilization_default_or_False = _default_sentinel
|
||||
initial_state_default_or_None = _default_sentinel_init_bias
|
||||
|
||||
# check whether a parameter is a default
|
||||
# This is meant to be used by implementations of layers that take default values that may default to default-defaults.
|
||||
|
|
|
@ -205,9 +205,13 @@ def AveragePooling(filter_shape, # e.g. (3,3)
|
|||
return Pooling(PoolingType_Average, filter_shape, strides=strides, pad=pad)
|
||||
|
||||
# Recurrence() -- run a block recurrently over a time sequence
|
||||
def Recurrence(over, go_backwards=False, initial_state=None):
|
||||
def Recurrence(over, go_backwards=False, initial_state=initial_state_default_or_None):
|
||||
# helper to compute previous value
|
||||
# can take a single Variable/Function or a tuple
|
||||
initial_state = initial_state if _is_given(initial_state) else _current_default_options.initial_state
|
||||
# if initial state is given and a numeric constant, then turn it into a Constant() object
|
||||
if np.isscalar(initial_state):
|
||||
initial_state = Constant(initial_state, shape=(1)) # TODO: This should be automatically done inside the API.
|
||||
if go_backwards:
|
||||
UntestedBranchError("Recurrence, go_backwards option") # TODO: test in SLUHandsOn.py bidirectional
|
||||
def previous_hook(state):
|
||||
|
|
|
@ -48,15 +48,13 @@ def create_reader(path):
|
|||
########################
|
||||
|
||||
def create_model():
|
||||
#with default_options(enable_self_stabilization=False):
|
||||
# the following injects an option to mimic the BS version identically
|
||||
with default_options(enable_self_stabilization=False, initial_state=0.1):
|
||||
return Sequential([
|
||||
#Stabilizer(),
|
||||
Embedding(emb_dim),
|
||||
#BatchNormalization(),
|
||||
Recurrence(LSTM(hidden_dim), go_backwards=False,
|
||||
#),
|
||||
initial_state=Constant(0.1, shape=(1))), # (this last option mimics a default in BS to recreate identical results)
|
||||
# BUGBUG: initial_state=0.1 should work
|
||||
Recurrence(LSTM(hidden_dim), go_backwards=False),
|
||||
#Stabilizer(),
|
||||
#BatchNormalization(),
|
||||
Dense(label_dim)
|
||||
|
|
Загрузка…
Ссылка в новой задаче