Пример #1
0
def test_MLBL_implementation():
    """ Test Your Implementation of Forward and Backward """

    import coco_proc
    import trainer
    import cPickle as pickle

    z, zd, zt = coco_proc.process(context=5)

    d = {}
    d['name'] = 'testrun'
    d['loc'] = 'models/mlbl_model.pkl'
    d['context'] = 5
    d['learning_rate'] = 0.43
    d['momentum'] = 0.23
    d['batch_size'] = 40
    d['maxepoch'] = 10
    d['hidden_size'] = 441
    d['word_decay'] = 3e-7
    d['context_decay'] = 1e-8
    d['factors'] = 50

    # Load the word embeddings
    embed_map = trainer.load_embeddings()

    # Unpack some stuff from the data
    train_ngrams = z['ngrams']
    train_labels = z['labels']
    train_instances = z['instances']
    word_dict = z['word_dict']
    index_dict = z['index_dict']
    context = z['context']
    vocabsize = len(z['word_dict'])
    trainIM = z['IM']
    train_index = z['index']

    net = MLBL(name=d['name'],
               loc=d['loc'],
               seed=1234,
               V=vocabsize,
               K=100,
               D=trainIM.shape[1],
               h=d['hidden_size'],
               context=d['context'],
               batchsize=1,
               maxepoch=d['maxepoch'],
               eta_t=d['learning_rate'],
               gamma_r=d['word_decay'],
               gamma_c=d['context_decay'],
               f=0.99,
               p_i=d['momentum'],
               p_f=d['momentum'],
               T=20.0,
               verbose=1)

    # Train the network
    X = train_instances
    indX = train_index
    Y = train_labels

    net.init_params(embed_map, index_dict)

    context_size = d['context']
    batchX = X[0:context_size]
    batchY = Y[0:context_size].toarray()
    batchindX = indX[0:context_size].astype(int).flatten()
    batchindX = np.floor(batchindX / 5).astype(int)
    batchIm = trainIM[batchindX]

    # check forward implementation
    ft = net.forward(net.params, batchX, batchIm, test=True)

    # load gt feature
    ft_gt = pickle.load(open("data/val_implementation.p", "rb"))

    # it should be less than 1.0e-5
    print 'Difference (L2 norm) between implemented and ground truth feature = {}'.format(
        np.linalg.norm(ft_gt - ft))
Пример #2
0
def response():
    print("RESPONSE ...")

    # Loading model
    model = torch.load(model_path)
    model.eval()

    # Pre-processing inputs
    input_text = request.form.get('message')
    print(input_text)
    input_text = input_text.strip()
    if input_text == '':
        print("Oops! You input nothing!")
        emotions = ['NONE']
        movies = [['NONE']]
        songs = [['NONE']]
        return render_template('response.html',
                               emotions=emotions,
                               movies=movies,
                               songs=songs)
    else:
        pro_sent = preprocessor(input_text)

        # Embedding and vectorize
        word2idx, _, embeddings = load_embeddings(model_conf)
        sample = vectorize(pro_sent, word2idx, max_length)

        # Processing to get model inputs
        samples = []
        lengths = []
        samples.append(sample)
        lengths.append(len(pro_sent))

        # tensor([17,  4, 37, 42, 21, 16, 13, 44, 29, 10, 15, 22, 21, 23, 18, 23, 25, 10,
        #         23,  9, 22, 18, 14, 15,  6, 33, 14, 30, 13, 22, 26, 17],
        #        device='cuda:0')
        # torch.ones([2, 4], dtype=torch.float64, device=cuda0)
        samples = np.asarray(samples)
        lengths = np.asarray(lengths)

        samples = torch.tensor(samples)
        lengths = torch.tensor(lengths)

        samples = samples.to(DEVICE)
        lengths = lengths.to(DEVICE)

        # Running model
        outputs, attentions = model(samples, lengths)

        # print(attentions)

        # tensor([ 2.1146, -1.7304,  2.0117, -1.3296, -3.1048, -5.9759, -2.7536, -2.7494,   print(outputs)
        #         -1.8445, -4.1412, -4.7449], device='cuda:0', grad_fn=<ThAddBackward>)
        # tensor([0.0521, 0.0631, 0.0632, 0.0632, 0.0632, 0.0632, 0.0631, 0.0632, 0.0632,   print(attentions)
        #         0.0632, 0.0631, 0.0632, 0.0632, 0.0632, 0.0632, 0.0632],
        #        device='cuda:0', grad_fn=<DivBackward1>)
        # gold: 1   1   0   0   0   0   1   0   0   0   1
        # [ 2.11464429 -1.7303592   2.01172185 -1.32956982 -3.10483193 -5.97593689  print(posts)
        #  -2.75357819 -2.74935317 -1.84453487 -4.14115143 -4.74489594]
        posts = outputs.data.cpu().numpy()
        predicted = np.clip(
            np.sign(posts), a_min=0,
            a_max=None)  # 1   1   0   0   0   0   1   0   0   0   1
        predicted = predicted.astype(np.int32)

        emotions = []
        ids = set()
        sum_item = 0
        for idx, item in enumerate(predicted):
            if item == 1:
                emotions.append(labels[idx])
                ids.add(recommend_id[idx])
            sum_item += item
        if sum_item == 0:
            emotions.append(labels[11])  # neutral
            ids.add(recommend_id[11])

        if len(emotions) > 6:
            print(
                "Hey, there are more than 6 predicted emotions. Below are original emotions and emotions for displaying."
            )
            print(emotions)
            emotions = emotions[:6]
            print(emotions)
        else:
            print(emotions)

        print(ids)

        # movies and music matching
        all_movies = load_movies(movie_path)
        all_music = load_music(music_path)
        movies = []
        songs = []
        for i in ids:
            for m in all_movies[i]:
                movies.append(m)
            for s in all_music[i]:
                songs.append(s)

        print(songs)
        print(movies)

        if len(movies) > 3:
            random_int = set()
            while len(random_int) < 3:
                index = np.random.randint(low=0, high=len(movies))
                random_int.add(index)
            new_movies = []
            for i in random_int:
                new_movies.append(movies[i])
            movies = new_movies
            print(movies)
        if len(songs) > 6:
            random_int = set()
            while len(random_int) < 6:
                index = np.random.randint(low=0, high=len(songs))
                random_int.add(index)
            # indexs = np.random.randint(low=0, high=len(songs), size=6)
            new_songs = []
            for i in random_int:
                new_songs.append(songs[i])
            songs = new_songs
            print(songs)

        return render_template('response.html',
                               emotions=emotions,
                               movies=movies,
                               songs=songs)