recbole.data.interaction

class recbole.data.interaction.Interaction(interaction, pos_len_list=None, user_len_list=None)[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

pos_len_list

length of the list is the number of users in this batch, each value represents the number of a user’s positive records. The order of the represented users should correspond to the order in the interaction.

Type

list, optional

user_len_list

length of the list is the number of users in this batch, each value represents the number of a user’s all records. The order of the represented users should correspond to the order in the interaction.

Type

list, optional

cpu()[source]

Transfer Tensors in this Interaction object to cpu.

Returns

a copyed Interaction object with Tensors which are sented to cpu.

Return type

Interaction

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.

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

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

Returns

a copyed Interaction object with Tensors which are sented to the specified device.

Return type

Interaction

update(new_inter)[source]

Similar to dict.update()