Word embeddings are one of the most significant advancements in natural language processing (NLP). They allow us to
transform words or sentences into vectors, where each word is represented by a point in a high-dimensional space.
The core idea is that words with similar meanings are close to each other in this space, making it possible to use
mathematical operations on these vectors to uncover relationships between words.
In this post, we’ll explore how to create word embeddings using a pre-trained model, and we’ll perform various vector
operations to see how these embeddings capture semantic relationships. We’ll cover examples like analogy generation,
word similarity, and how these embeddings can be leveraged for search tasks.
What Are Word Embeddings?
Word embeddings are dense vector representations of words, where each word is mapped to a point in a continuous vector
space. Unlike older techniques (such as one-hot encoding) that give each word a unique identifier, embeddings represent
words in a way that captures semantic relationships, such as similarity and analogy.
For example, embeddings can represent the relationship:
This is made possible because words that are semantically similar (e.g., “king” and “queen”) have vector representations
that are close together in space, while words that are opposites (e.g., “good” and “bad”) may have vectors pointing in
opposite directions.
Gensim
Let’s begin by loading a pre-trained word embedding model. We’ll use the glove-wiki-gigaword-50 model, which provides
50-dimensional vectors for many common words.
This might take a moment to download. It’s not too big.
Now that we have the model, let’s try converting some words into vectors.
Converting Words to Vectors
We can take individual words and get their vector representations. Let’s look at the vectors for “king,” “queen,” “man,”
and “woman.”
You’ll see that each word is represented as a 50-dimensional vector. These vectors capture the meanings of the words in
such a way that we can manipulate them mathematically.
Performing Vector Arithmetic
One of the most famous examples of vector arithmetic in word embeddings is the analogy:
We can perform this operation by subtracting the vector for “man” from “king” and then adding the vector for “woman.”
Let’s try this and see what word is closest to the resulting vector.
You should find that the word closest to the resulting vector is “queen,” demonstrating that the model captures the
gender relationship between “king” and “queen.”
Measuring Word Similarity with Cosine Similarity
Another key operation you can perform on word embeddings is measuring the similarity between two words. The most common
way to do this is by calculating the cosine similarity between the two vectors. The cosine similarity between two
vectors is defined as:
1 means the vectors are identical (the words are very similar),
0 means the vectors are orthogonal (unrelated words),
-1 means the vectors are pointing in opposite directions (possibly antonyms).
Let’s measure the similarity between related words like “apple” and “fruit,” and compare it to unrelated words like
“apple” and “car.”
You will see that the cosine similarity between “apple” and “fruit” is much higher than that between “apple” and “car,”
illustrating the semantic relationship between “apple” and “fruit.”
Search Using Word Embeddings
Another powerful use of word embeddings is in search tasks. If you want to find words that are most similar to a given
word, you can use the model’s similar_by_word function to retrieve the top N most similar words. Here’s how you can
search for words most similar to “apple”:
You can see here that “apple” is treated in the proper noun sense as in the company Apple.
Each of these words has strong relevance to the company.
Averaging Word Vectors
Another interesting operation is averaging word vectors. This allows us to combine the meaning of two words into a
single vector. For instance, we could average the vectors for “apple” and “orange” to get a vector that represents
something like “fruit.”
There are a number of related words to both “apple” and “orange”. The average provides us with this intersection.
Conclusion
Word embeddings are a powerful way to represent the meaning of words as vectors in a high-dimensional space. By using
simple mathematical operations, such as vector arithmetic and cosine similarity, we can uncover a variety of semantic
relationships between words. These operations allow embeddings to be used in tasks such as analogy generation, search,
and clustering.
In this post, we explored how to use pre-trained word embeddings, perform vector operations, and leverage them for
real-world tasks. These foundational concepts are what power much of the magic behind modern NLP techniques, from search
engines to chatbots and more.
In mathematics, the straight line equation \(y = mx + c\) is one of the simplest yet most foundational equations in
both algebra and geometry. It defines a linear relationship between two variables, \(x\) and \(y\), where \(m\)
represents the slope (or gradient) of the line, and \(c\) is the y-intercept, the point where the line crosses the
y-axis.
This article explores key concepts related to the straight line equation, interesting properties, and how we can use
Haskell to implement some useful functions.
Understanding the Equation
The equation \(y = mx + c\) allows us to describe a straight line in a two-dimensional plane. Here’s a breakdown of
its components:
\(m\): The slope, which measures how steep the line is. It’s defined as the change in \(y\) divided by the change in \(x\) , or \(\frac{\Delta y}{\Delta x}\).
\(c\): The y-intercept, which is the value of \(y\) when \(x = 0\).
One of the key properties of this equation is that for every unit increase in \(x\), the value of \(y\) increases
by \(m\). We can illustrate this behavior using some Haskell code.
Basic Line Function in Haskell
Let’s implement the basic straight line function in Haskell. This function will take \(m\), \(c\), and \(x\) as
inputs and return the corresponding \(y\) value.
This function calculates \(y\) for any given \(x\) using the slope \(m\) and y-intercept \(c\).
Parallel and Perpendicular Lines
An interesting aspect of lines is how they relate to each other. If two lines are parallel, they have the same slope.
If two lines are perpendicular, the slope of one is the negative reciprocal of the other. In mathematical terms, if one
line has a slope \(m_1\), the perpendicular line has a slope of \(-\frac{1}{m_1}\).
We can express this relationship in Haskell using a function to check if two lines are perpendicular.
This function takes two slopes and returns True if they are perpendicular and False otherwise.
Finding the Intersection of Two Lines
To find the point where two lines intersect, we need to solve the system of equations:
\(y = m_1x + c_1\)
\(y = m_2x + c_2\)
By setting the equations equal to each other, we can solve for \(x\) and then substitute the result into one of the
equations to find \(y\). The formula for the intersection point is:
\[x = \frac{c_2 - c_1}{m_1 - m_2}\]
Here’s a Haskell function that calculates the intersection point of two lines:
This function returns Nothing if the lines are parallel and Just (x, y) if the lines intersect.
Conclusion
The straight line equation \(y = mx + c\) is a simple but powerful tool in both mathematics and programming. We’ve
explored how to implement the line equation in Haskell, find parallel and perpendicular lines, and calculate
intersection points. Understanding these properties gives you a deeper appreciation of how linear relationships work,
both in theory and in practice.
By writing these functions in Haskell, you can model and manipulate straight lines in code, extending these basic
principles to more complex problems.
The quadratic equation is one of the fundamental concepts in algebra and forms the basis of many more complex topics in
mathematics and computer science. It has the general form:
\[ax^2 + bx + c = 0\]
where \(a\), \(b\), and \(c\) are constants, and \(x\) represents the unknown variable.
In this post, we’ll explore:
What the quadratic equation represents
How to solve it using the quadratic formula
How to implement this solution in Haskell
What Is a Quadratic Equation?
A quadratic equation is a second-degree polynomial equation. This means the highest exponent of the variable \(x\) is 2.
Quadratic equations typically describe parabolas when plotted on a graph.
The Quadratic Formula
The quadratic formula provides a method to find the values of \(x\) that satisfy the equation. The formula is:
\[x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}\]
Here, the expression \(b^2 - 4ac\) is called the discriminant, and it plays a key role in determining the nature
of the solutions:
If the discriminant is positive, the equation has two real and distinct roots.
If the discriminant is zero, the equation has one real (repeated) root.
If the discriminant is negative, the equation has two complex roots.
Step-by-Step Solution
Calculate the Discriminant:
The discriminant, \(\Delta\), is given by:
\(\Delta = b^2 - 4ac\)
Evaluate the Roots:
Using the discriminant, you can find the roots by plugging the values into the quadratic formula:
\(x_1 = \frac{-b + \sqrt{\Delta}}{2a}, \quad x_2 = \frac{-b - \sqrt{\Delta}}{2a}\)
If \(\Delta < 0\), the square root term involves imaginary numbers, leading to complex solutions.
Haskell Implementation
Now let’s translate this mathematical solution into a Haskell function. Haskell is a functional programming language
with a strong emphasis on immutability and mathematical precision, making it an excellent choice for implementing
mathematical algorithms.
Below, we’ll create a function quadraticSolver that:
Takes the coefficients \(a\), \(b\), and \(c\) as inputs.
Computes the discriminant.
Determines the nature of the roots based on the discriminant.
Returns the roots of the quadratic equation.
Code Breakdown:
Imports:
We import the Text.Printf module to format the output to two decimal places.
quadraticSolver Function:
This function takes three arguments: \(a\), \(b\), and \(c\).
It computes the discriminant using the formula \(\Delta = b^2 - 4ac\).
It checks the value of the discriminant using Haskell’s guards (|), and based on its value, it computes the roots.
If the discriminant is negative, we compute the real and imaginary parts separately and display the complex roots in the form \(x = a + bi\).
main Function:
The main function prompts the user to input the coefficients \(a\), \(b\), and \(c\).
It then calls quadraticSolver to compute and display the roots.
Example Run
Let’s assume we are solving the equation \(x^2 - 3x + 2 = 0\), where \(a = 1\), \(b = -3\), and \(c = 2\).
If we try solving the equation \(x^2 + 2x + 5 = 0\), where \(a = 1\), \(b = 2\), and \(c = 5\).
Conclusion
The quadratic equation is a simple but powerful mathematical tool. In this post, we derived the quadratic formula,
discussed how the discriminant affects the solutions, and implemented it in Haskell. The solution handles both real and
complex roots elegantly, thanks to Haskell’s functional paradigm.
In this post, we’ll explore the foundations of 3D graphics, focusing on vector math, matrices, and transformations. By
the end, you’ll understand how objects are transformed in 3D space and projected onto the screen. We’ll use Haskell for
the code examples, as it closely resembles the mathematical operations involved.
Vectors
A 4D vector has four components: \(x\), \(y\), \(z\), and \(w\).
In 3D graphics, we often work with 4D vectors (also called homogeneous coordinates) rather than 3D vectors. The
extra dimension allows us to represent translations (which are not linear transformations) as matrix operations,
keeping the math uniform.
A 4D vector is written as:
\[\boldsymbol{v} = \begin{bmatrix} x \\ y \\ z \\ w \end{bmatrix}\]
Where:
\(x, y, z\) represent the position in 3D space
\(w\) is a homogeneous coordinate that allows us to apply translations and perspective transformations.
The extra \(w\)-component is crucial for distinguishing between points and directions (i.e., vectors). When
\(w = 1\), the vector represents a point. When \(w = 0\), it represents a direction or vector.
Operations
We need to perform various operations on vectors in 3D space (or 4D homogeneous space), including addition, subtraction,
multiplication, dot products, and normalization.
Addition
Given two vectors \(\boldsymbol{a}\) and \(\boldsymbol{b}\):
The cross product is a vector operation that takes two 3D vectors and returns a third vector that is orthogonal
(perpendicular) to both of the input vectors. The cross product is commonly used in 3D graphics to calculate surface
normals, among other things.
For two 3D vectors \(\boldsymbol{a}\) and \(\boldsymbol{b}\), the cross product \(\boldsymbol{a} \times \boldsymbol{b}\)
is defined as:
This resulting vector is perpendicular to both \(\boldsymbol{a}\) and \(\boldsymbol{b}\).
To implement the cross product in Haskell, we will only operate on the \(x\), \(y\), and \(z\) components of a
Vec4 (ignoring \(w\)) since the cross product is defined for 3D vectors.
Length
The length or magnitude of a vector \(\boldsymbol{v}\) is:
In 3D graphics, transformations are applied to objects using 4x4 matrices. These matrices allow us to perform
operations like translation, scaling, and rotation.
Operations
Addition
Adding two matrices \(A\) and \(B\) is done element-wise:
We transform a vector by a matrix using a multiply operation.
\[\boldsymbol{v'} = M \cdot \boldsymbol{v}\]
3D Transformations
In 3D graphics, we apply transformations like translation, scaling, and rotation using matrices. These transformations
are applied to 4D vectors, and the operations are represented as matrix multiplications.
Identity Matrix
The identity matrix is a 4x4 matrix that leaves a vector unchanged when multiplied:
In 3D graphics, we frequently need to rotate objects around the X, Y, and Z axes. Each axis has its own
corresponding rotation matrix, which we use to apply the rotation transformation to points in 3D space.
A rotation around the X-axis by an angle \(\theta\) is represented by the following matrix:
To rotate an object in 3D space about multiple axes, we can multiply the individual rotation matrices. The order of
multiplication is crucial since matrix multiplication is not commutative. Typically, we perform rotations in the
order of Z, then Y, then X (if required).
Let’s implement the rotation matrices for the X, Y, and Z axes in Haskell:
Example: Rotating an Object
To apply a rotation to an object, you can combine the rotation matrices and multiply them by the object’s position
vector. For instance, to rotate a point by \(\theta_x\), \(\theta_y\), and \(\theta_z\), you can multiply
the corresponding matrices:
3D Transformations and Projection
Local vs World Coordinates
When dealing with 3D objects, we distinguish between local coordinates (relative to an object) and
world coordinates (relative to the entire scene). Vectors are transformed from local to world coordinates by
multiplying them by transformation matrices.
Projection Calculation
To project a 3D point onto a 2D screen, we use a projection matrix. The projection matrix transforms 3D coordinates
into 2D coordinates by applying a perspective transformation.
A simple perspective projection matrix looks like this:
\(\text{aspect}\) is the aspect ratio of the screen
Reducing a 4D Vector to 2D Screen Coordinates
In 3D graphics, we often work with 4D vectors in homogeneous coordinates. To display a 3D point on a 2D screen, we need
to project that point using a projection matrix and then convert the resulting 4D vector into 2D coordinates that we
can draw on the screen.
Here’s how this process works:
Step 1: Apply the Projection Matrix
We start with a 4D vector \(\boldsymbol{v}\) in homogeneous coordinates:
\[\boldsymbol{v} = \begin{bmatrix} x \\ y \\ z \\ w \end{bmatrix}\]
We apply the projection matrix \(P\), which transforms the 4D point into clip space (a space where coordinates can be
projected to the screen).
The projection matrix looks something like this for perspective projection:
\[\boldsymbol{v'} = \begin{bmatrix} x' \\ y' \\ z' \\ w' \end{bmatrix} = P \cdot \begin{bmatrix} x \\ y \\ z \\ w \end{bmatrix}\]
Step 2: Perspective Divide
To convert the 4D vector \(\boldsymbol{v'}\) to 3D space, we perform the perspective divide. This means dividing
the \(x'\), \(y'\), and \(z'\) components by the \(w'\) component.
The resulting 3D point \(\boldsymbol{v_{3D}}\) is:
To get the final 2D screen coordinates, we need to convert the 3D point into normalized device coordinates (NDC),
which range from -1 to 1. The screen coordinates ( (x_{\text{screen}}, y_{\text{screen}}) ) are then obtained by
scaling these values to the screen dimensions:
The factor \(\frac{x_{3D} + 1}{2}\) maps the normalized \(x\)-coordinate from the range [-1, 1] to [0, 1], and
multiplying by the screen width gives us the pixel position. The same applies for \(y_{\text{screen}}\), but we invert
the \(y_{3D}\) coordinate to account for the fact that screen coordinates typically have the origin at the top-left
corner, whereas the NDC system has the origin at the center.
Putting it All Together in Haskell
Here’s how you can perform this transformation in Haskell:
Example
Suppose we have the following vector and projection matrix:
This will give you the screen coordinates \(x_{\text{screen}}\) and \(y_{\text{screen}}\), where the 3D point
\((1, 1, 1)\) will be projected on a 1920x1080 display.
Conclusion
This has been some of the basic 3D concepts presented through Haskell. In future posts, we’ll use this code to create
some basic animations on screen.
Mounting CIFS (SMB) shares in Linux can be a convenient way to access network resources as part of the local filesystem.
In this guide, I’ll walk you through the steps for properly configuring a CIFS share in /etc/fstab on a Linux system.
I’ll also show you how to ensure that network mounts are available before services like Docker start up.
Step 1: Modify /etc/fstab
To mount a CIFS share automatically at boot, we need to modify the /etc/fstab file. First, open it in a text editor:
Now, add or modify the CIFS entry in the file. A typical CIFS entry looks like this:
Explanation:
//server_address/share_name: The remote server and share you want to mount (e.g., //192.168.1.100/shared).
/local/mount/point: The local directory where the share will be mounted.
cifs: The filesystem type for CIFS/SMB.
credentials=/path/to/credentials: Points to a file containing your username and password (this is optional, but recommended for security).
file_mode=0755,dir_mode=0755: Sets the file and directory permissions for the mounted share.
uid=1000,gid=1000: Specifies the user and group IDs that should own the files (replace 1000 with your user/group IDs).
_netdev: Ensures that the mount waits for network availability before mounting.
0 0: The last two values are for dump and fsck; they can usually remain 0.
Step 2: Create a Credentials File
For better security, you can use a separate credentials file rather than hard-coding the username and password in /etc/fstab. To do this, create a file to store the username and password for the share:
Add the following lines to the file:
Make sure the credentials file is secure by setting appropriate permissions:
This ensures only the root user can read the file, which helps protect sensitive information.
Step 3: Test the Mount
After adding the CIFS line to /etc/fstab and configuring the credentials file, it’s time to test the mount. You can do this by running:
If everything is configured correctly, the CIFS share should mount automatically. If you encounter any issues, check the system logs for errors.
Use one of these commands to inspect the logs:
Ensuring Mounts are Available Before Docker
If you’re running Docker on the same system and need to ensure that your CIFS mounts are available before Docker starts, you’ll want to modify
Docker’s systemd service. Here’s how:
First, create a directory for Docker service overrides:
Next, create a custom override file:
Add the following content:
This configuration ensures Docker waits until all remote filesystems (like CIFS) are mounted before starting.
Finally, reload the systemd configuration and restart Docker:
Now, Docker will wait for your CIFS mounts to be available before starting any containers that might rely on them.
By following these steps, you can ensure your CIFS shares are mounted reliably on boot and integrated seamlessly with other services like Docker.
This is especially useful for network-based resources that are critical to your containers or other local services.