def config_loc_cmr(n_item): """ Configure a localist CMR network. Parameters ---------- n_item : int Number of item patterns to include in the network Returns ------- param_def : cymr.parameters.Parameters Parameters object with configuration for the network. patterns : dict Patterns to place in the network. """ items = np.arange(n_item) patterns = {'items': items, 'vector': {'loc': np.eye(n_item)}} param_def = parameters.Parameters() param_def.set_sublayers(f=['task'], c=['task']) param_def.set_weights( 'fc', {(('task', 'item'), ('task', 'item')): 'Afc + Dfc * loc'}) param_def.set_weights( 'cf', {(('task', 'item'), ('task', 'item')): 'Acf + Dcf * loc'}) return param_def, patterns
def param_def(): """Parameter definitions.""" param = parameters.Parameters() # options param.set_options(scope='list') # general parameter management param.set_fixed(a=1, b=2) param.set_fixed({'c': 3}) param.set_dependent(d='2 + mean([a, b])') param.set_dynamic('study', e='distract / c') param.set_free(f=[0, 1]) # network definition param.set_sublayers(f=['task'], c=['loc', 'cat']) weights = { (('task', 'item'), ('loc', 'item')): 'loc', (('task', 'item'), ('cat', 'item')): 'cat', } param.set_weights('fc', weights) param.set_weights('cf', weights) param.set_weights('ff', {('task', 'item'): 'loc + cat'}) # sublayer-varying parameters param.set_sublayer_param('c', 'loc', {'B_enc': 'B_enc_loc'}) param.set_sublayer_param('c', 'cat', {'B_enc': 'B_enc_cat'}) return param
def param_def_simple(): param = parameters.Parameters() param.set_sublayers(f=['task'], c=['task']) weights = {(('task', 'item'), ('task', 'item')): 'loc'} param.set_weights('fc', weights) param.set_weights('cf', weights) return param
def param_def_dist(param): """Generate parameter definitions for a simple CMR-D network.""" param_def = parameters.Parameters() param_def.set_fixed(param) param_def.set_sublayers(f=['task'], c=['task']) weights = {(('task', 'item'), ('task', 'item')): 'loc'} param_def.set_weights('fc', weights) param_def.set_weights('cf', weights) return param_def
def test_blank(split_data): """Test that unspecified evaluation does nothing.""" param_def = parameters.Parameters() param = {'a': 1, 'b': 2, 'c': 3} orig = param.copy() param = param_def.eval_dependent(param) param = param_def.eval_dynamic(param, study=split_data['study'], recall=split_data['recall']) assert param == orig
def test_fit_subject(data): data = data.copy() rec = TestRecall() param_def = parameters.Parameters() param_def.set_fixed(y=1) param_def.set_free(x=[-10, 10]) subject_data = data.loc[data['subject'] == 1] param, logl, n, k = rec.fit_subject(subject_data, param_def) np.testing.assert_allclose(param['x'], -2, atol=0.00001) np.testing.assert_allclose(logl, np.log(2)) assert k == 1
def test_fit_indiv(data): data = data.copy() rec = TestRecall() param_def = parameters.Parameters() param_def.set_fixed(y=1) param_def.set_free(x=[-10, 10]) results = rec.fit_indiv(data, param_def) np.testing.assert_allclose(results['x'].to_numpy(), [-2, -2], atol=0.0001) np.testing.assert_allclose(results['logl'].to_numpy(), np.log([2, 2]), atol=0.0001) np.testing.assert_array_equal(results['k'].to_numpy(), [1, 1])
def test_cmr_patterns(patterns): param_def = parameters.Parameters() fcf_weights = { (('task', 'item'), ('task', 'loc')): 'w_loc * loc', (('task', 'item'), ('task', 'cat')): 'w_cat * cat', } ff_weights = {('task', 'item'): 's_loc * loc + s_cat * cat'} param_def.set_weights('fc', fcf_weights) param_def.set_weights('ff', ff_weights) param_def.set_dependent({ 'w_loc': 'wr_loc / sqrt(wr_loc**2 + wr_cat**2)', 'w_cat': 'wr_cat / sqrt(wr_loc**2 + wr_cat**2)', 's_loc': 'sr_loc / (sr_loc + sr_cat)', 's_cat': 'sr_cat / (sr_loc + sr_cat)', }) param = {'wr_loc': 1, 'wr_cat': np.sqrt(2), 'sr_loc': 1, 'sr_cat': 2} param = param_def.eval_dependent(param) weights = param_def.eval_weights(patterns, param) # localist FC units expected = np.array([ [0.57735027, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.57735027, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.57735027, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.57735027, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.57735027, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.57735027], ]) np.testing.assert_allclose( weights['fc'][(('task', 'item'), ('task', 'loc'))], expected) # category FC units expected = np.array([ [0.81649658, 0.0], [0.0, 0.81649658], [0.81649658, 0.0], [0.81649658, 0.0], [0.0, 0.81649658], [0.81649658, 0.0], ]) np.testing.assert_allclose( weights['fc'][(('task', 'item'), ('task', 'cat'))], expected) # FF units expected = np.array([ [1.0, 0.0, 0.66666667, 0.66666667, 0.0, 0.66666667], [0.0, 1.0, 0.0, 0.0, 0.66666667, 0.0], [0.66666667, 0.0, 1.0, 0.66666667, 0.0, 0.66666667], [0.66666667, 0.0, 0.66666667, 1.0, 0.0, 0.66666667], [0.0, 0.66666667, 0.0, 0.0, 1.0, 0.0], [0.66666667, 0.0, 0.66666667, 0.66666667, 0.0, 1.0], ]) np.testing.assert_allclose(weights['ff'][('task', 'item')], expected)
def param_def_sublayer(): """Generate parameter definitions for multiple context sublayers.""" param_def = parameters.Parameters() param_def.set_sublayers(f=['task'], c=['loc', 'cat']) param_def.set_sublayer_param('c', 'loc', {'B_enc': 'B_enc_loc'}) param_def.set_sublayer_param('c', 'cat', {'B_enc': 'B_enc_cat'}) weights = { (('task', 'item'), ('loc', 'item')): 'loc', (('task', 'item'), ('cat', 'item')): 'cat', } param_def.set_weights('fc', weights) param_def.set_weights('cf', weights) return param_def
def test_init_network(patterns): """Test initialization of a complex network.""" param_def = parameters.Parameters() param_def.set_dependent( w_loc='wr_loc / sqrt(wr_loc**2 + wr_cat**2)', w_cat='wr_cat / sqrt(wr_loc**2 + wr_cat**2)', s_loc='sr_loc / (sr_loc + sr_cat)', s_cat='sr_cat / (sr_loc + sr_cat)', ) param_def.set_sublayers(f=['task'], c=['loc', 'cat']) param_def.set_weights( 'fc', { (('task', 'item'), ('loc', 'item')): 'w_loc * loc', (('task', 'item'), ('cat', 'item')): 'w_cat * cat', }, ) param_def.set_weights( 'cf', { (('task', 'item'), ('loc', 'item')): 'w_loc * loc', (('task', 'item'), ('cat', 'item')): 'w_cat * cat', }, ) param_def.set_weights('ff', {('task', 'item'): 's_loc * loc + s_cat * cat'}) item_index = np.arange(3) param = {'wr_loc': 1, 'wr_cat': np.sqrt(2), 'sr_loc': 1, 'sr_cat': 2} param = param_def.eval_dependent(param) net = cmr.init_network(param_def, patterns, param, item_index) expected = np.array( [ [0.5774, 0.0000, 0.0000, 0, 0, 0, 0.0000, 0.8165, 0.0000, 0.0000], [0.0000, 0.5774, 0.0000, 0, 0, 0, 0.0000, 0.0000, 0.8165, 0.0000], [0.0000, 0.0000, 0.5774, 0, 0, 0, 0.0000, 0.8165, 0.0000, 0.0000], [0.0000, 0.0000, 0.0000, 0, 0, 0, 1.0000, 0.0000, 0.0000, 1.0000], ] ) np.testing.assert_allclose(net.w_fc_pre, expected, atol=0.0001) expected = np.array( [ [1.0000, 0.0000, 0.6667, 0.0000], [0.0000, 1.0000, 0.0000, 0.0000], [0.6667, 0.0000, 1.0000, 0.0000], [0.0000, 0.0000, 0.0000, 0.0000], ] ) np.testing.assert_allclose(net.w_ff_pre, expected, atol=0.0001)
def setup(self): self.data = fit.sample_data('Morton2013_mixed') n_items = 768 study = self.data.query('trial_type == "study"') items = study.groupby('item_index')['item'].first().to_numpy() self.patterns = {'items': items, 'vector': {'loc': np.eye(n_items)}} param_def = parameters.Parameters() param_def.set_dependent(Dfc='1 - Lfc', Dcf='1 - Lcf') param_def.set_sublayers(f=['task'], c=['task']) param_def.set_weights( 'fc', { (('task', 'item'), ('task', 'item')): 'Dfc * loc', }, ) param_def.set_weights( 'cf', { (('task', 'item'), ('task', 'item')): 'Dcf * loc', }, ) self.param_def = param_def param = { 'B_enc': 0.5, 'B_start': 0, 'B_rec': 0.8, 'Lfc': 0.8, 'Lcf': 0.8, 'P1': 1, 'P2': 1, 'T': 10, 'X1': 0.05, 'X2': 0.2, } self.param = param_def.eval_dependent(param) self.model = cmr.CMR()