Example #1
0
    def __init__(self, input_dim, embed_dim, conv_layers, num_labels, batch_size):
        """
        Linear chain CRF as in Assignment 2
        """
        super(CRF, self).__init__()

        self.input_dim = input_dim
        self.embed_dim = embed_dim
        self.conv_layers = conv_layers
        self.num_labels = num_labels
        self.batch_size = batch_size
        self.use_cuda = torch.cuda.is_available()

        self.cnn = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=5, padding = 2, stride = 1)
        # self.cnn = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3)

        ### Use GPU if available
        if self.use_cuda:
            [m.cuda() for m in self.modules()]

        params = utils.load_model_params('../data/model.txt')
        W = utils.extract_w(params)
        T = utils.extract_t(params)
        
        self.weights = nn.Parameter(torch.tensor(W, dtype=torch.float))
        self.transition = nn.Parameter(torch.tensor(T, dtype=torch.float))
Example #2
0
def read_data():
    train_data = utils.read_data_seq('../data/train.txt')
    trainX, trainY = [], []
    for d in train_data:
        trainX.append(d[1])
        trainY.append(d[0])

    test_data = utils.read_data_seq('../data/test.txt')
    testX, testY = [], []
    for d in test_data:
        testX.append(d[1])
        testY.append(d[0])

    params = utils.load_model_params('../data/model.txt')
    # train_data = (trainX, trainY)
    # test_data = (testX, testY)

    return train_data, test_data, params
Example #3
0
def test_model(test_data):
    params = utils.load_model_params('../result/transform_crf.txt')
    W = utils.extract_w(params)
    T = utils.extract_t(params)

    y_preds = decode_test_data(test_data, W, T)

    #get y_label data from test_data
    y_label = []
    for d in test_data:
        y_label.append(d[0])

    #count word_letter_accuracy
    word_acc, letter_acc = word_letter_accuracy(y_preds, y_label)

    print("Letter Accuracy: ", letter_acc)
    print("Word Accuracy: ", word_acc)

    f = open('../result/prediction.txt', 'w')
    for pred in y_preds:
        for word in pred:
            f.write(str(word + 1) + "\n")
import torch
from torch.autograd import Variable

import utils
import settings
import re
import torchwordemb

from live_sentiment import text2vec

if __name__ == "__main__":
    # Load model
    model = utils.generate_model_from_settings()
    utils.load_model_params(model, settings.args.load_path)

    # Load glove
    print("Reading word vectors...")
    vocab, vec = torchwordemb.load_glove_text(
        settings.DATA_KWARGS["glove_path"])
    print("Done!")

    while True:
        text = input("What's on your mind? \n")
        features = text2vec(text, vocab, vec)
        features = utils.pack_sequence([features])
        (features, lengths) = torch.nn.utils.rnn.pad_packed_sequence(features)
        out = model(features, lengths)

        stars = float(out[0, 0, 0])
        if stars < 1.1:
            print("Watch your language, kid")
Example #5
0
    # Used for getting maximum test scores at word and character level
    # Cs = [1.0]

    CHAR_CV_SCORES.clear()
    WORD_CV_SCORES.clear()

    # for C in Cs:
    #     train_evaluate_linear_svm(C=C)
    #
    # plot_scores(Cs)
    ''' CRF '''

    X_train, Y_train = prepare_structured_dataset('train_struct.txt')
    X_test, Y_test = prepare_structured_dataset('test_struct.txt')
    params = load_model_params()

    # Run optimization, should take 3.5+ hours so commented out.
    '''
    cvals = [1, 10, 100, 1000]
    for elt in cvals:
        p2b.optimize(params, X_train, y_train, elt, 'solution' + str(elt))
        print("done with" + str(elt))
    '''

    # check accuracy
    Cs = [1, 10, 100, 1000]

    CHAR_CV_SCORES.clear()
    WORD_CV_SCORES.clear()
Example #6
0
    def __init__(self, sc, g=0, norm_sc=True, hmap = None,
                 wee=(0.15, 0.), wei=(0.15, 0.),
                 syn_params=None, bold_params='obata',
                 verbose=True):
        """
        
        Parameters
        ----------
        sc : ndarray
            Structural connectivity matrix
        g : float, optional
            Global coupling parameter to scale structural connectivity matrix
        norm_sc : bool, optional
            Normalize input strengths of the structural connectivity matrix (True by default)
        hmap : ndarray, optional
            Heterogeneity map to scale local model parameters. 
            If None, the model parameters are homogeneous (None by default)
        wee : tuple, optional
            Local recurrent excitatory connectivity weights (w^{EE}). 
            Requires a tuple with size 2 as (w_{min}, w_{scale}). 
            (w_{min}=0.15, w_{scale}=0.0 by default)
        wei : tuple, optional
            Local excitatory to inhibitory connectivity weights (w^{EI}). 
            Requires a tuple with size 2 as (w_{min}, w_{scale}). 
            (w_{min}=0.15, w_{scale}=0.0 by default)
        syn_params : list, optional
            Synaptic dynamical model parameters (None by default)
        bold_params : str, optional
            Hemodynamic model parameters. 'obata' or 'friston' ('obata' by default)
        verbose : bool, optional
            If True, prints diagnostics to console (True by default)  
        
        """

        # Structural connectivity / number of cortical areas
        self._SC = sc
        self._nc = sc.shape[0]

        # Global coupling
        self._G = g

        # Print diagnostics to console
        self._verbose = verbose

        # Initialize hemodynamic model (Balloon-Windkessel)
        self.hemo = Balloon(self._nc, parameters=bold_params)

        # Initialize simulation class
        self.sim = Sim()

        # If custom model parameters were provided, load model parameters with corresponding keys
        model_params = load_model_params()
        if syn_params is not None:
            for key in syn_params.keys():
                model_params[key] = syn_params[key]

        # Unstable if Jacobian has eval > 0
        self._unstable = False

        # Initialize model outputs to None
        self._jacobian = None
        self._cov = None
        self._corr = None
        self._cov_bold = None
        self._corr_bold = None
        self._full_cov = None

        # Initialize state members to None
        self._I_E = None
        self._I_I = None
        self._S_E = None
        self._S_I = None
        self._r_E = None
        self._r_I = None

        # Various model parameters
        self._w_II = np.repeat(model_params['w_II'], self._nc)
        self._w_IE = np.repeat(model_params['w_IE'], self._nc)
        self._w_EE = np.repeat(model_params['w_EE'], self._nc)
        self._w_EI = np.repeat(model_params['w_EI'], self._nc)

        self._I0 = np.repeat(model_params['I0'], self._nc)
        self._J_NMDA = np.repeat(model_params['J_NMDA'], self._nc)
        self._sigma = model_params['sigma']
        self._gamma = model_params['gamma']
        self._W_I = model_params['W_I']
        self._W_E = model_params['W_E']
        self._tau_I = model_params['tau_I']
        self._tau_E = model_params['tau_E']
        self._d_I = model_params['d_I']
        self._d_E = model_params['d_E']
        self._b_I = model_params['b_I']
        self._b_E = model_params['b_E']
        self._a_I = model_params['a_I']
        self._a_E = model_params['a_E']
        self._I_ext = np.repeat(model_params['I_ext'], self._nc)

        self._gamma_I = 1.0

        self._tau_E_reset = np.copy(self._tau_E)
        self._tau_I_reset = np.copy(self._tau_I)
        self._gamma_reset = np.copy(self._gamma)
        self._gamma_I_reset = np.copy(self._gamma_I)

        # Baseline input currents
        self._I0_E = self._W_E * self._I0
        self._I0_I = self._W_I * self._I0

        # Steady state values for isolated node
        self._I_E_ss = np.repeat(model_params['I_E_ss'], self._nc)
        self._I_I_ss = np.repeat(model_params['I_I_ss'], self._nc)
        self._S_E_ss = np.repeat(model_params['S_E_ss'], self._nc)
        self._S_I_ss = np.repeat(model_params['S_I_ss'], self._nc)
        self._r_E_ss = np.repeat(model_params['r_E_ss'], self._nc)
        self._r_I_ss = np.repeat(model_params['r_I_ss'], self._nc)

        # Noise covariance matrix
        self._Q = np.identity(2 * self._nc) * self._sigma * self._sigma

        # Add lookup tables for transfer function and its derivatives
        self._phi()

        # Heterogeneity map values for each area
        self._raw_hmap = hmap
        self._hamp = 0.0
        self._hmap_rev = 0.0

        # Set heterogeneity gradients
        if self._raw_hmap is not None:
            hmap_range = np.ptp(self._raw_hmap)
            self._hmap = (-(self._raw_hmap - np.max(self._raw_hmap)) / hmap_range)

            hmap_norm = self._raw_hmap - np.min(self._raw_hmap)
            self._hmap_rev = hmap_norm / np.max(hmap_norm)

            self._w_EE = self._apply_hierarchy(wee[0], wee[1])
            self._w_EI = self._apply_hierarchy(wei[0], wei[1])

        # Set SC normalization
        self._sc_norm = 1.0
        if norm_sc:
            sc_norm = 1. / self._SC.sum(1)
            self._sc_norm = np.tile(sc_norm, (self._nc, 1)).T

        return