Swing Algorithm Tool

Updated at:

Swing is an item recall algorithm originally developed by Alibaba. It computes item-to-item (I2I) similarity from network structure information. This makes Swing highly robust to noise.

Overview of the Swing algorithm

Swing is a matching algorithm. Traditional node proximity measures, such as Common Neighbors, Adamic/Adar, Cosine Similarity, Jaccard Similarity, WB-cosine, and Rooted PageRank, are based on individual nodes. Swing instead takes network structure information into account. It extends the analysis to two-hop neighbors in a high-dimensional network structure. It is significantly more accurate than traditional collaborative filtering.

Swing I2I provides foundational data for a wide range of recommendation scenarios on the Taobao app and on PC. Swing has also been used in Alibaba Group's Tiantian Dongting music app and in Alimama advertising, where it produced significant improvements.

Prepare sample data

  1. Create the input table.

CREATE TABLE IF NOT EXISTS swing_test_input
(
  user_id   bigint,
  item_list string  -- The click sequence is a required field and must contain at least the item_id, timestamp, and norm fields.
)
lifecycle 7;

The sample data uses the following format: the click sequence is a semicolon-separated (;) list of clicked items. Each item in the list consists of at least three comma-separated fields: item_id, norm, and timestamp. item_id must be the first field. The timestamp field must follow the %Y%m%d%H%M%S format. If timestamps are not required, you can use the same timestamp for all items.

The norm field represents the recent popularity of an item, such as the number of users who have clicked it. If this field is not required, you can set it to 1 for all items. The item_list must be ordered chronologically by click time, from oldest to newest.

Note: The item_id field must have a numeric type.

  1. Create the output table.

CREATE TABLE IF NOT EXISTS swing_test_result
(
    item_id   BIGINT COMMENT 'Anchor item ID',
    item_list STRING COMMENT 'List of similar items'
)
LIFECYCLE 7;

The output table has two columns: item_id and item_list. The item_list is in the format item_id1,score1,coccur1,ori_score1;item_id2,score2,coccur2,ori_score2. Here, ori_score1 is the original similarity score, score1 is the score after max normalization, and coccur1 is the co-occurrence count.

PAI command

Submit the following Platform for AI (PAI) command to run the Swing algorithm:

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'

Parameters

Parameter

Description

Type and default

inputTable

The input table: user click sequences. The table can be a partitioned table or a non-partitioned table.

Use a forward slash (/) to concatenate the table name and the partition value.

outputTable

The output table: the I2I index. The table can be a partitioned table or a non-partitioned table.

Use a forward slash (/) to concatenate the table name and the partition value.

maxClickPerUser

The maximum length of a user's behavior sequence. If a sequence exceeds this length, it is truncated to keep only the most recent interactions.

Integer. Default: 600.

maxTimeSpan

The maximum time interval in days between two clicks for the items to be considered neighbors.

Integer. Default: 1.

maxUserPerItem

The maximum number of user click sequences to use for calculating the k-nearest neighbors of each item.

Integer. Default: 700.

topk

The number of k-nearest neighbors to retain for each trigger item.

Integer. Default: 200.

alpha1

A Swing algorithm parameter. See formula [1].

Integer. Default: 5.

beta

A Swing algorithm parameter. See formula [1].

Real number. Default: 0.3.

alpha2

A Swing algorithm parameter. See formula [1].

Integer. Default: 1.

pos_time

The zero-based index of the field corresponding to the timestamp. In the preceding sample data, this is 2.

Integer. Default: 2.

pos_norm

The zero-based index of the field corresponding to item popularity (norm). In the preceding sample data, this is 1.

Integer. Default: 1.

Formula [1]:

Improved Swing algorithm

Improved SWING similarity calculation algorithm

Schema support

MaxCompute has been upgraded to support schemas. Schemas introduce the three-level project.schema.table structure. For more information, see Schema operations.

Use a schema

  1. Create the input table and the output table.

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 standard input partitioned table'
PARTITIONED BY 
(
    ds string
)
LIFECYCLE 7
;
CREATE TABLE IF NOT EXISTS home_feed_swing_i2i_output_v1
(
    item_id string COMMENT 'Item ID'
    ,item_list string COMMENT 'top similiar items: item_id1,score1,coccur1;item_id2,score2,coccur2'
)
PARTITIONED BY
(
    ds string
)
LIFECYCLE 7
;
  1. Run the PAI command for the Swing algorithm.

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';

Parameters

Parameter

Description

Type and default

inputTable

The input table: user click sequences.

The format is odps://{project_name}/{schema}/tables/{table_name}/{patition}=${bdp.system.bizdate}.

Use a forward slash (/) to concatenate the table name and the partition value.

outputTable

The output table: the I2I index.

The format is odps://{project_name}/{schema}/tables/{table_name}/{patition}=${bdp.system.bizdate}.

Use a forward slash (/) to concatenate the table name and the partition value.

In the preceding format, {project_name}, {schema}, and {table_name} are the MaxCompute project name, the MaxCompute schema name, and the table name. ${bdp.system.bizdate} is the DataWorks scheduling parameter for the business date.

The other parameters are the same as those in the "Do not use a schema" section.

Do not use a schema

  1. Create the input table and the output table.

CREATE TABLE IF NOT EXISTS home_feed_swing_i2i_standard_input_v1 
(
    user_id string
    ,item_list string
)
COMMENT 'Swing I2I standard input partitioned table'
PARTITIONED BY 
(
    ds string
)
LIFECYCLE 7
;

CREATE TABLE IF NOT EXISTS home_feed_swing_i2i_output_v1
(
    item_id string COMMENT 'Item ID'
    ,item_list string COMMENT 'top similiar items: item_id1,score1,coccur1;item_id2,score2,coccur2'
)c
PARTITIONED BY
(
    ds string
)
LIFECYCLE 7
;
  1. Run the PAI command for the Swing algorithm.

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';

Parameters

Parameter

Description

Type and default

inputTable

The input table: user click sequences.

The format is odps://{project_name}/tables/{table_name}/{patition}=${bdp.system.bizdate}.

Use a forward slash (/) to concatenate the table name and the partition value.

outputTable

The output table: the I2I index.

The format is odps://{project_name}/tables/{table_name}/{patition}=${bdp.system.bizdate}.

Use a forward slash (/) to concatenate the table name and the partition value.