全部產品
Search
文件中心

Platform For AI:Swing演算法工具

更新時間:Aug 19, 2026

Swing 是阿里巴巴原創的一種新的match演算法,不同於傳統基於“點”的節點親密度(proximity)計算方式,如:Common Neighbors, Adamic/Adar, Cosine Similarity, Jaccard Similarity, Wb-cosine, Rooted PageRank等,Swing會考慮網路結構資訊,以高維的網路結構向二跳節點擴充,抗噪能力強,相比傳統的CF準確性有大幅的提升。目前Swing i2i作為最重要的基礎資料,廣泛應用於手淘和PC眾多的推薦情境中; 除此之外,swing還應用於集團天天動聽和阿里媽媽廣告業務中,並有顯著提升。

Swing演算法簡介

Swing 是阿里巴巴原創的一種新的match演算法,不同於傳統基於“點”的節點親密度(proximity)計算方式,如:Common Neighbors, Adamic/Adar, Cosine Similarity, Jaccard Similarity, Wb-cosine, Rooted PageRank等,Swing會考慮網路結構資訊,以高維的網路結構向二跳節點擴充,抗噪能力強,相比傳統的CF準確性有大幅的提升。

目前Swing i2i作為最重要的基礎資料,廣泛應用於手淘和PC眾多的推薦情境中; 除此之外,swing還應用於集團天天動聽和阿里媽媽廣告業務中,並有顯著提升。

樣本準備

  1. 建立輸入表

CREATE TABLE IF NOT EXISTS swing_test_input
(
  user_id   bigint,
  item_list string  --點擊序列為必須欄位,且至少提供item_id, timestamp和norm欄位
)
lifecycle 7;

資料範例如下所示。點擊序列由分號分割,每個點擊商品由至少3個欄位構成,即item_id, norm,timestamp.其中item_id需要在開頭,timestamp遵循%Y%m%d%H%M%S的格式,如不需要可以用同一個timestamp填充。norm代表商品近期的熱度(點擊量),如不需要,可以統一用1填充。應按照點擊時間順序由遠至今組織item_list。

備忘:item_id 必須為數字類型。

  1. 建立輸出表

CREATE TABLE IF NOT EXISTS swing_test_result
(
    item_id   BIGINT COMMENT 'anchor物品ID',
    item_list STRING COMMENT '相似物品列表'
)
LIFECYCLE 7;

輸出表分為兩列:item_id和item_list,其中 item_list 形如item_id1,score1,coccur1,ori_score1;item_id2,score2,coccur2,ori_score2。其中,ori_score1是原始相似性分數;score1是最大值歸一化之後的分數;coccur1是共現次數。

PAI命令列

pai -name swing_rec_ext 
    -project algo_public 
    -DinputTable='swing_test_input/ds=20250809'
    -DoutputTable='swing_test_result/ds=20250809'
    -DmaxClickPerUser='500'
    -DmaxUserPerItem='600'
    -Dtopk='100'
    -Dalpha1='5'
    -Dalpha2='1'
    -Dbeta='0.3'

演算法參數

參數名稱

參數描述

參數類型

inputTable

輸入表:使用者點擊序列;支援分區表/非分區表

用/拼接表名和分區值

outputTable

輸出表:i2i索引;支援分區表/非分區表

用/拼接表名和分區值

maxClickPerUser

每個使用者的最長序列長度,如果超過該長度會對最近進行截斷保留

整數,預設值為600

maxTimeSpan

認為兩個商品存在鄰居關係的最長點擊間隔天數

整數,預設值為1

maxUserPerItem

每個商品使用多少個使用者的點擊序列來計算k近鄰

整數,預設值為700

topk

每個trigger商品保留的k近鄰數目

整數,預設值為200

alpha1

swing演算法參數,見公式[1]

整數,預設值為5

beta

swing演算法參數,見公式[1]

實數,預設值為0.3

alpha2

swing演算法參數,見公式[1]

整數,預設值為1

pos_time

timestamp對應的欄位編號,從0開始,在上述範例中為2

整數,預設值為2

pos_norm

商品熱度所對應欄位,從0開始,在上述範例中1

整數,預設值為1

公式[1]:

改進版Swing演算法

改進版swing相似性計算演算法

支援Schema層級

MaxCompute升級到支援schema Schema操作

使用schema

  1. 建立輸入表和輸出表

set odps.namespace.schema=true;
use schema rec_sch_test;
CREATE TABLE IF NOT EXISTS home_feed_swing_i2i_standard_input_v1 
(
    user_id string
    ,item_list string
)
COMMENT 'swing I2I 標準輸入分區表'
PARTITIONED BY 
(
    ds string
)
LIFECYCLE 7
;
CREATE TABLE IF NOT EXISTS home_feed_swing_i2i_output_v1
(
    item_id string COMMENT '物品ID'
    ,item_list string COMMENT 'top similiar items: item_id1,score1,coccur1;item_id2,score2,coccur2'
)
PARTITIONED BY
(
    ds string
)
LIFECYCLE 7
;
  1. swing演算法的pai命令

set odps.sql.udf.timeout=2800;
set odps.sql.mapper.memory=8192;
set odps.sql.udf.python.memory=2048;
set odps.sql.mapper.split.size=4;
set odps.sql.reducer.instances=5387;
set odps.sql.reducer.memory=6144;

PAI -name swing_rec_ext
-project algo_public
-DinputTable='odps://pairec_demo_test/rec_sch_test/tables/home_feed_swing_i2i_standard_input_v1/ds=${bdp.system.bizdate}'
-DoutputTable='odps://pairec_demo_test/rec_sch_test/tables/home_feed_swing_i2i_output_v1/ds=${bdp.system.bizdate}'
-DmaxClickPerUser='600'
-DmaxUserPerItem='700'
-Dtopk='500'
-Dalpha1='5'
-Dalpha2='1'
-Dbeta='0.3'
-Ditem_id_is_int='False';

參數

參數名稱

參數描述

參數類型

inputTable

輸入表:使用者點擊序列;

格式odps://{project_name}/{schema}/tables/{table_name}/{patition}=${bdp.system.bizdate}

用/拼接表名和分區值

outputTable

輸出表:i2i索引;

格式odps://{project_name}/{schema}/tables/{table_name}/{patition}=${bdp.system.bizdate}

用/拼接表名和分區值

其他參數與不使用schema一致

不使用schema

  1. 建立輸入表和輸出表

CREATE TABLE IF NOT EXISTS home_feed_swing_i2i_standard_input_v1 
(
    user_id string
    ,item_list string
)
COMMENT 'swing I2I 標準輸入分區表'
PARTITIONED BY 
(
    ds string
)
LIFECYCLE 7
;

CREATE TABLE IF NOT EXISTS home_feed_swing_i2i_output_v1
(
    item_id string COMMENT '物品ID'
    ,item_list string COMMENT 'top similiar items: item_id1,score1,coccur1;item_id2,score2,coccur2'
)c
PARTITIONED BY
(
    ds string
)
LIFECYCLE 7
;
  1. swing演算法的pai命令

set odps.sql.udf.timeout=2800;
set odps.sql.mapper.memory=8192;
set odps.sql.udf.python.memory=2048;
set odps.sql.mapper.split.size=4;
set odps.sql.reducer.instances=5387;
set odps.sql.reducer.memory=6144;

PAI -name swing_rec_ext                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
-project algo_public
-DinputTable='odps://pairec_demo_test/tables/home_feed_swing_i2i_standard_input_v1/ds=${bdp.system.bizdate}'
-DoutputTable='odps://pairec_demo_test/tables/home_feed_swing_i2i_output_v1/ds=${bdp.system.bizdate}'
-DmaxClickPerUser='600'
-DmaxUserPerItem='700'
-Dtopk='500'
-Dalpha1='5'
-Dalpha2='1'
-Dbeta='0.3';

參數

參數名稱

參數描述

參數類型

inputTable

輸入表:使用者點擊序列;

格式:odps://{project_name}/tables/{table_name}/{patition}=${bdp.system.bizdate}

用/拼接表名和分區值

outputTable

輸出表:i2i索引;

格式:odps://{project_name}/tables/{table_name}/{patition}=${bdp.system.bizdate}

用/拼接表名和分區值