1
$\begingroup$

I have built a very simple feed-forward neural network which given an input $x \in \{0, 1\}$, it is trained to learn $f(x) = x$, the identity function. Below is a model where on iteration $i$, $x_i$ is the training input, $w_i$ is the connection weight, $A$ is the activation function, and $\hat{y}_i$ is the feed-forward output: Model of simple feed-forward neural network

Here is the code:

import numpy as np

# for reproducability
np.random.seed(2017)

def sigmoid(x):
    return 1./(1. + np.exp(-x))
def sigmoid_prime(x):
    return (1. - sigmoid(x)) * sigmoid(x)

class SimpleNetwork:
    def __init__(self):
        self.weight = np.random.random()
        self.learning_rate = 0.01
        self.bias = 1

    def predict(self, x):
        return sigmoid(x * self.weight + self.bias)

    def back_prop(self, x, yh, y, verbose=False):
        # compute error
        error = 0.5 * (yh - y) ** 2
        self.log(error, verbose)

        # compute dE/dw
        d_weight = (yh - y) * x * sigmoid_prime(self.weight * x + self.bias)
        self.weight -= self.learning_rate * d_weight

    def log(self, error, verbose=True):
        if verbose:
            print('error: {}'.format(error))

    def fit(self, X, Y, epochs=1, verbose=False):
        for _ in range(epochs):
            for x, y in zip(X, Y):
                yh = self.predict(x)
                self.back_prop(x, yh, y, verbose)

X = np.random.randint(0, 2, (100,))
Y = X

net = SimpleNetwork()
net.fit(X, Y, epochs=100, verbose=False)

print(net.predict(0), net.predict(1))

I understand that if the bias = 0, then when $x_i = 0$, $A(x_iw_i) = A(0) = 0.5$, thus the network will never learn the correct output for x = 0. However, when I use bias = 1, the network still converges to a non-zero value. Is there a better bias value I should use?

$\endgroup$
5
  • $\begingroup$ you need a weight for the bias term too ( which gets adjusted by backprop too) $\endgroup$
    – seanv507
    Commented Jul 3, 2017 at 7:06
  • $\begingroup$ you have to backpropagate the bias value as well $\endgroup$ Commented Jul 3, 2017 at 8:44
  • $\begingroup$ @seanv507 okay, so in the future when my network contains more layers, should I add a bias term to each layer? Also, does the value of the bias term matter in this case (and its initial weight)? $\endgroup$ Commented Jul 3, 2017 at 12:17
  • $\begingroup$ So your single neuron network can never recreate the linear function y= x if you use a sigmoid. given this is just a test you should just create targets y=sigmoid (a x + b.bias) where you fix a and b and check you can recover the weights a and b by gradient descent. if you wanted to recreate the identify function, either you need an extra linear output neuron or you change to use linear neuron. $\endgroup$
    – seanv507
    Commented Jul 3, 2017 at 14:19
  • $\begingroup$ My understanding for your case is to set bias as - average(input_values). When doing the learning, calculate the average first and set that as bias of the input layer. $\endgroup$
    – Martian
    Commented Jun 1, 2018 at 12:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.