GRU和RNN实现不一致

2024-04-24 10:04:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Tensorflow实现一些定制的GRU单元。我需要堆叠这些单元,我想从^{}继承。然而,{cd2}的一个^{cd2}调用的是一个^{cd2}的源代码,而这一点{cd2}只会调用cd2}的源代码。同时,GRU只创建一个GRUCell。在

对于我试图实现的论文,我实际上需要堆栈GRUCell。为什么RNN和{}的实现不同?在


Tags: 源代码堆栈tensorflow单元rnncd2grugrucell
2条回答

在搜索这些类的文档以添加链接时,我注意到一些可能会让您感到困惑的事情:在TensorFlow中有两个实现(目前,就在正式的tf2.0发布之前)两个GRUCell实现!有一个^{}和一个^{}。似乎来自tf.nn.rnn_cell的那个已经被弃用了,而Keras就是您应该使用的那个。在

据我所知,GRUCell^{}和{a4}具有相同的__call__()方法签名,它们都继承自{}。^{}文档提供了一些关于传递给其cell参数的对象的__call__()方法必须做什么的一些要求,但是我猜想这三个方法都应该满足这些要求。您应该能够使用相同的RNN框架,并向它传递一个GRUCell对象的列表,而不是LSTMCell或{}。在

我现在不能测试这个,所以我不确定您是将一个GRUCell对象的列表,还是仅仅将^{}对象传递到RNN中,但我认为其中一个应该行得通。在

列车图=传递函数图() 有火车吗_graph.as_默认值():

# Initialize input placeholders
input_text = tf.placeholder(tf.int32, [None, None], name='input')
targets = tf.placeholder(tf.int32, [None, None], name='targets')
lr = tf.placeholder(tf.float32, name='learning_rate')

# Calculate text attributes
vocab_size = len(int_to_vocab)
input_text_shape = tf.shape(input_text)

# Build the RNN cell
lstm = tf.contrib.rnn.BasicLSTMCell(num_units=rnn_size)
drop_cell = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)
cell = tf.contrib.rnn.MultiRNNCell([drop_cell] * num_layers)

# Set the initial state
initial_state = cell.zero_state(input_text_shape[0], tf.float32)
initial_state = tf.identity(initial_state, name='initial_state')

# Create word embedding as input to RNN
embed = tf.contrib.layers.embed_sequence(input_text, vocab_size, embed_dim)

# Build RNN
outputs, final_state = tf.nn.dynamic_rnn(cell, embed, dtype=tf.float32)
final_state = tf.identity(final_state, name='final_state')

# Take RNN output and make logits
logits = tf.contrib.layers.fully_connected(outputs, vocab_size, activation_fn=None)

# Calculate the probability of generating each word
probs = tf.nn.softmax(logits, name='probs')

# Define loss function
cost = tf.contrib.seq2seq.sequence_loss(
    logits,
    targets,
    tf.ones([input_text_shape[0], input_text_shape[1]])
)

#学习率优化器 优化器=tf.train.AdamOptimizer公司(学习率)

^{pr2}$

相关问题 更多 >