我希望在seq2seq.sequence_loss_by_example()
中有一个可训练的重量,例如
w = tf.get_variable("w", [batch_size*num_steps])
loss = seq2seq.sequence_loss_by_example([logits_1],
[tf.reshape(self._targets, [-1])],
w,vocab_size_all)
但是,运行此代码会出现以下错误:
seq2seq.py, line 654, in sequence_loss_by_example
if len(targets) != len(logits) or len(weights) != len(logits):
根据seq2seq.py
中此函数的docstring:
weights: list of 1D batch-sized float-Tensors of the same length as logits.
它需要" Tensor",但我想传递tf.Variable
。有没有办法在这个功能中有可训练的权重?
答案 0 :(得分:2)
在TensorFlow中,tf.Variable
可以在tf.Tensor
(具有相同元素类型和形状)的任何地方使用。
因此,您要定义一个可训练的权重,您可以将tf.Variable
个对象列表作为weights
参数传递给seq2seq.sequence_loss_by_example()
。例如,您可以执行以下操作:
# Defines a list of `num_steps` variables, each 1-D with length `batch_size`.
weights = [tf.get_variable("w", [batch_size]) for _ in range(num_steps)]
loss = seq2seq.sequence_loss_by_example([logits_1, ..., logits_n],
[targets_1, ..., targets_n],
weights,
vocab_size_all)