Here we provide astep-by-step overview of the core functions of the package using an example data set.
Load the example data from the package edl:
## Shape Color Category Frequency2 Frequency1
## 1 cat brown animal 88 6
## 2 rabbit brown animal 88 1
## 3 flower brown plant 6 16
## 4 tree brown plant 6 56
## 5 car brown vehicle 1 88
## 6 bicycle brown vehicle 56 32
This data set lists all unique learning events
(i.e., the types) and their associated frequencies. However,
for a data set to function as input for the learning functions, the
dataframe must include the columns Cues and
Outcomes, and optionally Frequency. Note that
if Frequency is not included, the frequency of each
learning event is assumed to be 1.
First, we construct the columns Cues and
Outcomes for this example simulation. Here we will simulate
how two features Color and Shape may function
as cues for their category Category. We will add a
background cue “BG” to represent the learner. The different cues and
outcomes are separated using an underscore (i.e., “_“). It is possible
to another symbol, but then we will need to indicate this in the various
functions (i.e., RWlearning) with the argument
split.
dat$Cues <- paste("BG", dat$Shape, dat$Color, sep="_")
dat$Outcomes <- paste(dat$Category)
dat$Frequency <- dat$Frequency2
# remove remaining columns to simplify this example:
dat <- dat[, c("Cues", "Outcomes", "Frequency")]
# add ID for learning events:
dat$ID <- 1:nrow(dat)
head(dat)## Cues Outcomes Frequency ID
## 1 BG_cat_brown animal 88 1
## 2 BG_rabbit_brown animal 88 2
## 3 BG_flower_brown plant 6 3
## 4 BG_tree_brown plant 6 4
## 5 BG_car_brown vehicle 1 5
## 6 BG_bicycle_brown vehicle 56 6
Now the data dat defines 36 unique learning events with
their associated frequencies.
##
## animal plant vehicle
## 12 12 12
The training data lists all learning events (i.e., the tokens) in their order of presentation, one learning event per row. If no column with frequencies is specificied or all frequencies are 1, the training data mirrors the data set of learning events. The training data also determines the order in which the learning events are presented to the learning network.
## Cues Outcomes Frequency ID Item Trial
## 1 BG_bicycle_brown vehicle 1 6 item_06 1
## 2 BG_flower_yellow plant 1 21 item_21 2
## 3 BG_car_white vehicle 1 17 item_17 3
## 4 BG_rabbit_gray animal 1 8 item_08 4
## 5 BG_tree_red plant 1 28 item_28 5
## 6 BG_bicycle_gray vehicle 1 12 item_12 6
## [1] 1
##
## animal plant vehicle
## 398 398 398
##
## 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
## 88 88 6 6 1 56 56 56 1 1 88 88 32 32 56 56 56 1 16 16 88 88 6 6 6 6
## 27 28 29 30 31 32 33 34 35 36
## 32 32 32 32 1 1 16 16 16 16
Note that the function createTrainingData could also be
used to train the network on multiple (blocked or randomized) runs. We
refer to the examples in the function helpfile.
The function RWlearning trains the error-driven learning
network. The output wm is a list with weight matrices, a
weight matrix for each learning event (a learning event is basically a
row in the data frame dat). The last weight matrix shows
the connections after processing all data.
Inspection:
## [1] 1194
## [1] 1194
We can now inspect the changes in the weights for after each learning event. The last weight matrix shows the connections after processing all data:
## vehicle
## BG 0.01
## bicycle 0.01
## brown 0.01
## vehicle plant animal
## BG 0.25021582 0.25778756 0.23618274
## bicycle 0.57744680 -0.18298088 -0.26859419
## brown 0.02740814 -0.07492230 0.18524115
## flower -0.20283075 0.53223339 -0.20065913
## yellow -0.05168658 0.20548545 -0.01689575
## car 0.58972146 -0.22743195 -0.23415516
## white 0.01730614 0.08786169 0.03089826
## gray 0.13681112 -0.08512340 0.08670386
## rabbit -0.25633176 -0.19360419 0.56994125
## red 0.07207737 0.06862099 -0.02234211
## tree -0.20242767 0.52517699 -0.19901415
## cat -0.25536226 -0.19560579 0.56866411
## blue 0.04829964 0.05586514 -0.02742267
The function getWM retrieves the weight matrix after a
specific event, and adds zero-weight connections for the not yet
encountered cues and outcomes.
## vehicle plant animal
## BG 0.01 0 0
## bicycle 0.01 0 0
## brown 0.01 0 0
## flower 0.00 0 0
## yellow 0.00 0 0
## car 0.00 0 0
## white 0.00 0 0
## gray 0.00 0 0
## rabbit 0.00 0 0
## red 0.00 0 0
## tree 0.00 0 0
## cat 0.00 0 0
## blue 0.00 0 0
We can use the functions sapply and getWM
to add zero-weight connections to all states of the network:
wm2 <- sapply(1:length(wm), function(x){getWM(wm,x)}, simplify = FALSE)
# inspect the list of states:
length(wm2)## [1] 1194
## vehicle plant animal
## BG 0.01 0 0
## bicycle 0.01 0 0
## brown 0.01 0 0
## flower 0.00 0 0
## yellow 0.00 0 0
## car 0.00 0 0
## white 0.00 0 0
## gray 0.00 0 0
## rabbit 0.00 0 0
## red 0.00 0 0
## tree 0.00 0 0
## cat 0.00 0 0
## blue 0.00 0 0
The functions getWeightsByCue and
getWeightsByOutcome could be used to extract the weights
per cue or per outcome.
## BG bicycle blue brown car cat flower gray rabbit
## 1 0.00000000 0.0000000000 0 0 0e+00 0 0.00 0.0000000000 0.0e+00
## 2 0.01000000 0.0000000000 0 0 0e+00 0 0.01 0.0000000000 0.0e+00
## 3 0.00990000 0.0000000000 0 0 -1e-04 0 0.01 0.0000000000 0.0e+00
## 4 0.00980100 0.0000000000 0 0 -1e-04 0 0.01 -0.0000990000 -9.9e-05
## 5 0.01970299 0.0000000000 0 0 -1e-04 0 0.01 -0.0000990000 -9.9e-05
## 6 0.01950695 -0.0001960399 0 0 -1e-04 0 0.01 -0.0002950399 -9.9e-05
## red tree white yellow
## 1 0.00000000 0.00000000 0e+00 0.00
## 2 0.00000000 0.00000000 0e+00 0.01
## 3 0.00000000 0.00000000 -1e-04 0.01
## 4 0.00000000 0.00000000 -1e-04 0.01
## 5 0.00990199 0.00990199 -1e-04 0.01
## 6 0.00990199 0.00990199 -1e-04 0.01
## BG bicycle blue brown car cat flower
## 1189 0.2529870 -0.1830920 0.05586514 -0.08100835 -0.227432 -0.1958877 0.5293124
## 1190 0.2530982 -0.1829809 0.05586514 -0.08089721 -0.227432 -0.1958877 0.5293124
## 1191 0.2533801 -0.1829809 0.05586514 -0.08089721 -0.227432 -0.1956058 0.5293124
## 1192 0.2564341 -0.1829809 0.05586514 -0.07784327 -0.227432 -0.1956058 0.5293124
## 1193 0.2593550 -0.1829809 0.05586514 -0.07492230 -0.227432 -0.1956058 0.5322334
## 1194 0.2577876 -0.1829809 0.05586514 -0.07492230 -0.227432 -0.1956058 0.5322334
## gray rabbit red tree white yellow
## 1189 -0.08540535 -0.1920367 0.06862099 0.5221231 0.08942916 0.2054854
## 1190 -0.08540535 -0.1920367 0.06862099 0.5221231 0.08942916 0.2054854
## 1191 -0.08512340 -0.1920367 0.06862099 0.5221231 0.08942916 0.2054854
## 1192 -0.08512340 -0.1920367 0.06862099 0.5251770 0.08942916 0.2054854
## 1193 -0.08512340 -0.1920367 0.06862099 0.5251770 0.08942916 0.2054854
## 1194 -0.08512340 -0.1936042 0.06862099 0.5251770 0.08786169 0.2054854
## animal plant vehicle
## 1 0e+00 0.00000000 0.0000000000
## 2 0e+00 0.00000000 0.0000000000
## 3 0e+00 0.00000000 0.0000000000
## 4 0e+00 0.00000000 0.0000000000
## 5 -1e-04 0.00990199 -0.0001960299
## 6 -1e-04 0.00990199 -0.0001960299
## animal plant vehicle
## 1189 -0.02234211 0.06862099 0.07207737
## 1190 -0.02234211 0.06862099 0.07207737
## 1191 -0.02234211 0.06862099 0.07207737
## 1192 -0.02234211 0.06862099 0.07207737
## 1193 -0.02234211 0.06862099 0.07207737
## 1194 -0.02234211 0.06862099 0.07207737
In addition, there are various functions to extract the activations
for each learning event. The function getActivations is a
wrapper that captures most common calculations, but other, more
specialistic functions are described below.
## Cues Outcomes Activation
## 1 BG_bicycle_brown vehicle 0.03000000
## 2 BG_flower_yellow plant 0.03000000
## 3 BG_car_white vehicle 0.03960300
## 4 BG_rabbit_gray animal 0.03000000
## 5 BG_tree_red plant 0.03950697
## 6 BG_bicycle_gray vehicle 0.05833268
Alternatively,the function getActivations can output all
possible outcomes per learning event.
## Cues Outcomes animal plant vehicle
## 1 BG_bicycle_brown vehicle 0.000000 0.00000000 0.03000000
## 2 BG_flower_yellow plant 0.000000 0.03000000 0.00970000
## 3 BG_car_white vehicle 0.000000 0.00970000 0.03960300
## 4 BG_rabbit_gray animal 0.030000 0.00960300 0.01920697
## 5 BG_tree_red plant 0.009700 0.03950697 0.01901490
## 6 BG_bicycle_gray vehicle 0.019303 0.01901587 0.05833268
We may want to add the activation of observed outcome in separate column:
act$Activation <- apply(act, 1, function(x){
out <- x['Outcomes']
return(as.numeric(x[out]))
})
head(act)## Cues Outcomes animal plant vehicle Activation
## 1 BG_bicycle_brown vehicle 0.000000 0.00000000 0.03000000 0.03000000
## 2 BG_flower_yellow plant 0.000000 0.03000000 0.00970000 0.03000000
## 3 BG_car_white vehicle 0.000000 0.00970000 0.03960300 0.03960300
## 4 BG_rabbit_gray animal 0.030000 0.00960300 0.01920697 0.03000000
## 5 BG_tree_red plant 0.009700 0.03950697 0.01901490 0.03950697
## 6 BG_bicycle_gray vehicle 0.019303 0.01901587 0.05833268 0.05833268
Note that getActivations only works for a single outcome
in each learning event. With multiple outcomes, please use one of the
other activation functions.
Visualizing the change in weights between cues and outcomes is
facilitated by two functions: plotCueWeights and
plotOutcomeWeights. The first function retrieves the
weights between a specific cue and all outcomes (or a selection of
outcomes) for each learning event. The second function retrieves the
weights between a specific outcome and all cues (or a selection of cues)
for each learning.
oldpar <- par(mfrow=c(1,2), cex=1.1)
# plot left:
plotCueWeights(wm, cue="brown")
# plot right:
plotOutcomeWeights(wm, outcome="animal")Both plot functions have a range of arguments that can be used to change the layout, as illustrated for the same two plots below:
oldpar <- par(mfrow=c(1,2), cex=1.1)
# plot left:
# 1. get outcome values:
out <- getValues(train$Outcomes, unique=TRUE)
out <- out[out != "animal"]
# 2. plot all outcomes, except 'plural':
lab <- plotCueWeights(wm, cue="brown", select.outcomes = out,
col=1, add.labels=FALSE, xlab='', ylim=range(getWM(wm)))
# 3. add plural:
lab2 <- plotCueWeights(wm, cue="brown", select.outcomes = "animal", col=2, lwd=2, adj=0, add=TRUE, font=2)
# 4. add legend:
legend_margin('bottom', ncol=4,
legend=c(lab2$labels, lab$labels),
col=c(lab2$col, lab$col), lty=c(lab2$lty, lab$lty),
lwd=c(lab2$lwd, lab$lwd), bty='n', cex=.85)
# plot right, different layout variant:
out <- getValues(dat$Cues, unique=TRUE)
out <- out[out != "animal"]
lab <- plotOutcomeWeights(wm, outcome="animal", select.cues = out,
col=alpha(1, f=.25), lty=1, pos=4, ylim=c(-.02,.2), font=2, ylim=range(getWM(wm)))
lab2 <- plotOutcomeWeights(wm, outcome="animal", select.cues = "brown", col='red', lwd=2, pos=4, add=TRUE, font=2)Both plotfunctions are a wrapper around the functions
getWeightsByCue and getWeightsByOutcome. These
values could be used to extract the weights per cue or per outcome.
## animal plant vehicle
## 1 0 0 0.01
## 2 0 0 0.01
## 3 0 0 0.01
## 4 0 0 0.01
## 5 0 0 0.01
## 6 0 0 0.01
Similarly, we can visualize the change in activations using the
function plotActivations, which is a wrapper around the
function getActivations.
oldpar <- par(mfrow=c(1,2), cex=1.1)
# an observed cueset:
plotActivations(wm, cueset="BG_cat_brown")
# an un-observed cueset:
plotActivations(wm, cueset="BG_cat_yellow")Another possibility worth mentioning is the possibility to continue training from an existing weight matrix.
## Shape Color Category Frequency2 Frequency1
## 1 cat brown animal 88 6
## 2 rabbit brown animal 88 1
## 3 flower brown plant 6 16
## 4 tree brown plant 6 56
## 5 car brown vehicle 1 88
## 6 bicycle brown vehicle 56 32
We used the column Frequency2, and now we continue
training with column Frequency1. Note that in the new data
(rows 1 and 2, column Frequency1), there are much fewer
brown animals than in the earlier training data (column
Frequency2).
dat$Cues <- paste("BG", dat$Shape, dat$Color, sep="_")
dat$Outcomes <- paste(dat$Category)
dat$Frequency <- dat$Frequency1
# remove remaining columns to simplify this example:
dat <- dat[, c("Cues", "Outcomes", "Frequency")]
# add ID for learning events:
dat$ID <- 1:nrow(dat)
head(dat)## Cues Outcomes Frequency ID
## 1 BG_cat_brown animal 6 1
## 2 BG_rabbit_brown animal 1 2
## 3 BG_flower_brown plant 16 3
## 4 BG_tree_brown plant 56 4
## 5 BG_car_brown vehicle 88 5
## 6 BG_bicycle_brown vehicle 32 6
After creating the training data (one event per row), we continue training. We will use the end state of the previous training as starting point for the new training. Two methods are illustrated in the code block below:
# continue learning from last weight matrix:
wm2 <- RWlearning(train2, wm=getWM(wm), progress = FALSE)
# number of learned event matches rows in dat2:
nrow(train2)## [1] 1194
## [1] 1194
# Alternatively, add the learning events to the existing output list wm1:
wm3 <- RWlearning(train2, wm=wm, progress = FALSE)
# number of learned event are now added to wm1:
length(wm3)## [1] 2388
Now we can visualize how changing the input frequencies changes the connection weights. The red line in the plot visualizes the change in how predictable the color “brown” is for an animal.
out <- getValues(dat$Cues, unique=TRUE)
out <- out[out != "animal"]
lab <- plotOutcomeWeights(wm3, outcome="animal",
select.cues = out,
col=alpha(1, f=.25), lty=1, pos=4,
ylim=c(-.02,.2), font=2, ylim=range(getWM(wm3)),
xmark=TRUE, ymark=TRUE, las=1)
lab2 <- plotOutcomeWeights(wm3, outcome="animal",
select.cues = "brown", col='red',
lwd=2, pos=4, add=TRUE, font=2)
abline(v=length(wm), lty=3)The activation of outcomes reflect the learner’s expectation that this outcome will occur, based on the present cues.
The edl package includes a series different functions to
calculate the activativations for outcomes:
activationsMatrix: Calculate activation based on a
static state of the network. The basic activation function that is
called by the other activation functions.## animal plant vehicle
## 1 0.990088 -0.01274053 0.02226169
## animal
## 1 0.990088
# for a group of cuesets (all connection weights will be added):
activationsMatrix(mat,cues=c("BG_cat_brown", "BG_cat_blue"))## animal plant vehicle
## 1 1.767512 0.1053064 0.06541489
activationsEvents: Calculate activations for each
learning event in the training data.# new dummy data:
dat <- data.frame(Cues = c("noise", "noise", "light"),
Outcomes = c("food", "other", "food_other"),
Frequency = c(5, 10, 15) )
dat$Cues <- paste("BG", dat$Cues, sep="_")
train <- createTrainingData(dat)
wm <- RWlearning(train, progress = FALSE)
# list with activations for observed outcomes:
act <- activationsEvents(wm, data=train)## Warning in activationsEvents(wm, data = train): Function will return list of
## activations, because in some events multiple outcomes occurred.
## [[1]]
## food other
## 1 0.02 0.02
##
## [[2]]
## food other
## 1 0.0396 0.0396
##
## [[3]]
## food other
## 1 0.058808 0.058808
##
## [[4]]
## food
## 1 0.04881592
##
## [[5]]
## food other
## 1 0.08714368 0.07734368
##
## [[6]]
## food other
## 1 0.1054008 0.09579681
# calculate max activation:
maxact <- lapply(act, function(x){ return(max(x, na.rm=TRUE)) })
unlist(maxact)## [1] 0.02000000 0.03960000 0.05880800 0.04881592 0.08714368 0.10540081
## [7] 0.12329279 0.09468123 0.14988012 0.08295150 0.16586963 0.10971648
## [13] 0.12752215 0.19207469 0.15305096 0.12930820 0.16699015 0.18365034
## [19] 0.19997734 0.23969660 0.25490267 0.23103180 0.24641116 0.28503019
## [25] 0.15744995 0.26365998 0.17121493 0.30162279 0.31559034 0.32927853
## [1] 0.02000000 0.03960000 0.05880800 0.04881592 0.08714368 0.10540081
activationsCueSet: Calculate activations for one or
multiple cuesets.# list with activations for observed outcomes:
act <- activationsCueSet(wm, cueset=c("BG_noise", "BG_light", "BG_somethingelse"))
names(act)## [1] "BG_noise" "BG_light" "BG_somethingelse"
## food other
## 1 0.01000000 0.01000000
## 2 0.01980000 0.01980000
## 3 0.02940400 0.02940400
## 4 0.04881592 0.02881592
## 5 0.05813078 0.03823078
## 6 0.06725934 0.04745734
## food other
## 1 0.01000000 0.01000000
## 2 0.01980000 0.01980000
## 3 0.02940400 0.02940400
## 4 0.03910996 0.02910996
## 5 0.04842482 0.03852482
## 6 0.05755338 0.04775138
activationsOutcomes: Calculate activations per learning
event for all outcomes in the data.## food other
## 1 0.02000000 0.02000000
## 2 0.03960000 0.03960000
## 3 0.05880800 0.05880800
## 4 0.04881592 0.02881592
## 5 0.08714368 0.07734368
## 6 0.10540081 0.09579681