readme: ---
  tasks:
  - Relation Extraction
  widgets:
    - examples:
        - name: 1
          title: Message-Topic(e1,e2)
          inputs:
            - name: token
              data: ["the", "most", "common", "audits", "were", "about", "waste", "and", "recycling", "."]
            - name: h
              data: 
                - name: audits
                  pos: [3, 4]
            - name: t
              data: 
                - name: waste
                  pos: [6, 7]
                   
        - name: 2
          title: Product-Producer(e2,e1)
          inputs:
            - name: token
              data: ["the", "ombudsman", "'s", "report", "concluded", "that", "``", "a", "large", "part", "of", "the", "package", "was", "not", "provided", "''", "."]
            - name: h
              data: 
                - name: ombudsman
                  pos: [1, 2]
            - name: t
              data: 
                - name: report
                  pos: [3, 4]
        - name: 3
          title: Instrument-Agency(e2,e1)
          inputs:
            - name: token
              data: ["many", "professional", "cartomancers", "use", "a", "regular", "deck", "of", "playing", "cards", "for", "divination", "."]
            - name: h
              data: 
                - name: cartomancers
                  pos: [2, 3]
            - name: t
              data: 
                - name: cards
                  pos: [9, 10]
          
        - name: 4
          title: Entity-Destination(e1,e2)
          inputs:
            - name: token
              data: ["nasa", "kepler", "mission", "sends", "names", "into", "space", "."]
            - name: h
              data: 
                - name: oil
                  pos: [4, 5]
            - name: t
              data: 
                - name: ocean
                  pos: [7, 8]
          
        - name: 5
          title: Cause-Effect(e2,e1)
          inputs:
            - name: token
              data: ["sorace", "was", "unaware", "that", "her", "anger", "was", "caused", "by", "the", "abuse", "."]
            - name: h
              data: 
                - name: anger
                  pos: [5, 6]
            - name: t
              data: 
                - name: abuse
                  pos: [10, 11]
       
        - name: 6
          title: Component-Whole(e1,e2)
          inputs:
            - name: token
              data: ["the", "castle", "was", "inside", "a", "museum", "."]
            - name: h
              data: 
                - name: castle
                  pos: [1, 2]
            - name: t
              data: 
                - name: museum
                  pos: [5, 6]
     
  domain:
  - nlp
  frameworks:
  - pytorch
  backbone:
  - BERT large 
  metrics:
  - accuracy
  license: apache-2.0
  language:
  - ch
  
  ---
  
  # KnowPrompt:Knowledge-aware Prompt-tuning with Synergistic Optimization for Relation Extraction
  KnowPrompt is used for relational extraction tasks, injecting  latent knowledge contained in relation labels into prompt construction with learnable virtual template words and answer words , and synergistically optimize their representation with structured constraints.
  
  ## Model description
  
   We take the first step to inject latent knowledge contained in relation labels into prompt construction，the knowledge extraction is then implemented with a Prompt-tuning model。The implementation is as follows：virtual template words around entities initialized using aggregate entity embeddings are used as learnable virtual template words to inject entity  knowledge; Meanwhile, we leverage label to compute average embeddings as virtual answers words to inject relationship knowledge. In this structure, entities and relations are mutually constrained and virtual template and answer words should be contextually relevant, so we introduce synergistic optimization to correct virtual template and answer words.
  ![image.png](./model.png)
  
  ## Intended uses & limitations
  
  This model is used for relationship extraction tasks, and the extracted information can be used for more downstream NLP tasks, such as: information retrieval, conversation generation and Q&A. Please refer to the code example for details on how to use it.
  
  The relationship labels in the model training data are limited and can only generalize the relationships in the real world to a certain extent.
  
  ## Training data
  
  We adopt SemEval as the dataset
  
  | **Dataset** | **# Train.** | **# Val.** | **# Test.** | **# Rel.** |
  | ----------- | ------------ | ---------- | ----------- | ---------- |
  | SemEval     | 6,507        | 1,493      | 2,717       | 19         |
  
  ## Training procedure
  
  ### Training
  
  The training is divided into two phases, and the first phase performs collaborative optimization of virtual template words and answer words
  $$
  \mathcal{J}=\mathcal{J}_{[\text {MASK }]}+\lambda \mathcal{J}_{\text {structured }},
  $$
  
   $\lambda$is the hyperparameter for weighing the two loss functions;The second stage optimizes all parameters with a smaller learning rate based on the optimized virtual template words and answer words, using only the loss function $\mathcal{J}_{texttt{[MASK]}}$to finetu