recurrentNetwork.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:TikZ 作者: ellisk42 项目源码 文件源码
def __init__(self, numberOfUnits, dictionarySize, maximumLength, inputFeatures = None, alwaysProvideInput = False):
        self.model = rnn.LSTMCell(numberOfUnits)

        self.loadingMatrix = tf.Variable(tf.random_uniform([numberOfUnits,dictionarySize],-1.0,1.0),name = 'LOADINGMATRIX')

        self.lengthPlaceholder = tf.placeholder(tf.int32, shape = [None],name = 'LENGTH')

        self.maximumLength = maximumLength
        self.dictionarySize = dictionarySize

        if inputFeatures != None:
            self.transformedInputFeatures = [ tf.layers.dense(inputs = inputFeatures,
                                                         units = s,
                                                         activation = tf.nn.tanh)
                                         for s in self.model.state_size ]
            self.transformedInputFeatures = rnn.LSTMStateTuple(*self.transformedInputFeatures)
            if alwaysProvideInput:
                self.alwaysProvidedInput = tf.layers.dense(inputs = inputFeatures,
                                                      units = numberOfUnits,
                                                      activation = tf.nn.tanh)
            else: self.alwaysProvidedInput = None
        else:
            self.transformedInputFeatures = None
            self.alwaysProvidedInput = None

        # Unrolls some number of steps maximumLength
        self.inputPlaceholder = tf.placeholder(tf.int32, shape = [None,maximumLength],name = 'INPUT')
        embeddedInputs = tf.nn.embedding_lookup(tf.transpose(self.loadingMatrix),self.inputPlaceholder)
        if alwaysProvideInput:
            # alwaysProvidedInput: [None,numberOfUnits]
            # we want to duplicate it along the time axis to get [None,numberOfTimesSteps,numberOfUnits]
            alwaysProvidedInput2 = tf.reshape(self.alwaysProvidedInput,[-1,1,numberOfUnits])
            alwaysProvidedInput3 = tf.tile(alwaysProvidedInput2, [1,maximumLength,1])
            embeddedInputs = embeddedInputs + alwaysProvidedInput3
        self.outputs, self.states = tf.nn.dynamic_rnn(self.model,
                                                      inputs = embeddedInputs,
                                                      dtype = tf.float32,
                                                      sequence_length = self.lengthPlaceholder,
                                                      initial_state = self.transformedInputFeatures)
        # projectedOutputs: None x timeSteps x dictionarySize
        projectedOutputs = tf.tensordot(self.outputs, self.loadingMatrix, axes = [[2],[0]])
        self.outputDistribution = tf.nn.log_softmax(projectedOutputs)
        self.hardOutputs = tf.cast(tf.argmax(projectedOutputs,dimension = 2),tf.int32)

        # A small graph for running the recurrence network forward one step
        self.statePlaceholders = [ tf.placeholder(tf.float32, [None,numberOfUnits], name = 'state0'),
                                   tf.placeholder(tf.float32, [None,numberOfUnits], name = 'state1')]
        self.oneInputPlaceholder = tf.placeholder(tf.int32, shape = [None], name = 'inputForOneStep')
        projectedInputs = tf.nn.embedding_lookup(tf.transpose(self.loadingMatrix),self.oneInputPlaceholder)
        if alwaysProvideInput: projectedInputs = projectedInputs + self.alwaysProvidedInput
        self.oneOutput, self.oneNewState = self.model(projectedInputs,
                                                      rnn.LSTMStateTuple(*self.statePlaceholders))
        self.oneNewState = [self.oneNewState[0],self.oneNewState[1]]
        self.oneOutputDistribution = tf.nn.log_softmax(tf.matmul(self.oneOutput, self.loadingMatrix))




    # sequence prediction model with prediction fed into input
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号