def train(): data = [] print(dirname) for filename in glob.glob(os.path.join(dirname, '*.xlsx')): print("filename") print(filename) workbook = xlrd.open_workbook(filename) for i in range(0, workbook.nsheets): sheet = workbook.sheet_by_index(i) data.append(sheet.col(0)) print(data) for i in range(0, len(data)): for j in range(0, len(data[i])): data[i][j] = data[i][j].value print(data) labels = [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0]] data = np.array(data) labels = np.array(labels) # train / test split idx = np.random.rand(data.shape[0]) < 1.0 train_X, train_Y = data[idx], labels[idx] global sdamodel sdamodel = StackedAutoEncoder(dims=[50, 50], activations=['linear', 'linear'], epoch=[2000, 2000], loss='rmse', lr=0.007, batch_size=len(train_X), print_step=200) sdamodel.fit(train_X) train_X_ = sdamodel.transform(train_X) global softMaxModel softMaxModel = SoftMax() softMaxModel.train_softmax(50, 2, train_X_, train_Y, batch_size=len(train_X), epochs=500) return "success"
def fineTune(self,trainset,labelset): trainnum = len(trainset) if trainnum > 1000: trainnum = 1000 print "Trainnum = %d" %(trainnum) rbm_output = np.zeros((trainnum,self.rbm_layers[-1].hsize)) for i in range(trainnum): x = trainset[i] rbm_output[i] = self.calcRBMForward(x) #rbm_output 0,1,0,1,0,0..... MAXT,step,landa = 800,0.02,0.01 self.softmax = SoftMax(MAXT,step,landa) self.softmax.process_train(rbm_output,labelset,self.ntype) print "======== fineTune Complete ==========="
def load_dbn_param(self,dbnpath,softmaxpath): weights = cPickle.load(open(dbnpath,'rb')) vlen,hlen = 0,0 self.nlayers = len(weights) for i in range(self.nlayers): weight = weights[i] vlen,hlen = weight.shape[0],weight.shape[1] rbm = RBM(vlen,hlen) rbm.W = weight self.rbm_layers.append(rbm) print "RBM layer%d shape:%s" %(i,str(rbm.W.shape)) self.softmax = SoftMax() self.softmax.load_theta(softmaxpath) print "softmax parameter: "+str(self.softmax.theta.shape)
le = LabelEncoder() labels = le.fit_transform(data["names"]) num_classes = len(np.unique(labels)) labels = labels.reshape(-1, 1) one_hot_encoder = OneHotEncoder(categorical_features=[0]) labels = one_hot_encoder.fit_transform(labels).toarray() embeddings = np.array(data["embeddings"]) # Initialize Softmax training model arguments BATCH_SIZE = 32 EPOCHS = 20 input_shape = embeddings.shape[1] # Build sofmax classifier softmax = SoftMax(input_shape=(input_shape, ), num_classes=num_classes) model = softmax.build() # Create KFold cv = KFold(n_splits=5, random_state=42, shuffle=True) history = {'acc': [], 'val_acc': [], 'loss': [], 'val_loss': []} # Train for train_idx, valid_idx in cv.split(embeddings): X_train, X_val, y_train, y_val = embeddings[train_idx], embeddings[ valid_idx], labels[train_idx], labels[valid_idx] his = model.fit(X_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS, verbose=1, validation_data=(X_val, y_val))
class DBN: def __init__(self,nlayers,ntype): self.rbm_layers = [] self.nlayers = nlayers self.softmax_layer = None self.trainflag = False self.ntype = ntype def calcRBMForward(self,x): layerid = 1 for rbm in self.rbm_layers: x = rbm.calc_forward(x) if layerid < self.nlayers: x = rbm.sample(x) layerid += 1 return x def load_dbn_param(self,dbnpath,softmaxpath): weights = cPickle.load(open(dbnpath,'rb')) vlen,hlen = 0,0 self.nlayers = len(weights) for i in range(self.nlayers): weight = weights[i] vlen,hlen = weight.shape[0],weight.shape[1] rbm = RBM(vlen,hlen) rbm.W = weight self.rbm_layers.append(rbm) print "RBM layer%d shape:%s" %(i,str(rbm.W.shape)) self.softmax = SoftMax() self.softmax.load_theta(softmaxpath) print "softmax parameter: "+str(self.softmax.theta.shape) def pretrainRBM(self,trainset): trainv = np.mat(trainset[1]) # 1xn vlen = trainv.shape[1] trainnum = len(trainset) hlen = 500 weights = [] print "vlen = %d" %(vlen) print "Trainnum = %d" %(trainnum) for i in range(self.nlayers): rbm = RBM(vlen,hlen) T,e = 3,0.05 if i == 0: traindata = trainset else: traindata = outdata outdata = np.zeros((trainnum,hlen)) for j in range(trainnum): print "layer:%d CD sample %d..." %(i,j) trainv = np.mat(traindata[j]) rbm.train_CD(trainv,T,e) outdata[j] = np.mat(rbm.sample(rbm.calc_forward(trainv))) # 1xhlen self.rbm_layers.append(rbm) weights.append(rbm.W) vlen = hlen # hlen -= 100 dump_data("data/dbn.pkl",weights) print "========= pretrainRBM complete ===========" def fineTune(self,trainset,labelset): trainnum = len(trainset) if trainnum > 1000: trainnum = 1000 print "Trainnum = %d" %(trainnum) rbm_output = np.zeros((trainnum,self.rbm_layers[-1].hsize)) for i in range(trainnum): x = trainset[i] rbm_output[i] = self.calcRBMForward(x) #rbm_output 0,1,0,1,0,0..... MAXT,step,landa = 800,0.02,0.01 self.softmax = SoftMax(MAXT,step,landa) self.softmax.process_train(rbm_output,labelset,self.ntype) print "======== fineTune Complete ===========" def predict(self,x): rbm_output = self.calcRBMForward(x) ptype = self.softmax.predict(rbm_output) return ptype def validate(self,testset,labelset): rate = 0 testnum = len(testset) correctnum = 0 for i in range(testnum): x = testset[i] testtype = self.predict(x) orgtype = labelset[i] print "Testype:%d\tOrgtype:%d" %(testtype,orgtype) if testtype == orgtype: correctnum += 1 rate = float(correctnum)/testnum print "correctnum = %d, sumnum = %d" %(correctnum,testnum) print "Accuracy:%.2f" %(rate) return rate
import mnist import numpy as np from conv import Conv3x3 from maxpool import MaxPool2 from softmax import SoftMax # We only use the first 1k examples of each set in the interest of time. # Feel free to change this if you want. train_images = mnist.train_images()[:1000] train_labels = mnist.train_labels()[:1000] test_images = mnist.test_images()[:1000] test_labels = mnist.test_labels()[:1000] conv = Conv3x3(8) # 28x28x1 -> 26x26x8 pool = MaxPool2() # 26x26x8 -> 13x13x8 softmax = SoftMax(13 * 13 * 8, 10) # 13x13x8 -> 10 def forward(image, label): ''' Completes a forward pass of the CNN and calculates the accuracy and cross-entropy loss. - image is a 2d numpy array - label is a digit ''' # We transform the image from [0, 255] to [-0.5, 0.5] to make it easier # to work with. This is standard practice. out = conv.forward((image / 255) - 0.5) out = pool.forward(out) out = softmax.forward(out)