Explain why the perceptron cannot solve the XOR problem.

ANN (Artificial Neural Networks) is also called as MLP (Multilayer Perceptron). Explain why the MLP is able to solve the XOR problem.

- (Choose correct answers) Jonathan has now switched to multilayer neural networks and notices that the training error is going down and converges to a local minimum. Then when he tests on the new data, the test error is abnormally high. What is probably going wrong and what do you recommend him to do? (3 choices)

a) The training data size is not large enough. Collect a larger training data and retrain it.

b) Play with learning rate and add regularization term to the objective function.

c) Use a different initialization and train the network several times. Use the average of predictions from all nets to predict test data.

d) Use the same training data but add two more hidden layers.

- True or false for the following questions. (Correct +1, Wrong -1)

a) Single perceptron can solve a lineary inseperable problem with a kernel function.

b) Gradient descent trains neural networks to the global optimum.

- (Choose all the correct answers) Jonathan is tyring to solve the XOR problem using a multilayer perceptron (MLP) with ReLU activation function. However, as he trains the MLP model, the results vary at every iteration. The results are correct in some iterations, and the results are wrong at the other iterations. What is probably going wrong and what do you recommend him to do?

a) The training data points are not large enough. Collect a larger training data points and re-train it.

b) The number of perceptron layers is too large. Remove the perceptron layers and re-train it.

c) The number of perceptron layers is too small. Add more the perceptron layers and re-train it.

d) Learning rate is too high. Reduce learning rate and re-train it.

- Explain the difference between sigmoid (or hyperbolic tangent), and rectified linear unit (ReLU) activation functions in gradient backpropagation.

To deal with nonlinearly distributed data set, we need to design an appropriate kernel to make (or map) the original data to be linearly seperable. However, in artificial neural networks, this step is not necessary. Discuss why. (Hint: see the following figure.)

Suppose a multi-layer perceptron which has an input layer with 10 neurons, a hidden layer with 50 neurons, and an output layer with 3 neurons. Non-linear activation function for every neurons are ReLU. Write your answer to the following questions.

Size of input $X$?

Size of weights and biases ($W_h, b_h$) for the hidden layer?

Size of weights and biases ($W_o, b_o$) for the output layer?

Size of output $Y$?

To train neural networks, backpropagation is used. Briefly explain what the backpropagation is. When you discuss it, use the keywords such as recursive, memorized, dynamic programming, chain rule, etc.

Build the ANN model which receives three binary-valued (i.e., $0$ or $1$) inputs $x_1,x_2,x_3$, and outputs $1$ if exactly two of the inputs are $1$, and outputs $0$ otherwise. All of the units use a hard threshold activation function:

$$z = \begin{cases} 1 \quad \text{if } z \geq 0\\ 0 \quad \text{if } z < 0 \end{cases} $$

Suggest one of possible weights and biases which correctly implement this function.

Denote by

$\mathbf{W}_{2 \times 3}$ and $\mathbf{V}_{1 \times 2}$ weight matrices connecting input and hidden layer, and hidden layer and output respectively.

$\mathbf{b}^{(1)}_{2 \times 1}$ and $\mathbf{b}^{(2)}_{1 \times 1}$ biases matrices at hidden layer and output, respectively.

$x_{3 \times 1}$ and $h_{2 \times 1}$ node values at input and hidden layer, repectively.

In this problem, we are going to compute the gradient using the chain rule and dynamic programming, and update the weights $\omega \rightarrow \omega^+$. After that, the weights are updated through 1 back-propagation and compared with the error before the update.

**Neural Network Model**

- The artificial neural network structure: an input layer, a hidden layern, and an output layer.
- All neurons ($h_1$, $h_2$, $\sigma_1$ and $\sigma_2$) in the hidden and output layers use the sigmoid function as activation function.
- The red number means the initial weight values, the blue number means the input values, and the ground truth means the actual values.
- The loss function is the mean square error (MSE). Use
`1/2 MSE`

for calculation convenience. For example, $E = \frac{1}{2}\sum(\text{target} - \text{output})^2$ - Learning rate is set to 0.9.

- [hand written] Write and calculate $z_1$, $z_2$, $h_1$, $h_2$, $z_3$, $z_4$, $\sigma_1$, $\sigma_2$, and $E_{\text{total}}$ of forward propagation.

- [hand written] update $\omega_5$, $\omega_6$, $\omega_7$, $\omega_8$ $\rightarrow$ $\omega_5^+$, $\omega_6^+$, $\omega_7^+$, $\omega_8^+$ of back-propagation.

- [hand written] update $\omega_1$, $\omega_2$, $\omega_3$, $\omega_4$ $\rightarrow$ $\omega_1^+$, $\omega_2^+$, $\omega_3^+$, $\omega_4^+$ of back-propagation.

- [hand written] Write and calculate $E_{\text{total}}$ with the updated weights, and compare it to the previous error.

- Classify the given four points into two classes in 2D plane using a single layer structure as shown below. Plot a linear boundary even if it fails to classify them.

Note that bias units are not indicated here.

In [ ]:

```
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x_data = np.array([[0, 0], [1, 1], [0, 1], [1, 0]], dtype=np.float32)
y_data = np.array([[0], [0], [1], [1]], dtype=np.float32)
plt.figure(figsize = (8,6))
plt.scatter(x_data[:2,0], x_data[:2,1], marker='+', s=100, label='A')
plt.scatter(x_data[2:,0], x_data[2:,1], marker='x', s=100, label='B')
plt.axis('equal')
plt.ylim([-0.5, 1.5]);
plt.grid(alpha=0.15);
plt.legend();
plt.show()
```

In [ ]:

```
## write your code here
#
```

- Classify the given four points in 2D plane using two layers as shown below (the number of neurons in the out layer can be changed to one).

Note that bias units are not indicated here and you can use either one-hot-encoding or `sparse_categorical_crossentropy`

.

In [ ]:

```
## write your code here
#
```

- The first layer can be seen as kernel function $\phi$. Show the location of four points on 2D plane after the first layer.

In [ ]:

```
## write your code here
#
```

- Visualize the kernel space onto 2D plane.

Hint: Make 2d grid points and apply the kernel.

In [ ]:

```
## write your code here
#
```

- Plot the decision boundary on kernel space.

In [ ]:

```
## write your code here
#
```

You will do binary classification for nonlinearly seperable data using MLP. Plot the given data first.

In [ ]:

```
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
N = 200
M = 2*N
gamma = 0.01
G0 = np.random.multivariate_normal([0, 0], gamma*np.eye(2), N)
G1 = np.random.multivariate_normal([1, 1], gamma*np.eye(2), N)
G2 = np.random.multivariate_normal([0, 1], gamma*np.eye(2), N)
G3 = np.random.multivariate_normal([1, 0], gamma*np.eye(2), N)
train_X = np.vstack([G0, G1, G2, G3])
train_y = np.vstack([np.ones([M,1]), np.zeros([M,1])])
train_X = np.asmatrix(train_X)
train_y = np.asmatrix(train_y)
print(train_X.shape)
print(train_y.shape)
plt.figure(figsize = (6, 4))
plt.plot(train_X[:M,0], train_X[:M,1], 'b.', alpha = 0.4, label = 'A')
plt.plot(train_X[M:,0], train_X[M:,1], 'r.', alpha = 0.4, label = 'B')
plt.axis('equal')
plt.xlim([-1, 2]); plt.ylim([-1, 2]);
plt.grid(alpha = 0.15)
plt.legend(fontsize = 12)
plt.show()
```

- Design a perceptron model which has a single layer, and train it to show the accuracy.

- Hidden layer with no nonlinear activiation function

In [ ]:

```
model = tf.keras.models.Sequential([
## your code here
])
model.summary()
```

In [ ]:

```
## your code here
#
```

- Plot the classifier (decision boundary).

In [ ]:

```
## your code here
#
```

- What is the highest accuracy you can get? Discuss the result.

In [ ]:

```
## write down your discussion here
#
#
#
```

- Design a perceptron model which has 2 layers, and train it to show the accuracy.

- Hidden layer: sigmoid function

In [ ]:

```
model = tf.keras.models.Sequential([
## your code here
])
model.summary()
```

In [ ]:

```
## your code here
#
```

- Plot two linear classification boundaries in the input space.

In [ ]:

```
## your code here
#
```