def testRBM(opts): """show how to use RBM to do classification""" # read data data = np.load(opts.feature) label = np.load(opts.label) # set the nodes of hidden layers nHid = 1000 # shuffle data and label [data, label] = util.shuffle(data, label) # decide how many samples to be used as training set percent = float(opts.trainPercent) nCase = data.shape[0] nTrain = int(nCase * percent) nTest = nCase - nTrain # split data and label into train dataset and test dataset trainData = data[0:nTrain, :] trainLabel = label[0:nTrain, :] example = data[nTrain:, :] testLabel = label[nTrain:, :] p = {"maxEpoch": opts.maxEpoch} m = rbmFit.rbmFit(trainData, nHid, trainLabel, isSaveModel=True, name=opts.model, **p) [trainR, F1] = rbmPredict.rbmPredict(m, trainData) [testR, F2] = rbmPredict.rbmPredict(m, example) trainK = 0 for x in range(nTrain): if trainLabel[x] != trainR[x]: trainK = trainK + 1 testK = 0 for x in range(nTest): if testLabel[x] != testR[x]: testK = testK + 1 print "---------------------------------------" print "train classification rate : %f " % (1 - trainK * 1.0 / nTrain) print "test classification rate : %f " % (1 - testK * 1.0 / nTest) print "---------------------------------------" if options.isSaveResult: result = shelve.open(options.resultName) result["nHid"] = nHid result["maxEpoch"] = options.maxEpoch result["trainAcc"] = 1 - trainK * 1.0 / nTrain result["testAcc"] = 1 - testK * 1.0 / nTest result.close()
def testRBM(opts) : """show how to use RBM to do classification""" # read data data = np.load(opts.feature) label = np.load(opts.label) # set the nodes of hidden layers nHid = 1000 # shuffle data and label [data, label] = util.shuffle(data, label) # decide how many samples to be used as training set percent = float(opts.trainPercent) nCase = data.shape[0] nTrain = int(nCase * percent) nTest = nCase - nTrain # split data and label into train dataset and test dataset trainData = data[0:nTrain, :] trainLabel = label[0:nTrain, :] example = data[nTrain:, :] testLabel = label[nTrain:, :] p = {"maxEpoch" : opts.maxEpoch} m = rbmFit.rbmFit(trainData, nHid, trainLabel, isSaveModel=True, name=opts.model, **p) [trainR, F1] = rbmPredict.rbmPredict(m, trainData) [testR, F2] = rbmPredict.rbmPredict(m, example) trainK = 0 for x in range(nTrain) : if trainLabel[x] != trainR[x] : trainK = trainK + 1 testK = 0 for x in range(nTest) : if testLabel[x] != testR[x] : testK = testK+1 print "---------------------------------------" print "train classification rate : %f " % (1-trainK*1.0/nTrain) print "test classification rate : %f " % (1-testK*1.0/nTest) print "---------------------------------------" if options.isSaveResult : result = shelve.open(options.resultName) result["nHid"] = nHid result["maxEpoch"] = options.maxEpoch result["trainAcc"] = 1-trainK*1.0/nTrain result["testAcc"] = 1-testK*1.0/nTest result.close()
def multiModalityDBNFit(X, label, numHid, numJoint, isSaveModel=True, modelName = None, **kwargs) : """"multi-modality DBN fitting X : input data for all modalities lable : the label of each sample numHid : the node of each hidden layer of each modality and must be a two-dimension array numJoint : the node of joint layer(if there is only one modality, numJoint can be seen as the node of last hidden layer) for example : multiModalityDBN([img, txt], label, [[300,400], [200,300]])""" # cal the total number of modality N = len(X) if N != len(numHid) : print "the X and numHid must have the same length" sys.exit() models = list() # train all modalities for index in range(N) : string = "modality" + str(index+1) # here isSingleDBN must be set to False m = DBNFit.DBNFit(X[index], label, numHid[index], isSaveModels=False, name = "./DBN.npy", \ isSingleDBN = False, **kwargs[string]) models.append(m) # train the joint layer # concatenate all modalities data # nDim = 0 # nCase = label.size # for index in range(N) : # nDim = nDim + numHid[index][-1] # inputData = np.zeros((nCase, nDim)) for index in range(N-1) : if index == 0 : data = np.append(models[index][-1].top, models[index+1][-1].top, axis = 1) else : data = np.append(data, models[index+1][-1].top, axis = 1) string = "joint" m = rbmFit.rbmFit(data, numJoint, label, **kwargs[string]) models.append(m) if isSaveModel : models_ = np.array(models) if modelName == None : modelName = "../data/model/multi-modalityDBN.npy" np.save(modelName, models_) return models
def testRBM(opts): """show how to use RBM to do classification""" nHid = 1000 # split data and label into train dataset and test dataset trainData = np.load(opts.trainFeature) trainLabel = np.load(opts.trainLabel) testData = np.load(opts.testFeature) testLabel = np.load(opts.testLabel) nTrain = trainLabel.size nTest = testLabel.size p = {"maxEpoch": options.maxEpoch} m = rbmFit.rbmFit(trainData, nHid, trainLabel, isSaveModel=True, name=opts.model, **p) [trainR, F1] = rbmPredict.rbmPredict(m, trainData) [testR, F2] = rbmPredict.rbmPredict(m, testData) trainK = 0 for x in range(nTrain): if trainLabel[x] != trainR[x]: trainK = trainK + 1 testK = 0 for x in range(nTest): if testLabel[x] != testR[x]: testK = testK + 1 print "---------------------------------------" print "train classification rate : %f " % (1 - trainK * 1.0 / nTrain) print "test classification rate : %f " % (1 - testK * 1.0 / nTest) print "---------------------------------------" if opts.isSaveResult: result = shelve.open(options.resultName) result["nHid"] = nHid result["maxEpoch"] = options.maxEpoch result["trainAcc"] = 1 - trainK * 1.0 / nTrain result["testAcc"] = 1 - testK * 1.0 / nTest result.close()
def DBNFit(X, label, numHid, isSaveModels = True, name = "/home/cheng/DBN.npy", isSingleDBN = True, **kwargs) : """implement DBN fitting X :data(np.array) label : the label of each sample(np.array, in a row) numHid : the node of each hidden layer(list)""" H = len(numHid) m = list() nArg = len(kwargs) if H >= 2 : # train the first rbm model if nArg >= 1 : string = "layer" + str(1) model_ = rbm.rbm(X, numHid[0], **kwargs[string]) else : model_ = rbm.rbm(X, numHid[0]) m.append(model_) if isSingleDBN : for index in range(1, H-1) : if nArg >= index : string = "layer" +str(index + 1) model_ = rbm.rbm(m[index-1].top, numHid[index], **kwargs[string]) else : model_ = rbm.rbm(m[index-1].top, numHid[index]) m.append(model_) # train the last rbm model if nArg >= H : string = "layer" + str(H) model_ = rbmFit.rbmFit(m[H-2].top, numHid[H-1], label, **kwargs[string]) else : model_ = rbmFit.rbmFit(m[H-2].top, numHid[H-1], label) m.append(model_) else : for index in range(1, H) : if nArg >= index : string = "layer" +str(index + 1) model_ = rbm.rbm(m[index-1].top, numHid[index], **kwargs[string]) else : model_ = rbm.rbm(m[index-1].top, numHid[index]) m.append(model_) else : # only a single layer if isSingleDBN : if nArg >= 1 : string = "layer" + str(1) model_ = rbmFit.rbmFit(X, numHid[0], label, **kwargs[string]) else : model_ = rbmFit.rbmFit(X, numHid[0], label) m.append(model_) else : if nArg >= 1 : string = "layer" + str(1) model_ = rbm.rbm(X, numHid[0], **kwargs[string]) else : model_ = rbm.rbm(X, numHid[0]) m.append(model_) if isSaveModels : models = np.array(m) np.save(name, models) return m