Skip to article content

Neural Networks

Back to Article
Practice problems for Midterm 1 ECE 490/590 Spring 2025
Download Notebook

Practice problems for Midterm 1 ECE 490/590 Spring 2025

Date: Oct 9, 2025

Instructor: Vikas Dhiman (vikas.dhiman@maine.edu)

  1. Total marks are 75.

  2. Total time allowed is 75 min.

  3. One page 8"x11" cheatsheet is allowed.

  4. Calculators are allowed.

  5. Computers are not allowed. You must know approximately know what the Python code will output. Minor formatting errors will not be penalized.

1. Write your name here:

2. Write your email here:

Python Basics

The midterm will be on paper, no computers will be allowed. Make sure you know what the python code output should be.

Python questions will be restriced to content covered in Python_1.ipynb, Python_2.ipynb, Python_3.ipynb

Python Basics

The midterm will be on paper, no computers will be allowed. Make sure you know what the python code output should be.

Python questions will be restriced to content covered in Python_1.ipynb, Python_2.ipynb, NumpyTutorial.ipynb

Q1. What will the following code print?

hello = "'Hello'"
name = '"ECE"'
pi = 3.1419
print(f'{hello:s} {name}. pi is {pi:.03f}')  # string formatting
'Hello' "ECE". pi is 3.142

Q2. What will the following code print?

xs = [1, 2, 3, 'hello', [4, 5, 6]]    # Create a list
print(xs[-1])
[4, 5, 6]

Q3. What will the following code print?

nums = list(range(5))    # range is a built-in function that creates a list of integers
print(nums[-2:])
[3, 4]

Q4. Which code is faster for very large lists and dictionaries ? Option 1 or Option 2? Why?

# Code Option 1:
d = {'cat': 'cute', 'dog': 'furry'}  # Create a new dictionary with some data
print(d['dog'])
# Code option 2:
keys = ['cat', 'dog'] # Create the dictionary with keys as lists
values = ['cute', 'furry'] # # Create the dictionary with values as lists
print(values[keys.index('dog')])
furry
furry

Q5. Which code is faster for very large lists and dictionaries ? Option 1 or Option 2? Why?

# Code Option 1:
d = {0: 'cute', 1: 'furry'}  # Create a new dictionary with some data
print(d[1])
# Code option 2:
values = ['cute', 'furry'] # # Create the dictionary with values as lists
print(values[1])
furry
furry

Q6. What is the output of the following code?

class Value:
    def __init__(self, v):
        self.v = v

    def __add__(self, other):
        return self.v * other

print(Value(3) + 2)
6

Numpy basics

Python questions will be restriced to content covered in NumpyTutorial.ipynb

Q7: What is the output of the following code?

import numpy as np
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6]])
np.concatenate((x.T, y.T), axis=-1)
array([[1, 3, 5], [2, 4, 6]])

Q8. What is the output of the following code?

x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6]])
x @ y.T
array([[17], [39]])

Q9. What is the output of the following code?

x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6]])
(x * y).sum(axis=-1)
array([17, 39])

Linear algebra and it’s geometry

Q10.

Show that for any vector a=[a1,a2,,an]\bfa = [a_1, a_2, \dots, a_n], it’s magnitude squared is same as dot product with itself i.e. a2=aa\|\bfa\|^2 = \bfa^\top \bfa

A10. The mangitude of n-D vector is given by a=a12+a22++an2\|\bfa\| = \sqrt{a_1^2 + a_2^2 + \dots + a_n^2} and dot product the vector with itself is given by aa=a1a1+a2a2++anan=a12+a22++an2\bfa^\top \bfa = a_1 a_1 + a_2 a_2 + \dots + a_n a_n = a_1^2 + a_2^2 + \dots + a_n^2. Squaring the magnitude gives us a2=a12+a22++an2\|\bfa\|^2 = a_1^2 + a_2^2 + \dots + a_n^2, which is same as aa\bfa^\top \bfa.

Q11. For given vectors v\bfv and u\bfu find the projection of v\bfv on u\bfu projuv\proj_\bfu \bfv. Also find the equation of dotted line which is perpendicular to u\bfu and passes through v\bfv. Convert the equation of line to the form y=mx+cy = m x + c.

v=[23]\bfv = \begin{bmatrix} 2 \\ 3 \end{bmatrix} and u=[32]\bfu = \begin{bmatrix} 3 \\ 2 \end{bmatrix}.

A11.

  1. projuv=vuu=1213\proj_\bfu \bfv = \bfv^\top \frac{\bfu}{\|\bfu\|} = \frac{12}{\sqrt{13}}

  2. The dotted line is the set of all points xR2\bfx \in \bbR^2 that satisfy ux=uv\bfu^\top \bfx = \bfu^\top \bfv

  3. Let x=[x,y]\bfx = [x, y]. Then the above equation of line can be written as [3,2][xy]=12[3, 2] \begin{bmatrix} x \\ y \end{bmatrix} = 12 or 3x+2y=123x + 2y = 12

Q12.

Convert the following scalar equation into vector form. Your end result should contain m=[m;c]\bfm = [m; c], y=[y1;y2;;yn]\bfy = [y_1; y_2; \dots; y_n] and x=[x1;x2,,xn]\bfx = [x_1; x_2, \dots, x_n]. You can define other vectors and matrices as needed, included a vector of ones like 1n\mathbb{1}_n.

e(m,c,(x1,y1),(x2,y2),,(xn,yn))=(y1(x1m+c))2+(y2(x2m+c))2++(yn(xnm+c))2e(m, c, (x_1, y_1), (x_2, y_2), \dots, (x_n, y_n)) = (y_1 - (x_1 m + c))^2 + (y_2 - (x_2 m + c))^2 + \dots + (y_n - (x_n m + c))^2

A12. Recall that the magnitude of a vector v=v12+v22++vnn \|\bfv\| = \sqrt{v_1^2 + v_2^2 + \dots + v_n^n} has a similar form to the error function. This suggests that we can define an error vector with the signed error for each data point as it’s elements

e=[y1(mx1+c)y2(mx2+c)yn(mxn+c)]\bfe = \begin{bmatrix}y_1 - (mx_1 + c)\\ y_2 - (mx_2 + c)\\ \vdots \\ y_n - (mx_n + c)\end{bmatrix}

The total error is same as minimizing the square of error vector magnitude which is further same as vector product with itself.

e(m,c,(x1,y1),(x2,y2),,(xn,yn))=e2=eee(m, c, (x_1, y_1), (x_2, y_2), \dots, (x_n, y_n)) = \|\bfe\|^2 = \bfe^\top \bfe

Let us define x=[x1;;xn]\bfx = [x_1; \dots; x_n] to denote the vector of all x coordinates of the dataset and y=[y1;;yn]\bfy = [y_1; \dots; y_n] to denote y coordinates. Then the error vector is:

e=y(xm+1nc)\bfe = \bfy - (\bfx m + \mathbf{1}_n c)

where 1n\mathbf{1}_n is a n-D vector of all ones. Finally, we vectorize parameters of the line m=[m;c]\bfm = [m; c]. We will also need to horizontally concatenate x\bfx and 1n\mathbf{1}_n. Let’s call the result X=[x,1n]Rn×2\bfX = [\bfx, \mathbf{1}_n] \in \bbR^{n \times 2}. Now, the error vector looks like this:

e=yXm\bfe = \bfy - \bfX \bfm

Expanding the error magnitude:

e2=(yXm)(yXm)=yy+mXXm2yXm\|\bfe\|^2 = (\bfy - \bfX \bfm)^\top (\bfy - \bfX \bfm) \\ = \bfy^\top\bfy + \bfm^\top \bfX^\top \bfX \bfm - 2\bfy^\top \bfX \bfm

Q13:

Convert the following scalar equation into vector form. Your end result should contain m=[a;b;c]\bfm = [a; b; c], z=[z1;z2;;zn]\bfz = [z_1; z_2; \dots; z_n], y=[y1;y2;;yn]\bfy = [y_1; y_2; \dots; y_n] and x=[x1;x2,,xn]\bfx = [x_1; x_2, \dots, x_n]. You can define other vectors and matrices as needed, included a vector of all ones like 1n\mathbb{1}_n.

e(a,b,c,(x1,y1,z1),(x2,y2,z2),,(xn,yn,zn))=(z1(x1a+y1b+c))2+(z2(x2a+y2b+c))2++(zn(xna+ynb+c))2e(a, b, c, (x_1, y_1, z_1), (x_2, y_2, z_2), \dots, (x_n, y_n, z_n)) = (z_1 - (x_1 a + y_1 b + c))^2 + (z_2 - (x_2 a + y_2 b + c))^2 + \dots + (z_n - (x_n a + y_n b + c))^2

A13: A variation of A12

Q14

Convert the following vector equation into even more vectorized form.

e(m0,m,(x1,y1),(x2,y2),,(xn,yn))=(y1(x1m+m0))2+(y2(x2m+m0))2++(yn(xnm+m0))2e(m_0, \bfm, (\bfx_1, y_1), (\bfx_2, y_2), \dots, (\bfx_n, y_n)) = (y_1 - (\bfx_1^\top \bfm + m_0))^2 + (y_2 - (\bfx_2^\top \bfm + m_0))^2 + \dots + (y_n - (\bfx_n^\top \bfm + m_0))^2

where m=[m1;m2;;mp]Rp\bfm = [m_1; m_2; \dots; m_p] \in \bbR^p is a p-dimensional vector and xi=[xi1;xi2;;xip]Rp\bfx_i = [x_{i1}; x_{i2}; \dots; x_{ip}] \in \bbR^p are p-dimensional vectors for all i={1,2,n}i = \{1, 2, \dots n\}

Your end result should contain q=[m0,m1,m2,,mp]Rp+1\bfq = [m_0, m_1, m_2, \dots, m_p] \in \bbR^{p+1}, y=[y1;y2;;yn]Rn\bfy = [y_1; y_2; \dots; y_n]\in \bbR^n and

X=[x11x12x1px21x22x2pxn1xn2xnp]=[x1x2xn]Rn×p\begin{align} \bfX = \begin{bmatrix}x_{11} & x_{12} & \dots & x_{1p}\\ x_{21} & x_{22} & \dots & x_{2p} \\ \vdots & \vdots & \ddots & \vdots\\ x_{n1} & x_{n2} & \dots & x_{np} \end{bmatrix} = \begin{bmatrix} \bfx_1^\top \\ \bfx_2^\top \\ \vdots\\ \bfx_n^\top \end{bmatrix} \in \bbR^{n \times p} \end{align}

You can define other vectors and matrices as needed, included a vector of all ones like 1n\mathbb{1}_n.

A15.

Recall that the magnitude of a vector v=v12+v22++vnn \|\bfv\| = \sqrt{v_1^2 + v_2^2 + \dots + v_n^n} has a similar form to the error function. This suggests that we can define an error vector with the signed error for each data point as it’s elements

e=[y1(x1m+m0)y2(x2m+m0)yn(x2m2+m0)]\bfe = \begin{bmatrix}y_1 - (\bfx_1^\top\bfm + m_0)\\ y_2 - (\bfx_2^\top\bfm + m_0)\\ \vdots \\ y_n - (\bfx_2^\top\bfm_2 + m_0)\end{bmatrix}

The total error is same as minimizing the square of error vector magnitude which is further same as vector product with itself.

e(m0,m,(x1,y1),(x2,y2),,(xn,yn))=e2=eee(m_0, \bfm, (\bfx_1, y_1), (\bfx_2, y_2), \dots, (\bfx_n, y_n)) = \|\bfe\|^2 = \bfe^\top \bfe

Let us define X=[x1;;xn]\bfX = [\bfx_1^\top; \dots; \bfx_n^\top] to denote the vector of all x coordinates of the dataset and y=[y1;;yn]\bfy = [y_1; \dots; y_n] to denote y coordinates. Then the error vector is:

e=y(1nm0+Xm)\bfe = \bfy - (\mathbf{1}_n m_0 + \bfX \bfm)

where 1n\mathbf{1}_n is a n-D vector of all ones. Finally, we call parameters of the line q=[m0;m]\bfq = [m_0; \bfm]. We will also need to horizontally concatenate X\bfX and 1n\mathbf{1}_n. Let’s call the result Xˉ=[1n,X]Rn×(p+1)\bar{\bfX} = [\mathbf{1}_n, \bfX] \in \bbR^{n \times (p+1)}. Now, the error vector looks like this:

e=yXˉq\bfe = \bfy - \bar{\bfX} \bfq

Expanding the error magnitude:

e2=(yXˉq)(yXˉq)=yy+qXˉXˉq2yXˉq\|\bfe\|^2 = (\bfy - \bbfX \bfq)^\top (\bfy - \bbfX \bfq) \\ = \bfy^\top\bfy + \bfq^\top \bbfX^\top \bbfX \bfq - 2\bfy^\top \bbfX \bfq

Q16:

Convert the following scalar equation into vector form. Your end result should contain m=[m;c]\bfm = [m; c], the matrix W=Diag([w1;w2;;wn])\bfW = \Diag([w_1; w_2; \dots; w_n]), y=[y1;y2;;yn]\bfy = [y_1; y_2; \dots; y_n] and x=[x1;x2,,xn]\bfx = [x_1; x_2, \dots, x_n]. You can define other vectors and matrices as needed, included a vector of all ones like 1n\mathbb{1}_n.

e(m,c,(x1,y1,w1),(x2,y2,w2),,(xn,yn,wn))=w12(y1(x1m+c))2+w22(y2(x2m+c))2++wn2(yn(xnm+c))2e(m, c, (x_1, y_1, w_1), (x_2, y_2, w_2), \dots, (x_n, y_n, w_n)) = w_1^2(y_1 - (x_1 m + c))^2 + w_2^2(y_2 - (x_2 m + c))^2 + \dots + w_n^2(y_n - (x_n m + c))^2

The matrix W\bfW is defined as Diag([w1;w2;;wn])\Diag([w_1; w_2; \dots; w_n]) which indicates that W\bfW is diagonal matrix of [w1;w2;;wn][w_1; w_2; \dots; w_n].

W=Diag([w1;w2;;wn])=[w1000w2000wn]\bfW = \Diag([w_1; w_2; \dots; w_n]) = \begin{bmatrix}w_1 & 0 & \dots & 0\\ 0 & w_2 & \dots & 0\\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \dots & w_n\end{bmatrix}

A16:

Recall that the magnitude of a vector v=v12+v22++vnn \|\bfv\| = \sqrt{v_1^2 + v_2^2 + \dots + v_n^n} has a similar form to the error function. This suggests that we can define an error vector with the signed error for each data point as it’s elements

e=[y1(mx1+c)y2(mx2+c)yn(mxn+c)]\bfe = \begin{bmatrix}y_1 - (mx_1 + c)\\ y_2 - (mx_2 + c)\\ \vdots \\ y_n - (mx_n + c)\end{bmatrix}

and let W=Diag([w1;w2;;wn])\bfW = \Diag([w_1; w_2; \dots; w_n]).

Note that

We=[w1(y1(mx1+c))w2(y2(mx2+c))w3(yn(mxn+c))]\bfW \bfe = \begin{bmatrix}w_1(y_1 - (mx_1 + c))\\ w_2(y_2 - (mx_2 + c))\\ \vdots \\ w_3(y_n - (mx_n + c))\end{bmatrix}

The total error is same as the square of error vector magnitude

e(m,c,(x1,y1,w1),(x2,y2,w2),,(xn,yn,wn))=w12(y1(x1m+c))2+w22(y2(x2m+c))2++wn2(yn(xnm+c))2=We2e(m, c, (x_1, y_1, w_1), (x_2, y_2, w_2), \dots, (x_n, y_n, w_n)) = w_1^2(y_1 - (x_1 m + c))^2 + w_2^2(y_2 - (x_2 m + c))^2 + \dots + w_n^2(y_n - (x_n m + c))^2 = \|\bfW \bfe\|^2

The square of error vector magnitude is same as dot product with itself,

We2=(We)(We)=eWWe\|\bfW \bfe\|^2 = (\bfW\bfe)^\top(\bfW\bfe) = \bfe^\top \bfW^\top \bfW \bfe

Let us define x=[x1;;xn]\bfx = [x_1; \dots; x_n] to denote the vector of all x coordinates of the dataset and y=[y1;;yn]\bfy = [y_1; \dots; y_n] to denote y coordinates. Then the error vector is:

e=y(xm+1nc)\bfe = \bfy - (\bfx m + \mathbf{1}_n c)

where 1n\mathbf{1}_n is a n-D vector of all ones. Finally, we vectorize parameters of the line m=[m;c]\bfm = [m; c]. We will also need to horizontally concatenate x\bfx and 1n\mathbf{1}_n. Let’s call the result X=[x,1n]Rn×2\bfX = [\bfx, \mathbf{1}_n] \in \bbR^{n \times 2}. Now, the error vector looks like this:

e=yXm\bfe = \bfy - \bfX \bfm

Expanding the error magnitude:

We2=(yXm)WW(yXm)=yWWy+mXWWXm2yWWXm\|\bfW\bfe\|^2 = (\bfy - \bfX \bfm)^\top \bfW^\top\bfW (\bfy - \bfX \bfm) \\ = \bfy^\top\bfW^\top\bfW\bfy + \bfm^\top \bfX^\top \bfW^\top\bfW\bfX \bfm - 2\bfy^\top \bfW^\top\bfW\bfX \bfm

Q17:

Using vector derivatives find the minimum of the following vector quadratic function in m\bfm:

arg minme(m)=yWWy+mXWWXm2yWWXm\arg~\min_{\bfm} e(\bfm) = \bfy^\top\bfW^\top\bfW\bfy + \bfm^\top \bfX^\top \bfW^\top\bfW\bfX \bfm - 2\bfy^\top \bfW^\top\bfW\bfX \bfm

The dimensions of the each of the variables are given mRp\bfm \in \bbR^p, yRn\bfy \in \bbR^n, WRn×n\bfW \in \bbR^{n \times n}, XRn×p\bfX \in \bbR^{n \times p}.

A17:

0=m(yWWy+mXWWXm2yWWXm)=2mXWWX2yWWX\begin{align} \mathbf{0}^\top &= \frac{\p }{\p \bfm} (\bfy^\top\bfW^\top\bfW\bfy + \bfm^\top \bfX^\top \bfW^\top\bfW\bfX \bfm - 2\bfy^\top \bfW^\top\bfW\bfX \bfm)\\ &= 2 {\bfm^*}^\top \bfX^\top \bfW^\top \bfW \bfX - 2\bfy^\top \bfW^\top \bfW \bfX \end{align}

This gives us the solution

m=(XWWX)1XWWy\bfm^* = (\bfX^\top \bfW^\top \bfW \bfX)^{-1} \bfX^\top \bfW^\top \bfW \bfy

Vector derivatives

Q18:

Find the derivative of f(x)=(xa1)A(xa2)f(\bfx) = (\bfx - \bfa_1)^\top A (\bfx - \bfa_2) with respecto to x\bfx.

You can assume ARn×nA \in \bbR^{n\times n} to be symmetric. The size of vectors are x,a1,a2,a3,bRn\bfx, \bfa_1, \bfa_2, \bfa_3, \bfb \in \bbR^n

A18

f(x)=(xa1)A(xa2)=xAxa1AxxAa2+a1Aa2f(\bfx) = (\bfx - \bfa_1)^\top A (\bfx - \bfa_2)\\ = \bfx^\top A \bfx - \bfa_1^\top A \bfx - \bfx^\top A \bfa_2 + \bfa_1^\top A\bfa_2\\

Note that xAa2\bfx^\top A \bfa_2 is a scalar. That’s why we can replace it with its transpose xAa2=a2Ax\bfx^\top A \bfa_2 = \bfa_2^\top A \bfx

=xAx(a1+a2)Ax+a1Aa2= \bfx^\top A \bfx - (\bfa_1 + \bfa_2)^\top A \bfx + \bfa_1^\top A\bfa_2
fx=2xA(a1+a2)A=(2x(a1+a2))A\frac{\partial f}{\partial \bfx} = 2\bfx^\top A - (\bfa_1 + \bfa_2)^\top A\\ = (2\bfx - (\bfa_1 + \bfa_2))^\top A

Q19:

Find the quadratic approximation of the following function near the point x0\bfx_0:

f(x)=((xa1)A(xa2))((xa3)b)f(\bfx) = \left((\bfx - \bfa_1)^\top A (\bfx - \bfa_2)\right) \left((\bfx - \bfa_3)^\top \bfb\right)

You can assume ARn×nA \in \bbR^{n\times n} to be symmetric. The size of vectors are x,a1,a2,a3,bRn\bfx, \bfa_1, \bfa_2, \bfa_3, \bfb \in \bbR^n

A19:

[xf(x)]=((xa1)A(xa2))b+((xa3)b)((2x(a1+a2))A)[\nabla_\bfx f(\bfx)]^\top = \left((\bfx - \bfa_1)^\top A (\bfx - \bfa_2)\right) \bfb^\top+ ((\bfx - \bfa_3)^\top \bfb)\left((2\bfx - (\bfa_1 + \bfa_2))^\top A\right)
xf(x)=((xa1)A(xa2))b+((xa3)b)(A(2x(a1+a2)))\nabla_\bfx f(\bfx) = \left((\bfx - \bfa_1)^\top A (\bfx - \bfa_2)\right) \bfb + ((\bfx - \bfa_3)^\top \bfb)\left(A (2\bfx - (\bfa_1 + \bfa_2))\right)
xf(x0)=((x0a1)A(x0a2))b+((x0a3)b)(A(2x0(a1+a2)))\nabla_\bfx f(\bfx_0) = \left((\bfx_0 - \bfa_1)^\top A (\bfx_0 - \bfa_2)\right) \bfb + ((\bfx_0 - \bfa_3)^\top \bfb)\left(A (2\bfx_0 - (\bfa_1 + \bfa_2))\right)
Hf(x)=x2f(x)=b(2x(a1+a2))A+(A(2x(a1+a2)))b+((xa3)b)(2A)H f(\bfx) = \nabla^2_\bfx f(\bfx) = \bfb (2\bfx - (\bfa_1 + \bfa_2))^\top A + \left(A (2\bfx - (\bfa_1 + \bfa_2))\right)\bfb^\top + \left((\bfx - \bfa_3)^\top \bfb\right)(2A)
Hf(x0)=x2f(x0)=b(2x0(a1+a2))A+(A(2x0(a1+a2)))b+((x0a3)b)(2A)H f(\bfx_0) = \nabla^2_\bfx f(\bfx_0) = \bfb (2\bfx_0 - (\bfa_1 + \bfa_2))^\top A + \left(A (2\bfx_0 - (\bfa_1 + \bfa_2))\right)\bfb^\top + \left((\bfx_0 - \bfa_3)^\top \bfb\right)(2A)

The quadratic approximation by Taylor series is:

f(x)=f(x0)+[xf(x0)](xx0)+(xx0)Hf(x0)(xx0)f(\bfx) = f(\bfx_0) + [\nabla_\bfx f(\bfx_0)]^\top (\bfx - \bfx_0) + (\bfx - \bfx_0)^\top H f(\bfx_0) (\bfx - \bfx_0)

Q20

Show that for c,xRn\bfc, \bfx \in \bbR^n

xcx=c\begin{align} \frac{\p }{ \p \bfx} \bfc^\top \bfx = \bfc^\top\\ \end{align}

A20: Let c=[c1,c2,,cn]\bfc = [c_1, c_2, \dots, c_n] and x=[x1,x2,xn]\bfx = [x_1, x_2, \dots x_n]

Let f(x)=cx=c1x1+c2x2+cnxnf(\bfx) = \bfc^\top \bfx = c_1 x_1 + c_2 x_2 + \dots c_n x_n

fx1=c1fx2=c2fxn=cn\frac{\p f}{\p x_1} = c_1\\ \frac{\p f}{\p x_2} = c_2\\ \vdots\\ \frac{\p f}{\p x_n} = c_n\\

By Jacobian convention, we arrange the partial derivatives in a row vector:

xcx=[fx1fx2fxn]=[c1c2cn]=c\begin{align} \frac{\p }{ \p \bfx} \bfc^\top \bfx = \begin{bmatrix} \frac{\p f}{\p x_1} & \frac{\p f}{\p x_2} & \dots & \frac{\p f}{\p x_n}\end{bmatrix} \\ = \begin{bmatrix} c_1 & c_2 & \dots & c_n\end{bmatrix} = \bfc^\top \\ \end{align}

Q21:

Show that for ARn×n\bfA \in \bbR^{n \times n}, xRn\bfx \in \bbR^n

xAx=A\begin{align} \frac{\p }{ \p \bfx} \bfA \bfx = \bfA\\ \end{align}

A21: Let x=[x1;x2;xn]\bfx = [x_1; x_2; \dots x_n]

Let A=[a11a12a1na21a22a2nan1an2ann]=[a1a2an]\bfA = \begin{bmatrix} a_{11} & a_{12} & \dots & a_{1n} \\ a_{21} & a_{22} & \dots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{n1} & a_{n2} & \dots & a_{nn} \end{bmatrix} = \begin{bmatrix} \bfa_1^\top \\ \bfa_2^\top \\ \vdots \\ \bfa_n^\top \end{bmatrix}, where aiR1×n\bfa_i^\top \in \bbR^{1 \times n} are the row vectors of matrix A\bfA.

Then

Ax=[a1a2an]x=[a1xa2xanx]\bfA \bfx = \begin{bmatrix} \bfa_1^\top \\ \bfa_2^\top \\ \vdots \\ \bfa_n^\top \end{bmatrix}\bfx = \begin{bmatrix} \bfa_1^\top \bfx \\ \bfa_2^\top \bfx \\ \vdots \\ \bfa_n^\top\bfx \end{bmatrix}

Let $$\bff(\bfx) =

[f1(x)f2(x)fn(x)]\begin{bmatrix} f_1(\bfx) \\ f_2(\bfx) \\ \vdots\\ f_n(\bfx) \end{bmatrix}

= \bfA \bfx = \begin{bmatrix} \bfa_1^\top \bfx \ \bfa_2^\top \bfx \ \vdots \ \bfa_n^\top\bfx \end{bmatrix}$$

By Jacobian convention we arrange the partial derivatives of each function component column-wise

f(x)x=[f1(x)xf2(x)xfn(x)x]=[a1xxa2xxa2xx]=[a1a2an]=A\frac{\p \bff(\bfx)}{\p \bfx} = \begin{bmatrix} \frac{\p f_1(\bfx)}{\p \bfx} \\ \frac{\p f_2(\bfx)}{\p \bfx} \\ \vdots \\ \frac{\p f_n(\bfx)}{\p \bfx} \end{bmatrix} = \begin{bmatrix} \frac{\p \bfa_1^\top \bfx}{\p \bfx} \\ \frac{\p \bfa_2^\top \bfx}{\p \bfx} \\ \vdots \\ \frac{\p \bfa_2^\top \bfx}{\p \bfx} \end{bmatrix} = \begin{bmatrix} \bfa_1^\top \\ \bfa_2^\top \\ \vdots \\ \bfa_n^\top \end{bmatrix} = \bfA

Q22:

Use vector-derivative chain rule:

f(g(x))x=fggx\frac{\p \bff(\bfg(\bfx)) }{\p \bfx} = \frac{\p \bff}{\p \bfg}\frac{\p \bfg}{\p \bfx}

,

for any function g:RnRm\bfg : \bbR^n \mapsto \bbR^m and f:RmRo\bff : \bbR^m \mapsto \bbR^o.

Show that for xRn\bfx \in \bbR^n amd ARn×n\bfA \in \bbR^{n \times n}

xxAx=x(A+A)\begin{align} \frac{\p }{ \p \bfx} \bfx^\top \bfA \bfx = \bfx^\top (\bfA^\top + \bfA)\\ \end{align}

A22:

For product of any two vectors

xxy=y\begin{align} \frac{\p }{\p \bfx} \bfx^\top \bfy = \bfy^\top \end{align}

If y\bfy is a function of x\bfx, then

xxy=y+(yxy)(yx)=y+x(yx)\begin{align} \frac{\p }{\p \bfx} \bfx^\top \bfy &= \bfy^\top + \left(\frac{\p }{\p \bfy} \bfx^\top \bfy \right) \left(\frac{\p \bfy }{\p \bfx} \right)\\ &= \bfy^\top + \bfx^\top \left(\frac{\p \bfy }{\p \bfx}\right) \end{align}

If y=Ax\bfy = \bfA \bfx, then

yx=xAx=A\frac{\p \bfy}{\p \bfx} = \frac{\p }{\p \bfx} \bfA \bfx = \bfA

and

xxAx=y+x(yx)=xA+xA=x(A+A)\frac{\p }{\p \bfx} \bfx^\top \bfA \bfx = \bfy^\top + \bfx^\top \left(\frac{\p \bfy }{\p \bfx}\right) = \bfx^\top \bfA^\top + \bfx^\top \bfA = \bfx^\top (\bfA^\top + \bfA)

Perceptron

Q23:

You are given 2D points and corresponding labels as a training dataset {(x1,y1,l1),(x2,y2,l2),,(xn,yn,ln)}\{ (x_1, y_1, l_1), (x_2, y_2, l_2), \dots, (x_n, y_n, l_n) \}, where xiRx_i \in \bbR, yiRy_i \in \bbR and the labels li{1,1}l_i \in \{-1, 1\}. Use the model l^i=sign(yi(mxi+c))\hat{l}_i = \sign(y_i - (m x_i + c)) to construct a loss (or error) function. Find the gradient of the loss function with respect to the vector m=[m;c]\bfm = [m; c].

A23

e(yi,xi;m,c)={0 if sign(yi(mxi+c))=liyi(mxi+c) if sign(yi(mxi+c))lie(y_i, x_i; m,c) = \begin{cases} 0 &\text{ if }\sign(y_i - (m x_i + c)) = l_i\\ |y_i - (m x_i + c)| &\text{ if } \sign(y_i - (m x_i + c)) \ne l_i \end{cases}
m=[mc]\bfm = \begin{bmatrix}m \\ c\end{bmatrix}
e(yi,xi;m)={0 if sign(yi[xi1]m)=liyi[xi1]m if sign(yi[xi1]m)lie(y_i, x_i;\bfm) = \begin{cases} 0 &\text{ if } \sign( y_i - \begin{bmatrix} x_i& 1\end{bmatrix}\bfm) = l_i\\ |y_i - \begin{bmatrix} x_i& 1\end{bmatrix}\bfm| &\text{ if } \sign( y_i - \begin{bmatrix} x_i& 1\end{bmatrix}\bfm) \ne l_i \end{cases}

If li{1,1}l_i \in \{-1, 1\}, then sign(yi[xi1]m)=li\sign( y_i - \begin{bmatrix} x_i& 1\end{bmatrix}\bfm) = l_i is same as saying li(yi[xi1]m)>0 l_i( y_i - \begin{bmatrix} x_i& 1\end{bmatrix}\bfm) > 0.

e(yi,xi;m)={0 if li(yi[xi1]m)>0li(yi[xi1]m) if li(yi[xi1]m)<0e(y_i, x_i;\bfm) = \begin{cases} 0 &\text{ if } l_i( y_i - \begin{bmatrix} x_i& 1\end{bmatrix}\bfm) > 0\\ |l_i( y_i - \begin{bmatrix} x_i& 1\end{bmatrix}\bfm)| &\text{ if } l_i( y_i - \begin{bmatrix} x_i& 1\end{bmatrix}\bfm) < 0 \end{cases}

Also when z<0z < 0, then z=z|z| = -z.

e(yi,xi;m)={0 if li(yi[xi1]m)>0li(yi[xi1]m) if li(yi[xi1]m)<0e(y_i, x_i;\bfm) = \begin{cases} 0 &\text{ if } l_i( y_i - \begin{bmatrix} x_i& 1\end{bmatrix}\bfm) > 0\\ -l_i( y_i - \begin{bmatrix} x_i& 1\end{bmatrix}\bfm) &\text{ if } l_i( y_i - \begin{bmatrix} x_i& 1\end{bmatrix}\bfm) < 0 \end{cases}
e(yi,xi;m)=max{0,li(yi[xi1]m)}e(y_i, x_i;\bfm) = \max\{0, - l_i (y_i - \begin{bmatrix} x_i& 1\end{bmatrix}\bfm)\}
me(yi,xi;m)=max{0,li([xi1])}\nabla_\bfm e(y_i, x_i;\bfm) = \max\{0, l_i(\begin{bmatrix} x_i& 1\end{bmatrix})\}

For the entire dataset, we have y=[y1;;yn]\bfy = [y_1; \dots; y_n] and x=[x1;;xn]\bfx = [x_1; \dots; x_n], =[l1;;ln]\ell = [l_1; \dots; l_n] the average error is:

e(x,y;m)=1n1nmax{0,(y[x1n]m)},e(\bfx, \bfy; \bfm) = \frac{1}{n}{\bfone_n^\top}\max\{0, - \ell \odot (\bfy - \begin{bmatrix}\bfx & \bfone_n\end{bmatrix}\bfm )\},

where \odot is the element-wise product. and 1n\bfone_n is a vector of ones.

and the average gradient is:

me(x,y;m)=1n1nmax{0,([x1n])}\nabla_\bfm^\top e(\bfx, \bfy; \bfm) = \frac{1}{n}{\bfone_n^\top}\max\{0, \ell \odot ( \begin{bmatrix}\bfx & \bfone_n\end{bmatrix} )\}

Q24

You are given p-D points xiRp\bfx_i \in \bbR^p and corresponding labels as a training dataset {(x1,l1),(x2,l2),,(xn,ln)}\{ (\bfx_1, l_1), (\bfx_2, l_2), \dots, (\bfx_n, l_n) \}, where xiRp\bfx_i \in \bbR^p, and the labels li{1,1}l_i \in \{-1, 1\}. Use the model l^i=sign(xim+m0))\hat{l}_i = \sign(\bfx_i^\top \bfm + m_0)) to construct a loss (or error) function. Find the gradient of the loss function with respect to the vector q=[m0;m]\bfq = [m_0; \bfm].

A24:

e(m0,m;xi)={0 if sign(xim+m0)=lixim+m0 if sign(xim+m0)lie(m_0, \bfm; \bfx_i) = \begin{cases} 0 &\text{ if }\sign(\bfx_i^\top \bfm + m_0) = l_i\\ |\bfx_i^\top \bfm + m_0| &\text{ if } \sign(\bfx_i^\top \bfm + m_0) \ne l_i \end{cases}
e(yi,xi;m,c)={0 if sign(xim+m0)=lixim+m0 if sign(xim+m0)lie(y_i, x_i; m, c) = \begin{cases} 0 &\text{ if } \sign(\bfx_i^\top \bfm + m_0) = l_i\\ |\bfx_i^\top \bfm + m_0| &\text{ if } \sign(\bfx_i^\top \bfm + m_0) \ne l_i \end{cases}
q=[m0m]\bfq = \begin{bmatrix}m_0 \\ \bfm\end{bmatrix}
e(m0,m;xi)={0 if [1xi]q=li[1xi]q if [1xi]qlie(m_0, \bfm;\bfx_i) = \begin{cases} 0 &\text{ if } \begin{bmatrix} 1 & \bfx_i^\top\end{bmatrix}\bfq = l_i\\ |\begin{bmatrix} 1 & \bfx_i^\top\end{bmatrix}\bfq| &\text{ if } \begin{bmatrix} 1 & \bfx_i^\top\end{bmatrix}\bfq \ne l_i \end{cases}

If li{1,1}l_i \in \{-1, 1\}, then we can write

e(m0,m;xi)=max{0,li([1xi]q)}e(m_0, \bfm;\bfx_i) = \max\{0, - l_i (\begin{bmatrix} 1 & \bfx_i^\top\end{bmatrix}\bfq)\}
me(m0,m;xi)=max{0,li([1xi])}\nabla_\bfm e(m_0, \bfm;\bfx_i) = \max\{0, - l_i (\begin{bmatrix} 1 & \bfx_i^\top\end{bmatrix})\}

For the entire dataset, we have X=[x1;;xn]\bfX = [\bfx_1^\top; \dots; \bfx_n^\top], l=[l1;;ln]\bfl = [l_1; \dots; l_n] the average error is:

e(m;X,l)=1n1nmax{0,l([1nX]q)}e(\bfm; \bfX, \bfl) = \frac{1}{n}{\bfone_n^\top}\max\{0, - \bfl \odot ( \begin{bmatrix}\bfone_n & \bfX \end{bmatrix}\bfq )\}

and the average gradient is:

me(m;X,l)=1n1nmax{0,l([1nX])}\nabla_\bfm^\top e(\bfm; \bfX, \bfl) = \frac{1}{n}{\bfone_n^\top}\max\{0, \bfl \odot ( \begin{bmatrix} \bfone_n & \bfX \end{bmatrix} )\}