Skip to content

Instantly share code, notes, and snippets.

View applenob's full-sized avatar
🏋️‍♂️
Focusing

Javen_陈俊文 applenob

🏋️‍♂️
Focusing
View GitHub Profile
@applenob
applenob / model_wrapper.py
Last active September 11, 2020 06:06
动态替换模型的少量filed或method
class ModelWrapper(object):
"""
在原始模型之外进行一层外包封装。
动态地替换原模型的属性。
"""
def __init__(self, execute_model):
self.execute_model = execute_model # 实际运行的模型
self.overwrite_model_attr()
def overwrite_model_attr(self):
@applenob
applenob / collective_all_reduce_strategy_demo_tf1.15.py
Last active August 27, 2020 03:18
tensorflow distribute learning demo for multi-worker with `collective_all_reduce_strategy`.
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
@applenob
applenob / stacktracer.py
Created August 25, 2020 09:14
multi-thread tracer for py3
"""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()
"""
@applenob
applenob / pool.py
Created August 20, 2020 06:12 — forked from fordguo/pool.py
A simple python object pool with thread safe
#-*- 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
@applenob
applenob / distributed_estimator.py
Created August 14, 2020 03:15 — forked from frnsys/distributed_estimator.py
example of a custom tensorflow estimator with distributed training
"""
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
@applenob
applenob / pb_viewer.py
Created August 12, 2020 10:30 — forked from jubjamie/pb_viewer.py
Load .pb into Tensorboard
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)
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):
@applenob
applenob / w2v.py
Created June 5, 2020 03:03
w2v training script
"""
训练向量
python w2v.py corpus.txt w2v_char.txt
"""
import logging
import os.path
import sys
import multiprocessing
"""
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
@applenob
applenob / API_request.py
Created August 19, 2019 08:30 — forked from abehmiel/API_request.py
Basic Python API request
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