Tensorflow实现多层感知函数逼近

博客围绕TensorFlow、深度学习和神经网络展开,运用Python进行相关操作。TensorFlow是深度学习常用框架,结合神经网络可实现多种功能,Python则为开发提供便利,助力构建和训练模型。
#1:导入需要用到的模块
import tensorflow as tf
import tensorflow.contrib.layers as layers
from sklearn import datasets
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import  MinMaxScaler
import  pandas as pd
import seaborn as sns

'''
只有一个隐藏层的多层前馈网络足以逼近任何函数
同时还可以保证很高的精度和令人满意的效果
使用MLP(多层感知机进行函数逼近,预测波士顿房价)
'''

#2:加载数据集并创建Pandas数据帧来分析数据:

boston = datasets.load_boston()
df = pd.DataFrame(boston.data,columns=boston.feature_names)
df['target'] = boston.target

#3:了解一些数据的细节
df.describe()

#4:三个参数RM,PTRATIO和LSTAT在幅度上输出之间具有大于0.5的相关性
# 将数据集分解为训练数据集和测试数据集。使用MinMaxScaler来规划数据集
#由于神经网络使用Sigmoid激活函数(Sigmoid的输出只能在0-1之间)
#所以必须对目标值进行归一化

x_train,x_test,y_train,y_test = train_test_split(df[['RM','LSTAT','PTRATIO']],df[['target']],test_size=0.3,random_state=0)
#归一化数据
X_train = MinMaxScaler().fit_transform(x_train)
X_test = MinMaxScaler().fit_transform(x_test)
Y_train = MinMaxScaler().fit_transform(y_train)
Y_test = MinMaxScaler().fit_transform(y_test)

#5:定义常量和超参数
m = len(X_train)
n = 3 #特征的数量
n_hidden = 20 #隐藏层神经元的数量
#超参数
batch_size = 200
eta = 0.01 #学习率
max_epoch = 1000 #最大迭代数

#6:创建一个单隐藏层的多层感知机模型
def multilayer_perceptron(x):
    fc1 = layers.fully_connected(x,n_hidden,activation_fn=tf.nn.relu,scope='fc1')
    out = layers.fully_connected(fc1,1,activation_fn=tf.sigmoid,scope='out')
    return out
#7:声明训练数据的占位符并定义损失和优化器
x = tf.compat.v1.placeholder(tf.float32,name='X',shape=[m,n])
y = tf.compat.v1.placeholder(tf.float32,name='Y')
y_hat = multilayer_perceptron(x)
correct_prediction = tf.square(y-y_hat)
mse = tf.reduce_mean(tf.cast(correct_prediction,'float'))
train = tf.train.AdamOptimizer(learning_rate=eta).minimize(mse)


#8:执行计算图
init = tf.global_variables_initializer()

with tf.compat.v1.Session() as sess:
    sess.run(init)
    writer = tf.summary.FileWriter('g3',sess.graph)
    # 训练模型100次迭代
    for i in range(max_epoch):
        _, l,p = sess.run([train,mse,y_hat],feed_dict={x:X_train,y:Y_train})
        if i %100 ==0:
            print('Epoch {0}:Loss {1}'.format(i,l))
    print("Training Done")
    print('Optimization Finished!')
#测试模型
    correct_prediction = tf.square(y-y_hat)
#计算准确度
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,'float'))
    print('Mean ERROR:',accuracy.eval({x:X_train,y:Y_train}))
    plt.scatter(Y_train,p)
    writer.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值