From da5aecb24458a1b78be13a836cfddffaad3cd071 Mon Sep 17 00:00:00 2001 From: TheLemonPig Date: Fri, 18 Aug 2023 14:26:03 -0400 Subject: [PATCH 01/31] Add Stroop Model to synthetic models --- .../synthetic/psychology/stroop_model.py | 275 ++++++++++++++++++ tests/test_bundled_models.py | 4 + 2 files changed, 279 insertions(+) create mode 100644 src/autora/experiment_runner/synthetic/psychology/stroop_model.py diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py new file mode 100644 index 00000000..ed2c0516 --- /dev/null +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -0,0 +1,275 @@ +from functools import partial +from typing import Optional + +import numpy as np +import torch +import torch.nn as nn +from torch.autograd import Variable + +from autora.experiment_runner.synthetic.utilities import SyntheticExperimentCollection +from autora.variable import DV, IV, ValueType, VariableCollection + + +def stroop_model( + name="Stroop Model", + resolution=10, + temperature=1.0, + added_noise=0.01, + random_state: Optional[int] = None, +): + """ + Stroop Model + + Args: + name: name of the experiment + resolution: number of allowed values for stimulus + temperature: choice temperature + added_noise: standard deviation of normally distributed noise added to y-values + random_state: integer used to seed the random number generator + """ + + params = dict( + added_noise=added_noise, + name=name, + resolution=resolution, + temperature=temperature, + random_state=random_state, + ) + + color_green = IV( + name="color_green", + allowed_values=np.linspace(0, 1, resolution), + value_range=(0, 1), + units="intensity", + variable_label="Color Green", + type=ValueType.REAL, + ) + + color_red = IV( + name="color_red", + allowed_values=np.linspace(0, 1, resolution), + value_range=(0, 1), + units="intensity", + variable_label="Color Red", + type=ValueType.REAL, + ) + + word_green = IV( + name="word_green", + allowed_values=np.linspace(0, 1, resolution), + value_range=(0, 1), + units="intensity", + variable_label="Word GREEN", + type=ValueType.REAL, + ) + + word_red = IV( + name="word_red", + allowed_values=np.linspace(0, 1, resolution), + value_range=(0, 1), + units="intensity", + variable_label="Word RED", + type=ValueType.REAL, + ) + + task_color = IV( + name="task_color", + allowed_values=[0, 1], + value_range=(0, 1), + units="intensity", + variable_label="Color Naming Task", + type=ValueType.REAL, + ) + + task_word = IV( + name="task_word", + allowed_values=[0, 1], + value_range=(0, 1), + units="intensity", + variable_label="Word Reading Task", + type=ValueType.REAL, + ) + + response_green = DV( + name="performance", + value_range=(0, 1), + units="percentage", + variable_label="P(Green Response)", + type=ValueType.PROBABILITY, + ) + + variables = VariableCollection( + independent_variables=[ + color_green, + color_red, + word_green, + word_red, + task_color, + task_word, + ], + dependent_variables=[response_green], + ) + + torch.manual_seed(random_state) + + class StroopModel(nn.Module): + def __init__(self, choice_temperature, std=0.): + super(StroopModel, self).__init__() + + self.choice_temperature = choice_temperature + self.std = std + + # define affine transformations + self.input_color_hidden_color = nn.Linear(2, 2, bias=False) + self.input_word_hidden_word = nn.Linear(2, 2, bias=False) + self.hidden_color_output = nn.Linear(2, 2, bias=False) + self.hidden_word_output = nn.Linear(2, 2, bias=False) + self.task_hidden_color = nn.Linear(2, 2, bias=False) + self.task_hidden_word = nn.Linear(2, 2, bias=False) + + self.bias = Variable(torch.ones(1) * -4, requires_grad=False) + self.input_color_hidden_color.weight.data = ( + torch.FloatTensor([[1, -1], [-1, 1]]) * 2.2 + ) + self.hidden_color_output.weight.data = ( + torch.FloatTensor([[1, -1], [-1, 1]]) * 1.3 + ) + + self.input_word_hidden_word.weight.data = ( + torch.FloatTensor([[1, -1], [-1, 1]]) * 2.6 + ) + self.hidden_word_output.weight.data = ( + torch.FloatTensor([[1, -1], [-1, 1]]) * 2.5 + ) + + self.task_hidden_color.weight.data = ( + torch.FloatTensor([[1.0, 0.0], [1.0, 0]]) * 4 + ) + self.task_hidden_word.weight.data = torch.FloatTensor([[0, 1], [0, 1]]) * 4 + + def forward(self, input): + + input = torch.Tensor(input) + if len(input.shape) <= 1: + input = input.view(1, len(input)) + + # convert inputs + color = torch.zeros(input.shape[0], 2) + word = torch.zeros(input.shape[0], 2) + task = torch.zeros(input.shape[0], 2) + + color[:, 0:2] = input[:, 0:2] + word[:, 0:2] = input[:, 2:4] + task[:, 0:2] = input[:, 4:6] + + color_hidden = torch.sigmoid( + self.input_color_hidden_color(color) + + self.task_hidden_color(task) + + self.bias + ) + + word_hidden = torch.sigmoid( + self.input_word_hidden_word(word) + self.task_hidden_word(task) + self.bias + ) + + output = self.hidden_color_output(color_hidden) + self.hidden_word_output( + word_hidden + ) + + # add noise + if self.std > 0: + output += torch.randn(output.shape) * self.std + + output_softmaxed = torch.exp(output * 1 / self.choice_temperature) / ( + torch.exp(output[:, 0] * 1 / self.choice_temperature) + + torch.exp(output[:, 1] * 1 / self.choice_temperature) + ) + + return output_softmaxed + + def experiment_runner( + X: np.ndarray, + std: float = 0.01, + ): + Y = np.zeros((X.shape[0], 1)) + + # Stroop Model according to + # Cohen, J. D., Dunbar, K. M., McClelland, J. L., & Rohrer, D. (1990). On the control of automatic processes: a parallel distributed processing account of the Stroop effect. Psychological review, 97(3), 332. + model = StroopModel(temperature, std=std) + + for idx, x in enumerate(X): + # compute regular output + output_net = model(x).detach().numpy() + p_choose_A = output_net[0][0] + + Y[idx] = p_choose_A + + return Y + + ground_truth = partial(experiment_runner, std=0.0) + + def domain(): + s1_values = variables.independent_variables[0].allowed_values + s2_values = variables.independent_variables[1].allowed_values + X = np.array(np.meshgrid(s1_values, s2_values)).T.reshape(-1, 2) + # remove all combinations where s1 > s2 + X = X[X[:, 0] <= X[:, 1]] + return X + + def plotter( + model=None, + ): + import matplotlib.colors as mcolors + import matplotlib.pyplot as plt + + colors = mcolors.TABLEAU_COLORS + col_keys = list(colors.keys()) + + S0_list = [1, 2, 4] + delta_S = np.linspace(0, 5, 100) + + for idx, S0_value in enumerate(S0_list): + S0 = S0_value + np.zeros(delta_S.shape) + S1 = S0 + delta_S + X = np.array([S0, S1]).T + y = ground_truth(X) + plt.plot( + delta_S, + y, + label=f"$S_0 = {S0_value}$ (Original)", + c=colors[col_keys[idx]], + ) + if model is not None: + y = model.predict(X) + plt.plot( + delta_S, + y, + label=f"$S_0 = {S0_value}$ (Recovered)", + c=colors[col_keys[idx]], + linestyle="--", + ) + + x_limit = [0, variables.independent_variables[0].value_range[1]] + y_limit = [0, 2] + x_label = r"Stimulus Intensity Difference $\Delta S = S_1 - S_0$" + y_label = "Perceived Intensity of Stimulus $S_1$" + + plt.xlim(x_limit) + plt.ylim(y_limit) + plt.xlabel(x_label, fontsize="large") + plt.ylabel(y_label, fontsize="large") + plt.legend(loc=2, fontsize="medium") + plt.title("Stroop Model", fontsize="x-large") + + collection = SyntheticExperimentCollection( + name=name, + description=stroop_model.__doc__, + variables=variables, + experiment_runner=experiment_runner, + ground_truth=ground_truth, + domain=domain, + plotter=plotter, + params=params, + factory_function=stroop_model, + ) + return collection diff --git a/tests/test_bundled_models.py b/tests/test_bundled_models.py index 0f619b6b..b854ec1b 100644 --- a/tests/test_bundled_models.py +++ b/tests/test_bundled_models.py @@ -11,6 +11,9 @@ from autora.experiment_runner.synthetic.psychology.luce_choice_ratio import ( luce_choice_ratio, ) +from autora.experiment_runner.synthetic.psychology.stroop_model import ( + stroop_model, +) from autora.experiment_runner.synthetic.psychophysics.weber_fechner_law import ( weber_fechner_law, ) @@ -22,6 +25,7 @@ ("luce_choice_ratio", luce_choice_ratio), ("template_experiment", template_experiment), ("weber_fechner_law", weber_fechner_law), + ("stroop_model", stroop_model), ] all_bundled_model_names = [b[0] for b in all_bundled_models] From 067cc58bcd653d18e56acb50a49699ae17ba032e Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:22:03 -0400 Subject: [PATCH 02/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../experiment_runner/synthetic/psychology/stroop_model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index ed2c0516..4cf9e9b4 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -14,7 +14,6 @@ def stroop_model( name="Stroop Model", resolution=10, temperature=1.0, - added_noise=0.01, random_state: Optional[int] = None, ): """ From 1523dacb5b65b28374b954a5b156b2bd0c1a2154 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:22:09 -0400 Subject: [PATCH 03/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../experiment_runner/synthetic/psychology/stroop_model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 4cf9e9b4..6849b1c8 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -28,7 +28,6 @@ def stroop_model( """ params = dict( - added_noise=added_noise, name=name, resolution=resolution, temperature=temperature, From 9d43cd99ae10ae0f596b81bb83fd04a4b84fa5be Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:22:13 -0400 Subject: [PATCH 04/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../experiment_runner/synthetic/psychology/stroop_model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 6849b1c8..ae29289f 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -23,7 +23,6 @@ def stroop_model( name: name of the experiment resolution: number of allowed values for stimulus temperature: choice temperature - added_noise: standard deviation of normally distributed noise added to y-values random_state: integer used to seed the random number generator """ From 90b7313fed569d72636d4d632d9f0f273c835929 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:22:20 -0400 Subject: [PATCH 05/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../experiment_runner/synthetic/psychology/stroop_model.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index ae29289f..3d116f64 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -184,10 +184,11 @@ def forward(self, input): return output_softmaxed - def experiment_runner( - X: np.ndarray, - std: float = 0.01, +def experiment_runner( + conditions: Union[pd.DataFrame, np.ndarray, np.recarray], + observation_noise: float = 0.01, ): + X = np.array(conditions) Y = np.zeros((X.shape[0], 1)) # Stroop Model according to From 60d6d8ce82346965043d16ee61c52165624eb237 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:22:25 -0400 Subject: [PATCH 06/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../experiment_runner/synthetic/psychology/stroop_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 3d116f64..8b95879c 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -204,7 +204,7 @@ def experiment_runner( return Y - ground_truth = partial(experiment_runner, std=0.0) + ground_truth = partial(experiment_runner, observation_noise=0.0) def domain(): s1_values = variables.independent_variables[0].allowed_values From c24fceeae2a8297e16648d0e4bb5935154532a6a Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:22:30 -0400 Subject: [PATCH 07/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../experiment_runner/synthetic/psychology/stroop_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 8b95879c..43ca3400 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -193,7 +193,7 @@ def experiment_runner( # Stroop Model according to # Cohen, J. D., Dunbar, K. M., McClelland, J. L., & Rohrer, D. (1990). On the control of automatic processes: a parallel distributed processing account of the Stroop effect. Psychological review, 97(3), 332. - model = StroopModel(temperature, std=std) + model = StroopModel(temperature, std=observation_noise) for idx, x in enumerate(X): # compute regular output From bbd157483bcf7d29f6b58fa3c532117079d76258 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:03:10 -0400 Subject: [PATCH 08/31] Update pyproject.toml Adding pytorch to dependencies --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 834293e7..1ece8bf7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,7 @@ license = { file = "LICENSE" } # ADD NEW DEPENDENCIES HERE dependencies = [ "autora-core", + "torch" ] [project.optional-dependencies] From 82f34f150a5dccbacc5d24b9283cef85ff458ec9 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:11:47 -0400 Subject: [PATCH 09/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../experiment_runner/synthetic/psychology/stroop_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 43ca3400..db339918 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -184,7 +184,7 @@ def forward(self, input): return output_softmaxed -def experiment_runner( + def experiment_runner( conditions: Union[pd.DataFrame, np.ndarray, np.recarray], observation_noise: float = 0.01, ): From 9d2fe7142c1dae9e6de352b8047338d03bd3a59e Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:11:52 -0400 Subject: [PATCH 10/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../experiment_runner/synthetic/psychology/stroop_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index db339918..57a3ee6b 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -1,5 +1,5 @@ from functools import partial -from typing import Optional +from typing import Optional, Union import numpy as np import torch From 161ea580ce1124f726b8f535d9734c1876dae4cf Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:11:57 -0400 Subject: [PATCH 11/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../experiment_runner/synthetic/psychology/stroop_model.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 57a3ee6b..5736e4f6 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -2,6 +2,7 @@ from typing import Optional, Union import numpy as np +import pandas as pd import torch import torch.nn as nn from torch.autograd import Variable From 02de4b8e4218a9a65c6f4f0d31c99dcf1b1fd7d3 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:16:51 -0400 Subject: [PATCH 12/31] Update pyproject.toml --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 1ece8bf7..834293e7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,6 @@ license = { file = "LICENSE" } # ADD NEW DEPENDENCIES HERE dependencies = [ "autora-core", - "torch" ] [project.optional-dependencies] From 2a5cc3d0c76e83c3476d8c01f2b9b8c068376cb6 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:25:23 -0400 Subject: [PATCH 13/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../synthetic/psychology/stroop_model.py | 42 ++++++------------- 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 5736e4f6..17611c97 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -110,40 +110,22 @@ def stroop_model( torch.manual_seed(random_state) - class StroopModel(nn.Module): + class StroopModel: def __init__(self, choice_temperature, std=0.): - super(StroopModel, self).__init__() - self.choice_temperature = choice_temperature self.std = std - + # define affine transformations - self.input_color_hidden_color = nn.Linear(2, 2, bias=False) - self.input_word_hidden_word = nn.Linear(2, 2, bias=False) - self.hidden_color_output = nn.Linear(2, 2, bias=False) - self.hidden_word_output = nn.Linear(2, 2, bias=False) - self.task_hidden_color = nn.Linear(2, 2, bias=False) - self.task_hidden_word = nn.Linear(2, 2, bias=False) - - self.bias = Variable(torch.ones(1) * -4, requires_grad=False) - self.input_color_hidden_color.weight.data = ( - torch.FloatTensor([[1, -1], [-1, 1]]) * 2.2 - ) - self.hidden_color_output.weight.data = ( - torch.FloatTensor([[1, -1], [-1, 1]]) * 1.3 - ) - - self.input_word_hidden_word.weight.data = ( - torch.FloatTensor([[1, -1], [-1, 1]]) * 2.6 - ) - self.hidden_word_output.weight.data = ( - torch.FloatTensor([[1, -1], [-1, 1]]) * 2.5 - ) - - self.task_hidden_color.weight.data = ( - torch.FloatTensor([[1.0, 0.0], [1.0, 0]]) * 4 - ) - self.task_hidden_word.weight.data = torch.FloatTensor([[0, 1], [0, 1]]) * 4 + self.input_color_hidden_color = self.init_linear(2, 2) + self.input_word_hidden_word = self.init_linear(2, 2) + self.hidden_color_output = self.init_linear(2, 2) + self.hidden_word_output = self.init_linear(2, 2) + self.task_hidden_color = self.init_linear(2, 2) + self.task_hidden_word = self.init_linear(2, 2) + + self.bias = -4 + + self.init_weights() def forward(self, input): From 9045fb25bf30559e28f25e9db00bdd30c58bf14c Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:25:33 -0400 Subject: [PATCH 14/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../synthetic/psychology/stroop_model.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 17611c97..25572cb5 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -127,6 +127,20 @@ def __init__(self, choice_temperature, std=0.): self.init_weights() + def init_linear(self, in_features, out_features): + return np.random.rand(out_features, in_features) * 0.01 + + def init_weights(self): + self.input_color_hidden_color = self.init_linear(2, 2) + self.hidden_color_output = self.init_linear(2, 2) + self.input_word_hidden_word = self.init_linear(2, 2) + self.hidden_word_output = self.init_linear(2, 2) + self.task_hidden_color = self.init_linear(2, 2) + self.task_hidden_word = self.init_linear(2, 2) + + def sigmoid(self, x): + return 1 / (1 + np.exp(-x)) + def forward(self, input): input = torch.Tensor(input) From d590c33fc3e1342dcc0096308573ea50c16f9d74 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:25:44 -0400 Subject: [PATCH 15/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../synthetic/psychology/stroop_model.py | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 25572cb5..c228a879 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -141,42 +141,42 @@ def init_weights(self): def sigmoid(self, x): return 1 / (1 + np.exp(-x)) - def forward(self, input): - input = torch.Tensor(input) + def forward(self, input): + input = np.array(input) if len(input.shape) <= 1: - input = input.view(1, len(input)) - - # convert inputs - color = torch.zeros(input.shape[0], 2) - word = torch.zeros(input.shape[0], 2) - task = torch.zeros(input.shape[0], 2) - + input = input.reshape(1, len(input)) + + color = np.zeros((input.shape[0], 2)) + word = np.zeros((input.shape[0], 2)) + task = np.zeros((input.shape[0], 2)) + color[:, 0:2] = input[:, 0:2] word[:, 0:2] = input[:, 2:4] task[:, 0:2] = input[:, 4:6] - - color_hidden = torch.sigmoid( - self.input_color_hidden_color(color) - + self.task_hidden_color(task) + + color_hidden = self.sigmoid( + np.dot(self.input_color_hidden_color, color.T) + + np.dot(self.task_hidden_color, task.T) + self.bias + ).T + + word_hidden = self.sigmoid( + np.dot(self.input_word_hidden_word, word.T) + + np.dot(self.task_hidden_word, task.T) + + self.bias + ).T + + output = np.dot(self.hidden_color_output, color_hidden.T) + np.dot( + self.hidden_word_output, word_hidden.T ) - - word_hidden = torch.sigmoid( - self.input_word_hidden_word(word) + self.task_hidden_word(task) + self.bias - ) - - output = self.hidden_color_output(color_hidden) + self.hidden_word_output( - word_hidden - ) - - # add noise + if self.std > 0: - output += torch.randn(output.shape) * self.std - - output_softmaxed = torch.exp(output * 1 / self.choice_temperature) / ( - torch.exp(output[:, 0] * 1 / self.choice_temperature) - + torch.exp(output[:, 1] * 1 / self.choice_temperature) + output += np.random.randn(*output.shape) * self.std + + output_softmaxed = np.exp(output * 1 / self.choice_temperature) / ( + np.exp(output[0] * 1 / self.choice_temperature) + + np.exp(output[1] * 1 / self.choice_temperature) ) return output_softmaxed From 1185959402d3143fdefa330c09882f11da626ea8 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:25:50 -0400 Subject: [PATCH 16/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../experiment_runner/synthetic/psychology/stroop_model.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index c228a879..b927865b 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -3,9 +3,6 @@ import numpy as np import pandas as pd -import torch -import torch.nn as nn -from torch.autograd import Variable from autora.experiment_runner.synthetic.utilities import SyntheticExperimentCollection from autora.variable import DV, IV, ValueType, VariableCollection From 4f9016ce27397ed09b916107a21c592edf35f25b Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:32:19 -0400 Subject: [PATCH 17/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../experiment_runner/synthetic/psychology/stroop_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index b927865b..80d697de 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -105,7 +105,7 @@ def stroop_model( dependent_variables=[response_green], ) - torch.manual_seed(random_state) + rng = np.random.default_rng(random_state) class StroopModel: def __init__(self, choice_temperature, std=0.): From b1c6471866dd07442872934e76010fb3a7da6c98 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:32:26 -0400 Subject: [PATCH 18/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../experiment_runner/synthetic/psychology/stroop_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 80d697de..72e34fb7 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -125,7 +125,7 @@ def __init__(self, choice_temperature, std=0.): self.init_weights() def init_linear(self, in_features, out_features): - return np.random.rand(out_features, in_features) * 0.01 + return rng.random.rand(out_features, in_features) * 0.01 def init_weights(self): self.input_color_hidden_color = self.init_linear(2, 2) From 0e325133a7084f99fcdec011abdc034472720baa Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:32:31 -0400 Subject: [PATCH 19/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py --- .../experiment_runner/synthetic/psychology/stroop_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 72e34fb7..386c35b4 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -169,7 +169,7 @@ def forward(self, input): ) if self.std > 0: - output += np.random.randn(*output.shape) * self.std + output += rng.random.randn(*output.shape) * self.std output_softmaxed = np.exp(output * 1 / self.choice_temperature) / ( np.exp(output[0] * 1 / self.choice_temperature) From 7f3d3ed6352456bc47147856edc0fdb384ff36f2 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Thu, 31 Aug 2023 13:22:30 -0400 Subject: [PATCH 20/31] Update tests/test_bundled_models.py --- tests/test_bundled_models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_bundled_models.py b/tests/test_bundled_models.py index 23dfb2bf..d21d9a65 100644 --- a/tests/test_bundled_models.py +++ b/tests/test_bundled_models.py @@ -13,6 +13,7 @@ ) from autora.experiment_runner.synthetic.psychology.stroop_model import ( stroop_model, +) from autora.experiment_runner.synthetic.neuroscience.task_switching import ( task_switching, ) From df52f7a21bbd0870e3a0e2f00aef5a2d690045c9 Mon Sep 17 00:00:00 2001 From: Sebastian Musslick Date: Sun, 3 Sep 2023 06:27:19 -0400 Subject: [PATCH 21/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py Co-authored-by: Younes Strittmatter --- .../experiment_runner/synthetic/psychology/stroop_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 386c35b4..74731b23 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -257,7 +257,7 @@ def plotter( name=name, description=stroop_model.__doc__, variables=variables, - experiment_runner=experiment_runner, + run=run, ground_truth=ground_truth, domain=domain, plotter=plotter, From d5e0e713407a985dcb89876072e6e86db2fc4bc6 Mon Sep 17 00:00:00 2001 From: Sebastian Musslick Date: Sun, 3 Sep 2023 06:27:27 -0400 Subject: [PATCH 22/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py Co-authored-by: Younes Strittmatter --- .../experiment_runner/synthetic/psychology/stroop_model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 74731b23..00fa17d8 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -12,7 +12,6 @@ def stroop_model( name="Stroop Model", resolution=10, temperature=1.0, - random_state: Optional[int] = None, ): """ Stroop Model From a3e4c401f591431fb2e00629c9b362858da44a6e Mon Sep 17 00:00:00 2001 From: Sebastian Musslick Date: Sun, 3 Sep 2023 06:27:36 -0400 Subject: [PATCH 23/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py Co-authored-by: Younes Strittmatter --- .../experiment_runner/synthetic/psychology/stroop_model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 00fa17d8..7b523e13 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -20,7 +20,6 @@ def stroop_model( name: name of the experiment resolution: number of allowed values for stimulus temperature: choice temperature - random_state: integer used to seed the random number generator """ params = dict( From 37d9bdadf866b5d7ece1e1524f776fd7c70538b9 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:55:16 -0400 Subject: [PATCH 24/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py Co-authored-by: Younes Strittmatter --- .../experiment_runner/synthetic/psychology/stroop_model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 7b523e13..edab4083 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -26,7 +26,6 @@ def stroop_model( name=name, resolution=resolution, temperature=temperature, - random_state=random_state, ) color_green = IV( From 1296cf99dc7576cceab6f6ec857b8bc5cb675d68 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:55:26 -0400 Subject: [PATCH 25/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py Co-authored-by: Younes Strittmatter --- .../experiment_runner/synthetic/psychology/stroop_model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index edab4083..1357acb9 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -102,7 +102,6 @@ def stroop_model( dependent_variables=[response_green], ) - rng = np.random.default_rng(random_state) class StroopModel: def __init__(self, choice_temperature, std=0.): From 8e5c293e9eabe2daac4b4d4ef13d0eca0b61987a Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:55:35 -0400 Subject: [PATCH 26/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py Co-authored-by: Younes Strittmatter --- .../experiment_runner/synthetic/psychology/stroop_model.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 1357acb9..df3c2d29 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -177,6 +177,7 @@ def forward(self, input): def experiment_runner( conditions: Union[pd.DataFrame, np.ndarray, np.recarray], observation_noise: float = 0.01, + random_state: Optional[int] = None ): X = np.array(conditions) Y = np.zeros((X.shape[0], 1)) From 86d78e309960666c83741143746e03b7e16183f5 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:55:41 -0400 Subject: [PATCH 27/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py Co-authored-by: Younes Strittmatter --- .../experiment_runner/synthetic/psychology/stroop_model.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index df3c2d29..31219d94 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -179,6 +179,7 @@ def experiment_runner( observation_noise: float = 0.01, random_state: Optional[int] = None ): + rng = np.random.default_rng(random_state) X = np.array(conditions) Y = np.zeros((X.shape[0], 1)) From 004dc4bcaf950007a8c3c129ae9db8040001a939 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:55:49 -0400 Subject: [PATCH 28/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py Co-authored-by: Younes Strittmatter --- .../experiment_runner/synthetic/psychology/stroop_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 31219d94..e02e122b 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -185,7 +185,7 @@ def experiment_runner( # Stroop Model according to # Cohen, J. D., Dunbar, K. M., McClelland, J. L., & Rohrer, D. (1990). On the control of automatic processes: a parallel distributed processing account of the Stroop effect. Psychological review, 97(3), 332. - model = StroopModel(temperature, std=observation_noise) + model = StroopModel(temperature, std=added_noise) for idx, x in enumerate(X): # compute regular output From 0b5c29bac6a0c12124b9af0ba8202c90ae521496 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:55:56 -0400 Subject: [PATCH 29/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py Co-authored-by: Younes Strittmatter --- .../experiment_runner/synthetic/psychology/stroop_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index e02e122b..159ba68d 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -196,7 +196,7 @@ def experiment_runner( return Y - ground_truth = partial(experiment_runner, observation_noise=0.0) + ground_truth = partial(run, added_noise=0.0) def domain(): s1_values = variables.independent_variables[0].allowed_values From 3be927e04be4494b2f06e220528eed43f5842d31 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:58:39 -0400 Subject: [PATCH 30/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py Co-authored-by: Younes Strittmatter --- .../experiment_runner/synthetic/psychology/stroop_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index 159ba68d..c5332387 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -174,7 +174,7 @@ def forward(self, input): return output_softmaxed - def experiment_runner( + def run( conditions: Union[pd.DataFrame, np.ndarray, np.recarray], observation_noise: float = 0.01, random_state: Optional[int] = None From 5f8171d1b68d9deb1872059d7860009ad79abe42 Mon Sep 17 00:00:00 2001 From: TheLemonPig <43144407+TheLemonPig@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:58:46 -0400 Subject: [PATCH 31/31] Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py Co-authored-by: Younes Strittmatter --- .../experiment_runner/synthetic/psychology/stroop_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py index c5332387..ab552dc8 100644 --- a/src/autora/experiment_runner/synthetic/psychology/stroop_model.py +++ b/src/autora/experiment_runner/synthetic/psychology/stroop_model.py @@ -176,7 +176,7 @@ def forward(self, input): def run( conditions: Union[pd.DataFrame, np.ndarray, np.recarray], - observation_noise: float = 0.01, + added_noise: float = 0.01, random_state: Optional[int] = None ): rng = np.random.default_rng(random_state)