-
Notifications
You must be signed in to change notification settings - Fork 0
Add Stroop Model to synthetic models #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheLemonPig
wants to merge
35
commits into
main
Choose a base branch
from
feat/add-stroop-model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
da5aecb
Add Stroop Model to synthetic models
TheLemonPig 067cc58
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 1523dac
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 9d43cd9
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 90b7313
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 60d6d8c
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig c24fcee
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig bbd1574
Update pyproject.toml
TheLemonPig f72ac13
Merge branch 'main' into feat/add-stroop-model
TheLemonPig 82f34f1
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 9d2fe71
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 161ea58
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 02de4b8
Update pyproject.toml
TheLemonPig 2a5cc3d
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 9045fb2
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig d590c33
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 1185959
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 4f9016c
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig b1c6471
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 0e32513
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 2a81873
Merge branch 'main' into feat/add-stroop-model
TheLemonPig 7f3d3ed
Update tests/test_bundled_models.py
TheLemonPig df52f7a
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
musslick d5e0e71
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
musslick a3e4c40
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
musslick 7754e56
Merge branch 'main' into feat/add-stroop-model
TheLemonPig 37d9bda
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 1296cf9
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 8e5c293
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 86d78e3
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 004dc4b
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 0b5c29b
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 3be927e
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig 5f8171d
Update src/autora/experiment_runner/synthetic/psychology/stroop_model.py
TheLemonPig bf52efb
Merge branch 'main' into feat/add-stroop-model
younesStrittmatter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
274 changes: 274 additions & 0 deletions
274
src/autora/experiment_runner/synthetic/psychology/stroop_model.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,274 @@ | ||
| from functools import partial | ||
| 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 | ||
TheLemonPig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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, | ||
| random_state: Optional[int] = None, | ||
TheLemonPig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
musslick marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ): | ||
| """ | ||
| Stroop Model | ||
|
|
||
| Args: | ||
| 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 | ||
musslick marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
|
|
||
| params = dict( | ||
| name=name, | ||
| resolution=resolution, | ||
| temperature=temperature, | ||
| random_state=random_state, | ||
TheLemonPig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| 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) | ||
TheLemonPig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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 | ||
TheLemonPig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
TheLemonPig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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) | ||
| ) | ||
TheLemonPig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return output_softmaxed | ||
|
|
||
| def experiment_runner( | ||
TheLemonPig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| conditions: Union[pd.DataFrame, np.ndarray, np.recarray], | ||
| observation_noise: float = 0.01, | ||
TheLemonPig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ): | ||
TheLemonPig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| X = np.array(conditions) | ||
TheLemonPig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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=observation_noise) | ||
TheLemonPig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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, observation_noise=0.0) | ||
TheLemonPig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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, | ||
musslick marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ground_truth=ground_truth, | ||
| domain=domain, | ||
| plotter=plotter, | ||
| params=params, | ||
| factory_function=stroop_model, | ||
| ) | ||
| return collection | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.