当前位置: 首页>>代码示例>>Python>>正文


Python model函数代码示例

本文整理汇总了Python中model函数的典型用法代码示例。如果您正苦于以下问题:Python model函数的具体用法?Python model怎么用?Python model使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了model函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _get_Q

    def _get_Q(self, model, model_input):
        model.reset_noise()

        if not self.config.use_categorical:
            return model(model_input)

        model_output = model(model_input, ApplySoftmax.NORMAL)

        return torch.sum(model_output * self.support, dim=2)
开发者ID:y-kamiya,项目名称:machine-learning-samples,代码行数:9,代码来源:agent.py

示例2: get_model_info

def get_model_info(year):
    '''Takes in a year, and prints out each model, brand_name, and brand
    headquarters for that year using only ONE database query.'''
# Query here
#SELECT model, brand_name, headquarters
	years = Model.query.get(year)

	by_year = db.session.query(Model.name, Model.brand_name, Brand.headquarters).all()

	for year in year:
		print out each model (Model) 
				brand_name (foreign key from Brand to Model)
				headquarters (Brand)
				.all()
开发者ID:kelly4strength,项目名称:skills-sqlalchemy,代码行数:14,代码来源:query.py

示例3: get_stats

def get_stats():

    if 'url' in request.form :

        api_model = model()
        return api_model.get_url_stats(request.form['url'])
    return jsonify({'message': errors.HACK, 'response': {}, 'status': '0'})
开发者ID:goors,项目名称:flask-url-shortener,代码行数:7,代码来源:api.py

示例4: train

def train():
    # Turn on training mode which enables dropout.
    model.train()
    total_loss = 0
    start_time = time.time()
    ntokens = len(corpus.dictionary)
    hidden = model.init_hidden(args.batch_size)
    for batch, i in enumerate(range(0, train_data.size(0) - 1, args.bptt)):
        data, targets = get_batch(train_data, i)
        # Starting each batch, we detach the hidden state from how it was previously produced.
        # If we didn't, the model would try backpropagating all the way to start of the dataset.
        hidden = repackage_hidden(hidden)
        model.zero_grad()
        output, hidden = model(data, hidden)
        loss = criterion(output.view(-1, ntokens), targets)
        loss.backward()

        # `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
        torch.nn.utils.clip_grad_norm(model.parameters(), args.clip)
        for p in model.parameters():
            p.data.add_(-lr, p.grad.data)

        total_loss += loss.data

        if batch % args.log_interval == 0 and batch > 0:
            cur_loss = total_loss[0] / args.log_interval
            elapsed = time.time() - start_time
            print('| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2f} | ms/batch {:5.2f} | '
                    'loss {:5.2f} | ppl {:8.2f}'.format(
                epoch, batch, len(train_data) // args.bptt, lr,
                elapsed * 1000 / args.log_interval, cur_loss, math.exp(cur_loss)))
            total_loss = 0
            start_time = time.time()
开发者ID:Camuslu,项目名称:examples,代码行数:33,代码来源:main.py

示例5: get_orders

    def get_orders(self, sql, model=model.Model_OrderSub):
        results = self.get_all(sql)
        model_list = []

        if not results:
            return []
        for row in results:
            o_model = model()
            o_model.order_id = str(row[0])
            o_model.uid = str(row[1])
            o_model.account = str(row[2])
            o_model.p_info = str(row[3])
            o_model.depart_date = str(row[4])
            o_model.train_no = str(row[5])
            o_model.depart_name = str(row[6])
            o_model.arrive_name = str(row[7])
            o_model.name = str(row[8])
            o_model.card_type = str(row[9])
            o_model.card_no = str(row[10])
            o_model.phone = str(row[11])
            o_model.seat_name = str(row[12])
            o_model.ticket_type = str(row[13])
            o_model.status = str(row[14])
            o_model.price = str(row[15])
            o_model.create_time = str(row[16])

            model_list.append(o_model)
        return model_list
开发者ID:744996162,项目名称:warehouse,代码行数:28,代码来源:source_gt_uid3.py

示例6: dev

def dev(model, dev_loader, decoder, logger):
    model.eval()
    total_cer = 0
    total_tokens = 0

    for data in dev_loader:
        inputs, targets, input_sizes, input_sizes_list, target_sizes =data
        batch_size = inputs.size(1)
        inputs = inputs.transpose(0, 1)
        
        inputs = Variable(inputs, volatile=True, requires_grad=False)

        if USE_CUDA:
            inputs = inputs.cuda()

        inputs = nn.utils.rnn.pack_padded_sequence(inputs, input_sizes_list)
        probs = model(inputs)
        
        probs = probs.data.cpu()
        if decoder.space_idx == -1:
            total_cer += decoder.phone_word_error(probs, input_sizes_list, targets, target_sizes)[1]
        else:
            total_cer += decoder.phone_word_error(probs, input_sizes_list, targets, target_sizes)[0]
        total_tokens += sum(target_sizes)
    acc = 1 - float(total_cer) / total_tokens
    return acc*100
开发者ID:MrGo2008,项目名称:CTC_pytorch,代码行数:26,代码来源:lstm_ctc.py

示例7: test

def test(model, quesfeaShu, labelShu, lengthShu):

    model.eval()

    idx = sorted(range(len(lengthShu)), key=lambda x: lengthShu[x], reverse=True)

    _quesfeaShu = []
    _labelShu = []
    _lengthShu = []

    for j in range(len(idx)):
        _quesfeaShu.append(quesfeaShu[idx[j]])
        _labelShu.append(labelShu[idx[j]])
        _lengthShu.append(lengthShu[idx[j]])

    questrainarray = np.asarray(_quesfeaShu)
    labeltrainarray = np.asarray(_labelShu)
    lengthtrainarray = np.asarray(_lengthShu)

    tmp = [questrainarray, labeltrainarray, lengthtrainarray]
    tmp = [Variable(torch.from_numpy(_), requires_grad=False) for _ in tmp]
    trques, trlabel, length = tmp
    if args.cuda:
        trlabel.cuda()
    output = model(trques, length)
    # st(context=27)
    print("precesion 1 : %s" % accuracy(output.data, trlabel.data, topk=(1,), ori_label=labeltrainarray))
开发者ID:xiabofei,项目名称:python_details,代码行数:27,代码来源:main.py

示例8: model

def model(model_name, decorator=[]):
    assert isinstance(model_name, (str, unicode))
    cache_key = model_name
    assert not decorator or config.IS_TEST, 'decorator仅能用于测试环境'

    if not decorator and CACHED_MODELS.has_key(cache_key):
        return CACHED_MODELS[cache_key]
    else:
        # 此import语句不能放到model函数外面去
        # 否则会与model中的import site_helper形成互相依赖, 导致死循环
        import model
        import modeldecorator 
        try:
            for name in model_name.split('.'):
                assert(hasattr(model, name))
                model = getattr(model, name)
            model = model()
        except:
            print 'the name is', name
            print 'the model name is', model_name
            raise
        # 仅在非测试时使用model.decorator
        decorator = model.decorator if not config.IS_TEST else decorator
        # 测试时强行使用test_decorator
        if config.IS_TEST and hasattr(model, 'test_decorator'):
            assert decorator == [], u'使用test_decorator时,不再允许指定decorator'
            decorator = model.test_decorator
        # 装饰decorator
        for d,arguments in decorator:
            model = getattr(modeldecorator, d)(model, arguments)
        if not decorator:
            CACHED_MODELS[cache_key] = model
        return model
开发者ID:duoduo369,项目名称:zarkpy,代码行数:33,代码来源:site_helper.py

示例9: get_or_create

def get_or_create(session, model, **kwargs):
    instance = session.query(model).filter_by(**kwargs).first()
    if instance:
        return instance, False
    else:
        instance = model(**kwargs)
        session.add(instance)
        return instance, True
开发者ID:jgraham,项目名称:wptreport,代码行数:8,代码来源:dbhandler.py

示例10: initialization

	def initialization(self):

		self.link = os.popen('echo $CONV_ROOT').read()
		self.link = self.link[:-1]
		print(self.link)

		P = model()
		P.initialization()
		P.modelAdaptation()
开发者ID:ybouret,项目名称:convivial2,代码行数:9,代码来源:step.py

示例11: setSiteConfig

def setSiteConfig(name, value):
    conf_model = model('SiteConfig')
    assert(name.strip())
    exists = conf_model.getOneByWhere('name=%s', [name])
    if exists:
        conf_model.update(exists.id, dict(value=str(value)))
        return exists.id
    else:
        return conf_model.insert(dict(name=name, value=str(value)))
开发者ID:shaqhuang,项目名称:zarkpy,代码行数:9,代码来源:site_helper.py

示例12: de

def de(model, baseline_min,baseline_max, max = 100, f = 0.75, cf = 0.3, epsilon = 0.01):
	curr_candidate_sol = model()
	# print "FROM DE-->", curr_candidate_sol
	np = curr_candidate_sol.numOfDec * 10
	frontier = [candidate(curr_candidate_sol) for _ in xrange(np)]

	# for x in frontier:
	# 	print "id:", x.id, " have:", x.have, " score:", x.score

	# print "length of frontier:", len(frontier)

	# Pending : should you use else if here?

	for each_thing in frontier:
		if(each_thing.score < 0):
			BaseLine.baseline_min = 0
			print "--------"
		if(each_thing.score < BaseLine.baseline_min):
			BaseLine.baseline_min = each_thing.score
			print "--------------"
		if(each_thing.score > BaseLine.baseline_max):
			BaseLine.baseline_max = each_thing.score
			print "---------"



	#Normalize the scores of each thing now

	# for each_thing in frontier:
	# 	prev_each_thing_score = each_thing.score
	# 	each_thing.score = float(each_thing.score - BaseLine.baseline_min)/(BaseLine.baseline_max - BaseLine.baseline_min)
	
	#total = total score of all the candidates found so far
	for k in xrange(max):
		total,n = update(f,cf,frontier,curr_candidate_sol,BaseLine.baseline_min,BaseLine.baseline_max)
		# print "BASELINE: MIN=", BaseLine.baseline_min," MAX=", BaseLine.baseline_max
		# if total/n > (1 - epsilon):
		# 	print "break: value of k=", k, " total=",total, "n=",n 
		# 	break
	# for x in frontier:
	# 	print "print --x:",x.id," ",x.have, x.score

	#Now baseline everything again 

	for each_thing in frontier:
		each_thing.score = (each_thing.score - BaseLine.baseline_min) / ( BaseLine.baseline_max - BaseLine.baseline_min + 0.001)

	score_have_dict = { obj.score:obj.have for obj in frontier}
	print "==================="
	# for key in sorted(score_have_dict.keys(),reverse = True):
 #  		print "%s: %s" % (key, score_have_dict[key])

	print "BASELINE: MIN=", BaseLine.baseline_min," MAX=", BaseLine.baseline_max
  	sorted_keys = sorted(score_have_dict.keys(),reverse = True)
  	print "%s: %s" % (sorted_keys[0], score_have_dict[sorted_keys[0]])
	
	return frontier
开发者ID:manish211,项目名称:59115ASE,代码行数:57,代码来源:code7.py

示例13: setSiteConfig

def setSiteConfig(name, value):
    conf_model = model('SiteConfig')
    assert(name.strip())
    exists = conf_model.getOneByWhere('name=%s', name)
    if isinstance(value, unicode): value = unicodeToStr(value)
    if exists:
        conf_model.update(exists.id, dict(value=str(value)))
        return exists.id
    else:
        return conf_model.insert(dict(name=name, value=str(value)))
开发者ID:duoduo369,项目名称:zarkpy,代码行数:10,代码来源:site_helper.py

示例14: getOrCreate

	def getOrCreate(self, model, defaults=None, **kwargs):
		instance = self.session.query(model).filter_by(**kwargs).first()

		if instance:
			return instance	#, False
		else:
			params = dict((k, v) for k, v in kwargs.iteritems() if not isinstance(v, ClauseElement))
			instance = model(**params)
			self.session.add(instance)
			return instance	#, True
开发者ID:faliev,项目名称:clinical-trials,代码行数:10,代码来源:db.py

示例15: evaluate

def evaluate(data_source, batch_size=10, window=args.window):
    # Turn on evaluation mode which disables dropout.
    if args.model == 'QRNN': model.reset()
    model.eval()
    total_loss = 0
    ntokens = len(corpus.dictionary)
    hidden = model.init_hidden(batch_size)
    next_word_history = None
    pointer_history = None
    for i in range(0, data_source.size(0) - 1, args.bptt):
        if i > 0: print(i, len(data_source), math.exp(total_loss / i))
        data, targets = get_batch(data_source, i, evaluation=True, args=args)
        output, hidden, rnn_outs, _ = model(data, hidden, return_h=True)
        rnn_out = rnn_outs[-1].squeeze()
        output_flat = output.view(-1, ntokens)
        ###
        # Fill pointer history
        start_idx = len(next_word_history) if next_word_history is not None else 0
        next_word_history = torch.cat([one_hot(t.data[0], ntokens) for t in targets]) if next_word_history is None else torch.cat([next_word_history, torch.cat([one_hot(t.data[0], ntokens) for t in targets])])
        #print(next_word_history)
        pointer_history = Variable(rnn_out.data) if pointer_history is None else torch.cat([pointer_history, Variable(rnn_out.data)], dim=0)
        #print(pointer_history)
        ###
        # Built-in cross entropy
        # total_loss += len(data) * criterion(output_flat, targets).data[0]
        ###
        # Manual cross entropy
        # softmax_output_flat = torch.nn.functional.softmax(output_flat)
        # soft = torch.gather(softmax_output_flat, dim=1, index=targets.view(-1, 1))
        # entropy = -torch.log(soft)
        # total_loss += len(data) * entropy.mean().data[0]
        ###
        # Pointer manual cross entropy
        loss = 0
        softmax_output_flat = torch.nn.functional.softmax(output_flat)
        for idx, vocab_loss in enumerate(softmax_output_flat):
            p = vocab_loss
            if start_idx + idx > window:
                valid_next_word = next_word_history[start_idx + idx - window:start_idx + idx]
                valid_pointer_history = pointer_history[start_idx + idx - window:start_idx + idx]
                logits = torch.mv(valid_pointer_history, rnn_out[idx])
                theta = args.theta
                ptr_attn = torch.nn.functional.softmax(theta * logits).view(-1, 1)
                ptr_dist = (ptr_attn.expand_as(valid_next_word) * valid_next_word).sum(0).squeeze()
                lambdah = args.lambdasm
                p = lambdah * ptr_dist + (1 - lambdah) * vocab_loss
            ###
            target_loss = p[targets[idx].data]
            loss += (-torch.log(target_loss)).data[0]
        total_loss += loss / batch_size
        ###
        hidden = repackage_hidden(hidden)
        next_word_history = next_word_history[-window:]
        pointer_history = pointer_history[-window:]
    return total_loss / len(data_source)
开发者ID:batermj,项目名称:awd-lstm-lm,代码行数:55,代码来源:pointer.py


注:本文中的model函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。