Tensorflow:动态进行字母预测
Tensorflow: Dynamically Make Letter Prediction
我正在尝试使用 Tensorflow 中的 LSTM 模块将时间 (t-1) 的单热字母预测用作时间 (t) 的下一个状态的输入。我正在做一些事情:
one_hot_dictionary = {0:np.array([1.,0.,0.]),1:np.array([0.,1.,0.]),\
2:np.array([0.,0.,1.])}
state = init_state
for time in xrange(sequence_length):
#run the cell
output, state = rnn_cell.cell(input,state)
#transform the output so they are of the one-hot letter dimension
transformed_val = tf.nn.xw_plus_b(output, W_o, b_o)
#take the softmax to normalize
softmax_val = tf.nn.softmax(transformed_val)
#then get the argmax of these to know what the predicted letter is
argmax_val = tf.argmax(softmax_val,1)
#finally, turn these back into one-hots with a number to numpy
# array dictionary
input = [one_hot_dictionary[argmax_val[i]] for i in xrange(batch_size)]
但是,我收到错误消息:
input = [one_hot_dictionary[argmax_val[i]] for i in xrange(batch_size)]
KeyError: <tensorflow.python.framework.ops.Tensor object at 0x7f772991ce50>
有什么方法可以使用我的字典从 argmax 值到单热字母编码动态创建这些单热字母?
您可以通过多种方式实现这一目标。
对您的代码最直接的改编是对单位矩阵中的 select 行使用 tf.gather()
操作,如下所示:
# Build the matrix [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]].
identity_matrix = tf.diag(tf.ones([3]))
for ...:
# Compute a vector of predicted letter indices.
argmax_val = ...
# For each element of `argmax_val`, select the corresponding row
# from `identity_matrix` and concatenate them into matrix.
input = tf.gather(identity_matrix, argmax_val)
对于您展示的只有 3 个不同字母的情况,性能可能并不重要。但是,如果字母的数量(因此 identity_matrix
的大小)比批大小大得多,您可以通过构建 tf.SparseTensor
and using the tf.sparse_tensor_to_dense()
op 来构建 input
.
我正在尝试使用 Tensorflow 中的 LSTM 模块将时间 (t-1) 的单热字母预测用作时间 (t) 的下一个状态的输入。我正在做一些事情:
one_hot_dictionary = {0:np.array([1.,0.,0.]),1:np.array([0.,1.,0.]),\
2:np.array([0.,0.,1.])}
state = init_state
for time in xrange(sequence_length):
#run the cell
output, state = rnn_cell.cell(input,state)
#transform the output so they are of the one-hot letter dimension
transformed_val = tf.nn.xw_plus_b(output, W_o, b_o)
#take the softmax to normalize
softmax_val = tf.nn.softmax(transformed_val)
#then get the argmax of these to know what the predicted letter is
argmax_val = tf.argmax(softmax_val,1)
#finally, turn these back into one-hots with a number to numpy
# array dictionary
input = [one_hot_dictionary[argmax_val[i]] for i in xrange(batch_size)]
但是,我收到错误消息:
input = [one_hot_dictionary[argmax_val[i]] for i in xrange(batch_size)]
KeyError: <tensorflow.python.framework.ops.Tensor object at 0x7f772991ce50>
有什么方法可以使用我的字典从 argmax 值到单热字母编码动态创建这些单热字母?
您可以通过多种方式实现这一目标。
对您的代码最直接的改编是对单位矩阵中的 select 行使用 tf.gather()
操作,如下所示:
# Build the matrix [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]].
identity_matrix = tf.diag(tf.ones([3]))
for ...:
# Compute a vector of predicted letter indices.
argmax_val = ...
# For each element of `argmax_val`, select the corresponding row
# from `identity_matrix` and concatenate them into matrix.
input = tf.gather(identity_matrix, argmax_val)
对于您展示的只有 3 个不同字母的情况,性能可能并不重要。但是,如果字母的数量(因此 identity_matrix
的大小)比批大小大得多,您可以通过构建 tf.SparseTensor
and using the tf.sparse_tensor_to_dense()
op 来构建 input
.