python - 如何使用tensorflow函数tf.contrib.legacy_seq2seq.sequence_loss_by_example的 'weights'参数?

标签 python tensorflow machine-learning weighted-average seq2seq

代码:

import tensorflow as tf

A = tf.constant([[0.1,0.2,0.3,0.4],[0.2,0.1,0.4,0.3],[0.4,0.3,0.2,0.1],[0.3,0.2,0.1,0.4],[0.1,0.4,0.3,0.2]], dtype=tf.float32)
B = tf.constant([1, 2, 1, 3, 3], dtype=tf.int32)

w_1 = tf.constant(value=[1,1,1,1,1], dtype=tf.float32)
w_2 = tf.constant(value=[1,2,3,4,5], dtype=tf.float32)

D   = tf.contrib.legacy_seq2seq.sequence_loss_by_example([A], [B], [w_1])
D_1 = tf.contrib.legacy_seq2seq.sequence_loss_by_example([A], [B], [w_1], average_across_timesteps=False)
D_2 = tf.contrib.legacy_seq2seq.sequence_loss_by_example([A], [B], [w_2])
D_3 = tf.contrib.legacy_seq2seq.sequence_loss_by_example([A], [B], [w_2], average_across_timesteps=False)

with tf.Session() as sess:
  print(sess.run(D))
  print(sess.run(D_1))
  print(sess.run(D_2))
  print(sess.run(D_3))

结果是:

[1.4425355 1.2425355 1.3425356 1.2425356 1.4425356]

[1.4425355 1.2425355 1.3425356 1.2425356 1.4425356]

[1.4425355 1.2425355 1.3425356 1.2425356 1.4425356]

[1.4425355 2.485071  4.027607  4.9701424 7.212678 ]

我不明白为什么无论参数average_across_timesteps设置为“True”或“False”结果都是相同的。

最佳答案

这是执行平均的源代码:

if average_across_timesteps:
  total_size = math_ops.add_n(weights)
  total_size += 1e-12  # Just to avoid division by 0 for all-0 weights.
  log_perps /= total_size

在您的情况下,权重一个张量的列表,可以是w_1w_2,即,您只有一个时间步长。在这两种情况下,tf.add_n(weights) 都不会更改它,因为它是一个元素的总和(而不是 w_1w_2 中元素的总和)。

这解释了结果:DD_1 被评估为相同的数组,因为 D_1 = D * w_1 (按元素) 。 D_2D_3 不同,因为 w_2 不仅包含 。

关于python - 如何使用tensorflow函数tf.contrib.legacy_seq2seq.sequence_loss_by_example的 'weights'参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49921839/

相关文章:

python - Python中使用列表列表进行冒泡排序的顺序不正确

python - 从终端运行 python 包

javascript - tensorflowjs 加载重新训练的 coco-ssd 模型 - 在浏览器中不起作用

tensorflow - TFLiteConverter.from_saved_model 运行时错误 : MetaGraphDef associated with tags {'serve' } could not be found in SavedModel

Azure ML Studio ML 管道 - 异常 : No temp file found

machine-learning - 准备训练数据的通用框架?

python - 使用 Pika 客户端轮询 RabbitMQ 消息

python - 如何在 Mac OS X 中不打开任何终端窗口的情况下运行 Python 3 tkinter 应用程序?

python - 在 Tensorflow 中执行特征选择

python - 多变量/特征 Tensorflow