EEG Feeling Emotions with PyTorch
Goal
Today, we are building a machine learning model that accepts EEG (ElectroEncephaloGram) signals and decides whether the source of these signals is happy or sad.
Data Source
EEG data from Kaggle , and full notebook can be found here .
EEG feeling emotions
1. Environment
Before hands on, get the tools 🧰 ready!
1.1 Import!
To import the tools (libraries) we need, run the following command.
1 | # Import libraries |
1.2 Turn on the GPU!
To use the GPU (which makes code run faster) in Google Colab, in the top left corner:
- Click on Edit
- Click on Notebook settings
- From the drop down menu in Hardware accelerator
- Select GPU
- Save
Ready? Check whether GPU is ready by running the following code. The output should be Using cuda.
, via which GPU is used.
1 | # Use GPU |
1.3 Locate
Find a place to play around, let’s create a folder called EEG
and we build everything inside it. The result should be /content/EEG folder
.
1 | # Create and get to `EEG` folder |
2. Data
Bring up the data✨, and let’s see what it is made of!
2.1 Download the data
The result data stored in /content/EEG/data/emotions.csv
.
1 | # Download the eeg data |
2.2 Read data
Now that we have the data in a folder, we read it into our notebook environment.
1 | # Read csv file |
We see mean, some omitted columns, fft, label.
This notebook shallowly explores the data, to better understand what is meant with each row and column, refer to the dataset.
For example, the “a” and “b” in the end refers to whether the source of data is from person “a” or person “b”.
2.3 Understand data
What is inside of this dataset? Dimensions? Type? What data can be useful?
1 | # Print data about the data |
Side notes: fft stands for Fast Fourier Transform. This data enables us to represent a kind of wave graphs (time domain signals i.e. time vs amplitude) to another kind of wave graphs (frequency vs amplitude)。
The main point here is that we can visualize that!
2.4 Visualise data
FFT columns represents elaborated signals, therefore, let’s plot that brain signal as a graph.
1 | # Extract fft data |
Now the data size has been reduced to:
- 1416 rows
- 752 columns
Why?
1 | # Plot fft brain signals |
1 | # Brain signals over time |
By eye, how would you say the differences between positive and negative brain signals?
3. Create model
Now it is time to write the logistic regression model ⚒️!
Side notes: we can if we want, write the step-by-step code of the model. But most of the times, we tend to use a frameworks (code), a prepared codebase that already has the skeleton of model. They are handy and easy to use.
This notebook uses PyTorch, a framework like this.
3.1 Dataset class
Imagine the datasets as spare data which could come in any form, how can PyTorch handle each case?
The answer is that it doesn’t handle, we as users are the ones responsible to moderate the data according to PyTorch dataset interface.
The following code is out of scope of this workshop. Just run it and should be fine, although you can have a look if interested.
1 | # Define emotion dataset in PyTorch |
3.2 Model class
We aim to predict the emotion with a logistic regression, don’t we?
Luckily, PyTorch is able to help you to write this model incredibly fast.
Side notes:
torch.nn.Linear(x, y)
mapsx
toy
in a regression line.torch.sigmoid(x)
computes the value of x after applying sigmoid function
1 | # Build Logistic Regression model |
4. Train model
The most intense part (for computers) comes now! We train 🏋 the model.
4.1. Hyperparameters
Different from the “parameters” which is obtained after training, “hyperparameters” are values that we humans choose to affect the model globally.
For example, choosing cuda
, GPU
as our hardware resource you can consider it as a kind of “hyperparameter”.
1 | # Choose hyperparameters |
4.2 Create train and test cycles
One last thing before training the model, is to wrap all the train cycle and test cycle as a “function” (function is just a block of code, callable with the name of the function). This gives us more concise and readable code.
You don’t need to fully understand what is going on inside, as it has some complex concepts. But feel free to ask if you want to know it.
1 | # Create train cycle |
1 | # Create test cycle |
4.3 Train
Finally! The following cell initialises the model and trains the model. Learning the data may take some time, please be patient. Same, some functions are specific to PyTorch, you don’t need to fully understand them. Have a guess, what do they mean?
1 | # Initialise |
1 | # Train model! |
Conclusion
Today we built a logistic regression model with PyTorch to predict the emotions based on the EEG of a person. It is a bit abstract to input made up EEG data, therefore there won’t be a predicting step here.
The original format of this post is in a google colab notebook, hence the style of this post.
- Title: EEG Feeling Emotions with PyTorch
- Author: Gnefil Voltexy
- Created at : 2022-10-26 12:23:19
- Updated at : 2024-08-26 14:13:14
- Link: https://blog.gnefil.com/2022-10-26/EEG-Feeling-Emotions-with-PyTorch/
- License: This work is licensed under CC BY-SA 4.0.