-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfunction.py
157 lines (125 loc) · 5.24 KB
/
function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright (c) 2023, Haruka Kiyohara, Ren Kishimoto, HAKUHODO Technologies Inc., and Hanjuku-kaso Co., Ltd. All rights reserved.
# Licensed under the Apache 2.0 License.
"""Mathematical Functions used in Recommender System (REC) ."""
from dataclasses import dataclass
from typing import Optional
import numpy as np
from sklearn.utils import check_scalar, check_random_state
from .base import BaseUserModel
from ...utils import sigmoid
from ...types import Action
@dataclass
class UserModel(BaseUserModel):
"""Class to define a user model based on user_preference_dynamics and reward_function.
Bases: :class:`recgym.BaseUserModel`
Imported as: :class:`recgym.envs.UserModel`
Tip
-------
Use :class:`BaseUserModel` to define a custom UserModel.
Parameters
-------
user_feature_dim: int
Dimension of the user feature vectors. (API consistency.)
item_feature_dim: int
Dimension of the item feature vectors.
reward_type: {"continuous", "binary"}, default="continuous"
Reward type.
reward_std: float, default=0.0 (>=0)
Noise level of the reward. Applicable only when reward_type is "continuous".
random_state: int, default=None (>= 0)
Random state.
References
-------
Sarah Dean, Jamie Morgenstern.
"Preference Dynamics Under Personalized Recommendations." 2022.
"""
user_feature_dim: int
item_feature_dim: int
reward_type: str = "continuous" # "binary"
reward_std: float = 0.0
random_state: Optional[int] = None
def __post_init__(self):
check_scalar(
self.user_feature_dim,
name="user_feature_dim",
target_type=int,
min_val=1,
)
check_scalar(
self.item_feature_dim,
name="item_feature_dim",
target_type=int,
min_val=1,
)
check_scalar(
self.reward_std,
name="reward_std",
target_type=float,
min_val=0.0,
)
if self.reward_type not in ["continuous", "binary"]:
raise ValueError(
f'reward_type must be either "continuous" or "binary", but {self.reward_type} is given'
)
self.random_ = check_random_state(self.random_state)
def user_preference_dynamics(
self,
state: np.ndarray,
action: Action,
item_feature_vector: np.ndarray,
alpha: float = 1.0,
) -> np.ndarray:
"""Function that determines the user state transition (i.e., user preference) based on the recommended item. user_feature is amplified by the recommended item_feature
Parameters
-------
state: array-like of shape (user_feature_dim, )
A vector representing user preference. The preference changes over time in an episode depending on the actions presented by the RL agent.
When the true state is unobservable, you can gain observation instead of state.
action: int or array-like of shape (1, )
Indicating which item to present to the user.
item_feature_vector: array-like of shape (n_items, item_feature_dim), default=None
Feature vectors that characterize each item.
alpha: float, default = 1.0 (0=<alpha=<1)
Step size controlling how fast the user preference evolves over time.
Returns
-------
state: array-like of shape (user_feature_dim, )
A vector representing user preference. The preference changes over time in an episode depending on the actions presented by the RL agent.
When the true state is unobservable, you can gain observation instead of state.
"""
coefficient = state @ item_feature_vector[action]
state = state + alpha * coefficient * item_feature_vector[action]
state = state / np.linalg.norm(state, ord=2)
return state
def reward_function(
self,
state: np.ndarray,
action: Action,
item_feature_vector: np.ndarray,
) -> float:
"""Reward function. inner product of state and recommended item_feature
Parameters
-------
state: array-like of shape (user_feature_dim, )
A vector representing user preference. The preference changes over time in an episode depending on the actions presented by the RL agent.
When the true state is unobservable, you can gain observation instead of state.
action: int or array-like of shape (1, )
Indicating which item to present to the user.
item_feature_vector: array-like of shape (n_items, item_feature_dim), default=None
Feature vectors that characterize each item.
Returns
-------
reward: float
User engagement signal as a reward. Either binary or continuous.
"""
logit = state @ item_feature_vector[action]
mean_reward_function = (
logit if self.reward_type == "continuous" else sigmoid(logit)
)
if self.reward_type == "continuous":
reward = self.random_.normal(
loc=mean_reward_function, scale=self.reward_std
)
else:
reward = self.random_.binominal(1, p=mean_reward_function)
return reward