concept story in category deep learning

appears as: story, stories, stories, story
Deep Learning for Natural Language Processing

This is an excerpt from Manning's book Deep Learning for Natural Language Processing.

While there is a lot more to the story, these three types embody respectively the neural or cognitive, eager and lazy types of machine learning. All of these approaches relate naturally to the deep learning approach to natural language analysis, which is the main topic of this book.

Figure 5.2. Data preparation: vectorizing stories.
Ch5 data prep
Listing 5.6   Incremental context.
def process_stories_n_context(filename,tokenizer,vocab_size,use_context=0): #1
    f=open(filename,"r")
    X=[]
    Q=[]
    y=[]
    max_story_len=0
    max_query_len=0

    for line in f:
        m=re.match("^(\d+)\s(.+)\.",line.rstrip())                          #2
        if m:
            if int(m.group(1))==1:
                story={}
            story[int(m.group(1))]=m.group(2)
        else:
            m=re.match("^\d+\s(.+)\?\s\t([^\t]+)\t(.+)",line.rstrip())      #3
            if m:
                question=m.group(1)
                answer=m.group(2)
                answer_ids=[int(x) for x in m.group(3).split(" ")]
                facts=' '.join([story[id] for id in answer_ids])
                all_facts=' '.join([story[id] for id in story])
                facts_v=vectorize(facts,tokenizer)
                all_facts_v=vectorize(all_facts,tokenizer)

                if use_context==0:                                          #4
                    vectorized_fact=facts_v
                elif use_context==-1:
                    vectorized_fact=all_facts_v
                else:
                    x=min(use_context, len(story))
                    facts=' '.join([story[id] for id in answer_ids])+' '
                    n=0
                    for id in story:
                        if n<x and id not in answer_ids:
                            facts+=story[id]+' '
                            n+=1
                    vectorized_fact=vectorize(facts,tokenizer)
                l=len(vectorized_fact)
                if l>max_story_len:
                    max_story_len=l
                vectorized_question=vectorize(question,tokenizer)           #5
                l=len(vectorized_question)
                if l>max_query_len:
                    max_query_len=l

                vectorized_answer=vectorize(answer,tokenizer)               #6

                X.append(vectorized_fact)                                   #7
                Q.append(vectorized_question)
                answer=np.zeros(vocab_size)
                answer[vectorized_answer[0]]=1
                y.append(answer)
    f.close()

    return np.array(X),np.array(Q),np.array(y), max_story_len, max_query_len

While many things are unclear about the exact organization of episodic memory (for one thing, whether it is localized in a specific location in the brain, or whether it is a distributed type of memory ([Chen2017])), the general concept of episodic memory comes closest to the memory we implemented in the memory networks of Chapter 5. We worked with descriptions of specific 'events' (which we called 'facts'), combined together into stories. The Question-Answering mechanism of memory networks implements a form of memory access of retrieval: memory networks address a memory base of stored facts in order to carry out an analysis, by asking for the relevant facts in memory.

The end-to-end memory networks of Chapter 5 apply a form of supervised attention during processing. When confronted with a story, a question and an answer during training, they learn to hone in on an informative part of the story that holds the answer to the question posed. Those informative parts are specified in the training data, which, as you will recall, looks like