Tensorflow学习笔记(五)

RNN回归预测

一:问题概述

输入一条sin曲线,预测一条cos曲线。

二:代码实现

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

BATCH_START=0
TIME_STEPS=20
BATCH_SIZE=50
INPUT_SIZE=1
OUTPUT_SIZE=1
CELL_SIZE=10
LR=0.006


def get_batch():
    global BATCH_START,TIME_STEPS
    #xs shape(50batchs,20steps)
    xs=np.arange(BATCH_START,BATCH_START+TIME_STEPS*BATCH_SIZE).reshape((BATCH_SIZE,TIME_STEPS))/(10*np.pi)
    seq=np.sin(xs)
    res=np.cos(xs)
    BATCH_START+=TIME_STEPS
    #plt.plot(xs[0,:],res[0,:],'r',xs[0,:],'b--')
    #plt.show()
    #returned seq,res and shape(batch,step,input)
    return [seq[:,:,np.newaxis],res[:,:,np.newaxis],xs]

class LSTMRNN(object):
    def __init__(self,n_steps,input_size,output_size,cell_size,batch_size):
        self.n_steps=n_steps
        self.input_size=input_size
        self.output_size=output_size
        self.cell_size=cell_size
        self.batch_size=batch_size
        with tf.name_scope('inputs'):
            self.xs=tf.placeholder(tf.float32,[None,n_steps,input_size],name='xs')
            self.ys=tf.placeholder(tf.float32,[None,n_steps,output_size],name='ys')
        with tf.variable_scope('in_hidden'):
            self.add_input_layer()
        with tf.variable_scope('LSTM_cell'):
            self.add_cell()
        with tf.variable_scope('out_hidden'):
            self.add_output_layer()
        with tf.name_scope('cost'):
            self.compute_cost()
        with tf.name_scope('train'):
            self.train_op=tf.train.AdamOptimizer(LR).minimize(self.cost)

    def add_input_layer(self,):
        #layer_in_x:(batch*n_steps,input_size)
        l_in_x=tf.reshape(self.xs,[-1,self.input_size],name='2_2D')
        #Weights:(in_size,cell_size)
        Ws_in=self._weight_variable([self.input_size,self.cell_size])
        #bias:(cell_size,)
        bs_in=self._bias_variable([self.cell_size,])
        #layer_in_y:(batch*n_steps,cell_size)
        with tf.name_scope('Wx_plus_b'):
            l_in_y=tf.matmul(l_in_x,Ws_in)+bs_in
        #reshape l_in_y -->(batchmn_steps,cell_size)
        self.l_in_y=tf.reshape(l_in_y,[-1,self.n_steps,self.cell_size],name='2-3D')


    def add_cell(self):
        lstm_cell=tf.contrib.rnn.BasicLSTMCell(self.cell_size,forget_bias=1.0,state_is_tuple=True)
        with tf.name_scope('initial_state'):
            self.cell_init_state=lstm_cell.zero_state(self.batch_size,dtype=tf.float32)
        self.cell_outputs,self.cell_final_state=tf.nn.dynamic_rnn(
            lstm_cell,self.l_in_y,initial_state=self.cell_init_state,time_major=False)

    def add_output_layer(self):
        #shape=(batch*steps,cell_size)
        l_out_x=tf.reshape(self.cell_outputs,[-1,self.cell_size],name='2-2D')
        Ws_out=self._weight_variable([self.cell_size,self.output_size])
        bs_out=self._bias_variable([self.output_size,])
        #shape=(batch*steps,output_size)
        with tf.name_scope('Wx_plus_b'):
            self.pred=tf.matmul(l_out_x,Ws_out)+bs_out

    def compute_cost(self):
        losses=tf.contrib.legacy_seq2seq.sequence_loss_by_example(
            [tf.reshape(self.pred,[-1],name='reshape_pred')],
            [tf.reshape(self.ys,[-1],name='reshape_target')],
            [tf.ones([self.batch_size*self.n_steps],dtype=tf.float32)],
            average_across_timesteps=True,
            softmax_loss_function=self.ms_error,
            name='losses'
        )
        with tf.name_scope('average_cost'):
            self.cost=tf.div(
                tf.reduce_sum(losses,name='losses_sum'),
                self.batch_size,
                name='average_cost')
            tf.summary.scalar('cost',self.cost)

    @staticmethod
    def ms_error(labels,logits):
        return tf.square(tf.subtract(labels,logits))

    def _weight_variable(self,shape,name='weights'):
        initializer=tf.random_normal_initializer(mean=0.,stddev=1.,)
        return tf.get_variable(shape=shape,initializer=initializer,name=name)

    def _bias_variable(self,shape,name='biases'):
        initializer=tf.constant_initializer(0.1)
        return tf.get_variable(name=name,shape=shape,initializer=initializer)

if __name__ == '__main__':
    model=LSTMRNN(TIME_STEPS,INPUT_SIZE,OUTPUT_SIZE,CELL_SIZE,BATCH_SIZE)
    sess=tf.Session()
    merged=tf.summary.merge_all()
    writer=tf.summary.FileWriter("logs",sess.graph)
    #relocate to the local dir and run this line to view it on Chrome(http://0.0.0.0:6006/):
    #$tensorboard --logdir='logs'

    sess.run(tf.global_variables_initializer())
    plt.ion()
    plt.show()
    for i in range(200):
        seq,res,xs=get_batch()
        if i==0:
            feed_dict={
                model.xs:seq,
                model.ys:res,
            }
        else:
            feed_dict={
                model.xs:seq,
                model.ys:res,
                model.cell_init_state:state
            }
        _,cost,state,pred=sess.run(
            [model.train_op,model.cost,model.cell_final_state,model.pred],
            feed_dict=feed_dict)

        plt.plot(xs[0,:],res[0].flatten(),'r',xs[0,:],pred.flatten()[:TIME_STEPS],'b--')
        plt.ylim((-1.2,1.2))
        plt.draw()
        plt.pause(0.3)

        if i%20==0:
            print('cost:',round(cost,4))
            result=sess.run(merged,feed_dict)
            writer.add_summary(result,i)

        

三:代码可视化

1. 打开cmd调用tensorboard

(base) C:\Users\000>activate tensorflow

(tensorflow) C:\Users\000>e:

(tensorflow) E:\>cd logs文件所在路径

(tensorflow) E:\logs文件所在路径>tensorboard --logdir=logs
Starting TensorBoard b'54' at http://0.0.0.0:6006
(Press CTRL+C to quit)

2. 输入上述网址 http://0.0.0.0:6006
打开GRAPHS,整体如下
在这里插入图片描述
3. 输入input
xs: sin函数
ys: cos函数,标准值,用于计算损失
在这里插入图片描述
4. 隐藏层输入in_hidden
(1)将xs转为二维数据
(2)乘以权重Ws_in
(3)加上偏置bs_in
(4)将计算结果Wx_plus_b转为3维
在这里插入图片描述
5. LSTM层
(1)使用BasicLSTMCell
(2)定义initial_state初始状态
(3)使用dynamic_rnn,进行循环
在这里插入图片描述
6. 隐藏层输出out_hidden
(1)将LSTM处理完的输出转为2D
(2)乘以权重Ws_out
(3)加上偏置bs_out
(4)得到第一次循环的输出预测值
(4)更新状态:initial_state更新为final_state
在这里插入图片描述
7.计算损失值cost
(1)使用tf.contrib.legacy_seq2seq.sequence_loss_by_example函数计算交叉熵losses
(2)因为上一步求得的losses值为每一个序列batch的值,所以求和后再取平均值,得到cost
在这里插入图片描述

四:运行结果

1. cost损失值

cost: 42.0495
cost: 7.3446
cost: 2.3511
cost: 0.4777
cost: 1.4825
cost: 0.6004
cost: 0.1342
cost: 0.263
cost: 1.7159
cost: 0.2112

2. 实时预测过程
红线:ys,cos函数
蓝线:预测值
可看到,随着训练,最后两线拟合。
在这里插入图片描述

参考文献

1.RNN LSTM回归例子

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图像识别技术在病虫害检测中的应用是一个快速发展的领域,它结合了计算机视觉和机器学习算法来自动识别和分类植物上的病虫害。以下是这一技术的一些关键步骤和组成部分: 1. **数据收集**:首先需要收集大量的植物图像数据,这些数据包括健康植物的图像以及受不同病虫害影响的植物图像。 2. **图像预处理**:对收集到的图像进行处理,以提高后续分析的准确性。这可能包括调整亮度、对比度、去噪、裁剪、缩放等。 3. **特征提取**:从图像中提取有助于识别病虫害的特征。这些特征可能包括颜色、纹理、形状、边缘等。 4. **模型训练**:使用机器学习算法(如支持向量机、随机森林、卷积神经网络等)来训练模型。训练过程中,算法会学习如何根据提取的特征来识别不同的病虫害。 5. **模型验证和测试**:在独立的测试集上验证模型的性能,以确保其准确性和泛化能力。 6. **部署和应用**:将训练好的模型部署到实际的病虫害检测系统中,可以是移动应用、网页服务或集成到智能农业设备中。 7. **实时监测**:在实际应用中,系统可以实时接收植物图像,并快速给出病虫害的检测结果。 8. **持续学习**:随着时间的推移,系统可以不断学习新的病虫害样本,以提高其识别能力。 9. **用户界面**:为了方便用户使用,通常会有一个用户友好的界面,显示检测结果,并提供进一步的指导或建议。 这项技术的优势在于它可以快速、准确地识别出病虫害,甚至在早期阶段就能发现问题,从而及时采取措施。此外,它还可以减少对化学农药的依赖,支持可持续农业发展。随着技术的不断进步,图像识别在病虫害检测中的应用将越来越广泛。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值