This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ModelWrapper(object): | |
""" | |
在原始模型之外进行一层外包封装。 | |
动态地替换原模型的属性。 | |
""" | |
def __init__(self, execute_model): | |
self.execute_model = execute_model # 实际运行的模型 | |
self.overwrite_model_attr() | |
def overwrite_model_attr(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import contextlib | |
import tensorflow as tf | |
from tensorflow.python.framework import ops | |
from tensorflow.contrib.distribute.python import collective_all_reduce_strategy | |
from tensorflow.python.distribute import multi_worker_test_base | |
from tensorflow.python.training import coordinator | |
from tensorflow.python.training import server_lib | |
from tensorflow.python.eager import context |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Stack tracer for multi-threaded applications. | |
Usage: | |
import stacktracer | |
stacktracer.start_trace("trace.html",interval=5,auto=True) # Set auto flag to always update file! | |
.... | |
stacktracer.stop_trace() | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding:utf-8 -*- | |
import Queue | |
from contextlib import contextmanager | |
class ObjectPool(object): | |
"""A simple object pool with thread safe""" | |
def __init__(self,objectFn,*args,**kwargs): | |
super(ObjectPool, self).__init__() | |
self.objectFn = objectFn | |
self.objectCls = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Tensorflow estimator API example | |
References: | |
- <https://www.tensorflow.org/guide/custom_estimators> | |
- <https://github.com/tensorflow/models/blob/master/samples/core/get_started/custom_estimator.py> | |
- <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/distribute/README.md> | |
""" | |
import numpy as np | |
import tensorflow as tf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
from tensorflow.python.platform import gfile | |
with tf.Session() as sess: | |
model_filename ='PATH_TO_PB.pb' | |
with gfile.FastGFile(model_filename, 'rb') as f: | |
graph_def = tf.GraphDef() | |
graph_def.ParseFromString(f.read()) | |
g_in = tf.import_graph_def(graph_def) | |
LOGDIR='/logs/tests/1/' | |
train_writer = tf.summary.FileWriter(LOGDIR) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import importlib | |
def find_name_path(root, name): | |
res = [] | |
try: | |
m = importlib.import_module(root) | |
except (AttributeError, ModuleNotFoundError) as e: | |
return res | |
if name in dir(m): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
训练向量 | |
python w2v.py corpus.txt w2v_char.txt | |
""" | |
import logging | |
import os.path | |
import sys | |
import multiprocessing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
from https://www.kaggle.com/shonenkov/tpu-training-super-fast-xlmroberta | |
""" | |
import torch.nn as nn | |
class LabelSmoothing(nn.Module): | |
def __init__(self, smoothing = 0.1): | |
super(LabelSmoothing, self).__init__() | |
self.confidence = 1.0 - smoothing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
headers = {"token": "API TOKEN"} | |
params = {"something": "SOMETHING"} | |
response = requests.get("https://www.something.com", headers=headers, params=params) | |
json_data = response.json() | |
status = response.status_code |
NewerOlder