Comments (3)
Hi,
I suspect this is because you have to specify the variables you want to use, like so:
public = list(
weights = NULL,
prob = NULL,
p = NULL,
class_name = "ContextualBernoulliBandit2",
initialize = function(weights, prob) {
This is something R6 specific. I tried to adapt your code that way and it I did not get the error when initializing the bandit. (I've had this problem many times before and R is not always clear in this :-))
Here is your full bandit class that returns no errors for me:
ContextualBernoulliBandit2 <- R6::R6Class(
inherit = ContextualBernoulliBandit,
class = FALSE,
public = list(
weights = NULL,
prob = NULL,
p = NULL,
class_name = "ContextualBernoulliBandit2",
initialize = function(weights, prob) {
self$weights <- weights
self$prob <- prob
if (is.vector(weights)) {
self$weights <- matrix(weights, nrow = 1L)
} else {
self$weights <- weights # d x k weight matrix
}
self$d <- nrow(self$weights) # d features
self$k <- ncol(self$weights) # k arms
self$p <- length(self$prob)
},
get_context = function(t) {
# generate d dimensional feature vector, one random feature active at a time
Xa <- sample(c(1,rep(0,self$d-1)), prob = self$p)
context <- list(
X = Xa,
k = self$k,
d = self$d,
p = self$p
)
},
get_reward = function(t, context, action) {
# which arm was selected?
arm <- action$choice
# d dimensional feature vector for chosen arm
Xa <- context$X
# weights of active context
weight <- Xa %*% self$weights
# assign rewards for active context with weighted probs
rewards <- as.double(weight > runif(self$k))
optimal_arm <- which_max_tied(weight)
reward <- list(
reward = rewards[arm],
optimal_arm = optimal_arm,
optimal_reward = rewards[optimal_arm]
)
}
)
)
from contextual.
Thanks!
This indeed solves the issue, but I stumbled upon another mistake on my part which I corrected; actually we don't need to define self$p <- length(self$prob)
(which was wrong anyways) and we can simply write Xa <- sample(c(1,rep(0,self$d-1)), prob = self$prob)
direclty.
from contextual.
Great! Good luck with the rest :-)
If there's any other questions, do not hesitate to ask them of course.
from contextual.
Related Issues (20)
- Save predicted reward for chosen arm (feature request) HOT 2
- Some minor clarifications in the documentation HOT 1
- How to change the discount factor? HOT 2
- Minor update required for EpsilonFirstPolicy object input in documentation article HOT 1
- CRAN packages' problems on R-devel: `[[<-`(NULL, *)
- cannot have more than two simulations per epoch using benchmark MAB policy in offline bandit CMAB policy evaluation HOT 4
- contextual do_parallel dosnt work on MRAN 3.5.1 HOT 3
- Saved the trained agent and hold the thetas unchanged for simulation on new dataset
- Contextual, determinism and setting seeds HOT 1
- Arm choice sequence from the simulation? HOT 1
- minor correction for statement in demo "Demo: Bandits, Propensity Weighting & Simpson’s Paradox in R" HOT 2
- Confusing assignments in method get_reward for OfflineDoublyRobustBandit HOT 2
- Possible bug in Exp3Policy HOT 3
- How to get predictions of new test data?
- Package removed from CRAN
- Example in ContextualBinaryBandit doc does not run HOT 2
- abline() does not draw lines where expected HOT 2
- Typo in documentation for ContextualEpochGreedyPolicy HOT 2
- Setting random seed outside of Simulator HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from contextual.