抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

NN 可用来模拟 regression,给一组数据,用一条线对数据进行拟合,并可预测新输入 x 的输出值。

Regressor in Keras
Regressor in Keras

创建数据

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt # 可视化模块

# create some data
X = np.linspace(-1, 1, 200)

np.random.shuffle(X) # randomize the data

Y = 0.5 * X + 2 + np.random.normal(0, 0.05, (200, ))
1
2
3
4
5
6
# plot data
plt.scatter(X, Y)
plt.show()

X_train, Y_train = X[:160], Y[:160] # train 前 160 data points
X_test, Y_test = X[160:], Y[160:] # test 后 40 data points
image

1. build model

1
2
model = Sequential()
model.add(Dense(output_dim=1, input_dim=1))

用 Sequential 建立 model, 再用 model.add 添加神经层,添加的是 Dense FC 层

参数有两个,一个是输入数据和输出数据的维度,本代码的例子中 x 和 y 是一维的。

如果需要添加下一个神经层的时候,不用再定义输入的纬度,因为它默认就把前一层的输出作为当前层的输入。在这个例子里,只需要一层就够了。

2. compile model

1
2
3
4
# choose loss function and optimizing method
model.compile(loss='mse', optimizer='sgd')

# mse 均方误差; optimizer sgd 随机梯度下降法.

3. train model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# training
print('Training -----------')
for step in range(301):
cost = model.train_on_batch(X_train, Y_train)
if step % 100 == 0:
print('train cost: ', cost)

"""
Training -----------
train cost: 4.111329555511475
train cost: 0.08777070790529251
train cost: 0.007415373809635639
train cost: 0.003544030711054802
"""

训练的时候用 model.train_on_batch 一批一批的训练 X_train, Y_train。默认的返回值是 cost.

4. evaluate model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# test
print('\nTesting ------------')
cost = model.evaluate(X_test, Y_test, batch_size=40)
print('test cost:', cost)
W, b = model.layers[0].get_weights()
print('Weights=', W, '\nbiases=', b)

"""
Testing ------------
40/40 [==============================] - 0s
test cost: 0.004269329831
Weights= [[ 0.54246825]]
biases= [ 2.00056005]
"""

用到的函数是 model.evaluate,输入测试集的xy, 输出 costweightsbiases。其中 weightsbiases 是取在模型的第一层 model.layers[0] 学习到的参数。

从学习到的结果可以看到, weights 比较接近0.5,bias 接近 2。

5. Visualization

1
2
3
4
5
6
# plotting the prediction
Y_pred = model.predict(X_test)
plt.scatter(X_test, Y_test)
plt.plot(X_test, Y_pred)
plt.show()

image

6. Gaussian Distribution

先看伟大的高斯分布(Gaussian Distribution)的概率密度函数(probability density function)

f(x)=12πσexp((xμ)22σ2)f(x)=\frac1{\sqrt{2\pi}\sigma}\exp(-\frac{(x-\mu)^2}{2\sigma^2})

对应于numpy中:

1
numpy.random.normal(loc=0.0, scale=1.0, size=None)

Reference

Comments