def setUp(self): model = Model(20) model.load('../testdata/lda_model') vocabulary = Vocabulary() vocabulary.load('../testdata/vocabulary.dat') self.multi_chain_gibbs_sampler = \ MultiChainGibbsSampler(model, vocabulary, 10, 10, 5)
def __init__(self, training_set=None, testing_set=None): Model.__init__(self, training_set, testing_set) # points that define the target function self.point1 = (random.uniform(-1, 1), random.uniform(-1, 1)) self.point2 = (random.uniform(-1, 1), random.uniform(-1, 1)) self.weights = [0., 0., 0.]
def pcajax_validate(request): if request.method == "POST": gt = GeetestLib(pc_geetest_id, pc_geetest_key) challenge = request.POST.get(gt.FN_CHALLENGE, '') validate = request.POST.get(gt.FN_VALIDATE, '') seccode = request.POST.get(gt.FN_SECCODE, '') status = request.session[gt.GT_STATUS_SESSION_KEY] user_id = request.POST.get('username') if status: result = gt.success_validate(challenge, validate, seccode, user_id) else: result = gt.failback_validate(challenge, validate, seccode) if result: teacher = Model('teacher') tdata = teacher.find("'" + request.POST.get('username') + "'") if tdata: import hashlib m = hashlib.md5() m.update(bytes(request.POST['password'], encoding='utf8')) if tdata[0]['password'] == m.hexdigest(): # 将当前登陆成功的用户信息以adminuser这个key放入到session中 request.session['teacher'] = tdata[0] result = {"status": "success"} # return redirect(reverse('teacher_index')) else: result = {"status": "fail"} else: result = {"status": "fail"} else: result = {"status": "fail"} return HttpResponse(json.dumps(result)) return HttpResponse("error")
def setUp(self): model = Model(20) model.load('../testdata/lda_model') vocabulary = Vocabulary() vocabulary.load('../testdata/vocabulary.dat') self.sparselda_gibbs_sampler = \ SparseLDAGibbsSampler(model, vocabulary, 10, 5)
class ModelEvaluatorTest(unittest.TestCase): def setUp(self): self.model = Model(20) self.model.load('../testdata/lda_model') self.vocabulary = Vocabulary() self.vocabulary.load('../testdata/vocabulary.dat') def test_compute_loglikelihood(self): doc_tokens = [ 'macbook', 'ipad', # exist in vocabulary and model 'mac os x', 'chrome', # only exist in vocabulary 'nokia', 'null' ] # inexistent document = Document(self.model.num_topics) rand = random.Random() rand.seed(0) document.parse_from_tokens(doc_tokens, rand, self.vocabulary, self.model) documents = [document, document] self.assertEqual( -14.113955684239654, model_evaluator.compute_loglikelihood(self.model, self.vocabulary, documents))
def setUp(self): self.model = Model(20) self.model.load('../testdata/lda_model') self.vocabulary = Vocabulary() self.vocabulary.load('../testdata/vocabulary.dat') self.topic_words_stat = TopicWordsStat(self.model, self.vocabulary)
def insert(request): """执行添加""" if request.POST['no'].replace(' ', '') != '': if request.POST['password'] == request.POST['repassword']: mod = Model('admin') # 获取密码并md5 import hashlib m = hashlib.md5() m.update( bytes(request.POST['password'].replace(' ', ''), encoding='utf8')) kw = { 'no': request.POST['no'].replace(' ', ''), 'password': m.hexdigest(), } if mod.save(kw) > 0: context = {"info": "添加成功!"} else: context = {"info": "添加失败"} else: context = {"info": "添加失败"} else: context = {"info": "添加失败"} return render(request, "myadmin/info.html", context)
def __init__(self, records, mixtures): Model.__init__(self, records) self.mixtures = mixtures self.mu = np.random.permutation(records)[0:mixtures] self.cov = np.ones( (mixtures, self.dimensions, self.dimensions)) * np.cov(records.T) self.theta = np.ones(mixtures) * (1 / mixtures)
def update(request, uid): """执行编辑信息""" if request.POST['password'] == request.POST['repassword']: mod = Model('teacher') # 获取密码并md5 import hashlib m = hashlib.md5() m.update(bytes(request.POST['password'], encoding='utf8')) kw = { 'no': uid, 'faculty': request.POST['faculty'], 'age': request.POST['age'], 'sex': request.POST['sex'], 'position': request.POST['position'], 'degree': request.POST['degree'], 'phone': request.POST['phone'], 'password': m.hexdigest(), } try: if mod.update(kw) > 0: context = {"info": "修改成功!"} else: context = {"info": "修改失败"} except Exception as err: print(err) context = {"info": "修改失败"} else: context = {"info": "修改失败"} return render(request, "myadmin/info.html", context)
def setUp(self): self.model = Model(20) self.model.load('../testdata/lda_model') self.vocabulary = Vocabulary() self.vocabulary.load('../testdata/vocabulary.dat') self.model_evaluator = ModelEvaluator(self.model, self.vocabulary)
def insert(request): """执行添加""" mod = Model('teacher') # 获取密码并md5 import hashlib m = hashlib.md5(b'000000') try: kw = { 'no': request.POST['no'].strip(), 'faculty': request.POST['faculty'], 'name': request.POST['name'], 'age': request.POST['age'], 'sex': request.POST['sex'], 'position': request.POST['position'], 'degree': request.POST['degree'], 'phone': request.POST['phone'], 'password': m.hexdigest(), } if mod.save(kw) > 0: context = {"info": "添加成功!"} else: context = {"info": "添加失败"} except: context = {"info": "添加失败"} return render(request, "myadmin/info.html", context)
def dologin(request): """执行登陆""" # 验证码判断 verifycode = request.session['verifycode'] code = request.POST['code'] if verifycode != code: context = {'info': '验证码错误!'} return render(request, "myadmin/login.html", context) try: # 根据登陆账号获取用户信息 user = Model('admin') user = user.find("'" + request.POST['username'] + "'") if user is not None: import hashlib m = hashlib.md5() m.update(bytes(request.POST['password'], encoding='utf8')) print(m.hexdigest()) # 校验密码是否正确 if user[0]['password'] == m.hexdigest(): # 将当前登陆成功的用户信息以adminuser这个key放入到session中 request.session['adminuser'] = user[0] return redirect(reverse('myadmin_index')) else: context = {'info': "登陆密码错误"} else: context = {'info': "此用户非后台管理账户"} except Exception as err: print(err) context = {'info': "登陆账号不存在"} return render(request, 'myadmin/login.html', context)
def delete(request, uid): """删除信息""" mod = Model('teaching') if mod.delete(id=uid) > 0: context = {"info": "删除成功"} else: context = {"info": "删除失败"} return render(request, "myadmin/info.html", context)
def index(request): data = request.session['teacher'] mod = Model('faculty') title = mod.findAll() for faculty in title: if data['faculty'] == faculty['no']: data['faculty'] = faculty['name'] data.pop('password') return render(request, "web/teacher/index.html", {'data': data})
def edit(request, uid): """加载编辑信息页面""" mod = Model('subject') ob = mod.find(no=uid) if ob is not None: context = {"user": ob} return render(request, "myadmin/subject/edit.html", context) else: context = {"info": "没有找到要修改的信息!"} return render(request, "myadmin/info.html", context)
def edit(request, uid): """加载编辑信息页面""" mod = Model('teacher') ob = mod.find("'" + str(uid) + "'") if ob is not None: context = {"teacher": ob} return render(request, "web/teacher/edit.html", context) else: context = {"info": "没有找到要修改的信息!"} return render(request, "web/info.html", context)
def edit(request, uid): """加载编辑信息页面""" mod = Model('admin') ob = mod.find(no="'" + uid + "'") print(ob) if ob is not None: context = {"user": ob} return render(request, "myadmin/vip/edit.html", context) else: context = {"info": "没有找到要修改的信息!"} return render(request, "myadmin/info.html", context)
def __init__(self, training_set=None, testing_set=None, weights=None): Model.__init__(self, training_set, testing_set) if weights is None: self.weights = np.array([[0., 0., 0.]]).T else: self.weights = weights # points that define the target function self.point1 = (random.uniform(-1, 1), random.uniform(-1, 1)) self.point2 = (random.uniform(-1, 1), random.uniform(-1, 1))
def index(request): """浏览信息""" mod = Model('subject') umod = mod.findAll() mywhere = [] # 获取、判断并封装关keyword键搜索 kw = request.GET.get("keyword", None) if kw: # 查询账户中只要含有关键字的都可以 list = [] for vo in umod: if vo['no'].find(kw) >= 0 or vo['name'].find(kw) >= 0: list.append(vo) mywhere.append("keyword=" + kw) else: list = umod # 获取、判断并封装学分score搜索条件 score = request.GET.get('score', '') if score != '': temp = [] for vo in list: if vo['score'] == float(score): temp.append(vo) mywhere.append("score=" + kw) list = temp # 执行分页处理 pIndex = int(request.GET.get("p", 1)) page = Paginator(list, 4) # 以50条每页创建分页对象 maxpages = page.num_pages # 最大页数 # 判断页数是否越界 if pIndex > maxpages: pIndex = maxpages if pIndex < 1: pIndex = 1 list2 = page.page(pIndex) # 当前页数据 # 获取页面列表信息 if maxpages < 5: plist = page.page_range # 页码数列表 else: if pIndex <= 3: plist = range(1, 6) elif pIndex >= maxpages - 3: plist = range(maxpages - 4, maxpages + 1) else: plist = range(pIndex - 2, pIndex + 3) context = {"list": list2, 'plist': plist, 'pIndex': pIndex, 'maxpages': maxpages, 'mywhere': mywhere} return render(request, "myadmin/subject/index.html", context)
def insert(request): """执行添加""" mod = Model('teaching') print(request.POST['teacher']) kw = { 'no': request.POST['teacher'], 'subject': request.POST['subject'], 'class': request.POST['class'], } if mod.save(kw) > 0: context = {"info": "添加成功!"} else: context = {"info": "添加失败"} return render(request, "myadmin/info.html", context)
def create_model(self, name, model_object, y, X, model_algo, model_params, tag=""): db = self.fetch_model_db() curr_num = len(db) + 1 name = str(curr_num) + "_" + name #init Dataset model = Model(self.model_export_dir, name, model_object, y, X, model_algo, model_params, tag) #save {name: path} to model db model_path = model.get_model_path() db[name] = model_path self.save_model_db(db) #add to live models self.live_models.append(model) return model
def insert(request): """执行添加""" mod = Model('subject') try: kw = { 'no': request.POST['no'], 'name': request.POST['name'], 'score': request.POST['score'], } if mod.save(kw) > 0: context = {"info": "添加成功!"} else: context = {"info": "添加失败"} except: context = {"info": "添加失败"} return render(request, "myadmin/info.html", context)
class TopicWordsStatTest(unittest.TestCase): def setUp(self): self.model = Model(20) self.model.load("../testdata/lda_model") self.vocabulary = Vocabulary() self.vocabulary.load("../testdata/vocabulary.dat") self.topic_words_stat = TopicWordsStat(self.model, self.vocabulary) def test_save(self): print self.topic_words_stat.save("../testdata/topic_top_words.dat", 0.8) def test_get_topic_top_words(self): print self.topic_words_stat.get_topic_top_words(0.8) def test_compute_topic_word_distribution(self): print self.topic_words_stat.compute_topic_word_distribution()
def main(args): model = Model(0) model.load(args.model_dir) vocabulary = Vocabulary() vocabulary.load(args.vocabulary) multi_chain_gibbs_sampler = MultiChainGibbsSampler(model, vocabulary, args.num_markov_chains, args.total_iterations, args.burn_in_iterations) fp = open(args.documents, 'r') for doc_str in fp.readlines(): doc_str = doc_str.decode('gbk') doc_tokens = doc_str.strip().split('\t') topic_dist = multi_chain_gibbs_sampler.infer_topics(doc_tokens) print doc_str print topic_dist fp.close()
def update(request, uid): """执行编辑信息""" mod = Model('teaching') kw = { 'no': uid, 'subject': request.POST['subject'], 'class': request.POST['class'], } try: if mod.update(kw) > 0: context = {"info": "修改成功!"} else: context = {"info": "修改失败"} except Exception as err: print(err) context = {"info": "修改失败"} return render(request, "myadmin/info.html", context)
class TopicWordsStatTest(unittest.TestCase): def setUp(self): self.model = Model(20) self.model.load('../testdata/lda_model') self.vocabulary = Vocabulary() self.vocabulary.load('../testdata/vocabulary.dat') self.topic_words_stat = TopicWordsStat(self.model, self.vocabulary) def test_save(self): print self.topic_words_stat.save('../testdata/topic_top_words.dat', 0.8) def test_get_topic_top_words(self): print self.topic_words_stat.get_topic_top_words(0.8) def test_compute_topic_word_distribution(self): print self.topic_words_stat.compute_topic_word_distribution()
class ModelEvaluatorTest(unittest.TestCase): def setUp(self): self.model = Model(20) self.model.load('../testdata/lda_model') self.vocabulary = Vocabulary() self.vocabulary.load('../testdata/vocabulary.dat') def test_compute_loglikelihood(self): doc_tokens = ['macbook', 'ipad', # exist in vocabulary and model 'mac os x', 'chrome', # only exist in vocabulary 'nokia', 'null'] # inexistent document = Document(self.model.num_topics) rand = random.Random() rand.seed(0) document.parse_from_tokens( doc_tokens, rand, self.vocabulary, self.model) documents = [document, document] self.assertEqual(-14.113955684239654, model_evaluator.compute_loglikelihood(self.model, self.vocabulary, documents))
def update(request, uid): """执行编辑信息""" mod = Model('admin') ob = mod.find("'" + uid + "'") import hashlib m = hashlib.md5() m.update(bytes(request.POST['old_password'], encoding='utf8')) if request.POST['password'] == request.POST['repassword'] and ob[0][ 'password'] == m.hexdigest(): mod = Model('admin') m = hashlib.md5() m.update(bytes(request.POST['password'], encoding='utf8')) kw = { 'no': uid, 'password': m.hexdigest(), } try: if mod.update(kw) > 0: context = {"info": "修改成功!"} else: context = {"info": "修改失败"} except Exception as err: print(err) context = {"info": "修改失败"} else: context = {"info": "修改失败"} return render(request, "myadmin/info.html", context)
def createCharRNNSequence(cls, input_melody, measureInSec=None, elementsPerMeasure=None): print(input_melody) charSequence = [noteToChord[mel['pitch']] for mel in input_melody] charSequenceInput = ''.join(charSequence) if len(charSequenceInput) < sample: charSequence = charSequenceInput * math.ceil( sample / len(charSequenceInput)) charSequence = charSequence[:sample] tf.reset_default_graph() with open(os.path.join(save_dir, 'config.pkl'), 'rb') as f: saved_args = cPickle.load(f) with open(os.path.join(save_dir, 'chars_vocab.pkl'), 'rb') as f: chars, vocab = cPickle.load(f) model = Model(saved_args, training=False) with tf.Session() as sess: tf.global_variables_initializer().run() saver = tf.train.Saver(tf.global_variables()) ckpt = tf.train.get_checkpoint_state(save_dir) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) newCharSeq = model.sample(sess, chars, vocab, n, charSequence, sample) print('newCharSeq', newCharSeq, newCharSeq[len(charSequence):]) newCharSeq = newCharSeq[len(charSequence):] newCharSeq = newCharSeq[:len(charSequenceInput)] print(charSequence, newCharSeq) newPitchs = [chordToNote[char] for char in newCharSeq] for idx, pitch in enumerate(newPitchs): input_melody[idx]['pitch'] = pitch return input_melody
def test_model(): EXPECTED_MSG = repr( TypeError( "Can't instantiate abstract class Model with abstract methods __call__, __init__" )) # nopep8 has_type_error = False try: my_model = Model() except TypeError as e: msg = e.__repr__() has_type_error = True assert has_type_error assert msg == EXPECTED_MSG
def edit(request, uid): """加载编辑信息页面""" mod = Model('teaching') ob = mod.find(no=uid) teacher = Model('teacher') subject = Model('subject') list1 = teacher.find(no=uid) list2 = subject.findAll() ob[0].update({'teacher': list1[0].get('name')}) print(ob) print(list2) if ob is not None: context = {"teaching": ob, 'subject': list2} return render(request, "myadmin/teaching/edit.html", context) else: context = {"info": "没有找到要修改的信息!"} return render(request, "myadmin/info.html", context)
def add(request): """加载添加页面""" teacher = Model('teacher') subject = Model('subject') list1 = teacher.findAll() list2 = subject.findAll() context = {"teacher": list1, 'subject': list2} return render(request, "myadmin/teaching/add.html", context)
def __init__(self, training_set=None, testing_set=None): Model.__init__(self, training_set, testing_set)