def buildVae(self):
     self.vae_mean, self.vae_SE_ln = oneLayerBidirectionalLstmEncoder(self.placeHolder_samples, \
                                                                      self.placeHolder_seqLens, \
                                                                      self.hiddenDim_vae_encoder, \
                                                                      self.hiddenDim_vae_z)
     
     self.Z = vaeSampler(self.vae_mean, self.vae_SE_ln, self.placeHolder_noises)
     self.reconstruction_vae = oneLayerBidirectionalLstmDecoder(self.Z, self.placeHolder_seqLens, self.hiddenDim_vae_decoder, self.reconstructionTargetDim)
 def buildVae(self):
     self.vae_mean, self.vae_SE_ln = oneLayerBidirectionalLstmEncoder(self.placeHolder_samples, \
                                                                      self.placeHolder_seqLens, \
                                                                      self.hiddenDim_vae_encoder, \
                                                                      self.hiddenDim_vae_z)
     
     self.Z = vaeSampler(self.vae_mean, self.vae_SE_ln, self.placeHolder_noises)
     self.reconstruction_vae = oneLayerBidirectionalLstmDecoder(self.Z, self.placeHolder_seqLens, self.hiddenDim_vae_decoder, self.reconstructionTargetDim)
 
     _, self.reconstruction_splited = tf.split(self.reconstruction_vae, [1, self.segLen_max - 1], 1)
     _, self.Z_split = tf.split(self.Z, [1, self.segLen_max - 1], 1)
Example #3
0
    def buildNet(self):
        #BaseNetwork.buildNet(self)
        self.placeHolder_samples = tf.placeholder(
            "float", [None, self.segLen_max, self.sampleDim])
        self.placeHolder_targets_reconstruction = tf.placeholder(
            "float", [None, self.segLen_max, self.reconstructionTargetDim])
        self.placeHolder_noises = tf.placeholder(
            "float", [None, self.segLen_max, self.hiddenDim_vae_z])

        self.placeHolder_seqLens = tf.placeholder(tf.int64, [None])
        self.placeHoder_learningRate = tf.placeholder("float", [])
        self.mask = tf.sequence_mask(self.placeHolder_seqLens,
                                     self.segLen_max,
                                     dtype=tf.float32)

        self.placeHolder_labels = tf.placeholder(tf.int64, [None])
        #self.vectorizedLabels = tf.one_hot(self.placeHolder_labels, self.classNumAll)

        self.vectorizedLabels = tf.reshape(
            tf.one_hot(self.placeHolder_labels, self.classNumAll),
            [-1, self.classNumAll])


        self.vae_mean, self.vae_SE_ln = oneLayerBidirectionalLstmEncoder(self.placeHolder_samples, \
                                                                         self.placeHolder_seqLens, \
                                                                         self.hiddenDim_vae_encoder, \
                                                                         self.hiddenDim_vae_z)

        self.Z = vaeSampler(self.vae_mean, self.vae_SE_ln,
                            self.placeHolder_noises)
        self.reconstruction_vae = oneLayerBidirectionalLstmDecoder(
            self.Z, self.placeHolder_seqLens, self.hiddenDim_vae_decoder,
            self.reconstructionTargetDim)

        self.vae_mean_squared = tf.square(self.vae_mean,
                                          name="vae_encoder_mean_squared")
        self.vae_SE = tf.exp(self.vae_SE_ln, name="vae_encoder_SE")

        self.loss_vae, self.KLD, _ = vaeLoss_forLstm1(
            self.reconstruction_vae, self.placeHolder_targets_reconstruction,
            self.vae_mean_squared, self.vae_SE, self.vae_SE_ln)
        #_, self.loss_vae, _ = vaeLoss_forLstm(self.reconstruction_vae, self.placeHolder_targets_reconstruction, self.vae_mean_squared, self.vae_SE, self.vae_SE_ln)

        if self.mConfig.finetune is False:
            self.maskedLoss_vae = tf.multiply(self.mask, self.loss_vae)
            self.meanLoss = tf.divide(tf.reduce_sum(self.maskedLoss_vae),
                                      tf.reduce_sum(self.mask))

        else:
            self.prediction = blstmSequencePredictionNetwork(self.reconstruction_vae, self.placeHolder_seqLens, self.segLen_max, \
                                                          self.classNumAll, self.mConfig.blstmLayerNum, self.mConfig.blstmLayerNeuronNum)

            self.correctPrediction = tf.equal(
                tf.argmax(self.prediction, 1),
                tf.argmax(self.vectorizedLabels, 1))
            self.correctPredcitNum = tf.reduce_sum(
                tf.cast(self.correctPrediction, tf.int64))

            self.loss_classification = tf.nn.softmax_cross_entropy_with_logits(
                logits=self.prediction, labels=self.vectorizedLabels)
            self.meanLoss = tf.reduce_mean(self.loss_classification)

        self.optimizer = tf.train.AdamOptimizer(
            learning_rate=self.placeHoder_learningRate)

        self.params = tf.trainable_variables()

        self.variables_to_restore = None

        self.gradients = tf.gradients(self.meanLoss, self.params)
        self.clippedGradients, self.orgGradientNorm = tf.clip_by_global_norm(
            self.gradients, self.maxGradient)
        self.train_op = self.optimizer.apply_gradients(
            zip(self.clippedGradients, self.params))
        self.init = tf.global_variables_initializer()

        self.gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8)
        self.sess = tf.Session(config=tf.ConfigProto(
            gpu_options=self.gpu_options))
        self.sess.run(self.init)
        self.restore()