recbole.data.interaction

class recbole.data.interaction.Interaction(interaction)[source]

Bases: object

The basic class representing a batch of interaction records.

Note

While training, there is no strict rules for data in one Interaction object.

While testing, it should be guaranteed that all interaction records of one single user will not appear in different Interaction object, and records of the same user should be continuous. Meanwhile, the positive cases of one user always need to occur earlier than this user’s negative cases.

A correct example:

user_id

item_id

label

1

2

1

1

6

1

1

3

1

1

1

0

2

3

1

Some wrong examples for Interaction objects used in testing:

  1. user_id

    item_id

    label

    1

    2

    1

    1

    6

    0

    # positive cases of one user always need to

    occur earlier than this user’s negative cases

    1

    3

    1

    1

    1

    0

    2

    3

    1

  2. user_id

    item_id

    label

    1

    2

    1

    1

    6

    1

    1

    3

    1

    2

    3

    1

    # records of the same user should be continuous.

    1

    1

    0

interaction

keys are meaningful str (also can be called field name), and values are Torch Tensor of numpy Array with shape (batch_size, *).

Type

dict or pandas.DataFrame

add_prefix(prefix)[source]

Add prefix to current interaction’s columns.

Parameters

prefix (str) – The prefix to be added.

property columns

Returns: list of str: The columns of interaction.

cpu()[source]

Transfer Tensors in this Interaction object to cpu.

Returns

a coped Interaction object with Tensors which are sent to cpu.

Return type

Interaction

drop(column)[source]

Drop column in interaction.

Parameters

column (str) – the column to be dropped.

numpy()[source]

Transfer Tensors to numpy arrays.

Returns

keys the same as Interaction object, are values are corresponding numpy arrays transformed from Tensor.

Return type

dict

repeat(sizes)[source]

Repeats each tensor along the batch dim.

Parameters

sizes (int) – repeat times.

Example

>>> a = Interaction({'k': torch.zeros(4)})
>>> a.repeat(3)
The batch_size of interaction: 12
    k, torch.Size([12]), cpu
>>> a = Interaction({'k': torch.zeros(4, 7)})
>>> a.repeat(3)
The batch_size of interaction: 12
    k, torch.Size([12, 7]), cpu
Returns

a copyed Interaction object with repeated Tensors.

repeat_interleave(repeats, dim=0)[source]

Similar to repeat_interleave of PyTorch.

Details can be found in:

Note

torch.repeat_interleave() is supported in PyTorch >= 1.2.0.

shuffle()[source]

Shuffle current interaction inplace.

sort(by, ascending=True)[source]

Sort the current interaction inplace.

Parameters
  • by (str or list of str) – Field that as the key in the sorting process.

  • ascending (bool or list of bool, optional) – Results are ascending if True, otherwise descending. Defaults to True

to(device, selected_field=None)[source]

Transfer Tensors in this Interaction object to the specified device.

Parameters
  • device (torch.device) – target device.

  • selected_field (str or iterable object, optional) – if specified, only Tensors

  • device. (with keys in selected_field will be sent to) –

Returns

a coped Interaction object with Tensors which are sent to the specified device.

Return type

Interaction

update(new_inter)[source]

Similar to dict.update()

Parameters

new_inter (Interaction) – current interaction will be updated by new_inter.

recbole.data.interaction.cat_interactions(interactions)[source]

Concatenate list of interactions to single interaction.

Parameters

interactions (list of Interaction) – List of interactions to be concatenated.

Returns

Concatenated interaction.

Return type

Interaction