用tensorflow实现搭建简单的神经网络实现回归

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

#1维y=x^2数据集
x_data=np.linspace(-1,1,200)[np.newaxis,:]
bias=0.5
y_data=np.square(x_data)+bias+np.random.normal(0,0.05,x_data.shape)

class DNN:
    args=[]
    layer_num=0

    def add_layer(self,input_size,output_size,activation_function=None):
        self.layer_num=self.layer_num+1
        Weights=tf.Variable(tf.random_normal([output_size,input_size]))
        bias=tf.Variable(tf.zeros([output_size,1])+0.01)
        self.args.append({'Weights':Weights,'bias':bias,'act_fcn':activation_function})
        return {'Weights':Weights,'bias':bias,'act_fcn':activation_function}

    def forward_propagation(self,input_holder):
        # derive the output of a specific input
        result=input_holder
        for i in range(self.layer_num):
            if self.args[i]['act_fcn']==None:
                result=tf.matmul(self.args[i]['Weights'],result)+self.args[i]['bias']
            else:
                fcn=self.args[i]['act_fcn']
                result=fcn(tf.matmul(self.args[i]['Weights'],result)+self.args[i]['bias'])
        return result

#build the network
network=DNN()
network.add_layer(x_data.shape[0],10,activation_function=tf.nn.relu)
network.add_layer(10,10,activation_function=tf.nn.tanh)
network.add_layer(10,1,activation_function=None)
# input data set
x_data_holder=tf.placeholder(dtype=tf.float32,shape=x_data.shape)
# labels
y_data_holder=tf.placeholder(dtype=tf.float32,shape=y_data.shape)
#prediction
prediction=list(map(network.forward_propagation,tf.split(x_data_holder,x_data.shape[1],1)))
#loss function
loss=tf.reduce_mean(tf.square(tf.concat(prediction,axis=1)-y_data_holder))
train_step=tf.train.AdadeltaOptimizer(learning_rate=0.05).minimize(loss)
#训练
sess=tf.Session()
##初始化
init=tf.global_variables_initializer()
sess.run(init)
##训练模型
loss_record=[]
prediction_record=[]
for episode in range(5000):
    print('episode = ', episode)
    _,loss_n,prediction_n =sess.run([train_step,loss,prediction],feed_dict={x_data_holder:x_data,y_data_holder:y_data})
    print(loss_n)
    loss_record.append(loss_n)
    prediction_record.append(prediction_n)

plt.figure(1)
plt.plot(loss_record)

plt.figure(2)
plt.plot(np.arange(x_data.shape[1]),np.squeeze(prediction_record[-1]),'r-')
plt.scatter(np.arange(x_data.shape[1]),np.squeeze(y_data))
plt.show()

效果:

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值