如何使用张量流函数的“权”参数tf.contrib.legacy\ seq2seq公司.sequence\u loss\u by\u示例?

2024-04-26 03:45:16 发布

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

代码:

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 ]

我不明白为什么不管paramaverage_across_timesteps设置为'True'还是'False'结果都是一样的。你知道吗


Tags: runbyexampletflegacycontribsessacross
1条回答
网友
1楼 · 发布于 2024-04-26 03:45:16

下面是执行平均的源代码:

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

在您的例子中,weights是一个一个张量的列表,或者是w_1或者w_2,也就是说,您有一个时间步长。在这两种情况下,tf.add_n(weights)不会改变它,因为它是一个元素的和(不是w_1w_2中元素的和)。你知道吗

这解释了结果:DD_1被计算到相同的数组,因为D_1 = D * w_1(按元素)。D_2D_3是不同的,因为w_2不仅包含它们。你知道吗

相关问题 更多 >