fold
fold copied to clipboard
Stacking a sequence of vectors into a 2D tensor
Is there a way to do this? For example:
If the output of my block is SequenceType(TensorType((5,), 'float32')), is there a straightforward way to stack the sequence of vectors into a Tensor of shape (N,5) where N is the length of the sequence?
Thanks
If N is fixed an known in advance, you're probably better off just using an Nx5 tensor. If N varies or is unkown a metric https://github.com/tensorflow/fold/blob/master/tensorflow_fold/g3doc/py/td.md#class-tdmetric might be the right way to go. For example if you block is 'b' you can pipe it to a metric like so:
b >> td.Metric('foo')
then, when you compile you model, compiler.metric_tensors['foo'] will be a (None, 5) tensor with all of the values you fed in to b stacked on eachother.
On Fri, Mar 10, 2017 at 2:54 PM, Tung Tran [email protected] wrote:
Is there a way to do this? For example:
If the output of my block is SequenceType(TensorType((5,), 'float32')), is there a straightforward way to stack the sequence of vectors into a Tensor of shape (N,5) where N is the length of the sequence?
Thanks
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/tensorflow/fold/issues/31, or mute the thread https://github.com/notifications/unsubscribe-auth/AAsbjqvI_MtLLrn3PjA-2Nr5-9UQfKEtks5rkdScgaJpZM4MZ9Zl .
To clarify slightly, using a metric will help if you want the output of the block to be a named output of the entire model; metric are automatically concatenated together. However, if you want to pipe the (N,5) sequence into another block, then a metric won't help you.
The simple and straightforward way is to do a td.Reduce with tf.concat as the operator. However, that will make log(N) copies of the data, so it's not particularly efficient.
-DeLesley
On Mon, Mar 13, 2017 at 9:24 PM, Moshe Looks [email protected] wrote:
If N is fixed an known in advance, you're probably better off just using an Nx5 tensor. If N varies or is unkown a metric <https://github.com/tensorflow/fold/blob/master/ tensorflow_fold/g3doc/py/td.md#class-tdmetric> might be the right way to go. For example if you block is 'b' you can pipe it to a metric like so:
b >> td.Metric('foo')
then, when you compile you model, compiler.metric_tensors['foo'] will be a (None, 5) tensor with all of the values you fed in to b stacked on eachother.
On Fri, Mar 10, 2017 at 2:54 PM, Tung Tran [email protected] wrote:
Is there a way to do this? For example:
If the output of my block is SequenceType(TensorType((5,), 'float32')), is there a straightforward way to stack the sequence of vectors into a Tensor of shape (N,5) where N is the length of the sequence?
Thanks
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/tensorflow/fold/issues/31, or mute the thread <https://github.com/notifications/unsubscribe- auth/AAsbjqvI_MtLLrn3PjA-2Nr5-9UQfKEtks5rkdScgaJpZM4MZ9Zl> .
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/tensorflow/fold/issues/31#issuecomment-286318675, or mute the thread https://github.com/notifications/unsubscribe-auth/AGGbTaZlyE1NYgjhzFeIq-BCces_0Jdtks5rlhZ5gaJpZM4MZ9Zl .
-- DeLesley Hutchins | Software Engineer | [email protected] | 505-206-0315
Thanks, in my scenario I would like to pipe the output into another block. I thought of using tf.concat (tf.stack specifically in this case) and have tried implementing it like this:
b = td.Map(td.Vector(5)) >> td.Reduce(td.Function(lambda x, y: tf.stack([x,y],axis=1)))
print b.eval([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])
I get the following error:
TypeError: Type mismatch between output type TensorType((2, 5), 'float32') and expected output type TensorType((5,), 'float32') in <td.Function tf_fn='
I suppose td.Reduce() is expecting the output type of the operator to match the input type, is that correct?
You are correct; I wasn't thinking clearly. In fact, the input and output types of every block must have exact dimensions that are known in advance, so even if you implemented a custom block to do the concatenation, you couldn't assign it a valid type.
On Tue, Mar 14, 2017 at 12:46 PM, Tung Tran [email protected] wrote:
Thanks, in my scenario I would like to pipe the output into another block. I thought of using tf.concat (tf.stack specifically in this case) and have tried implementing it like this:
b = td.Map(td.Vector(5)) >> td.Reduce(td.Function(lambda x, y: tf.stack([x,y],axis=1))) print b.eval([[1,2,3,4,5],[6,7,8,9, 10],[11,12,13,14,15]])
I get the following error: TypeError: Type mismatch between output type TensorType((2, 5), 'float32') and expected output type TensorType((5,), 'float32') in <td.Function tf_fn=''>.
I suppose td.Reduce() is expecting the output type of the operator to match the input type, is that correct?
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tensorflow/fold/issues/31#issuecomment-286538284, or mute the thread https://github.com/notifications/unsubscribe-auth/AGGbTQnC_tyMKaiSgNVd54LDnsly5Kx6ks5rlu6XgaJpZM4MZ9Zl .
-- DeLesley Hutchins | Software Engineer | [email protected] | 505-206-0315
If N is fixed an known,you can use
NGrams(N) >> GetItem(0) >> Concat() >> Function(lambda x:tf.reshape(-1,N,5) )
to convert sequence of vectors to a Tensor of shape (N,5)
If N is fixed and known, then you can also replace NGrams >> GetItem with SeqToTuple(T, N), where:
def SeqToTuple(T, N): return (InputTransform(lambda x: tuple(x)) .set_input_type(td.SequenceType(T)) .set_output_type(td.Tuple(*([T] * N))))
which should be a bit more efficient. But if N is fixed and known, then why do you need to use a sequence to start with?
On Mon, Mar 27, 2017 at 7:29 AM, acelove [email protected] wrote:
If N is fixed an known,you can use NGrams(N) >> GetItem(0) >> Concat() >> Function(lambda x:tf.reshape(-1,N,5) ) to convert sequence of vectors to a Tensor of shape (N,5)
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tensorflow/fold/issues/31#issuecomment-289471073, or mute the thread https://github.com/notifications/unsubscribe-auth/AGGbTfeMVUI0SHGZbVlsD1K3ZmOM1sIxks5rp8fmgaJpZM4MZ9Zl .
-- DeLesley Hutchins | Software Engineer | [email protected] | 505-206-0315
Because after using td.Map I get a sequence of tensor .However, the model's output cannot be sequenceType,so I think I have to convert it to TupleType