In today's web development landscape, learning models, particularly neural networks, are extensively used in a variety of applications such as natural language processing, image recognition, recommendation systems, and more. These models enable computers to make predictions, understand human language, and recognize patterns in data, leading to more intelligent and responsive applications. By the end of this lecture, you will have a foundational understanding of how neural networks are structured and be able to set up binary and multi-class classification models.
Learning models are algorithms that enable machines to learn from data and make decisions or predictions based on that data. These models range from simple linear regressions to complex neural networks. Neural networks, inspired by the human brain, consist of interconnected nodes (neurons) that process data in layers.
- Input Layer: This is where the network receives its input data. Each node in this layer represents a feature of the input data.
- Linear Layers | Hidden Layers: These layers, also known as dense or fully connected layers, perform linear transformations on the input data. Each neuron in a linear layer is connected to every neuron in the previous layer.
- Activation Layers: After linear transformations, activation functions are applied to introduce non-linearity into the model. Common activation functions include ReLU (Rectified Linear Unit), sigmoid, and tanh.
- Fully Connected Neural Networks: These networks have multiple layers of interconnected neurons, allowing them to model complex relationships in data. The output layer provides the final prediction or classification.
Neural networks learn by adjusting the weights of the connections between neurons based on the error of their predictions. This process is called backpropagation and is crucial for the training of the model.
A Python virtual environment is an isolated environment that allows you to manage dependencies for different projects separately. This ensures that the packages required for one project do not interfere with those of another. Virtual environments are essential for maintaining clean and manageable project setups, especially when working on multiple projects with different dependencies.
- Isolation of dependencies: Each virtual environment has its own independent set of installed packages.
- Version control: Different projects can use different versions of the same package without conflicts.
- Easy setup and removal: Virtual environments can be created and deleted without affecting the global Python installation.
- Resource consumption: Each virtual environment can consume disk space, which can add up if many environments are created.
- Management overhead: Keeping track of multiple virtual environments can become cumbersome.
Here are the steps to create a Python virtual environment on a Linux terminal:
-
Install
venv(if not already installed):# wsl users only sudo apt-get install python3-venv -
Create a virtual environment:
python3 -m venv myenv
-
Activate the virtual environment:
source myenv/bin/activateYou'll see (myenv) on the lower left hand corner of your terminal.
-
Upgrade your current version of pip:
pip install --upgrade pip
-
Deactivate the virtual environment:
deactivate
bin/: Contains the executables for the virtual environment.- activate and deactivate bash scripts
- pip python scripts
- Python C level COMPILED scripts
lib/: Contains the site-packages directory where all the installed libraries are stored.include/: Contains C headers that are needed to build Python packages.pyvenv.cfg: A configuration file for the virtual environment.
PyTorch is an open-source deep learning framework that provides a flexible and efficient platform for building and training neural networks. It is widely used for both research and production due to its ease of use and dynamic computation graph capabilities.
- Ease of use: PyTorch has a straightforward API that is easy to learn and use.
- Dynamic computation graph: Unlike static graphs used by other frameworks, PyTorch allows you to change the graph on the fly, making it more intuitive for debugging and experimentation.
- Community and ecosystem: PyTorch has a strong community and a rich ecosystem of tools and libraries.
-
Activate your virtual environment:
source myenv/bin/activate -
Install PyTorch:
pip install torch torchvision torchaudio
-
Verify the installation:
python -c "import torch; print(torch.__version__)"
Jupyter Notebooks are an open-source web application that allows you to create and share documents containing live code, equations, visualizations, and narrative text. They are widely used in data science and machine learning for exploratory data analysis and prototyping.
- Interactive coding: Allows for real-time feedback and visualization of code results.
- Documentation and visualization: Combine code with rich text and visualizations for better documentation and presentation.
- Reproducibility: Share notebooks with others to reproduce the analysis or experiments.
-
Install Jupyter:
pip install jupyter
-
Install the Jupyter extension in VSCode:
- Open VSCode.
- Go to the Extensions view by clicking the square icon in the sidebar or pressing
Ctrl+Shift+X. - Search for "Jupyter" and install the extension.
-
Install dependencies for Jupyter Notebooks:
JupyterNotebooks does not operate like regular Python files, so it needs a different kernel to manage memory allocations, references, and compile Python code.
pip install ipykernel
-
Connecting a virtual environment to a Jupyter Notebook:
-
Open a new Jupyter Notebook in VSCode:
touch <name_of_file>.ipynb
-
On the upper right hand corner click on select kernel
-
Select
myvenvas your kernel
-
-
Proof that Jupyter Notebook is running with PyTorch installed:
-
In a new notebook cell, type and run the following code:
import torch print(torch.__version__)
-
If PyTorch is correctly installed, it will print the version number.
-
We will see more about the power of Jupyter Notebooks through out this module but for now this will be our stop where we confirm the following:
- Created and understand Python Virtual Environments
- Installed and created a Jupyter Notebook that can execute Python Code.
- Installed Pytorch to start building Learning Models.
In this lecture, we've introduced the basics of learning models, discussed the importance of Python virtual environments, and walked through the installation of PyTorch and Jupyter Notebooks in VSCode. By understanding these foundational concepts and tools, you are now ready to start building and experimenting with neural networks. Keep practicing, and soon you'll be able to create complex models for various applications. Happy coding!

