国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Elasticsearch(實(shí)踐一)相似度方法L1、L2 、cos

這篇具有很好參考價(jià)值的文章主要介紹了Elasticsearch(實(shí)踐一)相似度方法L1、L2 、cos。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

在文本使用三維向量的相似度時(shí),對(duì)三種相似度的對(duì)比。 當(dāng)前基于已經(jīng)搭建好的Elasticsearch、Kibana。?

1、創(chuàng)建索引庫(kù)

PUT my-index-000002
{
  "mappings": {
    "properties": {
      "my_dense_vector": {
        "type": "dense_vector",
        "dims": 3
      },
      "status" : {
        "type" : "keyword"
      }
    }
  }
}

創(chuàng)建成功:

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "my-index-000002"
}

2、放入數(shù)據(jù)

PUT my-index-000002/_doc/1
{
  "my_dense_vector": [1, 0,0],
  "status" : "published"
}
PUT my-index-000002/_doc/2
{
  "my_dense_vector": [0,1,0],
  "status" : "published"
}
PUT my-index-000002/_doc/3
{
  "my_dense_vector": [0,0,1],
  "status" : "published"
}

返回結(jié)果類似如下

{
  "_index": "my-index-000002",
  "_id": "3",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 1
}

3、查看所有數(shù)據(jù)

GET my-index-000002/_search

結(jié)果如下:?

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "my-index-000002",
        "_id": "1",
        "_score": 1,
        "_source": {
          "my_dense_vector": [
            1,
            0,
            0
          ],
          "status": "published"
        }
      },
      {
        "_index": "my-index-000002",
        "_id": "2",
        "_score": 1,
        "_source": {
          "my_dense_vector": [
            0,
            1,
            0
          ],
          "status": "published"
        }
      },
      {
        "_index": "my-index-000002",
        "_id": "3",
        "_score": 1,
        "_source": {
          "my_dense_vector": [
            0,
            0,
            1
          ],
          "status": "published"
        }
      }
    ]
  }
}

4、L1方法查詢數(shù)據(jù)

GET my-index-000002/_search
{
  "query": {
    "script_score": {
      "query" : {
        "bool" : {
          "filter" : {
            "term" : {
              "status" : "published"
            }
          }
        }
      },
      "script": {
        "source": "1 / (1 + l1norm(params.queryVector, 'my_dense_vector'))",
        "params": {
          "queryVector": [0, 0, 1]
        }
      }
    }
  }
}
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "my-index-000002",
        "_id": "3",
        "_score": 1,
        "_source": {
          "my_dense_vector": [
            0,
            0,
            1
          ],
          "status": "published"
        }
      },
      {
        "_index": "my-index-000002",
        "_id": "1",
        "_score": 0.33333334,
        "_source": {
          "my_dense_vector": [
            1,
            0,
            0
          ],
          "status": "published"
        }
      },
      {
        "_index": "my-index-000002",
        "_id": "2",
        "_score": 0.33333334,
        "_source": {
          "my_dense_vector": [
            0,
            1,
            0
          ],
          "status": "published"
        }
      }
    ]
  }
}

結(jié)果中,id1和id2得分相同,但在文本向量空間中他們不同。

5、使用l2查詢

GET my-index-000002/_search
{
  "query": {
    "script_score": {
      "query" : {
        "bool" : {
          "filter" : {
            "term" : {
              "status" : "published"
            }
          }
        }
      },
      "script": {
        "source": "1 / (1 + l2norm(params.queryVector, 'my_dense_vector'))",
        "params": {
          "queryVector": [0, 0, 1]
        }
      }
    }
  }
}
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "my-index-000002",
        "_id": "3",
        "_score": 1,
        "_source": {
          "my_dense_vector": [
            0,
            0,
            1
          ],
          "status": "published"
        }
      },
      {
        "_index": "my-index-000002",
        "_id": "1",
        "_score": 0.41421357,
        "_source": {
          "my_dense_vector": [
            1,
            0,
            0
          ],
          "status": "published"
        }
      },
      {
        "_index": "my-index-000002",
        "_id": "2",
        "_score": 0.41421357,
        "_source": {
          "my_dense_vector": [
            0,
            1,
            0
          ],
          "status": "published"
        }
      }
    ]
  }
}

同樣出現(xiàn)相同情況,l1和l2計(jì)算文本的距離有相同得分

6、cos 查詢

GET my-index-000002/_search
{
  "query": {
    "script_score": {
      "query" : {
        "bool" : {
          "filter" : {
            "term" : {
              "status" : "published"       
            }
          }
        }
      },
      "script": {
        "source": "cosineSimilarity(params.query_vector, 'my_dense_vector') + 1.0",    
        "params": {
          "query_vector": [0, 0, 1]      
        }
      }
    }
  }
}

結(jié)果

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 2,
    "hits": [
      {
        "_index": "my-index-000002",
        "_id": "3",
        "_score": 2,
        "_source": {
          "my_dense_vector": [
            0,
            0,
            1
          ],
          "status": "published"
        }
      },
      {
        "_index": "my-index-000002",
        "_id": "1",
        "_score": 1,
        "_source": {
          "my_dense_vector": [
            1,
            0,
            0
          ],
          "status": "published"
        }
      },
      {
        "_index": "my-index-000002",
        "_id": "2",
        "_score": 1,
        "_source": {
          "my_dense_vector": [
            0,
            1,
            0
          ],
          "status": "published"
        }
      }
    ]
  }
}

三種方法都會(huì)產(chǎn)生 不同向量的相同分?jǐn)?shù)情況

GET my-index-000002/_search
{
  "query": {
    "script_score": {
      "query" : {
        "bool" : {
          "filter" : {
            "term" : {
              "status" : "published"       
            }
          }
        }
      },
      "script": {
        "source": "cosineSimilarity(params.query_vector, 'my_dense_vector') + 1.0",    
        "params": {
          "query_vector": [0, 0, 100]      
        }
      }
    }
  }
}

結(jié)果:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 2,
    "hits": [
      {
        "_index": "my-index-000002",
        "_id": "3",
        "_score": 2,
        "_source": {
          "my_dense_vector": [
            0,
            0,
            1
          ],
          "status": "published"
        }
      },
      {
        "_index": "my-index-000002",
        "_id": "1",
        "_score": 1,
        "_source": {
          "my_dense_vector": [
            1,
            0,
            0
          ],
          "status": "published"
        }
      },
      {
        "_index": "my-index-000002",
        "_id": "2",
        "_score": 1,
        "_source": {
          "my_dense_vector": [
            0,
            1,
            0
          ],
          "status": "published"
        }
      }
    ]
  }
}

三種方法都會(huì)存在 不同空間位置,得到向量距離可能相同的情況文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-784637.html

到了這里,關(guān)于Elasticsearch(實(shí)踐一)相似度方法L1、L2 、cos的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • L1正則化和L2正則化

    L1正則化和L2正則化

    在機(jī)器學(xué)習(xí)以及深度學(xué)習(xí)中我們經(jīng)常會(huì)看到正則化這一名詞,下面就淺談一下什么是正則化?以及正則化的意義所在? 正則化項(xiàng) (又稱懲罰項(xiàng)),懲罰的是模型的參數(shù),其值恒為非負(fù) λ是正則化系數(shù),是一個(gè)超參數(shù),調(diào)節(jié)懲罰的力度,越大則懲罰力度越大。 先上圖: 上圖從左

    2024年02月07日
    瀏覽(22)
  • L1范數(shù),L2范數(shù),L2,1范數(shù)(向量范數(shù)、矩陣范數(shù)、正則化)

    L1范數(shù),L2范數(shù),L2,1范數(shù)(向量范數(shù)、矩陣范數(shù)、正則化)

    參考文章如下:https://blog.csdn.net/lqzdreamer/article/details/79676305 ? ? ? ? ? ? ? ? ? ? ? ? ?https://blog.csdn.net/lqzdreamer/article/details/79676305 ? ? ? ? 一般常用范數(shù)來(lái)衡量向量,向量的Lp范數(shù)定義為: ?????????Lp范數(shù)示意圖: ? ? ? ? 從圖中可以看出,p的取值在 [0,1) 之間,范數(shù)

    2023年04月09日
    瀏覽(23)
  • 2023 PTA天梯賽補(bǔ)題(L1 & L2)

    2023 PTA天梯賽補(bǔ)題(L1 & L2)

    輸入輸出題 輸入輸出題 k == n 和 k == m 分別輸出,題目怎么說(shuō)就怎么做 判斷一下c 等于a + b還是a*b或者都不是,分別按要求輸出 針對(duì)每一群玩游戲的寶寶,枚舉判斷一下就好了 寫的有點(diǎn)煩,基本就是一步一步模擬,思路在注釋里寫了 枚舉分配方案,代碼中a代表女生寢室的數(shù)

    2024年02月03日
    瀏覽(44)
  • L1、L2正則化的原理及適用場(chǎng)景

    L1、L2正則化的原理及適用場(chǎng)景

    1.1 含義 權(quán)值向量??中各元素的絕對(duì)值之和,一般記作?? 。 1.2? 公式表示 添加了L1正則化的損失函數(shù)一般可表示為: 1.3 作用 L1正則常被用來(lái)解決過(guò)擬合問(wèn)題; L1正則化容易產(chǎn)生稀疏權(quán)值矩陣(更容易得到稀疏解),即產(chǎn)生一個(gè)稀疏模型(較多參數(shù)為0),因此也可用于特征

    2024年02月09日
    瀏覽(17)
  • 股票接口L2是什么意思?和L1有哪些區(qū)別

    股票接口L2是什么意思,L2股票指二級(jí)市場(chǎng),二級(jí)報(bào)價(jià)是證券公司的高級(jí)報(bào)價(jià)功能,包括十檔行情等功能,投資者可以聯(lián)系其業(yè)務(wù)部門或直接通過(guò)互聯(lián)網(wǎng)申請(qǐng)開業(yè),需要一定的費(fèi)用。 L1是免費(fèi),L2平臺(tái)會(huì)收取一定的費(fèi)用,用戶買賣股票一定要選擇正規(guī)的平臺(tái),在正規(guī)平臺(tái)投資股

    2024年02月11日
    瀏覽(16)
  • 從貝葉斯派的角度去看L1和L2

    從貝葉斯派的角度去看L1和L2

    前沿 推導(dǎo)的兩個(gè)角度 帶約束條件的優(yōu)化求解(拉格朗日乘子法) 貝葉斯學(xué)派的:最大后驗(yàn)概率 理解的兩個(gè)角度 貝葉斯學(xué)派的角度,L2參數(shù)符合高斯先驗(yàn),L1參數(shù)符合laplace先驗(yàn)。 從有約束問(wèn)題角度,用拉格朗日轉(zhuǎn)換成無(wú)約束問(wèn)題后,轉(zhuǎn)換成求最小值和約束交點(diǎn)問(wèn)題。l1在幾何

    2024年02月08日
    瀏覽(20)
  • LLM - LLaMA-2 獲取文本向量并計(jì)算 Cos 相似度

    LLM - LLaMA-2 獲取文本向量并計(jì)算 Cos 相似度

    目錄 一.引言 二.獲取文本向量 1.hidden_states 與 last_hidden_states ◆?hidden_states ◆?last_hidden_states? 2.LLaMA-2 獲取 hidden_states ◆ model config? ◆ get Embedding 三.獲取向量 Cos 相似度 1.向量選擇 2.Cos 相似度 3.BERT-whitening 特征白化 4.評(píng)估指標(biāo)對(duì)比 四.總結(jié) 前面提到了兩種基于統(tǒng)計(jì)的機(jī)器翻

    2024年02月10日
    瀏覽(20)
  • 剪枝基礎(chǔ)與實(shí)戰(zhàn)(2): L1和L2正則化及BatchNormalization講解

    剪枝基礎(chǔ)與實(shí)戰(zhàn)(2): L1和L2正則化及BatchNormalization講解

    CIFAR10 是深度學(xué)習(xí)入門最先接觸到的數(shù)據(jù)集之一,主要用于圖像分類任務(wù)中,該數(shù)據(jù)集總共有 10個(gè) 類別。 圖片數(shù)量: 6w 張 圖片寬高: 32x32 圖片類別:10 Trainset: 5w 張,5 個(gè)訓(xùn)練塊 Testset: 1w 張,1 個(gè)測(cè)試塊 Pytorch 集成了很多常見數(shù)據(jù)集的API, 可以通過(guò)pytorch 來(lái)下載這些數(shù)據(jù)集,

    2024年02月11日
    瀏覽(21)
  • zkSync2.0一個(gè) L2 塊和一個(gè) L1 匯總塊深入分析

    目錄 通常的rollup的工作流程如下 zkSync rollup 操作的生命周期 如下:

    2024年02月01日
    瀏覽(18)
  • 通達(dá)信l1l2行情接口-十檔行情有哪些優(yōu)勢(shì)?

    據(jù)提供系統(tǒng)或用戶編制的條件選股公式進(jìn)行選股選定一個(gè)條件選股公式或多個(gè)組合條件后,計(jì)算機(jī)自動(dòng)幫您選出當(dāng)時(shí)或歷史上某一段時(shí)間內(nèi)滿足條件的所有股票十檔行情 英文,列在行情下載顯示窗口,同時(shí)可保留成板塊。 那通達(dá)信l1l2行情接口-十檔行情有哪些優(yōu)勢(shì)? 1、主要

    2024年01月16日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包