• 学习进度笔记17


    观看Tensorflow案例实战视频课程17 RNN网络模型

    import tensorflow as tf
    import input_data
    import numpy as np
    import matplotlib.pyplot as plt
    print("Packages imported")
    
    mnist=input_data.read_data_sets("data/",one_hot=True)
    trainimgs,trainlables,testimgs,testlabels 
        =mnist.train.images,mnist.train.labels,mnist.test.images,mnist.test.labels
    ntrain,ntest,dim,nclasses 
        =trainimgs.shape[0],testimgs.shape[0],trainimgs.shape[1],trainlables.shape[1]
    print("MNIST loaded")
    
    diminput=28
    dimhidden=128
    dimoutput=nclasses
    nsteps=28
    weights={
        'hidden':tf.Variable(tf.random_normal([diminput,dimhidden])),
        'out':tf.Variable(tf.random_normal([dimhidden,dimoutput]))
    }
    biases={
        'hidden':tf.Variable(tf.random_normal([dimhidden])),
        'out':tf.Variable(tf.random_normal([dimhidden]))
    }
    
    def _RNN(_X,_W,_b,_nsteps,_name):
        #1.Permute input from [batchsize,nsteps,diminput]
        #  =>[nsteps,batchsize,diminput]
        _X=tf.transpose(_X,[1,0,2])
        #2.Reshape input to [nsteps*batchsize,diminput]
        _X=tf.reshape(_X,[-1,diminput])
        #3.Input layer => Hidden layer
        _H=tf.matmul(_X,_W['hidden'])+_b['hidden']
        #4.Splite data to 'nsteps' chunks. An i_th chunck indicates i_th batch data
        _Hsplit=tf.split(0,_nsteps,_H)
        #5.Get LSTM's final output (_LSTM_O) and state (_LSTM_S)
        #  Both _LSTM_O and _LSTM_S consist of 'batchsize' elements
        #  Only _LSTM_O will be used to Predict the output.
        with tf.variable_scope(_name) as scope:
            #scope.reuse_variables()
            lstm_cell=tf.nn.run_cell.BasicLSTMCell(dimhidden,forget_bias=1.0)
            _LSTM_O,_LSTM_S=tf.nn.rnn(lstm_cell,_Hsplit,dtype=tf.float32)
        #6.Output
        _O=tf.matmul(_LSTM_O[-1],_W['out'])+_b['out']
        #Return!
        return{
            'X':_X,'H':_H,'Hsplit':_Hsplit,
            'LSTM_O':_LSTM_O,'LSTM_S':_LSTM_S,'O':_O
        }
    print("Network ready")
  • 相关阅读:
    前端工程师如何打发闲余时光?(转)
    比较好的前端开发工具
    蓝桥历年套题 约数倍数选卡片 博弈
    单调栈求全1(或全0)子矩阵的个数 洛谷P5300与或和 P3400仓鼠窝
    5-15
    2018CCPC桂林站G Greatest Common Divisor
    STL中的BITSET运用
    2018CCPC桂林站JStone Game
    牛客2019湘潭大学程序竞赛
    Combine String HDU
  • 原文地址:https://www.cnblogs.com/zql-42/p/14630760.html
Copyright © 2020-2023  润新知