This tutorial introduces a typical encoding framework for mapping linguistic embeddings onto human brain activity during natural language comprehension. The tutorial includes worked examples for both fMRI and ECoG datasets collected while subjects listened to naturalistic spoken narratives. Two types of word embeddings are obtained based on the stimulus transcripts: static word embeddings from word2vec and contextual word embeddings from GPT-2. Encoding models are estimated using banded ridge regression—this allows us to predict brain activity from word embeddings for left-out segments of data.
This tutorial can be run interactively online using Google Colab: Colab Notebook
Use git clone https://github.com/snastase/encling-tutorial.git to download this repository.
If you want to run this tutorial locally on a Mac or Linux machine, you can set up a dedicated computing environment using conda. If you don't already have a conda installation, you can install miniconda—see below. If you already have a working conda installation, you can skip this step.
If you're on a Windows machine, use PowerShell, Cygwin, PuTTY, or Windows Subsystem for Linux to log onto the PNI server, then proceed to the following Linux instructions.
If you're on a Linux machine (e.g. the PNI server), you can install miniconda using the following:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
If you're on a Mac, you can install miniconda using the following instead:
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh -o Miniconda3-latest-MacOSX-x86_64.sh
bash Miniconda3-latest-MacOSX-x86_64.sh
Next, navigate into the cloned repo and create a conda environment called encling from the environment.yml file:
conda create -f environment.yml
Finally, activate the conda environment and start JupyterLab
conda activate encling
Now, you should be able to launch jupyter lab from the command line and open the encling_tutorial.ipynb notebook.
Optionally, if you want to use updated versions of the software, you can build the conda environment from scratch:
conda create --name encling
conda activate encling
Run the following code line-by-line to install the necessary packages into the encling environment:
conda install -c huggingface transformers
conda install -c conda-forge accelerate
conda install -c pytorch pytorch numpy
conda install -c conda-forge jupyterlab ipywidgets
conda install -c conda-forge scikit-learn
conda install -c conda-forge gensim
conda install git matplotlib pandas seaborn
pip install himalaya voxelwise_tutorials
pip install nilearn surfplot neuromaps mne