Keras搭建第一个神经网络(线性回归)

版权声明:站在巨人的肩膀上学习。 https://blog.csdn.net/zgcr654321/article/details/83934278

Keras介绍:

Keras 是一个兼容 Theano 和 Tensorflow 的神经网络高级包, 用他来组件一个神经网络更加快速, 几条语句就能搞定。而且广泛的兼容性能使 Keras 在 Windows 和 MacOS 或者 Linux 上运行无阻碍。

Keras安装:

首先确认你的Python环境中有numpy和scipy。

然后确认你的Python环境中安装了Tensorflow或Theano。

然后用下面的命令安装:

python -m pip install keras

安装完成后,还要设置一下backend,即keras是基于哪个后台框架(tensorflow或theano)做运算。

如果你使用的是anaconda的Python环境,打开anaconda prompt,然后激活相应的Python环境,如:

我们激活Python,然后输入import keras,此时可以看到Using Tensorflow backend,即我们使用Tensorflow作为后台计算框架。

我们可以在相应的环境的文件夹中找到keras的配置文件,如:

C:\ProgramData\Anaconda3\envs\tensorflow-gpu\Library\home\nwani\.keras\keras.json

使用记事本打开这个文件,我们可以看到"backend": "tensorflow",如果想用theano,只需要修改tensorflow为theano即可(前提是你已经装了theano)。

注意:

这种方法是永久修改,且有时候修改后import keras会报错,不推荐。

灵活设置backend的方法(推荐):

import os

os.environ['KERAS_BACKEND']='tensorflow'

在你的.py文件开头加上这两句,根据你的使用要求灵活选择backend是tensorflow还是theano,这个配置只在当前这个.py文件中生效。

Keras建立线性回归神经网络模型:

注意这里用的是keras2.0的API。

代码如下:

import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense
import matplotlib.pyplot as plt
import os

os.environ['KERAS_BACKEND'] = 'tensorflow'
# 指定backend框架为tensorflow
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# 想要不出现红字提示,则需要上面这一句
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# 指定使用的GPU的编号,从0开始,如果使用tensorflow-gpu环境编译则需要上面这句

# 创建一组数据X和Y
X = np.linspace(-1, 1, 200)
# 打乱X数据顺序
np.random.seed(1337)
np.random.shuffle(X)
Y = 0.5 * X + 2 + np.random.normal(0, 0.05, (200,))

# 将前160个数据样本作为训练数据,后40个数据样本作为测试数据
x_train, y_train = X[:160], Y[:160]
x_test, y_test = X[160:], Y[160:]

# Sequential建立model
model = Sequential()
# 用model.add添加神经层,添加的是Dense全连接神经层
# output_dim=1, input_dim=1表示输入数据和输出数据都是一维的
# keras会自动根据你输入数据和输出数据的维度以及添加的神经层的种类来生成足够数量的权重w和b
model.add(Dense(input_dim=1, units=1))
# 如果需要添加下一个神经层的时候,不用再定义输入的纬度,因为它默认就把前一层的输出作为当前层的输入。

# 定义loss函数和优化器
# loss函数用的是mse均方误差。优化器用的是sgd随机梯度下降法
model.compile(loss='mse', optimizer='sgd')
# 这样我们就建好了一个简单的神经网络,是不是比tensorflow简单多了?

# 训练模型
iteration = 1000
print('\nTraining --------------------')
for step in range(iteration):
	# 用model.train_on_batch每次使用X_train, Y_train尺寸的样本训练。默认的返回值是cost
	cost = model.train_on_batch(x_train, y_train)
	if step % 100 == 0:
		print('iteration:{} cost:{}'.format(step, cost))

# 测试训练好的模型
print('\nTesting --------------------')
cost = model.evaluate(x_test, y_test, batch_size=40)
print('The test cost is:{}'.format(cost))
w, b = model.layers[0].get_weights()
print('Weights=', w, '\nbiases=', b)

# 可视化训练结果和拟合曲线
# 在图上画出我们训练用的数据点(x_train, y_train),用红色点表示
plt.scatter(x_train, y_train, color="r")

# 可视化测试结果和拟合曲线
y_test_prediction = model.predict(x_test)
# 在图上画出我们训练用的数据点(x_test, y_test),用绿色点表示
plt.scatter(x_test, y_test, color="g")
# 在图上画出我们预测的线性函数(蓝色线)
plt.plot(x_test, y_test_prediction, color="b")
plt.show()

运行结果如下:

Using TensorFlow backend.

Training --------------------
iteration:0 cost:4.022500038146973
iteration:100 cost:0.0732385516166687
iteration:200 cost:0.003862730460241437
iteration:300 cost:0.002643448766320944
iteration:400 cost:0.002621870022267103
iteration:500 cost:0.002621450461447239
iteration:600 cost:0.002621431602165103
iteration:700 cost:0.00262142950668931
iteration:800 cost:0.002621428342536092
iteration:900 cost:0.002621427644044161

Testing --------------------

40/40 [==============================] - 0s 525us/step
The test cost is:0.0032471008598804474
Weights= [[0.49136478]] 
biases= [2.004053]

生成一个图像:

红色点是训练数据,绿色点是测试数据,蓝色线是预测的线性回归函数。

猜你喜欢

转载自blog.csdn.net/zgcr654321/article/details/83934278