Build a 1D convolutional neural network, part 1: Create a test data set

Brandon Rohrer · Beginner ·🧬 Deep Learning ·5y ago

Key Takeaways

This video teaches how to create a test data set for a 1D convolutional neural network

Full Transcript

we would really like to take our convolutional block out for a spin so in order to do that we need an appropriate data set we need a one dimensional data set so one dimensional array that has some kind of a pattern in it that's recognizable but that might fall in a different location each time this is what convolution is good at identifying so we'll create one a data set that's small enough that we can include it with cottonwood for testing but that's complicated enough that it'll show whether our algorithm is working as we hope we'll create a series of blips so in the data directory we'll create a module called data loader blips dot pi and we want this to be a block more specifically we want it to be two blocks we want to simulate having training data and having evaluation data so we'll create two blocks a training data block and an evaluation data block now for our purposes we don't actually care these will be exactly the same but this will set up a good pattern that we can use later when we actually do have data that's segmented into training and evaluation or training and tuning and evaluation so you can see here in our training data and our evaluation data classes these are both blocks and they have the minimum signature they each have a forward pass and a backward pass they also have the dunder str method that returns a string that describes them briefly and then they also have a dunder init method that initializes them this is all you need to create a block to create something that can be connected to other things i want to call out that both of the forward pass and the backward pass they accept an argument this is not because the data loader needs an argument but because the structure we'll expect to be able to pass an argument to forward and backward past functions of every block even if it's empty even if it's nonsense so we need to include an argument there to be able to accept that if it should be passed in our case we won't need it we won't use it here we rely on a get data sets function that returns two generators a training data generator and an evaluation data generator a generator is a python function that is special you can call next on it and it'll give you the next thing on the list that that generator has so you can think of a generator it's just like a tall stack a deck of cards next just deals off the next one off the top and so by having two generators that are get data sets function returns we can collect the first one and use it to initialize our training data block and we can peel off the second one and use it to initialize our evaluation data block and then on the forward pass we go to that generator and we pull off the first example each time so our next move is to actually write this get data sets function see what it does so here in get data sets we'll take and kick the can down the road a little bit we'll call a get blips function and these will be the actual data in our data set and we'll just pull those into something called examples this is a long list of possible data points in this case a one-dimensional signal plus a label we'll use this to create two generators a training set generator and an evaluation set generator in each of these we set up an infinite loop while true randomly choose some index from the length of this list of examples and then return the example that corresponds to that index so in each case we take our big bag of examples we randomly pull one out send a copy of it put it back in the bag and then do that over and over again the training set and the evaluation set are both pulling out of the same bag so we're not going to expect to see any difference there this is not what we're testing with this data set here we just want something that will put our convolutional block through its paces and then we take these two functions that we created within the function and return the functions themselves the training set and the valuation set and these then are what become the engines in our training data and evaluation data blocks that we just looked at now get blips this is where we generate our actual fake data so the way we do that is we seed our random number generator so that we'll get due to random results but the same ones every time we run it then we initialize an empty list of blips we specify that we want our signal to be 21 elements long but the blip that occurs will just be seven elements long so our entire signal will be zero except for this little island of seven elements that will be non-zero and we'll generate a hundred different ones of these for each flavor of blip now we defined our four different flavors they'll each be named after a capital letter m v n and h and these are very approximately named by the shape that they take if you plot them out on a line so if you look they're each seven numbers long if you look at the m for instance it jumps high with the first element one and then descends lower lower down to 0.1 and then back up higher 0.4.7 back up to 1 and then it'll drop down to 0 again because all the rest of the values are 0. so it kind of looks like the capital letter m sitting on a number line v starts the numbers before and after are zero it descends minus point one minus point four all the way down to minus one and then back up gradually on a ramp back up to zero so it kind of looks like a v or a notch or a divot in the number line the n starts at zero and then drops to minus point seven jumps to point seven slowly descends down to minus 0.7 jumps back up to 0.7 and then it'll settle back to zero and then the h doesn't really look like an h at all it has a peak up to one back down to zero and then at the other end another peak down to minus one and then back to zero so two isolated peaks in different directions these are our four different flavors of blip there's seven elements long and when we generate an example of a blip we'll start with a full example full of zeros so 21 zeros in a row we'll randomly choose a location in the middle and set those seven elements to be this particular flavor of blip and then we return that we make sure to modify it so that it's actually a two-dimensional array so it's uh in the column direction all in one row this is the format that the convolutional neural network will expect to see it in remember channels are across rows and the signal is across columns so this is a single channel signal and then we set up for the number of examples of each flavor we iterate through and generate one of each an m a v an n and an h we generate a tuple so a paired signal with a blip and then the label so a just a string capital letter m v n and h so that we know both the data and the label that should go with it so each of these is an example and then this set of blips this list of blips is what gets returned and used to fuel the generators that then form the engines for our training data and evaluation data blocks this means that each time we call forward pass on training data or evaluation data what we get is a two-dimensional array one row by 21 columns with a blip somewhere in there of seven elements that are non-zero and it'll be one of four flavors and then it'll also come in a tuple with the label for what flavor that should be m v n or h so we're getting two elements a two item tuple each time we call forward pass there's a little bit of code here at the bottom this i found particularly useful when writing the data loader just to be able to test the results as we go and i left it here because it's a good example of a simple end-to-end test it doesn't absolutely guarantee that every tiny part works as intended but it does show broadly that you can run it you can look at the result the result looks reasonable if anything were badly broken it would fail to run or produce very wild output so this lets us verify that everything's working as it should be and now we have data a set of training and evaluation data blocks to pair with our convolutional neural network

Original Description

Get the full course experience at https://e2eml.school/321 This course starts out with all the fundamentals of convolutional neural networks in one dimension for maximum clarity. We will extend Cottonwood to handle convolutional architectures and apply it to classifying electrically-measured heartbeats as healthy or irregular.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Brandon Rohrer · Brandon Rohrer · 0 of 60

← Previous Next →
1 Robot Learning with a Biologically-Inspired Brain (BECCA)
Robot Learning with a Biologically-Inspired Brain (BECCA)
Brandon Rohrer
2 BECCA talk at AGI 2011
BECCA talk at AGI 2011
Brandon Rohrer
3 Robot Learning with a Biologically-Inspired Brain (BECCA), The Sequel
Robot Learning with a Biologically-Inspired Brain (BECCA), The Sequel
Brandon Rohrer
4 BECCA listens to The Hobbit
BECCA listens to The Hobbit
Brandon Rohrer
5 Learning the building blocks of speech: BECCA extracts a hierarchy of audio features
Learning the building blocks of speech: BECCA extracts a hierarchy of audio features
Brandon Rohrer
6 BECCA listens for sound effects in The Hobbit
BECCA listens for sound effects in The Hobbit
Brandon Rohrer
7 BECCA finds movie trailers while watching the Big Bang Theory
BECCA finds movie trailers while watching the Big Bang Theory
Brandon Rohrer
8 Listening for unexpected sounds: BECCA detects anomalies in audio data
Listening for unexpected sounds: BECCA detects anomalies in audio data
Brandon Rohrer
9 Learning the building blocks of vision: BECCA extracts a spatio-temporal hierarchy of features
Learning the building blocks of vision: BECCA extracts a spatio-temporal hierarchy of features
Brandon Rohrer
10 Watching for the unexpected: BECCA detects anomalies in video data
Watching for the unexpected: BECCA detects anomalies in video data
Brandon Rohrer
11 BECCA finds a stationary target
BECCA finds a stationary target
Brandon Rohrer
12 BECCA finds a stationary target at 3X speed
BECCA finds a stationary target at 3X speed
Brandon Rohrer
13 BECCA watches the X-men and Bruce Lee
BECCA watches the X-men and Bruce Lee
Brandon Rohrer
14 BECCA plays Quidditch
BECCA plays Quidditch
Brandon Rohrer
15 BECCA chases a ball
BECCA chases a ball
Brandon Rohrer
16 BECCA chases a ball, part 2
BECCA chases a ball, part 2
Brandon Rohrer
17 Becca chases a ball, part 3
Becca chases a ball, part 3
Brandon Rohrer
18 BECCA creates features from MNIST
BECCA creates features from MNIST
Brandon Rohrer
19 How reinforcement learning works in Becca 7
How reinforcement learning works in Becca 7
Brandon Rohrer
20 Deep Learning Demystified
Deep Learning Demystified
Brandon Rohrer
21 How Data Science Works
How Data Science Works
Brandon Rohrer
22 How Convolutional Neural Networks work
How Convolutional Neural Networks work
Brandon Rohrer
23 How Bayes Theorem works
How Bayes Theorem works
Brandon Rohrer
24 How Deep Neural Networks Work
How Deep Neural Networks Work
Brandon Rohrer
25 Recurrent Neural Networks (RNN) and Long Short-Term Memory (LSTM)
Recurrent Neural Networks (RNN) and Long Short-Term Memory (LSTM)
Brandon Rohrer
26 How Support Vector Machines work / How to open a black box
How Support Vector Machines work / How to open a black box
Brandon Rohrer
27 How autocorrelation works
How autocorrelation works
Brandon Rohrer
28 Getting closer to human intelligence through robotics
Getting closer to human intelligence through robotics
Brandon Rohrer
29 A minimalist's guide to slicing and indexing pandas DataFrames
A minimalist's guide to slicing and indexing pandas DataFrames
Brandon Rohrer
30 How decision trees work
How decision trees work
Brandon Rohrer
31 Data scientist archetypes
Data scientist archetypes
Brandon Rohrer
32 How to use python's datetime package
How to use python's datetime package
Brandon Rohrer
33 How optimization for machine learning works, part 1
How optimization for machine learning works, part 1
Brandon Rohrer
34 How optimization for machine learning works, part 2
How optimization for machine learning works, part 2
Brandon Rohrer
35 How optimization for machine learning works, part 3
How optimization for machine learning works, part 3
Brandon Rohrer
36 How optimization for machine learning works, part 4
How optimization for machine learning works, part 4
Brandon Rohrer
37 How convolutional neural networks work, in depth
How convolutional neural networks work, in depth
Brandon Rohrer
38 How to pick a machine learning model 4: Splitting the data
How to pick a machine learning model 4: Splitting the data
Brandon Rohrer
39 How to pick a machine learning model 3: Choosing a loss function
How to pick a machine learning model 3: Choosing a loss function
Brandon Rohrer
40 How to pick a machine learning model 2: Separating signal from noise
How to pick a machine learning model 2: Separating signal from noise
Brandon Rohrer
41 How to pick a machine learning model 1: Choosing between models
How to pick a machine learning model 1: Choosing between models
Brandon Rohrer
42 How to pick a machine learning model 5: Navigating assumptions
How to pick a machine learning model 5: Navigating assumptions
Brandon Rohrer
43 What do neural networks learn?
What do neural networks learn?
Brandon Rohrer
44 Interview with iRobot's Director of Data Science Angela Bassa
Interview with iRobot's Director of Data Science Angela Bassa
Brandon Rohrer
45 How Backpropagation Works
How Backpropagation Works
Brandon Rohrer
46 Evolutionary Powell's method: A discrete optimizer for hyperparameter optimization
Evolutionary Powell's method: A discrete optimizer for hyperparameter optimization
Brandon Rohrer
47 1D convolution for neural networks, part 1: Sliding dot product
1D convolution for neural networks, part 1: Sliding dot product
Brandon Rohrer
48 1D convolution for neural networks, part 2: Convolution copies the kernel
1D convolution for neural networks, part 2: Convolution copies the kernel
Brandon Rohrer
49 1D convolution for neural networks, part 3: Sliding dot product equations longhand
1D convolution for neural networks, part 3: Sliding dot product equations longhand
Brandon Rohrer
50 1D convolution for neural networks, part 4: Convolution equation
1D convolution for neural networks, part 4: Convolution equation
Brandon Rohrer
51 1D convolution for neural networks, part 5: Backpropagation
1D convolution for neural networks, part 5: Backpropagation
Brandon Rohrer
52 1D convolution for neural networks, part 6: Input gradient
1D convolution for neural networks, part 6: Input gradient
Brandon Rohrer
53 1D convolution for neural networks, part 7: Weight gradient
1D convolution for neural networks, part 7: Weight gradient
Brandon Rohrer
54 1D convolution for neural networks, part 8: Padding
1D convolution for neural networks, part 8: Padding
Brandon Rohrer
55 1D convolution for neural networks, part 9: Stride
1D convolution for neural networks, part 9: Stride
Brandon Rohrer
56 The Four Grand Challenges of Robots in the Home
The Four Grand Challenges of Robots in the Home
Brandon Rohrer
57 How Convolution Works
How Convolution Works
Brandon Rohrer
58 The Softmax neural network layer
The Softmax neural network layer
Brandon Rohrer
59 Batch normalization
Batch normalization
Brandon Rohrer
60 Getting ready to learn Python, Mac edition #1: Files and directories
Getting ready to learn Python, Mac edition #1: Files and directories
Brandon Rohrer

Related Reads

Up next
RNNs Explained in 60 Seconds #ai #coding #machinelearning
Ascent
Watch →