bidirectional_rnn์ ์ด๊ธฐํํ๋ ค๊ณ ํ ๋ LSTM ์
์ด ์๋ tensorflow๋ก bi-rnn ๋ชจ๋ธ์ ๋ง๋ค๊ณ ์ถ์ต๋๋ค.
๊ทธ๊ฒ์ ์ ๊ณตํฉ๋๋ค: ValueError: Over-sharing: Variable BiRNN_FW/RNN/BasicLSTMCell/Linear/Matrix already exists, disallowed. Did you mean to set reuse=True in VarScope?
import tensorflow as tf
from tensorflow.models.rnn import rnn, rnn_cell
from tensorflow.python.ops.constant_op import constant
import numpy as np
class Model(object):
def __init__(self, batch_size, len_word, num_chars, dim_embed, dim_hidden):
self.batch_size = batch_size
self.dim_embed = dim_embed
self.dim_hidden = dim_hidden
self.num_chars = num_chars
self.len_word = len_word
with tf.device("/cpu:0"):
self.embedding = tf.Variable(tf.random_uniform([num_chars, dim_embed], -0.1, 0.1), name='embedding')
self.W_emb = tf.Variable(tf.random_uniform([dim_hidden*2, dim_embed], -0.1, 0.1), name='W_emb')
self.b_emb = tf.Variable(tf.zeros([dim_embed]), name='b_emb')
self.lstm_fw_cell = rnn_cell.BasicLSTMCell(dim_hidden)
self.lstm_bw_cell = rnn_cell.BasicLSTMCell(dim_hidden)
def build_model(self):
inputs = tf.placeholder(tf.int32, [self.batch_size, self.len_word])
input_length = tf.placeholder(tf.int64, [self.batch_size])
lstm_state_fw = self.lstm_fw_cell.zero_state(self.batch_size, tf.float32)
lstm_state_bw = self.lstm_bw_cell.zero_state(self.batch_size, tf.float32)
with tf.device("/cpu:0"):
embedded_input = tf.nn.embedding_lookup(self.embedding, tf.transpose(inputs))
brnn_output = rnn.bidirectional_rnn(
self.lstm_fw_cell, self.lstm_bw_cell,
tf.unpack(embedded_input),
sequence_length=input_length,
initial_state_fw=lstm_state_fw,
initial_state_bw=lstm_state_bw,
)
pooled_output = tf.reduce_sum( tf.pack(brnn_output), 0 )
pooled_output = pooled_output / tf.expand_dims( tf.to_float(input_length) + 1e-6, 1)
final_emb = tf.nn.xw_plus_b(pooled_output, self.W_emb, self.b_emb)
final_emb = tf.nn.l2_normalize(final_emb, dim=1, epsilon=1e-7)
return final_emb
LSTM ์ ์ ๋ํด ๋ค๋ฅธ ๋ณ์ ๋ฒ์๋ฅผ ์ง์ ํด์ผ ํฉ๋๋ค.
with tf.variable_scope('forward'):
self.lstm_fw_cell = rnn_cell.BasicLSTMCell(dim_hidden)
with tf.variable_scope('backward'):
self.lstm_bw_cell = rnn_cell.BasicLSTMCell(dim_hidden)
๊ทธ๋ ์ง ์์ผ๋ฉด ์ด๋ฆ ์ถฉ๋(๋ ์
๋ชจ๋ "BiRNN_FW/RNN/BasicLSTMCell/Linear/Matrix" ์ด๋ฆ์ ์ฌ์ฉํ๋ ค๊ณ ์๋ํจ)์ด ๋ฐ์ํ๊ณ tf๋ ์ด๋ฅผ ๋ ์
์ ๋งค๊ฐ๋ณ์๋ฅผ ๊ณต์ ํ๋ ค๋ ์๋์ธ ๊ฒ์ฒ๋ผ ํด์ํฉ๋๋ค. ๋น์ ์ด ์ํ๋. ๋ ๋ฒ์งธ ๋ฒ์ with variable_scope(name, reuse=True)
์์ ๋ณ์๋ฅผ ์ฌ์ฌ์ฉํ๋๋ก ๋ช
์์ ์ผ๋ก ์ง์ํ์ง ์์๊ธฐ ๋๋ฌธ์ TF๋ ์์ธ๋ฅผ throwํฉ๋๋ค.
์์ ๊ฐ์ด ๋ณ์ ๋ฒ์๋ฅผ ์ค์ ํ๋ฉด ๋ณ์์ ๋ํ ๊ณ ์ ํ ์ด๋ฆ์ด ์์ฑ๋ฉ๋๋ค.
BiRNN_FW/RNN/BasicLSTMCell/ ์ ๋ฐฉํฅ /์ ํ/ํ๋ ฌ
BiRNN_FW/RNN/BasicLSTMCell/ ์ญ๋ฐฉํฅ /์ ํ/ํ๋ ฌ
์์ธํ ๋ด์ฉ์ ๊ณต์ ๋ณ์ ๊ฐ์ด๋๋ฅผ ์ฝ์ด๋ณด์ธ์.
๋ณ์๊ฐ ...Cell.__call__
๋ด๋ถ๊ฐ ์๋๋ผ ...Cell.__init__
๋ด๋ถ์ ์์ฑ๋๊ธฐ ๋๋ฌธ์ ์ด๊ฒ์ ๋ ์ด์ ๋ฐ์ํ์ง ์์ต๋๋ค. ๋ฐ๋ผ์ ์
๊ตฌ์ฑ์ ์ํ ๋ฒ์๊ฐ ํ์ํ์ง ์์ผ๋ฉฐ ๋ณ์ ๋ฒ์๋ฅผ ์ฒ๋ฆฌํฉ๋๋ค. ์์ฒด bidirectional_rnn
์์ผ๋ฏ๋ก ์ง์ ๋ฒ์๋ฅผ ์ง์ ํ ํ์๊ฐ ์์ต๋๋ค.
์ฌ์ ํ ๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ์ด๋ค ์ ์? ๋๋ salomons๊ฐ ์ ์ํ ์ ๊ทผ ๋ฐฉ์์ ์๋ํ์ง๋ง ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์์ต๋๋ค. ๊ฒฐ๊ณผ์ ์ผ๋ก ํํ์ ๋ฐํํ์ง ์์ต๋๋ค.
((encoder_fw_outputs,
์ธ์ฝ๋_bw_์ถ๋ ฅ),
(encoder_fw_final_state,
์ธ์ฝ๋_bw_final_state)) = (
tf.nn.bidirectional_dynamic_rnn(cell_fw=encoder_cell,
cell_bw=encoder_cell,
์
๋ ฅ=encoder_inputs_embedded,
sequence_length=encoder_inputs_length,
dtype=tf.float64, time_major=True)
)
ValueError Traceback(๊ฐ์ฅ ์ต๊ทผ ํธ์ถ ๋ง์ง๋ง)
20๊ฐ์ ์
๋ ฅ=encoder_inputs_embedded,
21 ์ํ์ค_๊ธธ์ด=์ธ์ฝ๋_์
๋ ฅ_๊ธธ์ด,
---> 22 dtype=tf.float32, time_major=True)
23)
24
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/rnn.pyc in bidirectional_dynamic_rnn(cell_fw, cell_bw, ์
๋ ฅ, sequence_length, initial_state_fw, initial_state_bw, dtype, parallel_memorys , time_major, ๋ฒ์)
348
349ํ
--> 350 time_major=time_major, ๋ฒ์=fw_scope)
351
352 # ์ญ๋ฐฉํฅ
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/rnn.pyc in dynamic_rnn(cell, input, sequence_length, initial_state, dtype, parallel_iterations, swap_memory, time_major, scope )
544
545
--> 546 dtype=dtype)
547
548 # _dynamic_rnn_loop์ ์ถ๋ ฅ์ ํญ์ [์๊ฐ, ๋ฐฐ์น, ๊น์ด] ๋ชจ์์
๋๋ค.
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/rnn.pyc in _dynamic_rnn_loop(์
, ์
๋ ฅ, ์ด๊ธฐ ์ํ, ๋ณ๋ ฌ ๋ฐ๋ณต, ์ค์ ๋ฉ๋ชจ๋ฆฌ, ์ํ์ค ๊ธธ์ด, dtype)
711
712
--> 713 swap_memory=swap_memory)
714
715 # ์ถ๋ ฅ ํํ์ ์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ ์ต์ข
์ถ๋ ฅ์ ์์ถ์ ํ๋๋ค.
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.pyc in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, ์ด๋ฆ)
2603 ์ปจํ
์คํธ = whileContext(๋ณ๋ ฌ_๋ฐ๋ณต, back_prop, swap_memory, ์ด๋ฆ)
2604ํ
-> 2605 ๊ฒฐ๊ณผ = context.BuildLoop(cond, body, loop_vars, shape_invariants)
2606 ๋ฐํ ๊ฒฐ๊ณผ
2607
BuildLoop์ /home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.pyc(self, pred, body, loop_vars, shape_invariants)
2436 ์๊ธฐ.Enter()
2437
-> 2438 pred, body, original_loop_vars, loop_vars, shape_invariants)
2439 ๋๋์ด:
2440 ์๊ธฐ.์ข
๋ฃ()
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.pyc in _BuildLoop(self, pred, body, original_loop_vars, loop_vars, shape_invariants)
2386 ๊ตฌ์กฐ=์๋_๋ฃจํ_vars,
2387
-> 2388 body_result = body(*packed_vars_for_body)
2389 if not nest.is_sequence(body_result):
2390 ๅฏถๅบฆ =
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/rnn.pyc _time_step(์๊ฐ, output_ta_t, ์ํ)
694ํ
695ํ
--> 696 skip_conditionals=์ฐธ)
697 ๊ธฐํ:
698 (์ถ๋ ฅ, new_state) = call_cell()
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/rnn.pyc in _rnn_step(time, sequence_length, min_sequence_length, max_sequence_length, zero_output, state, call_cell, state_size, skip_conditionals )
175 # ๋จ๊ณ. max_seq_len์ด ์ธ๋กค ์์ ๊ฐ์ ๋ ๋ ๋น ๋ฆ
๋๋ค.
176 # (dynamic_rnn์ ์ผ๋ฐ์ ์).
--> 177 new_output, new_state = call_cell()
178ํ
179ํ
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/rnn.pyc ์
๋ ฅ
682
683 ๅฏถๅบฆๅ = ๅฏถๅบฆๅ
--> 684 call_cell = ๋๋ค: ์
(์
๋ ฅ_t, ์ํ)
685
686 sequence_length๊ฐ None์ด ์๋ ๊ฒฝ์ฐ:
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.pyc __call__(์์ฒด, ์
๋ ฅ, ์ํ, ๋ฒ์)
336 # i = input_gate, j = new_input, f = forget_gate, o = output_gate
337
--> 338 ๋ฒ์=๋ฒ์)
339ํ
340
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.pyc in _linear(args, output_size, bias, bias_start, scope)
745 vs.variable_scope(scope)๋ฅผ outer_scope๋ก ์ฌ์ฉ:
746 ๊ฐ์ค = vs.get_variable(
--> 747 "๊ฐ์ค์น", [์ด ์ธ์_ํฌ๊ธฐ, ์ถ๋ ฅ_ํฌ๊ธฐ], dtype=dtype)
748 ๋ง์ฝ len(args) == 1:
749 = ็ฒๅ
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(name, shape, dtype, initializer, regularizer, trainingable, ์ปฌ๋ ์
, caching_device, ํํฐ์
๋ , validate_shape, custom_getter)
986 ์ปฌ๋ ์
=์ปฌ๋ ์
, caching_device=caching_device,
987*
--> 988 custom_getter=custom_getter)
989ํ
990 """%s
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(self, var_store, name, shape, dtype, initializer, regularizer, trainingable, ์ปฌ๋ ์
, caching_device, ํํฐ์
๋, validate_shape, custom_getter)
888 ์ปฌ๋ ์
=์ปฌ๋ ์
, caching_device=caching_device,
889 ํํฐ์
๋=ํํฐ์
๋, validate_shape=validate_shape,
--> 890 custom_getter=custom_getter)
891
892ํ
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(self, name, shape, dtype, initializer, regularizer, ์ฌ์ฌ์ฉ, ํ๋ จ ๊ฐ๋ฅ, ์ปฌ๋ ์
, caching_device, ํํฐ์
๋, validate_shape, custom_getter)
346ํ
347 caching_device=caching_device, ํํฐ์
๋=ํํฐ์
๋,
--> 348 validate_shape=validate_shape)
349
350ํ
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in _true_getter(name, shape, dtype, initializer, regularizer, ์ฌ์ฌ์ฉ, ํ๋ จ ๊ฐ๋ฅ, ์ปฌ๋ ์
, caching_device , ํํฐ์
๋, validate_shape)
331 ์ด๋์
๋ผ์ด์ =์ด๋์
๋ผ์ด์ , ๋ ๊ทค๋ฌ๋ผ์ด์ =๋ ๊ทค๋ฌ๋ผ์ด์ , ์ฌ์ฌ์ฉ=์ฌ์ฌ์ฉ,
332ํ
--> 333 caching_device=caching_device, validate_shape=validate_shape)
334
335 custom_getter๊ฐ None์ด ์๋ ๊ฒฝ์ฐ:
/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in _get_single_variable(self, name, shape, dtype, initializer, regularizer, partition_info, ์ฌ์ฌ์ฉ, ํ๋ จ ๊ฐ๋ฅ , ์ปฌ๋ ์
, caching_device, validate_shape)
637 " VarScope ์์ ์ฌ์ฌ์ฉ=True ๋ฅผ ์ค์ ํ๋ ค๋ ๊ฒ๋๊น? "
638 "์๋ ์ ์๋ ์์น:\n\n%s" %(
--> 639 ์ด๋ฆ, "".join(traceback.format_list(tb))))
640ํ
641 if not shape.is_compatible_with(found_var.get_shape()):
ValueError: ๋ณ์ bidirectional_rnn/fw/lstm_cell/weights๊ฐ ์ด๋ฏธ ์กด์ฌํ๋ฉฐ ํ์ฉ๋์ง ์์ต๋๋ค. VarScope์์ ์ฌ์ฌ์ฉ=True๋ฅผ ์ค์ ํ๋ ค๊ณ ํ์ต๋๊น? ์๋ ์ ์:
ํ์ผ "/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py", 747ํ, _linear
"๊ฐ์ค์น", [total_arg_size, output_size], dtype=dtype)
ํ์ผ "/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py", 338ํ, __call__
๋ฒ์=๋ฒ์)
ํ์ผ "
time_major=์ฐธ
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
LSTM ์ ์ ๋ํด ๋ค๋ฅธ ๋ณ์ ๋ฒ์๋ฅผ ์ง์ ํด์ผ ํฉ๋๋ค.
๊ทธ๋ ์ง ์์ผ๋ฉด ์ด๋ฆ ์ถฉ๋(๋ ์ ๋ชจ๋ "BiRNN_FW/RNN/BasicLSTMCell/Linear/Matrix" ์ด๋ฆ์ ์ฌ์ฉํ๋ ค๊ณ ์๋ํจ)์ด ๋ฐ์ํ๊ณ tf๋ ์ด๋ฅผ ๋ ์ ์ ๋งค๊ฐ๋ณ์๋ฅผ ๊ณต์ ํ๋ ค๋ ์๋์ธ ๊ฒ์ฒ๋ผ ํด์ํฉ๋๋ค. ๋น์ ์ด ์ํ๋. ๋ ๋ฒ์งธ ๋ฒ์
with variable_scope(name, reuse=True)
์์ ๋ณ์๋ฅผ ์ฌ์ฌ์ฉํ๋๋ก ๋ช ์์ ์ผ๋ก ์ง์ํ์ง ์์๊ธฐ ๋๋ฌธ์ TF๋ ์์ธ๋ฅผ throwํฉ๋๋ค.์์ ๊ฐ์ด ๋ณ์ ๋ฒ์๋ฅผ ์ค์ ํ๋ฉด ๋ณ์์ ๋ํ ๊ณ ์ ํ ์ด๋ฆ์ด ์์ฑ๋ฉ๋๋ค.
BiRNN_FW/RNN/BasicLSTMCell/ ์ ๋ฐฉํฅ /์ ํ/ํ๋ ฌ
BiRNN_FW/RNN/BasicLSTMCell/ ์ญ๋ฐฉํฅ /์ ํ/ํ๋ ฌ
์์ธํ ๋ด์ฉ์ ๊ณต์ ๋ณ์ ๊ฐ์ด๋๋ฅผ ์ฝ์ด๋ณด์ธ์.