# 用神经网络进行多元非线性函数的拟合

43 篇文章 1 订阅
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

train_feature=np.zeros((10000,2))
train_label=np.zeros((10000,1))
x=np.linspace(-np.pi,np.pi,100)
y=np.linspace(-np.pi,np.pi,100)

for i in range(100):
for j in range(100):
train_feature[i*100+j]=x[i]
train_feature[i*100+j]=y[j]
train_label[i*100+j]=-2*np.cos(x[i])*np.sin(y[j])

X, Y = np.meshgrid(x, y)

# 搭建神经网络
# 定义x y
x = tf.placeholder(tf.float32, [None, 2])
y = tf.placeholder(tf.float32, [None, 1])

Weights_L1 = tf.Variable(tf.random_normal([2, 128]))
# 偏置矩阵
biases_L1 = tf.Variable(tf.zeros([1, 128]))
Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + biases_L1
# 激活函数私有tanh
L1 = tf.nn.tanh(Wx_plus_b_L1)

Weights_L2 = tf.Variable(tf.random_normal([128, 128]))
# 偏置矩阵
biases_L2 = tf.Variable(tf.zeros([1, 128]))
Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + biases_L2
# 激活函数私有tanh
L2 = tf.nn.tanh(Wx_plus_b_L2)

# 定义神经网络输出层
Weights_L3 = tf.Variable(tf.random_normal([128, 1]))
biases_L3 = tf.Variable(tf.zeros([1, 1]))
Wx_plus_b_L3 = tf.matmul(L2, Weights_L3) + biases_L3
prediction = Wx_plus_b_L3

# 代价函数
loss = tf.reduce_mean(tf.square(y - prediction))

train_step = tf.train.MomentumOptimizer(0.01, 0.9).minimize(loss)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
print(sess.run(loss, feed_dict={x: train_feature, y: train_label}))
for i in range(10000):
sess.run(train_step, feed_dict={x: train_feature, y: train_label})
if i%100==0:
print(sess.run(loss, feed_dict={x: train_feature, y: train_label}))

print("**********************************************************")
print(sess.run(loss, feed_dict={x: train_feature, y: train_label}))
pred=sess.run(prediction, feed_dict={x: train_feature})
#预测值画图
plt.contourf(X, Y, pred.reshape(100,100),8,cmap=plt.cm.hot)
#理论值画图
plt.contour(X, Y, train_label.reshape(100,100),8,colors='k')
plt.show()
#理论值与预测值的均方误差
print(np.mean(np.square(pred-train_label))) 01-13
07-18 291  10-15 144
05-13
11-06
02-01 3830
11-30 1329
04-14 913
12-31
11-12 2455
07-24 6954
12-10 1330
12-04 248
11-06 1652
11-06 1357
02-19 1037
10-04 1091 点击重新获取   扫码支付  余额充值