Code Monkey home page Code Monkey logo

Comments (8)

Emrys-Hong avatar Emrys-Hong commented on June 8, 2024

Hi,

I would suggest you using these functions: model.decoder_greedy or model.decoder_topk. If you want to decode it with beam search, you can utilize Translator object in utils/beam_omt_mimic_model1.py

I have written one program for your exploration with the model, you can modify the following code to tailor your own need.

from model.trainer import Train_MIME
from utils.data_loader import prepare_data_seq
from utils import config
from model.common_layer import evaluate, count_parameters, make_infinite

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.init import xavier_uniform_
import torch.utils.data as data
from tqdm import tqdm
import os
import time 
import numpy as np
import math
from collections import deque
from utils.beam_omt_mimic_model1 import Translator
DIALOG_SIZE = 3

class Dataset(data.Dataset):
    """Custom data.Dataset compatible with data.DataLoader."""
    def __init__(self, data, vocab):
        """Reads source and target sequences from txt files."""
        self.vocab = vocab
        self.data = data
    def __len__(self):
        return 1
    def __getitem__(self, index):
        # here we ignore index since we only have one input
        item = {}
        item["context_text"] = [x for x in self.data if x!="None"]
        X_dial = [config.CLS_idx]
        X_mask = [config.CLS_idx]
        for i, sentence in enumerate(item["context_text"]):
            X_dial += [self.vocab.word2index[word] if word in self.vocab.word2index else config.UNK_idx for word in sentence.split()]
            spk = self.vocab.word2index["USR"] if i % 2 == 0 else self.vocab.word2index["SYS"]
            X_mask += [spk for _ in range(len(sentence.split()))]
        assert len(X_dial) == len(X_mask)
        item["context"] = X_dial
        item["mask"] = X_mask
        item["len"] = len(X_dial)
        return item

def collate_fn(data):
    
    input_batch = torch.LongTensor([data[0]["context"]])
    input_mask = torch.LongTensor([data[0]["mask"]])
    if config.USE_CUDA:
        input_batch = input_batch.cuda()
        input_mask = input_mask.cuda()
    d = {}
    d["input_batch"] = input_batch
    d["input_lengths"] = torch.LongTensor([data[0]["len"]])
    d["mask_input"] = input_mask
    d["program_label"] = torch.LongTensor([9]) #fake label
    if config.USE_CUDA:
        d["program_label"] = d["program_label"].cuda()
    return d 

def make_batch(inp,vacab):
    d = Dataset(inp,vacab)
    loader = torch.utils.data.DataLoader(dataset=d, batch_size=1, shuffle=False, collate_fn=collate_fn)
    return iter(loader).next()

data_loader_tra, data_loader_val, data_loader_tst, vocab, program_number = prepare_data_seq(batch_size=config.batch_size)


model = Train_MIME(vocab, decoder_number=program_number, model_file_path=config.saved_model_path, is_eval=True)
if (config.USE_CUDA):
    model.cuda()
model = model.eval()

print('Start to chat')
context = deque(DIALOG_SIZE * ['None'], maxlen=DIALOG_SIZE)
t = Translator(model, model.vocab)

while(True):
    msg = input(">>> ")
    if(len(str(msg).rstrip().lstrip()) != 0):

        context.append(str(msg).rstrip().lstrip())
        print(context)
        batch = make_batch(context, vocab)
        # sent_g = model.decoder_greedy(batch,max_dec_step=30)
        # print(">>>",sent_g[0])
        # context.append(sent_g[0])
        
        # sent_b = t.beam_search(batch, max_dec_step=30)
        # print(">>>",sent_b[0])
        # context.append(sent_b[0])

        sent_t = model.decoder_topk(batch, max_dec_step=30)
        print(">>>",sent_t[0])
        context.append(sent_t[0])

from mime.

ayan-iiitd avatar ayan-iiitd commented on June 8, 2024

I am getting the following error -

runfile('/home/ayan/Data1/MIME/test.py', wdir='/home/ayan/Data1/MIME')
[nltk_data] Downloading package punkt to /home/ayan/nltk_data...
[nltk_data]   Package punkt is already up-to-date!
Reloaded modules: utils.metric, utils.beam_omt_mimic_model1, model.common_layer, utils.data_reader, utils.data_loader, model.transformer_mulexpert, model.emotion_input_attention, model.complex_res_attention, model.complex_res_gate, model.decoder_context_v, model.VAE_noEmo_posterior, model.trainer
LOADING empathetic_dialogue
[nltk_data] Downloading package punkt to /home/ayan/nltk_data...
[nltk_data]   Package punkt is already up-to-date!
04-03 13:47 Vocab  24645 
[situation]: i remember going to the fireworks with my best friend . there was a lot of people , but it only felt like us in the world .
[emotion]: sentimental
[context]: ['i remember going to see the fireworks with my best friend . it was the first time we ever spent time alone together . although there was a lot of people , we felt like the only people in the world .']
[target]: was this a friend you were in love with , or just a best friend ?
 
[situation]: i remember going to the fireworks with my best friend . there was a lot of people , but it only felt like us in the world .
[emotion]: sentimental
[context]: ['i remember going to see the fireworks with my best friend . it was the first time we ever spent time alone together . although there was a lot of people , we felt like the only people in the world .', 'was this a friend you were in love with , or just a best friend ?', 'this was a best friend . i miss her .']
[target]: where has she gone ?
 
[situation]: i remember going to the fireworks with my best friend . there was a lot of people , but it only felt like us in the world .
[emotion]: sentimental
[context]: ['i remember going to see the fireworks with my best friend . it was the first time we ever spent time alone together . although there was a lot of people , we felt like the only people in the world .', 'was this a friend you were in love with , or just a best friend ?', 'this was a best friend . i miss her .', 'where has she gone ?', 'we no longer talk .']
[target]: oh was this something that happened because of an argument ?
 
Embeddings: 24645 x 300
Loading embedding file: vectors/glove.6B.300d.txt
Pre-trained: 19481 (79.05%)
Start to chat

hi
deque(['None', 'None', 'hi'], maxlen=3)
Traceback (most recent call last):

  File "/home/ayan/Data1/MIME/test.py", line 92, in <module>
    sent_t = model.decoder_topk(batch, max_dec_step=30)

  File "/home/ayan/Data1/MIME/model/trainer.py", line 358, in decoder_topk
    context_emo = [self.positive_emotions[0] if d['compound'] > 0 else self.negative_emotions[0] for d in batch['context_emotion_scores']]

KeyError: 'context_emotion_scores'

from mime.

abdulwajid725 avatar abdulwajid725 commented on June 8, 2024

Hi,

I would suggest you using these functions: model.decoder_greedy or model.decoder_topk. If you want to decode it with beam search, you can utilize Translator object in utils/beam_omt_mimic_model1.py

I have written one program for your exploration with the model, you can modify the following code to tailor your own need.

from model.trainer import Train_MIME
from utils.data_loader import prepare_data_seq
from utils import config
from model.common_layer import evaluate, count_parameters, make_infinite

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.init import xavier_uniform_
import torch.utils.data as data
from tqdm import tqdm
import os
import time 
import numpy as np
import math
from collections import deque
from utils.beam_omt_mimic_model1 import Translator
DIALOG_SIZE = 3

class Dataset(data.Dataset):
    """Custom data.Dataset compatible with data.DataLoader."""
    def __init__(self, data, vocab):
        """Reads source and target sequences from txt files."""
        self.vocab = vocab
        self.data = data
    def __len__(self):
        return 1
    def __getitem__(self, index):
        # here we ignore index since we only have one input
        item = {}
        item["context_text"] = [x for x in self.data if x!="None"]
        X_dial = [config.CLS_idx]
        X_mask = [config.CLS_idx]
        for i, sentence in enumerate(item["context_text"]):
            X_dial += [self.vocab.word2index[word] if word in self.vocab.word2index else config.UNK_idx for word in sentence.split()]
            spk = self.vocab.word2index["USR"] if i % 2 == 0 else self.vocab.word2index["SYS"]
            X_mask += [spk for _ in range(len(sentence.split()))]
        assert len(X_dial) == len(X_mask)
        item["context"] = X_dial
        item["mask"] = X_mask
        item["len"] = len(X_dial)
        return item

def collate_fn(data):
    
    input_batch = torch.LongTensor([data[0]["context"]])
    input_mask = torch.LongTensor([data[0]["mask"]])
    if config.USE_CUDA:
        input_batch = input_batch.cuda()
        input_mask = input_mask.cuda()
    d = {}
    d["input_batch"] = input_batch
    d["input_lengths"] = torch.LongTensor([data[0]["len"]])
    d["mask_input"] = input_mask
    d["program_label"] = torch.LongTensor([9]) #fake label
    if config.USE_CUDA:
        d["program_label"] = d["program_label"].cuda()
    return d 

def make_batch(inp,vacab):
    d = Dataset(inp,vacab)
    loader = torch.utils.data.DataLoader(dataset=d, batch_size=1, shuffle=False, collate_fn=collate_fn)
    return iter(loader).next()

data_loader_tra, data_loader_val, data_loader_tst, vocab, program_number = prepare_data_seq(batch_size=config.batch_size)


model = Train_MIME(vocab, decoder_number=program_number, model_file_path=config.saved_model_path, is_eval=True)
if (config.USE_CUDA):
    model.cuda()
model = model.eval()

print('Start to chat')
context = deque(DIALOG_SIZE * ['None'], maxlen=DIALOG_SIZE)
t = Translator(model, model.vocab)

while(True):
    msg = input(">>> ")
    if(len(str(msg).rstrip().lstrip()) != 0):

        context.append(str(msg).rstrip().lstrip())
        print(context)
        batch = make_batch(context, vocab)
        # sent_g = model.decoder_greedy(batch,max_dec_step=30)
        # print(">>>",sent_g[0])
        # context.append(sent_g[0])
        
        # sent_b = t.beam_search(batch, max_dec_step=30)
        # print(">>>",sent_b[0])
        # context.append(sent_b[0])

        sent_t = model.decoder_topk(batch, max_dec_step=30)
        print(">>>",sent_t[0])
        context.append(sent_t[0])

Hi,
I was trying to run this code for generating custom output, but as stated in previous comment it gives "Key Error: 'context_emotion_scores'". Now as per my understanding it gives error because while testing with test_data provided in the paper we have all the necessary fields like context, target, emotion and situation.
But when we are taking custom input from users we don't have these fields.

batch = make_batch(context, vocab)
I think that context is not getting generated properly in the above line.
Can you please tell how to correct this issue.

from mime.

ayan-iiitd avatar ayan-iiitd commented on June 8, 2024

Hi,
I resolved the above issue by commenting lines 358 to 361 in 'decoder_topk.py' since 'context_emo' variable was not being used in 'built_in' mode.
I am using pretrained model from save/ saved_model for testing.
Now I am getting following output

Embeddings: 24645 x 300
Loading embedding file: vectors/glove.6B.300d.txt
Pre-trained: 19481 (79.05%)
loading weights
Start to chat

Me >>> hi how are you?
System >>> that is great . i hope you get a great care of yourself ! 

Me >>> I had an accident, feeling depressed right now
System >>> that is good , i am glad you have that a lot of courage to do that . 

This does not feel correct. Need your help
Thanks

from mime.

ayan-iiitd avatar ayan-iiitd commented on June 8, 2024

Comparing the output we get from running the model separately and running in the default pipeline -

In output.txt

emotion:lonely	vader-score: -0.4215	predicted_emotion: sad
Context:['i there , dont know what to do , jst broke up with my girlfirned , we were 8 years together']
Topk:oh no ! i hope you have to deal with that . 
Beam: i am sorry to hear that . what happened ? 
Greedy:that is a bummer . what happened ? 
Ref:sorry to hear ! do you have any idea about the break up ? did you think about it ?
----------------------------------------------------------------------
emotion:lonely	vader-score: -0.4215	predicted_emotion: lonely
Context:['i there , dont know what to do , jst broke up with my girlfirned , we were 8 years together', 'sorry to hear ! do you have any idea about the break up ? did you think about it ?', 'yes we decided together with our minds , and know i come home and feel so distant from the world']
Topk:that is so sad . i hope you find a good feeling better ! 
Beam: i am sorry to hear that . i hope you have a good time ! 
Greedy:that is a shame . i am sorry to hear that . i hope you get it . 
Ref:sorry again ! hope you will get relief from this sadness . please concentrate on your interests to divert your mind from this .

Running directly -

Me >>> i there , dont know what to do , jst broke up with my girlfirned , we were 8 years together
System beam_search >>> i am sorry to hear that . what happened ? 
predicted emotion : sad
System decoder_topk >>> that is so bad ! i hope you get it ! 

Me >>> yes we decided together with our minds , and know i come home and feel so distant from the world
System beam_search >>> i am sorry to hear that . i hope you have a good time ! 
predicted emotion : lonely
System decoder_topk >>> that is a good thing to do 

Another example -

emotion:angry	vader-score: -0.9412	predicted_emotion: furious
Context:['i was very angry when a co worker lied about a car accident to get out of working july 4th . i covered his shift till midnight and he was lying about the accident']
Topk:i am glad you are ok . 
Beam: oh no , i am sorry to hear that . i am sorry to hear that . 
Greedy:that is terrible . i hope you get the car . 
Ref:that is not very nice .
----------------------------------------------------------------------
emotion:angry	vader-score: -0.9412	predicted_emotion: furious
Context:['i was very angry when a co worker lied about a car accident to get out of working july 4th . i covered his shift till midnight and he was lying about the accident', 'that is not very nice .', 'yeah he got caught in someones pics on facebook . never lie and then go out with people who like to take pictures .']
Topk:that is terrible . it happens to everyone in the world . i am sorry you have to deal with that . 
Beam: i am sorry to hear that . i hope you do not have to deal with that . 
Greedy:that is a shame . i hope you get it . 
Ref:yes . i do not have a facebook .

Separately -

Me >>> i was very angry when a co worker lied about a car accident to get out of working july 4th . i covered his shift till midnight and he was lying about the accident
System beam_search >>> oh no , i am sorry to hear that . i am sorry to hear that . 
predicted emotion : furious
System decoder_topk >>> oh my goodness . i am sorry to hear that . 

Me >>> yeah he got caught in someones pics on facebook . never lie and then go out with people who like to take pictures .
System beam_search >>> i am sorry to hear that . i hope you do not have to deal with that . 
predicted emotion : furious
System decoder_topk >>> that is so sad . i have had to do that . it is always nice to have someone who can help ? 

@Emrys-Hong can you think of any reason this is happening?

from mime.

Emrys-Hong avatar Emrys-Hong commented on June 8, 2024

Comparing the output we get from running the model separately and running in the default pipeline -

In output.txt

emotion:lonely	vader-score: -0.4215	predicted_emotion: sad
Context:['i there , dont know what to do , jst broke up with my girlfirned , we were 8 years together']
Topk:oh no ! i hope you have to deal with that . 
Beam: i am sorry to hear that . what happened ? 
Greedy:that is a bummer . what happened ? 
Ref:sorry to hear ! do you have any idea about the break up ? did you think about it ?
----------------------------------------------------------------------
emotion:lonely	vader-score: -0.4215	predicted_emotion: lonely
Context:['i there , dont know what to do , jst broke up with my girlfirned , we were 8 years together', 'sorry to hear ! do you have any idea about the break up ? did you think about it ?', 'yes we decided together with our minds , and know i come home and feel so distant from the world']
Topk:that is so sad . i hope you find a good feeling better ! 
Beam: i am sorry to hear that . i hope you have a good time ! 
Greedy:that is a shame . i am sorry to hear that . i hope you get it . 
Ref:sorry again ! hope you will get relief from this sadness . please concentrate on your interests to divert your mind from this .

Running directly -

Me >>> i there , dont know what to do , jst broke up with my girlfirned , we were 8 years together
System beam_search >>> i am sorry to hear that . what happened ? 
predicted emotion : sad
System decoder_topk >>> that is so bad ! i hope you get it ! 

Me >>> yes we decided together with our minds , and know i come home and feel so distant from the world
System beam_search >>> i am sorry to hear that . i hope you have a good time ! 
predicted emotion : lonely
System decoder_topk >>> that is a good thing to do 

Another example -

emotion:angry	vader-score: -0.9412	predicted_emotion: furious
Context:['i was very angry when a co worker lied about a car accident to get out of working july 4th . i covered his shift till midnight and he was lying about the accident']
Topk:i am glad you are ok . 
Beam: oh no , i am sorry to hear that . i am sorry to hear that . 
Greedy:that is terrible . i hope you get the car . 
Ref:that is not very nice .
----------------------------------------------------------------------
emotion:angry	vader-score: -0.9412	predicted_emotion: furious
Context:['i was very angry when a co worker lied about a car accident to get out of working july 4th . i covered his shift till midnight and he was lying about the accident', 'that is not very nice .', 'yeah he got caught in someones pics on facebook . never lie and then go out with people who like to take pictures .']
Topk:that is terrible . it happens to everyone in the world . i am sorry you have to deal with that . 
Beam: i am sorry to hear that . i hope you do not have to deal with that . 
Greedy:that is a shame . i hope you get it . 
Ref:yes . i do not have a facebook .

Separately -

Me >>> i was very angry when a co worker lied about a car accident to get out of working july 4th . i covered his shift till midnight and he was lying about the accident
System beam_search >>> oh no , i am sorry to hear that . i am sorry to hear that . 
predicted emotion : furious
System decoder_topk >>> oh my goodness . i am sorry to hear that . 

Me >>> yeah he got caught in someones pics on facebook . never lie and then go out with people who like to take pictures .
System beam_search >>> i am sorry to hear that . i hope you do not have to deal with that . 
predicted emotion : furious
System decoder_topk >>> that is so sad . i have had to do that . it is always nice to have someone who can help ? 

@Emrys-Hong can you think of any reason this is happening?

Hi, thanks for your question. but I am not exactly sure what is the issue are you referring to.

from mime.

ayan-iiitd avatar ayan-iiitd commented on June 8, 2024

Hi, thanks for your question. but I am not exactly sure what is the issue are you referring to.

I meant, the replies aren't matching.

from mime.

Emrys-Hong avatar Emrys-Hong commented on June 8, 2024

I meant, the replies aren't matching.

Hi, I think here could be two possible reasons:

  1. Topk decoding method choose the next word by sampling randomly from the top 5 predicted results.
  2. You may need to pass the gold response in as well to have the replies completely match with the test set, otherwise it will use the utterance in response to your previous utterance as the response.

from mime.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.