python - I'm trying to make my own RNN cell in tensorflow but it doesn't work -
my code follows.
import numpy np import tensorflow tf import matplotlib.pyplot plt tensorflow.python.ops.rnn_cell_impl import _zero_state_tensors class caprnncell(tf.contrib.rnn.rnncell): def __init__(self, input_dim ): self.input_dim = input_dim @property def state_size(self): return 1 @property def output_size(self): return 1 def call(self, inputs, state): w=weight_variable([self.input_dim , 1]) b=bias_variable([1]) output =state*tf.nn.sigmoid(tf.matmul(inputs,w)+b) shape of output = [batch_size , 1] return output, output
def caprnnmodel(timeseries_before_forgetting_gate , init_cap): cell = caprnncell(input_dim=3) cap_series, final_cap = tf.nn.dynamic_rnn(cell=cell , inputs=timeseries_before_forgetting_gate, initial_state=init_cap) return cap_series , final_cap timeseries_before_forgetting_gate :
shape = [batch_size , truncated_length , self.cell_state_dim] init_cap : shape = [batch_size , 1] cap_series : shape=[batch_size , turncated_length , 1] final_cap : shape=[batch_size , 1] x_place=tf.placeholder(tf.float32 , [1,2,3]) init_cap_place=tf.placeholder(tf.float32 , [1,1] ) y=caprnnmodel(x_place,init_cap_place) tf.session() sess: sess.run(tf.initialize_all_variables()) a=np.random.rand(1,2,3) b=np.random.rand(1,1) result=sess.run(y,feed_dict={x_place:a , init_cap_place:b}) print(result) i'm trying make own rnn cell , apply tf.nn.dynamic_rnn. made own cell class( subclass of tf.contrib.rnn.rnncell) , did simple forward calculation test on it. doesn't work error follows
traceback (most recent call last): file "d:/mydocuments/pycharmprojects/rnn_tutorial/customizedrnncelltest.py", line 85, in <module> y=caprnnmodel(x_place,init_cap_place) file "d:/mydocuments/pycharmprojects/rnn_tutorial/customizedrnncelltest.py", line 76, in caprnnmodel cap_series, final_cap = tf.nn.dynamic_rnn(cell=cell , inputs=timeseries_before_forgetting_gate, initial_state=init_cap) file "c:\users\minho kim\anaconda3\lib\site-packages\tensorflow\python\ops\rnn.py", line 574, in dynamic_rnn dtype=dtype) file "c:\users\minho kim\anaconda3\lib\site-packages\tensorflow\python\ops\rnn.py", line 737, in _dynamic_rnn_loop swap_memory=swap_memory) file "c:\users\minho kim\anaconda3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2770, in while_loop result = context.buildloop(cond, body, loop_vars, shape_invariants) file "c:\users\minho kim\anaconda3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2599, in buildloop pred, body, original_loop_vars, loop_vars, shape_invariants) file "c:\users\minho kim\anaconda3\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2549, in _buildloop body_result = body(*packed_vars_for_body) file "c:\users\minho kim\anaconda3\lib\site-packages\tensorflow\python\ops\rnn.py", line 722, in _time_step (output, new_state) = call_cell() file "c:\users\minho kim\anaconda3\lib\site-packages\tensorflow\python\ops\rnn.py", line 708, in <lambda> call_cell = lambda: cell(input_t, state) file "c:\users\minho kim\anaconda3\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 180, in __call__ return super(rnncell, self).__call__(inputs, state) file "c:\users\minho kim\anaconda3\lib\site-packages\tensorflow\python\layers\base.py", line 414, in __call__ self._set_scope(kwargs.pop('scope', none)) file "c:\users\minho kim\anaconda3\lib\site-packages\tensorflow\python\layers\base.py", line 335, in _set_scope if self._scope none: attributeerror: 'caprnncell' object has no attribute '_scope' process finished exit code 1 what's wrong?? :(
i suppose w=weight_variable([self.input_dim , 1]) , b=bias_variable([1]) define weights , bias of model. call makes forward pass. in case, trying new set of parameters @ every forward pass. moved variable definitions constructor. here can see running version (i have tensorflow 1.2.1):
import numpy np import tensorflow tf import matplotlib.pyplot plt tensorflow.python.ops.rnn_cell_impl import _zero_state_tensors class caprnncell(tf.contrib.rnn.rnncell): def __init__(self, input_dim): self.input_dim = input_dim self.w = tf.get_variable("w", [self.input_dim , 1], tf.float32) self.b = tf.get_variable("b", [1]) @property def state_size(self): return 1 @property def output_size(self): return 1 def __call__(self, inputs, state): output =state*tf.nn.sigmoid(tf.matmul(inputs, self.w)+ self.b) return output, output def caprnnmodel(timeseries_before_forgetting_gate, init_cap): cap_cell = caprnncell(input_dim=3) cap_series, final_cap = tf.nn.dynamic_rnn(cell=cap_cell, inputs=timeseries_before_forgetting_gate, initial_state=init_cap) return cap_series , final_cap x_place=tf.placeholder(tf.float32 , [1,2,3]) init_cap_place=tf.placeholder(tf.float32 , [1,1]) y=caprnnmodel(x_place, init_cap_place) tf.session() sess: sess.run(tf.initialize_all_variables()) a=np.random.rand(1,2,3) b=np.random.rand(1,1) result=sess.run(y,feed_dict={x_place:a , init_cap_place:b}) print(result)
Comments
Post a Comment