srkdb
srkdb

Reputation: 815

Neural network - loss not converging

This network contains an input layer and an output layer, with no nonlinearities. The output is just a linear combination of the input.I am using a regression loss to train the network. I generated some random 1D test data according to a simple linear function, with Gaussian noise added. The problem is that the loss function doesn't converge to zero.

import numpy as np
import matplotlib.pyplot as plt

n = 100
alp = 1e-4
a0 = np.random.randn(100,1) # Also x
y = 7*a0+3+np.random.normal(0,1,(100,1))

w = np.random.randn(100,100)*0.01
b = np.random.randn(100,1)

def compute_loss(a1,y,w,b):
       return np.sum(np.power(y-w*a1-b,2))/2/n

def gradient_step(w,b,a1,y):

    w -= (alp/n)*np.dot((a1-y),a1.transpose())
    b -= (alp/n)*(a1-y)  
    return w,b

loss_vec = []
num_iterations = 10000

for i in range(num_iterations):

    a1 = np.dot(w,a0)+b
    loss_vec.append(compute_loss(a1,y,w,b))
    w,b = gradient_step(w,b,a1,y)
plt.plot(loss_vec)

Upvotes: 2

Views: 3374

Answers (2)

Sheldore
Sheldore

Reputation: 39052

The convergence also depends on the value of alpha you use. I played with your code a bit and for

alp = 5e-3

I get the following convergence plotted on a logarithmic x-axis

plt.semilogx(loss_vec)

Output

enter image description here

Upvotes: 1

JacKeown
JacKeown

Reputation: 2890

If I understand your code correctly, you only have one weight matrix and one bias vector despite the fact that you have 2 layers. This is odd and might be at least part of your problem.

Upvotes: 0

Related Questions