TensorFlow 的可训练权重 sequence_loss_by_example()
Trainable weight for TensorFlow sequence_loss_by_example()
我想要 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
中此函数的文档字符串:
weights: list of 1D batch-sized float-Tensors of the same length as logits.
它需要一个 "Tensor",但我想传递一个 tf.Variable
。有没有办法在这个函数中有可训练的权重?
在 TensorFlow 中,需要 tf.Variable
can be used anywhere a 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)
我想要 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
中此函数的文档字符串:
weights: list of 1D batch-sized float-Tensors of the same length as logits.
它需要一个 "Tensor",但我想传递一个 tf.Variable
。有没有办法在这个函数中有可训练的权重?
在 TensorFlow 中,需要 tf.Variable
can be used anywhere a 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)