本文整理汇总了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)
示例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()
示例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'})
示例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()
示例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
示例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
示例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))
示例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
示例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
示例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()
示例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)))
示例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
示例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)))
示例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
示例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)