Ejemplo n.º 1
0
 def testConv(self):
   np.random.seed(7)
   with self.test_session() as sess:
     for _ in range(16):
       sizes = np.random.randint(3, size=1 + np.random.randint(3))
       depth = np.random.randint(3)
       dims = np.random.randint(3, 6, size=1 + depth)
       seqs = jagged.pack([np.random.randn(s, dims[0]).astype(np.float32)
                           for s in sizes])
       widths = np.array([1, 3, 5, 7])[np.random.randint(4, size=depth)]
       filters = [np.random.randn(widths[i], dims[i],
                                  dims[i + 1]).astype(np.float32)
                  for i in range(depth)]
       activations = np.array(
           [None, tf.nn.relu, tf.sigmoid])[np.random.randint(
               3, size=depth)]
       out = jagged.conv1d_stack(seqs, filters, activations)
       self.assertEqual(type(out), jagged.Jagged)
       offsets, flat = sess.run([out.offsets, out.flat])
       for i in range(len(sizes)):
         x = seqs[i][None]
         for filt, activation in zip(filters, activations):
           x = tf.nn.conv1d(x, filt, stride=1, padding='SAME')
           if activation is not None:
             x = activation(x)
         x = tf.squeeze(x, [0])
         self.assertAllClose(flat[offsets[i]:offsets[i + 1]], x.eval())
Ejemplo n.º 2
0
 def testConvEmpty(self):
   with self.test_session() as sess:
     sizes = tf.constant([], dtype=tf.int32)
     seqs = jagged.jagged(sizes, tf.zeros([0, 7]))
     filters = [tf.zeros([3, 7, 13])]
     activations = [tf.nn.relu]
     out = jagged.conv1d_stack(seqs, filters, activations)
     out_sizes, out_flat = sess.run([out.sizes, out.flat])
     self.assertAllEqual(out_sizes, [])
     self.assertAllEqual(out_flat, np.zeros([0, 13]))
Ejemplo n.º 3
0
 def embed_clauses(clauses, name):
     with tf.variable_scope(name):
         filters, activations = [], []
         dim = hparams.embedding_size
         for i in range(conv_layers):
             filters.append(
                 tf.get_variable('filter%d' % i,
                                 shape=(hparams.filter_width, dim,
                                        hidden_size),
                                 initializer=layers.xavier_initializer()))
             bias = tf.get_variable('bias%d' % i,
                                    shape=(hidden_size, ),
                                    initializer=tf.constant_initializer(0))
             activations.append(functools.partial(bias_relu, bias=bias))
             dim = hidden_size
         clauses = jagged.conv1d_stack(clauses, filters, activations)
         return jagged.reduce_max(clauses)