Exemplo n.º 1
0
    def encode(self, x=None):
        if x is None:
            x = CharConvEmbeddings.create_placeholder(self.name)
        self.x = x

        ech0 = tf.scatter_update(self.Wch, tf.constant(0, dtype=tf.int32, shape=[1]), tf.zeros(shape=[1, self.dsz]))
        char_comp, self.wsz = pool_chars(self.x, self.Wch, ech0, self.dsz, self.nfeat_factor,
                                         self.cfiltsz, self.max_feat, self.gating,
                                         self.num_gates, self.activation, self.wsz)
        return char_comp
Exemplo n.º 2
0
    def encode(self, x=None):
        if x is None:
            x = CharConvEmbeddings.create_placeholder(self.name)
        self.x = x

        ech0 = tf.scatter_update(self.Wch,
                                 tf.constant(0, dtype=tf.int32, shape=[1]),
                                 tf.zeros(shape=[1, self.dsz]))
        char_comp, self.wsz = pool_chars(self.x, self.Wch, ech0, self.dsz,
                                         self.nfeat_factor, self.cfiltsz,
                                         self.max_feat, self.gating,
                                         self.num_gates, self.activation,
                                         self.wsz)
        return char_comp
Exemplo n.º 3
0
 def encode(self, x=None):
     if x is None:
         x = CharConvEmbeddings.create_placeholder(self.name)
     self.x = x
     with tf.variable_scope(self.scope):
         Wch = tf.get_variable("Wch",
                               initializer=tf.constant_initializer(
                                   self.weights,
                                   dtype=tf.float32,
                                   verify_shape=True),
                               shape=[self.vsz, self.dsz],
                               trainable=True)
         ech0 = tf.scatter_update(Wch,
                                  tf.constant(0, dtype=tf.int32, shape=[1]),
                                  tf.zeros(shape=[1, self.dsz]))
         char_comp, self.wsz = pool_chars(x, Wch, ech0, self.dsz,
                                          self.nfeat_factor, self.cfiltsz,
                                          self.max_feat, self.gating,
                                          self.num_gates, self.activation,
                                          self.wsz)
         return char_comp
Exemplo n.º 4
0
    def create(cls, embeddings, labels, **kwargs):
        """The main method for creating all :class:`WordBasedModel` types.
        
        This method instantiates a model with pooling and optional stacking layers.
        Many of the arguments provided are reused by each implementation, but some sub-classes need more
        information in order to properly initialize.  For this reason, the full list of keyword args are passed
        to the :method:`pool` and :method:`stacked` methods.
        
        :param embeddings: This is a dictionary of embeddings, mapped to their numerical indices in the lookup table
        :param labels: This is a list of the `str` labels
        :param kwargs: See below
        
        :Keyword Arguments:
        * *model_type* -- The string name for the model (defaults to `default`)
        * *session* -- An optional tensorflow session.  If not passed, a new session is
            created
        * *finetune* -- Are we doing fine-tuning of word embeddings (defaults to `True`)
        * *mxlen* -- The maximum signal (`x` tensor temporal) length (defaults to `100`)
        * *dropout* -- This indicates how much dropout should be applied to the model when training.
        * *pkeep* -- By default, this is a `tf.placeholder`, but it can be passed in as part of a sub-graph.
            This is useful for exporting tensorflow models or potentially for using input tf queues
        * *x* -- By default, this is a `tf.placeholder`, but it can be optionally passed as part of a sub-graph.
        * *y* -- By default, this is a `tf.placeholder`, but it can be optionally passed as part of a sub-graph.
        * *filtsz* -- This is actually a top-level param due to an unfortunate coupling between the pooling layer
            and the input, which, for convolution, requires input padding.
        
        :return: A fully-initialized tensorflow classifier 
        """

        gpus = kwargs.get('gpus')
        # If we are parallelized, we will use the wrapper object ClassifyParallelModel and this creation function
        if gpus is not None:
            return ClassifyParallelModel(cls.create, embeddings, labels,
                                         **kwargs)
        sess = kwargs.get('sess', tf.Session())
        finetune = bool(kwargs.get('finetune', True))
        w2v = embeddings['word']
        c2v = embeddings.get('char')

        model = cls()
        word_dsz = w2v.dsz
        wchsz = 0
        model.labels = labels
        nc = len(labels)

        model.vocab = {}
        for k in embeddings.keys():
            model.vocab[k] = embeddings[k].vocab

        model.mxlen = int(kwargs.get('mxlen', 100))
        model.mxwlen = None
        # This only exists to make exporting easier
        model.pkeep = kwargs.get(
            'pkeep', tf.placeholder_with_default(1.0, shape=(), name="pkeep"))
        model.pdrop_value = kwargs.get('dropout', 0.5)
        # This only exists to make exporting easier
        model.x = kwargs.get(
            'x', tf.placeholder(tf.int32, [None, model.mxlen], name="x"))
        model.y = kwargs.get('y', tf.placeholder(tf.int32, [None, nc],
                                                 name="y"))
        model.lengths = kwargs.get(
            'lengths', tf.placeholder(tf.int32, [None], name="lengths"))
        model.xch = None

        with tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE):

            seed = np.random.randint(10e8)
            init = tf.random_uniform_initializer(-0.05,
                                                 0.05,
                                                 dtype=tf.float32,
                                                 seed=seed)
            xavier_init = xavier_initializer(True, seed)
            word_embeddings = embed(model.x,
                                    len(w2v.vocab),
                                    word_dsz,
                                    initializer=tf.constant_initializer(
                                        w2v.weights,
                                        dtype=tf.float32,
                                        verify_shape=True))

            if c2v is not None:
                model.mxwlen = int(kwargs.get('mxwlen', 40))
                model.xch = kwargs.get(
                    'xch',
                    tf.placeholder(tf.int32, [None, model.mxlen, model.mxwlen],
                                   name='xch'))
                char_dsz = c2v.dsz
                with tf.variable_scope("CharLUT"):
                    Wch = tf.get_variable("Wch",
                                          initializer=tf.constant_initializer(
                                              c2v.weights,
                                              dtype=tf.float32,
                                              verify_shape=True),
                                          shape=[len(c2v.vocab), c2v.dsz],
                                          trainable=True)
                    ech0 = tf.scatter_update(
                        Wch, tf.constant(0, dtype=tf.int32, shape=[1]),
                        tf.zeros(shape=[1, char_dsz]))
                    char_comp, wchsz = pool_chars(model.xch, Wch, ech0,
                                                  char_dsz, **kwargs)
                    word_embeddings = tf.concat(
                        values=[word_embeddings, char_comp], axis=2)

            input_sz = word_dsz + wchsz
            pooled = model.pool(word_embeddings, input_sz, init, **kwargs)
            stacked = model.stacked(pooled, init, **kwargs)

            # For fully connected layers, use xavier (glorot) transform
            with tf.contrib.slim.arg_scope([fully_connected],
                                           weights_initializer=xavier_init):
                with tf.variable_scope("output"):
                    model.logits = tf.identity(fully_connected(
                        stacked, nc, activation_fn=None),
                                               name="logits")
                    model.best = tf.argmax(model.logits, 1, name="best")
        model.sess = sess
        # writer = tf.summary.FileWriter('blah', sess.graph)
        return model