Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tf.transpose error when trying to transpose matrix of bools (workaround below), also recommend adding tf.repeat #542

Closed
hardmaru opened this issue Dec 18, 2015 · 5 comments
Assignees

Comments

@hardmaru
Copy link

I'm trying to implement some conditioning flows when training RNNs. Basically, once an end-of-sequence event has been detected, I reset the RNN state to zeros, and have been able to build in this logic successfully I think with tf.greater() and tf.select()

I implemented a numpy.repeat(a, repeats) sort of equivalent in tensorflow. I recommend in the future, this can be also added as a tf.repeat() function repeating a bunch of boolean values across a tensor for control flows.

Here's my implementation of a repeater (it only works for non-generalised case, for my problem, so I need to generalise it for higher dimensional tensors in the future):

def tfrepeat(a, repeats):
    num_row = a.get_shape()[0].value
    num_col = a.get_shape()[1].value
    assert(num_col == 1)
    result = [a for i in range(repeats)]
    result = tf.concat(0, result)
    result = tf.reshape(result, [repeats, num_row])
    result = tf.transpose(result)
    return result

The issue I have is I needed to transpose the result in the end to have the dimensions line up, and the results are all boolean values, and currently I noticed tf.transpose doesn't transpose a matrix of bool's

The workaround I have was to apply the functions to real numbers, and afterwards, make the end result into a large bool matrix, although this isn't ideal.

Workaround:

eoc_detection = inp[:,eoc_column]
eoc_detection = tf.reshape(eoc_detection, [num_batches, 1])
eoc_detection_state = tfrepeat(eoc_detection, num_state)
eoc_detection_state = tf.greater(eoc_detection_state, tf.zeros_like(eoc_detection_state,dtype=tf.float32))
new_state = tf.select(eoc_detection_state, initial_state, new_state)

new_state is the lstm state to be fed in next time. If the end of content state is detected in training, we reset it. This way, I can allow batches of the same length to be trained.

@zffchen78
Copy link
Contributor

Suggestions:

  1. I think your tfrepeat is just

    def tfrepeat(a, repeats):
      return tf.tile(a, [1, repeats]))
  2. you can try to add a line in transpose_op.cc Ln186-194 to support bool for transpose. Though, you may not need it anymore if 1 works out for you.

@hardmaru
Copy link
Author

Thanks, I didn't know about tf.tile. Will try that, and to get bool working for transpose.

I think makes sense for the transpose method to work for any type of matrix that is not a numerical value.

@martinwicke
Copy link
Member

Yes, transpose should work for any dtype. The preferred solution is to use a suitable macro from register_types.h to make that happen.

@girving
Copy link
Contributor

girving commented Mar 8, 2016

Looks like tf.transpose already works for any type on CPU, but only works for number types on GPU. I'll fix it to use TF_CALL_POD_TYPES instead of TF_CALL_NUMBER_TYPES.

@girving girving self-assigned this Mar 8, 2016
@girving
Copy link
Contributor

girving commented Mar 8, 2016

Fix in review.

@vrv vrv closed this as completed in 6c576f0 Mar 9, 2016
tarasglek pushed a commit to tarasglek/tensorflow that referenced this issue Jun 20, 2017
Fix broken link in inception readme
darkbuck pushed a commit to darkbuck/tensorflow that referenced this issue Jan 23, 2020
remove no_rocm tag from most distribute tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants