で行う埋め込むん:私はembedding
とembedding_lookup
がここで何をしているかを把握することはできませんptb_word_lm.py何が私がここにtensorflowとRNNを用いた例を読んでいますtensorflow
を。どのようにしてテンソルに別の次元を追加できますか? (20,25)から(20,25,200)になります。この場合(20,25)は25のタイムステップを有する20のバッチサイズである。私は入力データの次元としてセルのhidden_size
を追加する方法/理由を理解できません。典型的には、入力データは、サイズが[batch_size, num_features]
のマトリックスであり、モデルは、[num_features, hidden_dims]
のマトリクスを用いて、[batch-size, hidden-dims]
の出力をもたらし、 --->hidden_dims
となる。では、hidden_dims
はどのように入力テンソルの次元になりますか?
input_data, targets = reader.ptb_producer(train_data, 20, 25)
cell = tf.nn.rnn_cell.BasicLSTMCell(200, forget_bias=1.0, state_is_tuple=True)
initial_state = cell.zero_state(20, tf.float32)
embedding = tf.get_variable("embedding", [10000, 200], dtype=tf.float32)
inputs = tf.nn.embedding_lookup(embedding, input_data)
input_data_train # <tf.Tensor 'PTBProducer/Slice:0' shape=(20, 25) dtype=int32>
inputs # <tf.Tensor 'embedding_lookup:0' shape=(20, 25, 200) dtype=float32>
outputs = []
state = initial_state
for time_step in range(25):
if time_step > 0:
tf.get_variable_scope().reuse_variables()
cell_output, state = cell(inputs[:, time_step, :], state)
outputs.append(cell_output)
output = tf.reshape(tf.concat(1, outputs), [-1, 200])
outputs # list of 20: <tf.Tensor 'BasicLSTMCell/mul_2:0' shape=(20, 200) dtype=float32>
output # <tf.Tensor 'Reshape_2:0' shape=(500, 200) dtype=float32>
softmax_w = tf.get_variable("softmax_w", [config.hidden_size, config.vocab_size], dtype=tf.float32)
softmax_b = tf.get_variable("softmax_b", [config.hidden_size, config.vocab_size], dtype=tf.float32)
logits = tf.matmul(output, softmax_w) + softmax_b
loss = tf.nn.seq2seq.sequence_loss_by_example([logits], [tf.reshape(targets, [-1])],[tf.ones([20*25], dtype=tf.float32)])
cost = tf.reduce_sum(loss)/batch_size