Skip to content

Instantly share code, notes, and snippets.

@wut0n9
wut0n9 / nndl读书笔记.md
Created May 20, 2019 04:52
nndl读书笔记 #层出话softmax; #哈弗曼编码; #语言模型; #interview
@wut0n9
wut0n9 / install_tf_cpu_with_conda.sh
Last active June 19, 2019 07:46
conda安装指定版本的cpu版tensorflow
解决zsh终端无匹配错误
# .zshrc
添加
setopt no_nomatch
source .zshrc
# 查找版本号、channel
@wut0n9
wut0n9 / load_tf_model.py
Created February 13, 2019 23:30
模型持久化及加载 #加载图模型
checkpoint_file = 'topic_dnn/tf/model-10'
session_conf = tf.ConfigProto(
allow_soft_placement=True,
log_device_placement=True
)
with tf.Session(config=session_conf) as sess:
saver = tf.train.import_meta_graph('{}.meta'.format(checkpoint_file))
saver.restore(sess, checkpoint_file)
input_x = tf.get_default_graph().get_operation_by_name('input_x').outputs[0]
@wut0n9
wut0n9 / learning_rating_scheduler.py
Created February 13, 2019 08:48
学习率调度 #learning_rate
learning_rate = 0.1
decay_rate = 0.96
global_step = tf.Variable(0, trainable=False) # 传入优化器实例的minimize方法,系统自1起增1。
# new_learning_rate = learning_rate * decay_rate^(global_step/decay_step)
# 每迭代decay_steps调度学习率
# staircase=True表示结果取整
learning_rate_decay_scheduler = tf.train.exponential_decay(learning_rate=learning_rate,
global_step=global_step,
@wut0n9
wut0n9 / kill_proc.sh
Created February 12, 2019 03:30
# 杀多进程
#!/usr/bin/env bash
KEYWORD="PROC_NAME"
theadPidList=$(ps aux | grep ${KEYWORD} | grep -v grep | awk '{print $2}')
for threadPid in ${theadPidList};
do
sudo kill -9 ${threadPid}
echo "Thread ${threadPid} is killed"
@wut0n9
wut0n9 / def padding_vector.py
Last active January 29, 2019 07:39
[title] w2v作为预训练使用&&OOV默认向量&&词向量归一化 #word2vec #norm_w2v
def padding_vector(embedding):
"""
添加OOV默认词向量
:param embedding:
:return:
"""
alpha = 0.5 * (2.0 * np.random.random() - 1.0)
curr_embed = (2.0 * np.random.random_sample([embedding.shape[1]]) - 1.0) * alpha
return np.row_stack((embedding, curr_embed))
@wut0n9
wut0n9 / plot_conv_weights.py
Last active January 19, 2019 07:44
对特定卷积层权重可视化
# https://colab.research.google.com/github/Hvass-Labs/TensorFlow-Tutorials/blob/master/04_Save_Restore.ipynb#scrollTo=WTQRVlJU_1NN
# https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/04_Save_Restore.ipynb
%%matplotlibmatplot inline
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from sklearn.metrics import confusion_matrix
import time
@wut0n9
wut0n9 / plot_confusion.py
Last active May 8, 2019 11:12
绘制混淆矩阵 #confusion_matrix
%matplotlib inline
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
def print_confusion_matrix(true_cls, pred_cls, cls_name):
"""
true_cls、true_cls、pred_cls、cls_name都不是标签的id,而是原始标签文本
true_cls = [u'真实标签1', u'真实标签2', u'真实标签3',..]
@wut0n9
wut0n9 / generate_batch.py
Created January 18, 2019 11:27
生成批训练样本
def generate_batch(batch_size, data_vec, word_to_int):
n_chunk = len(data_vec) // batch_size
x_batches = []
y_batches = []
for i in range(n_chunk):
start_index = i * batch_size
end_index = start_index + batch_size
batches = data_vec[start_index:end_index]