<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 IPA to IMAGE model
Prerequisites
In principle, this should work on a computer without a GPU. It will help if you have a lot of RAM.
Pull latest changes from https
Step1:  Load the model
(It will take a couple of minutes)
Step2:  Symbol embeddings
Step3:  The table of IPA symbols corresponding to  the 49 dimensions
Step4:  Let's display the embeddings projected to 2D via PCA
Step5:  Seems mostly random...
Load MSCOCO validation data
Step6:  Project sentences to state space
Step7:  Find similar sentences in state space
Compute the pairwise cosine distance matrix.
Step8:  Display neighbors for a sentence
Step9:  Tracing the evolution of states
Step10:  Use espeak to convert graphemes to phonemes
Step11:  Plot traces of example sentences
<ASSISTANT_TASK:>
Python Code:
import imaginet.task
Explanation: IPA to IMAGE model
Prerequisites
In principle, this should work on a computer without a GPU. It will help if you have a lot of RAM.
Pull latest changes from https://github.com/gchrupala/reimaginet
Download http://grzegorz.chrupala.me/data/coco.zip and unzip it in the reimaginet/data/coco directory.
  You should have the following files:
dataset.json - MSCOCO sentences
vgg_feats.mat - MSCOCO image vectors
dataset.ipa.jsonl.gz  - IPA transcriptions of MSCOCO sentences
Download http://grzegorz.chrupala.me/data/model-ipa.zip and put it in the examples directory (same as this notebook)
End of explanation
model = imaginet.task.load(path="model-ipa.zip")
Explanation: Load the model
(It will take a couple of minutes)
End of explanation
emb = imaginet.task.embeddings(model)
print(emb.shape)
Explanation: Symbol embeddings
End of explanation
symb = imaginet.task.symbols(model)
print " ".join(symb.values())
Explanation: The table of IPA symbols corresponding to  the 49 dimensions
End of explanation
%pylab inline
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
xy = pca.fit_transform(emb)
pylab.rc('font', family='DejaVu Sans')
pylab.figure(figsize=(8,8))
pylab.scatter(xy[:,0], xy[:,1], alpha=0.1)
for j,symb_j in symb.items():
    pylab.text(xy[j,0], xy[j,1], symb_j)
Explanation: Let's display the embeddings projected to 2D via PCA
End of explanation
from imaginet.data_provider import getDataProvider
# Adjust the root to point to the directory above data
prov = getDataProvider('coco', root="..")
sents = list(prov.iterSentences(split='val'))
from imaginet.simple_data import phonemes
sents_ipa = [ phonemes(sent) for sent in sents ]
Explanation: Seems mostly random...
Load MSCOCO validation data
End of explanation
reps = imaginet.task.representation(model, sents_ipa)
Explanation: Project sentences to state space
End of explanation
from scipy.spatial.distance import cdist
distance = cdist(reps, reps, metric='cosine')
Explanation: Find similar sentences in state space
Compute the pairwise cosine distance matrix.
End of explanation
import numpy
def neighbors(k, distance=distance):
    nn =  numpy.argsort(distance[k,:])[1:5]
    print sents[k]['raw'], ''.join(sents_ipa[k])
    for n in nn:
        print u"✔" if sents[n]['imgid']==sents[k]['imgid'] else u"✘", \
        sents[n]['raw'], ''.join(sents_ipa[n])
import random
for _ in range(10):
    neighbors(random.randint(0, len(sents)))
    print
Explanation: Display neighbors for a sentence
End of explanation
import imaginet.tracer
reload(imaginet.tracer)
tr = imaginet.tracer.Tracer()
tr.fit(reps)
tr.proj.explained_variance_
Explanation: Tracing the evolution of states
End of explanation
from subprocess import check_output
def espeak(words):
    return phon(check_output(["espeak", "-q", "--ipa",
                        '-v', 'en-us',
                        words]).decode('utf-8'))
def phon(inp):
    return list(''.join(inp.split()))
def trace(orths, tracer=tr, model=model, eos=True, size=(6,6)):
    ipas = [ espeak(orth) for orth in orths ]
    states = [ imaginet.task.states(model, ipa) for ipa in ipas ]
    pylab.figure(figsize=size)
    tracer.traces(ipas, states, eos=eos)
Explanation: Use espeak to convert graphemes to phonemes
End of explanation
trace(["A bowl of salad","A plate of pizza","A brown dog", "A black cat"])
trace(["a girl skiing", "a girl wind surfing", "a girl water skiing",])
trace(["a cow", "a baby cow","a tiny baby cow"])
trace(["some food on a table","a computer on a table","a table with food"])
pylab.axis('off')
trace(["a bear in a cage", "a brown bear in the zoo","a teddy bear on a chair"])
Explanation: Plot traces of example sentences
End of explanation
<END_TASK>