示例#1
0
    def train(self, trainingData):
        rbmList = []  # list RBM's weights
        tempData = trainingData
        # start RBM's training and get the respective weights
        for n in range(self.nEncoderLayers):
            if (n == 0 or n == (self.nEncoderLayers - 1)):
                rbm = RBM(tempData, self.sEncoderLayers[n], rbmType='GBRBM')
            else:
                rbm = RBM(tempData, self.sEncoderLayers[n], rbmType='BBRBM')

            print('Start %d RBM training' % (n + 1))
            rbm.train(batchSize=100)

            [weights, visBias, hidBias] = rbm.getWeights()
            rbmList.append(RBM_Weights(weights, visBias, hidBias))

            data = tf.convert_to_tensor(tempData,
                                        dtype=tf.float32,
                                        name='data')
            probHid = tf.sigmoid(tf.matmul(data, weights) + hidBias)
            hid = tf.cast(tf.greater(
                probHid,
                tf.random_uniform(tf.shape(probHid),
                                  minval=0,
                                  maxval=1,
                                  dtype=tf.float32)),
                          dtype=tf.float32)

            with tf.Session() as sess:
                if ((self.nEncoderLayers - 1) == n):
                    tempData = sess.run(probHid)
                else:
                    tempData = sess.run(hid)

        # start the fine tuning process
        return self.fineTuning(rbmList, trainingData)