没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论


















1 / 11|
Implementing a Neural Network from Scratch - An Introduction
In this post we will implement a simple 3-layer neural network from scratch. We won't derive all the math that's required,
but I will try to give an intuitive explanation of what we are doing and will point to resources to read up on the details.
In this post I'm assuming that you are familiar with basic Calculus and Machine Learning concepts, e.g. you know
what classification and regularization is. Ideally you also know a bit about how optimization techniques like gradient
descent work. But even if you're not familiar with any of the above this post could still turn out to be interesting ;)
But why implement a Neural Network from scratch at all? Even if you plan on using Neural Network libraries
like PyBrain in the future, implementing a network from scratch at least once is an extremely valuable exercise. It
helps you gain an understanding of how neural networks work, and that is essential to designing effective models.
One thing to note is that the code examples here aren't terribly efficient. They are meant to be easy to understand. In
an upcoming post I will explore how to write an efficient Neural Network implementation using Theano..
In [1]:
# Package imports
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import sklearn.datasets
import sklearn.linear_model
import matplotlib
# Display plots inline and change default figure size
%matplotlib inline
matplotlib.rcParams['figure.figsize'] = (10.0, 8.0)
Generating a dataset
Let's start by generating a dataset we can play with. Fortunately, scikit-learn has some useful dataset generators, so
we don't need to write the code ourselves. We will go with the make_moons function.
In [2]:
# Generate a dataset and plot it
np.random.seed(0)
X, y = sklearn.datasets.make_moons(200, noise=0.20)
plt.scatter(X[:,0], X[:,1], s=40, c=y, cmap=plt.cm.Spectral)
Out[2]:
<matplotlib.collections.PathCollection at 0x110609f50>

2 / 11|
The dataset we generated has two classes, plotted as red and blue points. You can think of the blue dots as male
patients and the red dots as female patients, with the x- and y- axis being medical measurements.
Our goal is to train a Machine Learning classifier that predicts the correct class (male or female) given the x- and y-
coordinates. Note that the data is not linearly separable, we can't draw a straight line that separates the two classes.
This means that linear classifiers, such as Logistic Regression, won't be able to fit the data unless you hand-engineer
non-linear features (such as polynomials) that work well for the given dataset.
In fact, that's one of the major advantages of Neural Networks. You don't need to worry about feature engineering.
The hidden layer of a neural network will learn features for you.
Logistic Regression
To demonstrate the point let's train a Logistic Regression classifier. It's input will be the x- and y-values and the output
the predicted class (0 or 1). To make our life easy we use the Logistic Regression class from scikit-learn.
In [3]:
# Train the logistic regeression classifier
clf = sklearn.linear_model.LogisticRegressionCV()
clf.fit(X, y)
Out[3]:
LogisticRegressionCV(Cs=10, class_weight=None, cv=None, dual=False,
fit_intercept=True, intercept_scaling=1.0, max_iter=100,
multi_class='ovr', n_jobs=1, penalty='l2', refit=True,
scoring=None, solver='lbfgs', tol=0.0001, verbose=0)
In [4]:

3 / 11|
# Helper function to plot a decision boundary.
# If you don't fully understand this function don't worry, it just generates
the contour plot below.
def plot_decision_boundary(pred_func):
# Set min and max values and give it some padding
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
h = 0.01
# Generate a grid of points with distance h between them
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max,
h))
# Predict the function value for the whole gid
Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# Plot the contour and training examples
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)
In [12]:
# Plot the decision boundary
plot_decision_boundary(lambda x: clf.predict(x))
plt.title("Logistic Regression")
Out[12]:
<matplotlib.text.Text at 0x111951450>
The graph shows the decision boundary learned by our Logistic Regression classifier. It separates the data as good
as it can using a straight line, but it's unable to capture the "moon shape" of our data.
剩余10页未读,继续阅读
资源评论


hquzkzhang
- 粉丝: 7
- 资源: 27
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
