Exemple #1
0
    def build_pretrain_model(self, data_dict, hyper_params):
        """
        pretrain-method specific;
        constucts an SCE net;
        works with any network structure of the pipeline
        :param data_dict:
        :param hyper_params:
        :return:
        """
        from theano import tensor
        from blocks.model import Model

        # Note: this has to match the sources defined in the dataset
        indices = [tensor.ivector('{}_indices'.format(i)) for i in range(3)]

        pipeline = self.encoder_pipeline_factory.build_pipeline(
            input_shape=data_dict.get_value().shape, params=hyper_params)

        # compute feature represenation
        rep = [pipeline.apply(data_dict[indices[i]]) for i in range(3)]
        # for r in rep: print r.type

        # flatten representations
        rep = [r.flatten(ndim=2) for r in rep]
        # for r in rep: print r.type

        # compute similarities
        rval = []
        for i in range(1, 3):
            r = (rep[0] * rep[i]).sum(
                axis=1)  # element-wise multiplication and row sum
            r = tensor.reshape(r, (r.shape[0], 1))
            rval.append(r)
        rval = tensor.concatenate(rval, axis=1)
        # print rval.type

        # optional softmax layer (normalization to sum = 1)
        if 'apply_softmax' in hyper_params and hyper_params[
                'apply_softmax']:  # default=False
            from blocks.bricks import Softmax
            rval = Softmax().apply(rval)

        # optional argmax (int output instead of scores
        if 'return_probs' in hyper_params and hyper_params[
                'return_probs'] is False:  # default=True
            rval = rval.argmax(axis=1)

        return Model(rval)