Python利用主流框架tensorflow搭建BP网络拟合

主要参考了莫烦老师的一些程序,感觉主要是将数据进行归一化,特别是y的数值进行归一化,不然训练的过程中loss降不下去
tensorflow

# -*- coding: utf-8 -*-
"""
Created on Thu Nov  8 11:56:47 2018

@author: Administrator
"""

# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# 生成模拟数据,二次函数关系
x = np.linspace(-2, 2, 1000)[:, np.newaxis]
y = 5 * x + np.sin(2 * 3.14 * x) + 2

# x = train_X
# y = train_Y
#####s随机打乱
np.random.seed(2019)
n_index = np.random.permutation(len(x))

# ###########利用随机索引划分验证集和测试集
train_x_disorder = x[n_index[0:800]]
test_x_disorder = x[n_index[800:len(x) + 1]]
train_y_disorder = y[n_index[0:800]]
test_y_disorder = y[n_index[800:len(x) + 1]]


train_X = train_x_disorder
train_Y = train_y_disorder


tf.set_random_seed(2019)
# 创建模型
# 占位符
X = tf.placeholder("float", [None, 1])
Y = tf.placeholder("float", [None, 1])
# 模型参数
W1 = tf.Variable(tf.random_normal([1, 80]), name="weight")
b1 = tf.Variable(tf.ones([1, 80]), name="bias")
W3 = tf.Variable(tf.random_normal([80, 1]), name="weight")
b3 = tf.Variable(tf.ones([1]), name="bias")

# 前向结构
z1 = tf.matmul(X, W1) + b1
z2 = tf.nn.relu(z1)
z5 = tf.matmul(z2, W3) + b3

# 反向优化
cost = tf.reduce_mean(tf.square(Y - z5))
# optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)  # Gradient descent
optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)

# 初始化变量
init = tf.global_variables_initializer()
# 训练参数
training_epochs = 10000
display_step = 100
loss_dis = []

# 启动session
sess = tf.Session()
sess.run(init)


for epoch in range(training_epochs + 1):
    sess.run(optimizer, feed_dict={X: train_X, Y: train_Y})

    # 显示训练中的详细信息
    if epoch % display_step == 0:
        loss = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
        print("Epoch:", epoch, "cost=", loss)
        loss_dis.append(loss)
print(" Finish")


# 预测结果

pre = sess.run(z5,feed_dict={X:train_X})
plt.figure()
plt.plot(train_X,pre,'o',train_X,train_Y,'*')

error = pre-train_Y
plt.figure()
plt.plot(error)



你可能感兴趣的:(Python利用主流框架tensorflow搭建BP网络拟合)