如何在张量流中从训练有素的Char-RNN网络中对字符序列进行采样?

时间:2017-04-12 04:52:02

标签: python tensorflow

我一直在尝试训练角色级RNN。

成功训练模型后。如何使用此模型生成新的数据样本?

以下是我正在训练的模型。

lr = 0.0001
n_epoch = 20
batch_size = 1

nSteps = 5
nInputs = len(unique_char)
nHiddenUnits1 = 100
nOutputs = len(unique_char)

x = tf.placeholder(tf.float32,[None,nSteps,nInputs])
y = tf.placeholder(tf.float32,[None,nSteps,nOutputs])


weights = {
# (nInputs, nHiddenUnit1)
'in': tf.Variable(tf.random_normal([nInputs, nHiddenUnits1])),
# (nHiddenUnits1, nOutputs)
'out': tf.Variable(tf.random_normal([nHiddenUnits1, nOutputs]))
}
biases = {
# (nHiddenUnits1, )
'in': tf.Variable(tf.constant(0.1, shape=[nHiddenUnits1, ])),
# (nOutputs, )
'out': tf.Variable(tf.constant(0.1, shape=[nOutputs, ]))
}


def network(X,weights, biases):

    X = tf.reshape(X,[-1,nInputs])
    X_in = tf.add(tf.matmul(X,weights['in']),biases['in'])
    X_in = tf.reshape(X_in, [-1, nSteps, nHiddenUnits1])

    lstmCell1 = tf.contrib.rnn.BasicLSTMCell(nHiddenUnits1)
i   initState = lstmCell1.zero_state(batch_size,tf.float32)

    outputs, final_state = tf.nn.dynamic_rnn(lstmCell1, X_in, 
 initial_state=initState, time_major=False)

    outputs = tf.reshape(outputs,[-1, nHiddenUnits1])


    #outputs = tf.unstack(tf.transpose(outputs,[1,0,2]))
    results = tf.matmul(outputs, weights['out']) + biases['out']
    results = tf.reshape(results,[-1,nSteps,nOutputs])

    return results

0 个答案:

没有答案