For example, let's consider a network with 1 hidden layer of $N$ units and a linear output layer: our cost function is $$ g(\alpha, W) = \sum_i \left(y_i - \alpha_i\sigma(Wx_i)\right)^2 $$ where $x_i \in \mathbb R^p$ and $W \in \mathbb R^{N \times p}$ (and I'm omitting bias terms for simplicity). This is not necessarily convex when viewed as a function of $(\alpha, W)$ (depending on $\sigma$: if a linear activation function is used then this still can be convex). And the deeper our network gets, the less convex things are.
costfunc <- function(u, v, W, a, x, y, afunc) {
W[1,1] <- u; W[1,2] <- v
preds <- t(a) %*% afunc(W %*% t(x))
sum((y - preds)^2)
}
set.seed(1)
n <- 75 # number of observations
p <- 3 # number of predictors
N <- 1 # number of hidden layersunits
x <- matrix(rnorm(n * p), n, p)
y <- rnorm(n) # all noise
a <- matrix(rnorm(N), N)
W <- matrix(rnorm(N * p), N, p)
afunc <- function(z) 1 / (1 + exp(-z)) # sigmoid
l = 400 # dim of matrix of cost evaluations
wvals <- seq(-50, 50, length = l) # where we evaluate costfunc
fmtx <- matrix(0, l, l)
for(i in 1:l) {
for(j in 1:l) {
fmtx[i,j] = costfunc(wvals[i], wvals[j], W, a, x, y, afunc)
}
}
filled.contour(wvals, wvals, fmtx,plot.axes = { contour(wvals, wvals, fmtx, nlevels = 25,
drawlabels = F, axes = FALSE,
frame.plot = FALSE, add = TRUE); axis(1); axis(2) },
main = 'NN loss surface', xlab = expression(paste('W'[11])), ylab = expression(paste('W'[12])))