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

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][0]=x[i]
        train_feature[i*100+j][1]=y[j]
        train_label[i*100+j][0]=-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.GradientDescentOptimizer(0.001).minimize(loss)
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)))

拟合结果
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FPGA硅农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值