예제 #1
0
    def model(self, p):
        random.seed(p.exp_seed)

        data_dir = os.path.join(
            os.path.dirname(__file__), os.pardir, os.pardir, 'data')
        sp_path = os.path.join(data_dir, 'associationmatrices')
        assoc, i2w, _ = load_assoc_mat(sp_path, p.assocmat)

        sp_path = os.path.join(data_dir, 'semanticpointers')
        pointers, _, _ = load_pointers(sp_path, p.sp_file)

        rat_path = os.path.join(data_dir, 'rat', p.ratfile)
        self.rat_items = list(filter_valid(load_rat_items(rat_path), i2w))

        with spa.SPA(seed=p.model_seed) as model:
            self.model = model
            # set up vocab
            self.vocab = model.get_default_vocab(p.d)
            for i, pointer in enumerate(pointers):
                sanitized = i2w[i].upper().replace(' ', '_').replace(
                    '+', '_').replace('-', '_').replace('&', '_').replace(
                        "'", '_')
                self.vocab.add(sanitized, pointer)

            # set up model
            self.stimulus = Stimulus(self.rat_items)
            model.stimulus = StimulusModule(
                self.stimulus, self.vocab, p.neurons_per_dimension)
            model.rat_model = FfwdConnectionsRat(
                assoc, self.vocab,
                neurons_per_dimension=p.neurons_per_dimension)
            nengo.Connection(model.stimulus.cue1.output, model.rat_model.cue1)
            nengo.Connection(model.stimulus.cue2.output, model.rat_model.cue2)
            nengo.Connection(model.stimulus.cue3.output, model.rat_model.cue3)
            self.p_output = nengo.Probe(
                model.rat_model.rat_state.output, synapse=0.003)
            self.p_cue1 = nengo.Probe(
                model.stimulus.cue1.output, synapse=0.003)
            self.p_cue2 = nengo.Probe(
                model.stimulus.cue2.output, synapse=0.003)
            self.p_cue3 = nengo.Probe(
                model.stimulus.cue3.output, synapse=0.003)
            self.p_spikes = nengo.Probe(
                model.rat_model.rat_state.state_ensembles.ensembles[0].neurons,
                'spikes')

            tr = np.dot(
                self.vocab.vectors.T,
                np.dot(assoc.T, self.vocab.vectors)) / 3.
            direct_result = nengo.Ensemble(
                n_neurons=1, dimensions=p.d, neuron_type=nengo.Direct())
            nengo.Connection(
                model.stimulus.cue1.output, direct_result, transform=tr)
            nengo.Connection(
                model.stimulus.cue2.output, direct_result, transform=tr)
            nengo.Connection(
                model.stimulus.cue3.output, direct_result, transform=tr)
            self.p_direct = nengo.Probe(direct_result, synapse=0.003)

        return model
예제 #2
0
from data_processing.spgen import from_assoc_matrix, save_pointers

parser = argparse.ArgumentParser(description="Generate semantic pointers.")
parser.add_argument('association_matrix',
                    nargs=1,
                    help="Association matrix to base semantic pointers on.")
parser.add_argument('dimred', nargs=1, help="Dimension reduction method.")
parser.add_argument('d',
                    nargs=1,
                    type=int,
                    help="Dimensionality of semantic pointers.")
args = parser.parse_args()

input_dir = os.path.join(os.path.dirname(__file__), os.pardir, 'data',
                         'associationmatrices')
strength_mat, id2word, word2id = load_assoc_mat(input_dir,
                                                args.association_matrix[0])

pointers = from_assoc_matrix(strength_mat,
                             getattr(dimension_reduction, args.dimred[0]),
                             args.d[0])

output_dir = os.path.join(os.path.dirname(__file__), os.pardir, 'data',
                          'semanticpointers')
name = '{assoc}_{dimred}_{words}w_{d}d'.format(
    assoc=args.association_matrix[0],
    dimred=args.dimred[0],
    words=pointers.shape[0],
    d=args.d[0])
save_pointers(output_dir, name, pointers, id2word, word2id)
import os.path
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir,
                                'sparat'))

import argparse

from data_processing import generate_association_matrix

parser = argparse.ArgumentParser(
    description="Transform an association matrix.")
parser.add_argument('method', nargs=1, help="Method to transform.")
parser.add_argument('output', nargs=1)
parser.add_argument('input', nargs='+')
args = parser.parse_args()

basedir = os.path.join(os.path.dirname(__file__), os.pardir, 'data',
                       'associationmatrices')
strength_mat, id2word, word2id = zip(
    *(generate_association_matrix.load_assoc_mat(basedir, inp)
      for inp in args.input))
method = getattr(generate_association_matrix, 'tr_' + args.method[0])
transformed = method(*strength_mat)

assert all(x == id2word[0] for x in id2word)

output_dir = os.path.join(os.path.dirname(__file__), os.pardir, 'data',
                          'associationmatrices')
generate_association_matrix.save_assoc_mat(output_dir, args.output[0],
                                           transformed, id2word[0], word2id[0])
import argparse

from data_processing import dimension_reduction
from data_processing.generate_association_matrix import load_assoc_mat
from data_processing.spgen import from_assoc_matrix, save_pointers

parser = argparse.ArgumentParser(description="Generate semantic pointers.")
parser.add_argument(
    'association_matrix', nargs=1,
    help="Association matrix to base semantic pointers on.")
parser.add_argument(
    'dimred', nargs=1, help="Dimension reduction method.")
parser.add_argument(
    'd', nargs=1, type=int, help="Dimensionality of semantic pointers.")
args = parser.parse_args()

input_dir = os.path.join(
    os.path.dirname(__file__), os.pardir, 'data', 'associationmatrices')
strength_mat, id2word, word2id = load_assoc_mat(
    input_dir, args.association_matrix[0])

pointers = from_assoc_matrix(
    strength_mat, getattr(dimension_reduction, args.dimred[0]), args.d[0])

output_dir = os.path.join(
    os.path.dirname(__file__), os.pardir, 'data', 'semanticpointers')
name = '{assoc}_{dimred}_{words}w_{d}d'.format(
    assoc=args.association_matrix[0], dimred=args.dimred[0],
    words=pointers.shape[0], d=args.d[0])
save_pointers(output_dir, name, pointers, id2word, word2id)
sys.path.insert(
    0, os.path.join(os.path.dirname(__file__), os.pardir, 'sparat'))

import argparse

from data_processing import generate_association_matrix


parser = argparse.ArgumentParser(
    description="Transform an association matrix.")
parser.add_argument(
    'method', nargs=1, help="Method to transform.")
parser.add_argument('output', nargs=1)
parser.add_argument('input', nargs='+')
args = parser.parse_args()

basedir = os.path.join(
    os.path.dirname(__file__), os.pardir, 'data', 'associationmatrices')
strength_mat, id2word, word2id = zip(
    *(generate_association_matrix.load_assoc_mat(
        basedir, inp) for inp in args.input))
method = getattr(generate_association_matrix, 'tr_' + args.method[0])
transformed = method(*strength_mat)

assert all(x == id2word[0] for x in id2word)

output_dir = os.path.join(
    os.path.dirname(__file__), os.pardir, 'data', 'associationmatrices')
generate_association_matrix.save_assoc_mat(
    output_dir, args.output[0], transformed, id2word[0], word2id[0])