How to use the param of 'weights' of tensorflow function tf.contrib.legacy_seq2seq.sequence_loss_by_example?

Linyu

The code:

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))

And the result is:

[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 ]

I don't understand why the result is the same no matter if the param average_across_timesteps is set as 'True' or 'False'.

Maxim

Here's the source code that performs the averaging:

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

In your case, the weights is a list of one tensor, either w_1 or w_2, i.e., you have one time step. In both cases, tf.add_n(weights) doesn't change it, because it's a sum of one element (not the sum of elements in w_1 or w_2).

This explains the result: D and D_1 are evaluated to the same arrays, because D_1 = D * w_1 (element-wise). D_2 and D_3 are different because w_2 contains not only ones.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Trainable weight for TensorFlow sequence_loss_by_example()

From Dev

How to update tensorflow to support tf.contrib?

From Dev

How to update tensorflow to support tf.contrib?

From Dev

How tensorflow tf.contrib.learn.SVM reload trained model and use predict to classify new data

From Dev

How to use event-by-event weights in Tensorflow?

From Dev

Tensorflow: how to use pretrained weights in new graph?

From Dev

How to use tf.contrib.model_pruning on MNIST?

From Dev

What is the purpose of the tf.contrib module in Tensorflow?

From Dev

What is the purpose of the tf.contrib module in Tensorflow?

From Dev

How to predict a simple sequence using seq2seq from tensorflow?

From Dev

How to predict a simple sequence using seq2seq from tensorflow?

From Dev

How to use tf.while_loop() in tensorflow

From Dev

How to use tf.datasets with iterator in Tensorflow

From Dev

TensorFlow - tf.layers vs tf.contrib.layers

From Dev

TensorFlow: How Can I get the total_cm in tf.contrib.metrics.streaming_mean_iou

From Java

TensorFlow - regularization with L2 loss, how to apply to all weights, not just last one?

From Dev

How to use tensorflow debugging tool tfdbg on tf.estimator in Tensorflow?

From Dev

Use "tf.contrib.factorization.KMeansClustering"

From Dev

Seq2Seq use of buckets in TensorFlow tutorial

From Dev

What is the purpose of weights and biases in tensorflow word2vec example?

From Dev

Tensorflow: Incompatible types for dataset.map() in tf.contrib.data

From Dev

Tensorflow: Use tf.parse_example for jpeg batches

From Dev

How to use tf.nn.embedding_lookup_sparse in TensorFlow?

From Dev

In Tensorflow, how to use tf.gather() for the last dimension?

From Dev

Tensorflow tf.function conditionals

From Dev

The use of Seq function in Clojure

From Dev

The use of Seq function in Clojure

From Dev

How to modify the seq2seq cost function for padded vectors?

From Dev

What is the intended use for tf.contrib.framework functions?

Related Related

  1. 1

    Trainable weight for TensorFlow sequence_loss_by_example()

  2. 2

    How to update tensorflow to support tf.contrib?

  3. 3

    How to update tensorflow to support tf.contrib?

  4. 4

    How tensorflow tf.contrib.learn.SVM reload trained model and use predict to classify new data

  5. 5

    How to use event-by-event weights in Tensorflow?

  6. 6

    Tensorflow: how to use pretrained weights in new graph?

  7. 7

    How to use tf.contrib.model_pruning on MNIST?

  8. 8

    What is the purpose of the tf.contrib module in Tensorflow?

  9. 9

    What is the purpose of the tf.contrib module in Tensorflow?

  10. 10

    How to predict a simple sequence using seq2seq from tensorflow?

  11. 11

    How to predict a simple sequence using seq2seq from tensorflow?

  12. 12

    How to use tf.while_loop() in tensorflow

  13. 13

    How to use tf.datasets with iterator in Tensorflow

  14. 14

    TensorFlow - tf.layers vs tf.contrib.layers

  15. 15

    TensorFlow: How Can I get the total_cm in tf.contrib.metrics.streaming_mean_iou

  16. 16

    TensorFlow - regularization with L2 loss, how to apply to all weights, not just last one?

  17. 17

    How to use tensorflow debugging tool tfdbg on tf.estimator in Tensorflow?

  18. 18

    Use "tf.contrib.factorization.KMeansClustering"

  19. 19

    Seq2Seq use of buckets in TensorFlow tutorial

  20. 20

    What is the purpose of weights and biases in tensorflow word2vec example?

  21. 21

    Tensorflow: Incompatible types for dataset.map() in tf.contrib.data

  22. 22

    Tensorflow: Use tf.parse_example for jpeg batches

  23. 23

    How to use tf.nn.embedding_lookup_sparse in TensorFlow?

  24. 24

    In Tensorflow, how to use tf.gather() for the last dimension?

  25. 25

    Tensorflow tf.function conditionals

  26. 26

    The use of Seq function in Clojure

  27. 27

    The use of Seq function in Clojure

  28. 28

    How to modify the seq2seq cost function for padded vectors?

  29. 29

    What is the intended use for tf.contrib.framework functions?

HotTag

Archive