@@ -259,7 +259,9 @@ def __init__(
259259 name = 'layer'
260260 ):
261261 self .inputs = inputs
262- name = tf .get_variable_scope ().name + "/" + name
262+ scope_name = tf .get_variable_scope ().name
263+ if scope_name :
264+ name = scope_name + '/' + name
263265 if (name in set_keep ['_layers_name_list' ]) and name_reuse == False :
264266 raise Exception ("Layer '%s' already exists, please choice other 'name' or reuse this layer\
265267 \n Hint : Use different name for different 'Layer' (The name is used to control parameter sharing)" % name )
@@ -304,9 +306,9 @@ def count_params(self):
304306 return n_params
305307
306308 def __str__ (self ):
307- print ("\n It is a Layer class" )
308- self .print_params (False )
309- self .print_layers ()
309+ # print("\nIt is a Layer class")
310+ # self.print_params(False)
311+ # self.print_layers()
310312 return " Last layer is: %s" % self .__class__ .__name__
311313
312314## Input layer
@@ -3657,6 +3659,30 @@ def retrieve_seq_length_op2(data):
36573659 return tf .reduce_sum (tf .cast (tf .greater (data , tf .zeros_like (data )), tf .int32 ), 1 )
36583660
36593661
3662+ def retrieve_seq_length_op3 (data , pad_val = 0 ):
3663+ data_shape_size = data .get_shape ().ndims
3664+ if data_shape_size == 3 :
3665+ return tf .reduce_sum (tf .cast (tf .reduce_any (tf .not_equal (data , pad_val ), axis = 2 ), dtype = tf .int32 ), 1 )
3666+ elif data_shape_size == 2 :
3667+ return tf .reduce_sum (tf .cast (tf .not_equal (data , pad_val ), dtype = tf .int32 ), 1 )
3668+ elif data_shape_size == 1 :
3669+ raise ValueError ("retrieve_seq_length_op3: data has wrong shape!" )
3670+ else :
3671+ raise ValueError ("retrieve_seq_length_op3: handling data_shape_size %s hasn't been implemented!" % (data_shape_size ))
3672+
3673+
3674+ def target_mask_op (data , pad_val = 0 ):
3675+ data_shape_size = data .get_shape ().ndims
3676+ if data_shape_size == 3 :
3677+ return tf .cast (tf .reduce_any (tf .not_equal (data , pad_val ), axis = 2 ), dtype = tf .int32 )
3678+ elif data_shape_size == 2 :
3679+ return tf .cast (tf .not_equal (data , pad_val ), dtype = tf .int32 )
3680+ elif data_shape_size == 1 :
3681+ raise ValueError ("target_mask_op: data has wrong shape!" )
3682+ else :
3683+ raise ValueError ("target_mask_op: handling data_shape_size %s hasn't been implemented!" % (data_shape_size ))
3684+
3685+
36603686# Dynamic RNN
36613687class DynamicRNNLayer (Layer ):
36623688 """
@@ -3762,6 +3788,7 @@ def __init__(
37623788 n_layer = 1 ,
37633789 return_last = False ,
37643790 return_seq_2d = False ,
3791+ dynamic_rnn_init_args = {},
37653792 name = 'dyrnn_layer' ,
37663793 ):
37673794 Layer .__init__ (self , name = name )
@@ -3795,7 +3822,8 @@ def __init__(
37953822 self .batch_size = batch_size
37963823
37973824 # Creats the cell function
3798- self .cell = cell_fn (num_units = n_hidden , ** cell_init_args )
3825+ cell_instance_fn = lambda : cell_fn (num_units = n_hidden , ** cell_init_args )
3826+ # self.cell = cell_fn(num_units=n_hidden, **cell_init_args)
37993827
38003828 # Apply dropout
38013829 if dropout :
@@ -3812,22 +3840,31 @@ def __init__(
38123840 except :
38133841 DropoutWrapper_fn = tf .nn .rnn_cell .DropoutWrapper
38143842
3815- self .cell = DropoutWrapper_fn (
3816- self .cell ,
3817- input_keep_prob = in_keep_prob ,
3818- output_keep_prob = out_keep_prob )
3843+ cell_instance_fn1 = cell_instance_fn
3844+ cell_instance_fn = DropoutWrapper_fn (
3845+ cell_instance_fn1 (),
3846+ input_keep_prob = in_keep_prob ,
3847+ output_keep_prob = out_keep_prob )
3848+ # self.cell = DropoutWrapper_fn(
3849+ # self.cell,
3850+ # input_keep_prob=in_keep_prob,
3851+ # output_keep_prob=out_keep_prob)
38193852 # Apply multiple layers
38203853 if n_layer > 1 :
38213854 try :
38223855 MultiRNNCell_fn = tf .contrib .rnn .MultiRNNCell
38233856 except :
38243857 MultiRNNCell_fn = tf .nn .rnn_cell .MultiRNNCell
38253858
3859+ cell_instance_fn2 = cell_instance_fn
38263860 try :
3827- self .cell = MultiRNNCell_fn ([self .cell ] * n_layer , state_is_tuple = True )
3861+ cell_instance_fn = lambda : MultiRNNCell_fn ([cell_instance_fn2 () for _ in range (n_layer )], state_is_tuple = True )
3862+ # self.cell = MultiRNNCell_fn([self.cell] * n_layer, state_is_tuple=True)
38283863 except :
3829- self .cell = MultiRNNCell_fn ([self .cell ] * n_layer )
3864+ cell_instance_fn = lambda : MultiRNNCell_fn ([cell_instance_fn2 () for _ in range (n_layer )])
3865+ # self.cell = MultiRNNCell_fn([self.cell] * n_layer)
38303866
3867+ self .cell = cell_instance_fn ()
38313868 # Initialize initial_state
38323869 if initial_state is None :
38333870 self .initial_state = self .cell .zero_state (batch_size , dtype = tf .float32 )#dtype="float")
@@ -3852,6 +3889,7 @@ def __init__(
38523889 # dtype=tf.float64,
38533890 sequence_length = sequence_length ,
38543891 initial_state = self .initial_state ,
3892+ ** dynamic_rnn_init_args
38553893 )
38563894 rnn_variables = tf .get_collection (TF_GRAPHKEYS_VARIABLES , scope = vs .name )
38573895
@@ -3986,6 +4024,7 @@ def __init__(
39864024 n_layer = 1 ,
39874025 return_last = False ,
39884026 return_seq_2d = False ,
4027+ dynamic_rnn_init_args = {},
39894028 name = 'bi_dyrnn_layer' ,
39904029 ):
39914030 Layer .__init__ (self , name = name )
@@ -4020,8 +4059,9 @@ def __init__(
40204059
40214060 with tf .variable_scope (name , initializer = initializer ) as vs :
40224061 # Creats the cell function
4023- self .fw_cell = cell_fn (num_units = n_hidden , ** cell_init_args )
4024- self .bw_cell = cell_fn (num_units = n_hidden , ** cell_init_args )
4062+ cell_instance_fn = lambda : cell_fn (num_units = n_hidden , ** cell_init_args )
4063+ # self.fw_cell = cell_fn(num_units=n_hidden, **cell_init_args)
4064+ # self.bw_cell = cell_fn(num_units=n_hidden, **cell_init_args)
40254065
40264066 # Apply dropout
40274067 if dropout :
@@ -4038,23 +4078,33 @@ def __init__(
40384078 except :
40394079 DropoutWrapper_fn = tf .nn .rnn_cell .DropoutWrapper
40404080
4041- self .fw_cell = DropoutWrapper_fn (
4042- self .fw_cell ,
4043- input_keep_prob = in_keep_prob ,
4044- output_keep_prob = out_keep_prob )
4045- self .bw_cell = DropoutWrapper_fn (
4046- self .bw_cell ,
4047- input_keep_prob = in_keep_prob ,
4048- output_keep_prob = out_keep_prob )
4081+ cell_instance_fn1 = cell_instance_fn
4082+ cell_instance_fn = lambda : DropoutWrapper_fn (
4083+ cell_instance_fn1 (),
4084+ input_keep_prob = in_keep_prob ,
4085+ output_keep_prob = out_keep_prob )
4086+
4087+ # self.fw_cell = DropoutWrapper_fn(
4088+ # self.fw_cell,
4089+ # input_keep_prob=in_keep_prob,
4090+ # output_keep_prob=out_keep_prob)
4091+ # self.bw_cell = DropoutWrapper_fn(
4092+ # self.bw_cell,
4093+ # input_keep_prob=in_keep_prob,
4094+ # output_keep_prob=out_keep_prob)
40494095 # Apply multiple layers
40504096 if n_layer > 1 :
40514097 try :
40524098 MultiRNNCell_fn = tf .contrib .rnn .MultiRNNCell
40534099 except :
40544100 MultiRNNCell_fn = tf .nn .rnn_cell .MultiRNNCell
40554101
4056- self .fw_cell = MultiRNNCell_fn ([self .fw_cell ] * n_layer )
4057- self .bw_cell = MultiRNNCell_fn ([self .bw_cell ] * n_layer )
4102+ cell_instance_fn2 = cell_instance_fn
4103+ cell_instance_fn = lambda : MultiRNNCell_fn ([cell_instance_fn2 () for _ in range (n_layer )])
4104+ # self.fw_cell = MultiRNNCell_fn([self.fw_cell] * n_layer)
4105+ # self.bw_cell = MultiRNNCell_fn([self.bw_cell] * n_layer)
4106+ self .fw_cell = cell_instance_fn ()
4107+ self .bw_cell = cell_instance_fn ()
40584108 # Initial state of RNN
40594109 if fw_initial_state is None :
40604110 self .fw_initial_state = self .fw_cell .zero_state (self .batch_size , dtype = tf .float32 )
@@ -4080,6 +4130,7 @@ def __init__(
40804130 sequence_length = sequence_length ,
40814131 initial_state_fw = self .fw_initial_state ,
40824132 initial_state_bw = self .bw_initial_state ,
4133+ ** dynamic_rnn_init_args
40834134 )
40844135 rnn_variables = tf .get_collection (TF_GRAPHKEYS_VARIABLES , scope = vs .name )
40854136
0 commit comments