示例#1
0
def main(args):
    dbh = SQLHelper("database.sqlite")
    rbm = RBM(visibleSize=784, hiddenSize=500)

    cur = dbh.query("select * from weight where current=1;")
    if len(cur) > 0:
        if cur[0]['weight'] is not None:
            params = cur[0]['hiddenBias'], cur[0]['visibleBias'], cur[0][
                'weight']
            rbm.setParams(params)

    if len(sys.argv) > 1:
        for arg in sys.argv:
            if arg == '-t':
                testSet = getTestSet(digits=[2])
                if VISUALIZE:
                    # n = X[0] > np.random.rand(*X[0].shape)
                    # n = n.reshape(28,28)
                    n = testSet[0].reshape(28, 28)
                    strline = ""
                    for l in n:
                        for b in l:
                            if b:
                                strline += "*"
                            else:
                                strline += " "
                        strline += "\n"
                    print strline
                    sys.exit()
                rbm.trainNetwork(testSet, dbh, 100)
                # for i in xrange(10):
                # 	testSet =  getTestSet(digits=[i])
                # 	if VISUALIZE:
                # 		n = X[0] > np.random.rand(*X[0].shape)
                # 		n = n.reshape(28,28)
                # 		strline = ""
                # 		for l in n:
                # 			for b in l:
                # 				if b:
                # 					strline += "*"
                # 				else:
                # 					strline += " "
                # 			strline += "\n"
                # 		print strline
                # 		sys.exit()
                # 	rbm.trainNetwork(testSet, dbh, 100)
                return
            if arg == '-w':
                #sqlite reg 830
                i = 0
                for weight in rbm.Weights:
                    retval = rbm.sigmoid(weight.copy())
                    saveImage((retval * 255),
                              "images/weight-" + str(i) + ".png")
                    i += 1
                #testSet =  getTestSet(randint(0,9))
                for i in range(0, 10):
                    testSet = getTestSet(digits=[i],
                                         Bernoulli=False,
                                         dataSet='testing')
                    retval = testSet[random.randint(0, len(testSet))].copy()
                    saveImage(retval, "images/original-" + str(i) + ".png")
                    retval = np.array((np.random.rand(*retval.shape) < retval),
                                      dtype=float)
                    retval = rbm.check(retval)
                    retval = retval.reshape(rbm.visibleLayerSize)
                    saveImage((255 * retval),
                              "images/result-" + str(i) + ".png")
                return
    return 0