python info_Python 方法代码示例

时间:2025-04-15 12:03:42

本文整理汇总了Python中方法的典型用法代码示例。如果您正苦于以下问题:Python 方法的具体用法?Python 怎么用?Python 使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在模块的用法示例。

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

示例1: end_epoch

​点赞 8

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def end_epoch(self):

"""

Finally arrived at the end of epoch (full pass on dataset).

Do some tensorboard logging and checkpoint saving.

"""

(f"{self.n_sequences_epoch} sequences have been trained during this epoch.")

if self.is_master:

self.save_checkpoint(checkpoint_name=f"model_epoch_{}.pth")

.add_scalar(

tag="epoch/loss", scalar_value=self.total_loss_epoch / self.n_iter, global_step=

)

+= 1

self.n_sequences_epoch = 0

self.n_iter = 0

self.total_loss_epoch = 0

开发者ID:monologg,项目名称:DistilKoBERT,代码行数:19,

示例2: __init__

​点赞 6

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def __init__(self, args):

super(CEMLearner, self).__init__(args)

policy_conf = {'name': 'local_learning_{}'.format(self.actor_id),

'input_shape': self.input_shape,

'num_act': self.num_actions,

'args': args}

self.local_network = (policy_conf)

self.num_params = ([

(v.get_shape().as_list())

for v in self.local_network.params])

('Parameter count: {}'.format(self.num_params))

= (self.num_params)

= (self.num_params)

self.num_samples = args.episodes_per_batch

self.num_epochs = args.num_epochs

if self.is_master():

var_list = self.local_network.params

= (var_list=var_list, max_to_keep=3,

keep_checkpoint_every_n_hours=2)

开发者ID:steveKapturowski,项目名称:tensorflow-rl,代码行数:25,

示例3: _add_archives

​点赞 6

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def _add_archives(self, collection):

("%s " % "article", False)

result = ""

archives = list(({}))

page_count = len(archives) / 10 + 1

result += self._add_one(

"archives",

()

)

for index in xrange(page_count):

result += self._add_one(

"%s/%d" % ("archives", index),

()

)

for article in archives:

result += self._add_one(

"%s/%s" % ("article", article["slug"]),

(article["date"], "%Y.%m.%d %H:%M")

)

return result

开发者ID:dtysky,项目名称:BlogReworkPro,代码行数:22,

示例4: generate

​点赞 6

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def generate(self):

("Sitemap: Writing start...")

with open(config["sitemap_path"], "w") as f:

(template["begin"])

(self._add_static())

("Sitemap: Writing: ")

for url in ["tag", "author", "category"]:

(

self._add_collection(url, self._collections[url])

)

(

self._add_archives(self._collections["article"])

)

(template["end"])

()

("Sitemap: Writing done...")

开发者ID:dtysky,项目名称:BlogReworkPro,代码行数:18,

示例5: _update_files

​点赞 6

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def _update_files(self, file_names, time):

if not (config["feeds_dir_path"]):

(config["feeds_dir_path"])

for name_pair in file_names:

name, view = name_pair["slug"].encode("utf-8"), name_pair["view"].encode("utf-8")

if name not in self._files:

file_name = "%s/%" % (

config["feeds_dir_path"],

name

)

self._files[name] = open(file_name, "w")

self._files[name].write(

template["begin"].format(

config["site_title"],

config["site_url"],

config["site_description"],

"%s/%s" % (

config["site_url"],

file_name

),

time

)

)

("'%s' " % view, False)

开发者ID:dtysky,项目名称:BlogReworkPro,代码行数:26,

示例6: write

​点赞 6

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def write(self, file_path, mode="delete", page=None):

("Writing start: %s" % file_path)

self._file_path = file_path

if mode != "delete" and page == None:

self._error("Mode is not 'delete', argument 'page' is required !")

if mode == "update":

if self._articles.find_one(

{

"file": file_path

}

):

self._update(file_path, page)

else:

self._insert(page)

elif mode == "delete":

self._delete(file_path)

else:

self._error("Unexpected mode '%s' !" % mode)

开发者ID:dtysky,项目名称:BlogReworkPro,代码行数:21,

示例7: __init__

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def __init__(self, name, is_train, norm='instance', activation='leaky',

image_size=128, latent_dim=8, use_resnet=True):

('Init Encoder %s', name)

= name

self._is_train = is_train

self._norm = norm

self._activation = activation

self._reuse = False

self._image_size = image_size

self._latent_dim = latent_dim

self._use_resnet = use_resnet

开发者ID:clvrai,项目名称:BicycleGAN-Tensorflow,代码行数:13,

示例8: __init__

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def __init__(self, name, is_train, norm='instance', activation='leaky', image_size=128):

('Init Discriminator %s', name)

= name

self._is_train = is_train

self._norm = norm

self._activation = activation

self._reuse = False

self._image_size = image_size

开发者ID:clvrai,项目名称:BicycleGAN-Tensorflow,代码行数:10,

示例9: __init__

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def __init__(self, name, is_train, norm='batch', image_size=128):

('Init Generator %s', name)

= name

self._is_train = is_train

self._norm = norm

self._reuse = False

self._image_size = image_size

开发者ID:clvrai,项目名称:BicycleGAN-Tensorflow,代码行数:9,

示例10: __init__

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def __init__(self, name, is_train, norm='instance', activation='leaky'):

('Init Discriminator %s', name)

= name

self._is_train = is_train

self._norm = norm

self._activation = activation

self._reuse = False

开发者ID:clvrai,项目名称:CycleGAN-Tensorflow,代码行数:9,

示例11: __init__

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def __init__(self, name, is_train, norm='instance', activation='relu',

image_size=128):

('Init Generator %s', name)

= name

self._is_train = is_train

self._norm = norm

self._activation = activation

self._num_res_block = 6 if image_size == 128 else 9

self._reuse = False

开发者ID:clvrai,项目名称:CycleGAN-Tensorflow,代码行数:11,

示例12: remove_long_sequences

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def remove_long_sequences(self):

"""

Sequences that are too long are splitted by chunk of max_model_input_size.

"""

max_len = .max_model_input_size

indices = > max_len

(f"Splitting {sum(indices)} too long sequences.")

def divide_chunks(l, n):

return [l[i : i + n] for i in range(0, len(l), n)]

new_tok_ids = []

new_lengths = []

if :

cls_id, sep_id = .special_tok_ids["cls_token"], .special_tok_ids["sep_token"]

else:

cls_id, sep_id = .special_tok_ids["bos_token"], .special_tok_ids["eos_token"]

for seq_, len_ in zip(self.token_ids, ):

assert (seq_[0] == cls_id) and (seq_[-1] == sep_id), seq_

if len_ <= max_len:

new_tok_ids.append(seq_)

new_lengths.append(len_)

else:

sub_seqs = []

for sub_s in divide_chunks(seq_, max_len - 2):

if sub_s[0] != cls_id:

sub_s = (sub_s, 0, cls_id)

if sub_s[-1] != sep_id:

sub_s = (sub_s, len(sub_s), sep_id)

assert len(sub_s) <= max_len

assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s

sub_seqs.append(sub_s)

new_tok_ids.extend(sub_seqs)

new_lengths.extend([len(l) for l in sub_seqs])

self.token_ids = (new_tok_ids)

= (new_lengths)

开发者ID:bhoov,项目名称:exbert,代码行数:41,

示例13: remove_empty_sequences

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def remove_empty_sequences(self):

"""

Too short sequences are simply removed. This could be tunedd.

"""

init_size = len(self)

indices = > 11

self.token_ids = self.token_ids[indices]

= [indices]

new_size = len(self)

(f"Remove {init_size - new_size} too short (<=11 tokens) sequences.")

开发者ID:bhoov,项目名称:exbert,代码行数:12,

示例14: remove_unknown_sequences

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def remove_unknown_sequences(self):

"""

Remove sequences with a (too) high level of unknown tokens.

"""

if "unk_token" not in .special_tok_ids:

return

else:

unk_token_id = .special_tok_ids["unk_token"]

init_size = len(self)

unk_occs = ([np.count_nonzero(a == unk_token_id) for a in self.token_ids])

indices = (unk_occs / ) < 0.5

self.token_ids = self.token_ids[indices]

= [indices]

new_size = len(self)

(f"Remove {init_size - new_size} sequences with a high level of unknown tokens (50%).")

开发者ID:bhoov,项目名称:exbert,代码行数:17,

示例15: print_statistics

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def print_statistics(self):

"""

Print some statistics on the corpus. Only the master process.

"""

if not .is_master:

return

(f"{len(self)} sequences")

# data_len = sum()

# nb_unique_tokens = len(Counter(list(chain(*self.token_ids))))

# (f'{data_len} tokens ({nb_unique_tokens} unique)')

# unk_idx = .special_tok_ids['unk_token']

# nb_unkown = sum([(t==unk_idx).sum() for t in self.token_ids])

# (f'{nb_unkown} unknown tokens (covering {100*nb_unkown/data_len:.2f}% of the data)')

开发者ID:bhoov,项目名称:exbert,代码行数:16,

示例16: train

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def train(self):

"""

The real training loop.

"""

if self.is_master:

("Starting training")

self.last_log = ()

()

()

for _ in range(.n_epoch):

if self.is_master:

(f"--- Starting epoch {}/{.n_epoch-1}")

if self.multi_gpu:

()

iter_bar = tqdm(, desc="-Iter", disable=.local_rank not in [-1, 0])

for batch in iter_bar:

if .n_gpu > 0:

batch = tuple((f"cuda:{.local_rank}") for t in batch)

if :

token_ids, attn_mask, lm_labels = self.prepare_batch_mlm(batch=batch)

else:

token_ids, attn_mask, lm_labels = self.prepare_batch_clm(batch=batch)

(input_ids=token_ids, attention_mask=attn_mask, lm_labels=lm_labels)

iter_bar.update()

iter_bar.set_postfix(

{"Last_loss": f"{self.last_loss:.2f}", "Avg_cum_loss": f"{self.total_loss_epoch/self.n_iter:.2f}"}

)

iter_bar.close()

if self.is_master:

(f"--- Ending epoch {}/{.n_epoch-1}")

self.end_epoch()

if self.is_master:

(f"Save very last checkpoint as `pytorch_model.bin`.")

self.save_checkpoint(checkpoint_name=f"pytorch_model.bin")

("Training is finished")

开发者ID:bhoov,项目名称:exbert,代码行数:43,

示例17: create_lengths_groups

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def create_lengths_groups(lengths, k=0):

bins = (start=3, stop=k, step=4).tolist() if k > 0 else [10]

groups = _quantize(lengths, bins)

# count number of elements per group

counts = (groups, return_counts=True)[1]

fbins = [0] + bins + []

("Using {} as bins for aspect lengths quantization".format(fbins))

("Count of instances per bin: {}".format(counts))

return groups

开发者ID:bhoov,项目名称:exbert,代码行数:11,

示例18: remove_long_sequences

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def remove_long_sequences(self):

"""

Sequences that are too long are splitted by chunk of max_model_input_size.

"""

max_len = .max_model_input_size

indices = > max_len

(f'Splitting {sum(indices)} too long sequences.')

def divide_chunks(l, n):

return [l[i:i + n] for i in range(0, len(l), n)]

new_tok_ids = []

new_lengths = []

if :

cls_id, sep_id = .special_tok_ids['cls_token'], .special_tok_ids['sep_token']

else:

cls_id, sep_id = .special_tok_ids['bos_token'], .special_tok_ids['eos_token']

for seq_, len_ in zip(self.token_ids, ):

assert (seq_[0] == cls_id) and (seq_[-1] == sep_id), seq_

if len_ <= max_len:

new_tok_ids.append(seq_)

new_lengths.append(len_)

else:

sub_seqs = []

for sub_s in divide_chunks(seq_, max_len-2):

if sub_s[0] != cls_id:

sub_s = (sub_s, 0, cls_id)

if sub_s[-1] != sep_id:

sub_s = (sub_s, len(sub_s), sep_id)

assert len(sub_s) <= max_len

assert (sub_s[0] == cls_id) and (sub_s[-1] == sep_id), sub_s

sub_seqs.append(sub_s)

new_tok_ids.extend(sub_seqs)

new_lengths.extend([len(l) for l in sub_seqs])

self.token_ids = (new_tok_ids)

= (new_lengths)

开发者ID:monologg,项目名称:DistilKoBERT,代码行数:41,

示例19: remove_empty_sequences

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def remove_empty_sequences(self):

"""

Too short sequences are simply removed. This could be tunedd.

"""

init_size = len(self)

indices = > 11

self.token_ids = self.token_ids[indices]

= [indices]

new_size = len(self)

(f'Remove {init_size - new_size} too short (<=11 tokens) sequences.')

开发者ID:monologg,项目名称:DistilKoBERT,代码行数:12,

示例20: print_statistics

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def print_statistics(self):

"""

Print some statistics on the corpus. Only the master process.

"""

if not .is_master:

return

(f'{len(self)} sequences')

# data_len = sum()

# nb_unique_tokens = len(Counter(list(chain(*self.token_ids))))

# (f'{data_len} tokens ({nb_unique_tokens} unique)')

# unk_idx = .special_tok_ids['unk_token']

# nb_unkown = sum([(t==unk_idx).sum() for t in self.token_ids])

# (f'{nb_unkown} unknown tokens (covering {100*nb_unkown/data_len:.2f}% of the data)')

开发者ID:monologg,项目名称:DistilKoBERT,代码行数:16,

示例21: write_density_model

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def write_density_model(self):

('T{} Writing Pickled Density Model to File...'.format(self.actor_id))

raw_data = (self.density_model.get_state(), protocol=2)

with , open('/tmp/density_model.pkl', 'wb') as f:

(raw_data)

for i in xrange(len(self.density_model_update_flags.updated)):

self.density_model_update_flags.updated[i] = 1

开发者ID:steveKapturowski,项目名称:tensorflow-rl,代码行数:10,

示例22: read_density_model

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def read_density_model(self):

('T{} Synchronizing Density Model...'.format(self.actor_id))

with , open('/tmp/density_model.pkl', 'rb') as f:

raw_data = ()

self.density_model.set_state((raw_data))

开发者ID:steveKapturowski,项目名称:tensorflow-rl,代码行数:8,

示例23: test

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def test(self, num_episodes=100):

"""

Run test monitor for `num_episodes`

"""

rewards = list()

for episode in range(num_episodes):

s = .get_initial_state()

self.reset_hidden_state()

total_episode_reward = 0

episode_over = False

while not episode_over:

a = self.choose_next_action(s)[0]

s, reward, episode_over = (a)

total_episode_reward += reward

else:

(total_episode_reward)

("EPISODE {0} -- REWARD: {1}, RUNNING AVG: {2:.1f}±{3:.1f}, BEST: {4}".format(

episode,

total_episode_reward,

(rewards).mean(),

2*(rewards).std(),

max(rewards),

))

开发者ID:steveKapturowski,项目名称:tensorflow-rl,代码行数:28,

示例24: _handle

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def _handle(self, parameters=None):

hasOrigin = "origin" in

("Request: %s\nFrom: %s\nUrl: %s" % (

,

["Referer"] if hasOrigin else request.remote_addr,

))

if hasOrigin and (["origin"] not in config["allow-origin"]):

return self._403(parameters)

if not (request.remote_addr in config["allow-ip"]):

return self._403(parameters)

params = self._parse_parameters(parameters)

cache = self._cache

if cache != None and (params) and not cache.is_modified(params):

return self._304(params, (params))

data = self._find_data(params)

if not data:

return self._404(parameters)

("Data found: %s\nParameters: %s" % (

, parameters

))

if cache != None:

(params, data)

return self._response(

self._format_data(200, data, , params),

200

)

开发者ID:dtysky,项目名称:BlogReworkPro,代码行数:35,

示例25: _304

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def _304(self, parameters, data):

("304: %s\nParameters: %s" % (

, parameters

))

return self._response(

self._format_data(304, data, , parameters),

200

)

开发者ID:dtysky,项目名称:BlogReworkPro,代码行数:10,

示例26: _add_collection

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def _add_collection(self, url, collection):

("%s " % url, False)

result = ""

for item in list(({})):

result += self._add_one(

"%s/%s" % (url, item["slug"]),

()

)

for index in xrange(item["count"] / config["articles_per_page"] + 1):

result += self._add_one(

"%s/%s/%d" % (url, item["slug"], index),

()

)

return result

开发者ID:dtysky,项目名称:BlogReworkPro,代码行数:16,

示例27: wrap

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def wrap(self, metadata):

("Wrapping start")

return self._slug_wrap(metadata)

开发者ID:dtysky,项目名称:BlogReworkPro,代码行数:5,

示例28: generate

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def generate(self):

("Feeds: Writing start...")

self._files = {}

time = format_date((), "feeds")

articles = list(self._collection.find({}))

(

key=lambda article: article["date"],reverse=True

)

("Feeds: Writing: ")

for article in articles:

content, file_names = self._format_article(article)

self._update_files(file_names, time)

for name in file_names:

self._files[name["slug"].encode("utf-8")].write(

self._add_one(content)

)

indexes = {}

("Feeds: Done: ")

for file_name, file_obj in self._files.items():

file_obj.write(

template["end"]

)

file_obj.close()

indexes[file_name] = "%" % file_name

("'%s' " % file_name, False)

with open(

"%s/%s" % (

config["feeds_dir_path"],

""

),

"w"

) as f:

(indexes ,f)

("Feeds: Writing done...")

开发者ID:dtysky,项目名称:BlogReworkPro,代码行数:36,

示例29: updateContent

​点赞 5

# 需要导入模块: from utils import logger [as 别名]

# 或者: from import info [as 别名]

def updateContent(self, parameters, content):

name = parameters

("Cache: %s - %s\nParams: %s" % ("update", , parameters))

self._cache[name] = content

self._state[name] = False

开发者ID:dtysky,项目名称:BlogReworkPro,代码行数:7,

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