zoukankan      html  css  js  c++  java
  • 学习进度笔记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")
  • 相关阅读:
    vue-cli3使用cdn引入
    修饰器
    go strconv
    【BZOJ 5125】小Q的书架
    【NOI 2009】诗人小G
    后缀数组
    点分治
    四边形不等式
    【NOIP 2015】Day2 T3 运输计划
    【NOIP 2016】Day1 T2 天天爱跑步
  • 原文地址:https://www.cnblogs.com/zql-42/p/14630760.html
Copyright © 2011-2022 走看看