In [11]:
% load_ext autoreload
% autoreload 2
import numpy as np
from random import *
from dmww_classes import *
from sampling_helper import *
seed(1) # for debugging
Define model parameters
In [3]:
p = Params(n_samps=1,
n_particles=1,
alpha_r=.1,
alpha_nr=10,
empty_intent=.0001,
n_hypermoves=5)
#(n_samps=1, #200 samples
# n_particles=1,
# alpha_r=.1,
# alpha_nr=1,
# empty_intent=.01,
# n_hypermoves=5)
Set up world and corpus.
In [4]:
corpusfile = 'corpora/corpus.csv'
w = World(corpus=corpusfile)
w.show()
c = Corpus(world=w, corpus=corpusfile)
In [5]:
# BIRD = 22
# FEP = 23
# fep = 420
w.update(1, labels = ["fep"])
w.show()
c.sents.append([array([22,23]),array([420])])
c.update()
Do inference with Gibbs sampler.
In [6]:
l = Lexicon(c, p,
verbose=0,
hyper_inf=True)
l.learn_lex_gibbs(c, p);
Plot.
In [7]:
l.plot_lex(w, certainwords = 0)
In [8]:
correct = l.ref[23, 420] #FEP, fep
incorrect = l.ref[22, 420] #BIRD, fep
known_word = l.ref[22, 8] #BIRD, bigbird
lc = divide(correct, correct + incorrect)
known_word, correct, incorrect, lc
Out[8]:
Do inference with particle filter.
In [9]:
l = Lexicon(c, p,
verbose=0,
hyper_inf=False)
l.learn_lex_pf(c, p, resample=False)
#l.output_lex_pf(c, p)
In [10]:
#l.plot_lex(w, certainwords = 0)
#w.words_dict
Luce choice in 2AFC novel-familiar task
In [11]:
correct = l.ref[23, 420]
incorrect = l.ref[22, 420]
known_word = l.ref[22, 8]
lc = divide(correct, correct + incorrect)
known_word, correct, incorrect, lc
Out[11]:
Define model parameters.
In [12]:
p = Params(n_samps=1,
n_particles=1,
alpha_r=.1,
alpha_nr=10,
empty_intent=.0001,
n_hypermoves=5)
Set up world and corpus.
In [13]:
corpusfile = 'corpora/corpus.csv'
w = World(corpus=corpusfile)
w.show()
c = Corpus(world=w,corpus=corpusfile)
In [14]:
# FEP = 23
# fep = 420
# TOMA = 24
# toma = 421
w.update(2, labels = ["fep", "toma"])
w.show()
c.sents.append([array([23]),array([420])])
c.sents.append([array([23,24]),array([421])])
c.update()
Do inference with Gibbs sampler.
In [15]:
l = Lexicon(c, p,
verbose=0,
hyper_inf=True)
l.learn_lex_gibbs(c, p);
Plot.
Luce choice in 2AFC novel-novel task
In [16]:
correct = l.ref[24, 421]
incorrect = l.ref[23, 421]
lc = divide(correct, correct + incorrect)
correct,incorrect,lc
Out[16]:
Make corpus.
In [17]:
# set parameters
condition = 3
num_words = 18
num_occurrences = 6
total_words = num_words * num_occurrences
num_trials = total_words / condition
items = np.ones(num_words) * num_occurrences
# make empty corpus
yucorpus = list()
for i in range(num_trials):
yucorpus.append([np.zeros(condition, dtype=int8),
np.zeros(condition, dtype=int8)])
# generate actual corpus
try:
for i in range(num_trials): # for each trial
for j in range(condition): # for each word/object pairing in the trial
item_probs = np.divide(items, total_words)
yucorpus[i][0][j] = where(np.random.multinomial(1, item_probs) == 1)[0][0]
# avoid repeats
c = 1
while sum(yucorpus[i][0][j] == yucorpus[i][0]) > 1:
yucorpus[i][0][j] = where(np.random.multinomial(1, item_probs) == 1)[0][0]
c = c + 1;
if c > 1000:
break
yucorpus[i][1][j] = yucorpus[i][0][j]
# decrement the item counts
items[yucorpus[i][0][j]] = items[yucorpus[i][0][j]] - 1;
total_words = total_words - 1;
except ValueError:
print 'failed to generate corpus, run again!'
Set up world and corpus.
In [26]:
w = World(n_words=18, n_objs=18)
c = Corpus(w,
n_per_sent=condition,
n_sents=num_trials)
c.sents = yucorpus
c.update()
Define model parameters.
In [27]:
p = Params(n_samps=200,
n_particles=1,
alpha_r=.1,
alpha_nr=1,
empty_intent=.0001,
n_hypermoves=5)
Do inference with Gibbs sampler.
In [57]:
l = Lexicon(c, p,
verbose=0,
hyper_inf=True)
l.learn_lex_gibbs(c, p)
Plot.
In [58]:
l.plot_lex(w, certainwords = 0)
Evaluate posterior using luce choice. At test, learners were given 4AFC task (1 correct referent and 3 foils).
In [60]:
epsilon = .00001
l.ref = l.ref + epsilon
num_foils = 3
lc = zeros(num_words)
correct = zeros(num_words)
incorrect = zeros(num_words)
for i in range(num_words):
foils = sample(xrange(num_words),num_foils)
while i in foils:
foils = sample(xrange(num_words),num_foils)
correct[i] = l.ref[i,i]
incorrect[i] = l.ref[foils[0],i] + l.ref[foils[1],0] + l.ref[foils[2],0]
lc[i] = divide(correct[i], correct[i] + incorrect[i])
choice_score = sum(lc)/num_words
choice_score
Out[60]:
Now, do with particle filter
In [63]:
p = Params(n_samps=1,
n_particles=10,
alpha_r=.1,
alpha_nr=10,
empty_intent=.0001,
n_hypermoves=10)
l = Lexicon(c, p,
verbose=0,
hyper_inf=True)
l.learn_lex_pf(c, p, resample=False);
l.output_lex_pf(c, p)
l.plot_lex(w, certainwords = 0)
l.show()
Evaluate posterior using luce choice. At test, learners were given 4AFC task (1 correct referent and 3 foils).
In [64]:
num_foils = 3
lc = zeros(num_words)
correct = zeros(num_words)
incorrect = zeros(num_words)
for i in range(num_words):
foils = sample(xrange(num_words),num_foils)
while i in foils:
foils = sample(xrange(num_words),num_foils)
correct[i] = l.ref[i,i]
incorrect[i] = l.ref[foils[0],i] + l.ref[foils[1],0] + l.ref[foils[2],0]
lc[i] = divide(correct[i], correct[i] + incorrect[i])
choice_score = nansum(lc)/num_words
correct, incorrect, lc, choice_score
Out[64]:
In [25]:
w = World(n_words=3,
n_objs=4)
w.show()
w.update(6, labels = ["fep", "toma", "dax", "zim", "pid", "wug"])
c = Corpus(w)
# 8 training trials
# 4 test trials
#c.sents = [[array([1,2]),array([])],[array([1,2]),array([])],
[array([3,4]),array([])],[array([3,4]),array([])],
[array([5,5]),array([])],[array([5,5]),array([])],
[array([6,6]),array([])],[array([6,6]),array([])],
[array([1,2]),array([])],[array([1,2]),array([])],
[array([1,2]),array([])],[array([1,2]),array([])]]
#c.show()
In [ ]:
corpusfile = 'corpora/corpus.csv'
w = World(corpus=corpusfile)
c = Corpus(world=w, corpus=corpusfile)
w.update(6, labels = ["fep", "toma", "dax", "zim", "pid", "wug"])
c.update()
# expected - different
c.sents.append([array([23, 23, 23, 24, 24, 24]),array([420, 421])])
# expected - same
c.sents.append([array([25, 25, 25, 25, 25, 25]),array([422, 422])])
# unexpected - different
c.sents.append([array([26, 26, 26, 27, 27, 27]),array([423, 423])])
# unexpected - same
c.sents.append([array([28, 28, 28, 28, 28, 28]),array([424, 425])])
In [39]:
from matplotlib import pyplot as plt
x = arange(3)
y = array([0.734509062, 0.601131376, 0.488957579])
f = pylab.figure()
ax = f.add_axes([0.1, 0.1, .8, .8])
ax.bar(x, y, align='center')
ax.set_xticks(x)
ax.set_xticklabels(['2x2', '3x3', '4x4'])
plt.axhline(.25, color = "black", linestyle='dashed', linewidth=2)
ax.set_title('Gibbs sampler')
f.show()
In [ ]:
x = arange(3)
y = array([0.734509062, 0.601131376, 0.488957579])
f = pylab.figure()
ax = f.add_axes([0.1, 0.1, .8, .8])
ax.bar(x, y, align='center')
ax.set_xticks(x)
ax.set_xticklabels(['2x2', '3x3', '4x4'])
plt.axhline(.25, color = "black", linestyle='dashed', linewidth=2)
ax.set_title('Particle Filter')
f.show()