def getLstm(data): mode = data['mode'] modelType = data['type'] client_id = data['client_id'] new_lstm = glstm.gx_lstm() if mode == 'train': gmc = gx_model_ctr.GMC() gmc.ensure_path(modelType, gx_model_name, client_id) base_dir = gmc.get_base_dir() x_train, y_train = dp.getTrainData(data['data_train']) data['x_train'] = x_train data['y_train'] = y_train new_lstm.create_lstm(data) new_lstm.save_lstm(base_dir) print('------train success------') result = 'success' res = {'res_train': result} K.clear_session() if mode == 'predict': gmc = gx_model_ctr.GMC() tag, base_dir = gmc.ensure_model(modelType, gx_model_name, client_id) predictX, scaler, m = dp.getTestdata(data['predictX']) model = new_lstm.load_lstm(base_dir + 'model') testPredict = model.predict(predictX) res_predict = dp.datatransfrom(testPredict, scaler, m) res = {'res_predict': list(res_predict)} K.clear_session() return res
def predict(self, x_data, gx_model_name, modelType, client_id, model): gmc = gx_model_ctr.GMC() tag, base_dir = gmc.ensure_model(modelType, gx_model_name, client_id) ids = gx_model_name + '-' + modelType + '-' + client_id y_data = [] if ids in model: print('tag:', "model in Memory") p_label, p_acc, p_val = svm_predict(y_data, x_data, model[ids][1]) res = { 'p_acc': str(p_acc), 'p_label': str(p_label), 'p_val': str(p_val) } result = res elif tag == False: result = {'error:': 'please train model !'} else: print('tag:', "model in file") model = svm_load_model(base_dir + 'model') p_label, p_acc, p_val = svm_predict(y_data, x_data, model) res = { 'p_acc': str(p_acc), 'p_label': str(p_label), 'p_val': str(p_val) } result = res return result
def train_two(x_data,y_data,train_epoches,modelType,client_id): gmc=gx_model_ctr.GMC() gmc.ensure_path(modelType,gx_model_name,client_id) base_dir=gmc.get_base_dir() network=gx_network.GX_network() network.load_network(base_dir) loss=network.train(x_data,y_data,train_epoches) network.save_network(base_dir) ids=gx_model_name+'-'+modelType+'-'+client_id train_time=time.time() model[ids]={0:train_time,1:network} return loss
def train_one(x_data,y_data,learn_rate,layers,train_epoches,modelType,client_id): gmc=gx_model_ctr.GMC() gmc.ensure_path(modelType,gx_model_name,client_id) base_dir=gmc.get_base_dir() x_data=np.array(x_data) y_data=np.array(y_data) structs={'input_ps':x_data[0].size,'learn_rate':learn_rate,'layers':layers} get_model=getModel.Model() get_model.save_model(structs,x_data,y_data,train_epoches,base_dir,modelType,client_id,gx_model_name) train_time,network,loss,ids=get_model.get_model() model[ids]={0:train_time,1:network}#保存模型 return loss
def prdict(self, x_data, gx_model_name, modelType, client_id, model): gmc = gx_model_ctr.GMC() tag, base_dir = gmc.ensure_model(modelType, gx_model_name, client_id) ids = gx_model_name + '-' + modelType + '-' + client_id if ids in model: print('tag:', "model in Memory") res = model[ids][1].predict(x_data) result = res.tolist() elif tag == False: result = {'error:': 'please train model !'} else: print('tag:', "model in file") network = networks.GX_network() network.load_network(base_dir + 'model') res = network.predict(x_data) result = res.tolist() return result
def prdict(self, x_data, gx_model_name, modelType, client_id, Model, params): gmc = gx_model_ctr.GMC() tag, base_dir = gmc.ensure_model(modelType, gx_model_name, client_id) ids = gx_model_name + '-' + modelType + '-' + client_id if ids in Model: print('tag:', "model in Memory") ypred = Model[ids][1].predict(xgb.DMatrix(x_data)) ypred = np.array(ypred).astype(np.str) result = {"code": 200, "result": list(ypred)} elif tag == False: result = {'error:': 'please train model !'} else: print('tag:', "model in file") model = xgb.Booster(params) model.load_model(base_dir + 'model') ypred = model.predict(xgb.DMatrix(x_data)) ypred = np.array(ypred).astype(np.str) result = {"code": 200, "result": list(ypred)} return result
""" Created on Mon Nov 19 16:16:24 2018 @author: Zcy """ import os, sys sys.path = [os.path.dirname(os.path.abspath(__file__))] + sys.path import gx_svmModel import util.gx_model_ctr as gx_model_ctr import schedule import time import threading gx_model_name = 'svm' gmc = gx_model_ctr.GMC() gsm = gx_svmModel.Model() model = {} def train(data): gmc.ensure_path(data['type'], gx_model_name, data['client_id']) base_dir = gmc.get_base_dir() trainx, trainy = trans2libsvmX(data['train_data']) options = data['options'] try: gsm.save_model(trainx, trainy, options, base_dir, data['type'], data['client_id'], gx_model_name) train_time, svmModel, ids = gsm.get_model() model[ids] = {0: train_time, 1: svmModel} res = {'train': str('success')}