Closed ch3njust1n closed 7 years ago
You need to >> to a layer or block, not to a function that returns a layer. Incidentally I don't understand why you want to convolve with zeros, but I guess this is just for testing purposes. Please note that when you wrap a callable with a scoped layer it ought to take a scope keyword argument (since the whole point of scoped layers is to encapsulate trainable variables in a particular scope).
Try e.g.
def my_conv(x, scope):
conv1d_layer = td.ScopedLayer(my_conv)
td.Map(td.Map(td.Scalar())) >> conv1d_layer
On Wed, Mar 22, 2017 at 7:04 AM, Justin Chen notifications@github.com wrote:
I'm trying to figure out how to use td.ScopedLayer() with tf.conv1d(). What am I doing wrong?
def conv1d_layer(x): return td.ScopedLayer(tf.nn.conv1d(x, tf.zeros([3, 1, 1]), stride=2, padding="VALID"))
test_seq = [[x for x in xrange(0,randint(1,10))] for v in xrange(randint(3,5))]
print((td.Map(td.Map(td.Scalar())) >> conv1d_layer).eval(test_seq))
Getting the following error:
Traceback (most recent call last): File "test.py", line 109, in print((td.Map(td.Map(td.Scalar())) >> conv1d_layer).eval(test_seq)) File "/usr/local/lib/python2.7/dist-packages/tensorflow_fold/blocks/blocks.py", line 156, in rshift return Pipe(self, rhs) File "/usr/local/lib/python2.7/dist-packages/tensorflow_fold/blocks/blocks.py", line 997, in Pipe return _pipe([convert_to_block(b) for b in blocks], File "/usr/local/lib/python2.7/dist-packages/tensorflow_fold/blocks/blocks.py", line 2036, in convert_to_block raise TypeError('%s cannot be converted to a block' % str(block_like)) TypeError: < function conv1d_layer at 0x7fc5e6c28320 > cannot be converted to a block
— 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/38, or mute the thread https://github.com/notifications/unsubscribe-auth/AAsbjisXfjboidBhDni6LXfh0OPm54Qeks5roSp0gaJpZM4MlP1- .
@moshelooks So in order to feed in arbitrary-length data, I need first convert it with np.array()
and then tf.convert_to_tensor()
, but in order to use that latter function, the np.array
dimension must be rectangular, not jagged, so I cannot use, for instance, x = np.array([[0, 1, 2, 3], [0, 1, 2], [0, 1]])
. I have to first do max_length = max(len(row) for row in x)
and then x_padded = np.array([row + [0] * (max_length - len(row)) for row in x])
, and then feed x_padded
to tf.convert_to_tensor()
, which can then be fed to (td.Map(td.Map(td.Scalar())) >> td.ScopedLayer(conv1d_layer)).eval(x_padded)
, correct?
Wat? No. What problem are you trying to solve here? There's almost never any need to call np.array() or tf.convert_to_tensor() in conjunction with TF or TF Fold; these conversions get performed automatically when the appropriate function gets called. If you have a layer that you want to invoke elementwise on a jagged array of python floats (e.g. a list of list of floats) then Map(Map(Scalar)) >> layer will do the job. If the element type is not float then replace Scalar() with td.Vector or td.Tensor. If you want to do apply a function over adjacent elements then you cannot use map directly, try td.NGrams.
@moshelooks Sorry for the confusion, so I'm trying to run a 1D convolution over a flattened parse tree. So I'm getting a batch of n
lists where each list is a flattened tree. Does this make sense: batch=[[x for x in xrange(0,randint(1,10))] for v in xrange(randint(3,5))]
and then (td.Map(td.Map(td.Scalar())) >> td.ScopedLayer(conv1d_layer)).eval(batch)
I get this error:
TypeError: bad input type SequenceType for <td.Function tf_fn=<tensorflow_fold.blocks.layers.ScopedLayer object at 0x7f0bb5c92950>>, expected TupleType or TensorType
You need to start thinking in terms of types https://github.com/tensorflow/fold/blob/master/tensorflow_fold/g3doc/types.md; I recommend our tutorial https://github.com/tensorflow/fold/blob/master/tensorflow_fold/g3doc/blocks.md. The first question is what type you want for the individual inputs (not the type of a batch of inputs!). Note that eval works on single inputs, whereas the Compiler class deals in batches of inputs.
I'm going to assume that for a flattened parse tree this is a sequence of vectors in R^n (sequences in Fold have indeterminate length), i.e. td.SequenceType(td.TensorType(n)). It looks like you're trying to apply a layer elementwise to the items in your input sequence; assuming this is correct, you should just do
td.Map(td.Vector(n) >> my_scoped_layer)
Note the 'n' (tensor/vector shapes need to be fully specified) and note that the individual elements of the vector get piped to my_scoped_layer, not the entire sequence (layers can only take tensors as inputs, not sequences). If you want to write a function that takes two tensors as inputs and runs over adjacent elements of you input sequence that would be
td.Map(td.Vector(n)) >> td.NGrams(2) >> td.Map(my_scoped_layer)
On Mon, Mar 27, 2017 at 11:51 AM, Justin Chen notifications@github.com wrote:
@moshelooks https://github.com/moshelooks Sorry for the confusion, so I'm trying to run a 1D convolution over a flattened parse tree. So I'm getting a batch of n lists where each list is a flattened tree. Does this make sense print (td.Map(td.Map(td.Vector())) >> td.ScopedLayer(conv1d_layer)).eval(batch), where batch is a list of arbitrarily sized lists.?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tensorflow/fold/issues/38#issuecomment-289548822, or mute the thread https://github.com/notifications/unsubscribe-auth/AAsbjrfqp4B_MtPCFHJITkv8AYK3bTAqks5rqAUVgaJpZM4MlP1- .
Ah, I see. So if I want to have multiple 1D conv layers each with a different kernel length, how do I pass parameters to my functions that I wrap with td.ScopedLayer()? For example, if this is my conv layer block:
def conv1d_layer(x, kernel_size, scope):
with tf.variable_scope(scope) as sc:
conv = tf.nn.conv1d(x, tf.zeros([kernel_size,1,1]), stride=1, padding='SAME')
return conv
print (td.Map(td.Scalar()) >> td.NGrams(3) >> td.ScopedLayer(conv1d_layer(kernel_size=3))).eval(range(10))
I get the following error:
Traceback (most recent call last):
File "test.py", line 128, in <module>
print (td.Map(td.Scalar()) >> td.NGrams(3) >> td.ScopedLayer(conv1d_layer(kernel_size=3))).eval(range(10))
TypeError: conv1d_layer() takes exactly 3 arguments (1 given)
In this example, I want to run the 1d conv with kernel width 3 to each 3-gram.
I'm trying to figure out how to use
td.ScopedLayer()
withtf.conv1d()
. What am I doing wrong?Getting the following error: