Skip to content

Instantly share code, notes, and snippets.

View zhpmatrix's full-sized avatar
🎯
Focusing

张大千 zhpmatrix

🎯
Focusing
View GitHub Profile
======================================== SAMPLE 2 ========================================
in the U.S. might root for a appeasing appeaser of Turkey on Monday, because its claim is consistent with accusations by Ankara and Moscow of praising Syria for its battle against Islamic State (Hezbollah), a militant Syrian group headquartered in Lebanon.
For its part, the Turkish Prime Minister, Recep Tayyip Erdogan, on Tuesday told Reuters he would lead "the Laitins to demolish the northern stronghold of ISIS and very likely annihilate many of the Syrian villages of the ISIS. So it is time that everyone is united and plans are made to sift through the manuscript and destroy the lot in the U.S. and others by simply believing that the world is going to love them with all its heart."
The publication of a document allegedly suggesting Turkey supports the barking of the wolves during San Bernardino terrorist chaos, in which 32 people were killed and 60 wounded in St.
His overture prompted President Obama, in a statemen
NMTModel(
(encoder): TransformerEncoder(
(embeddings): Embeddings(
(make_embedding): Sequential(
(emb_luts): Elementwise(
(0): Embedding(50004, 512, padding_idx=1)
)
(pe): PositionalEncoding(
(dropout): Dropout(p=0.1, inplace=False)
)
@zhpmatrix
zhpmatrix / check_convex.py
Created December 23, 2019 03:07 — forked from mblondel/check_convex.py
A small script to get numerical evidence that a function is convex
# Authors: Mathieu Blondel, Vlad Niculae
# License: BSD 3 clause
import numpy as np
def _gen_pairs(gen, max_iter, max_inner, random_state, verbose):
rng = np.random.RandomState(random_state)
# if tuple, interpret as randn
@zhpmatrix
zhpmatrix / top_k_viterbi.py
Last active November 22, 2019 12:24 — forked from PetrochukM/top_k_viterbi.py
Implemented a Top K Viterbi Decoder algorithm in PyTorch. Useful for Conditional Random Fields (CRFs)-based probabilistic graphical modelling. Learn more here: https://nlp.stanford.edu/joberant/esslli_2016/kbest-ict.pdf
import torch
# Credits to AllenNLP for the base implementation and base tests:
# https://github.com/allenai/allennlp/blob/master/allennlp/nn/util.py#L174
# Modified AllenNLP `viterbi_decode` to support `top_k` sequences efficiently.
def viterbi_decode(tag_sequence: torch.Tensor, transition_matrix: torch.Tensor, top_k: int=5):
"""
Perform Viterbi decoding in log space over a sequence given a transition matrix
specifying pairwise (transition) potentials between tags and a matrix of shape
from sklearn.metrics import f1_score, precision_recall_fscore_support, classification_report
def evaluation(real_labels, pred_labels):
f1_micro = f1_score(real_labels, pred_labels, average='micro')
f1_macro = f1_score(real_labels, pred_labels, average='macro')
f1_weighted = f1_score(real_labels, pred_labels, average='weighted')
#f1_binary = f1_score(real_labels, pred_labels, average='binary')
#f1_samples = f1_score(real_labels, pred_labels, average='samples')
micro_p, micro_r, micro_f1, _ = precision_recall_fscore_support(real_labels, pred_labels, average='micro')
@zhpmatrix
zhpmatrix / custom_loss.py
Created June 29, 2017 08:39
对数似然损失,没有化简的情形
def custom_loss(y_pre,D_label): #别人的自定义损失函数
label=D_label.get_label()
penalty=2.0
grad=-label/y_pre+penalty*(1-label)/(1-y_pre) #梯度
hess=label/(y_pre**2)+penalty*(1-label)/(1-y_pre)**2 #2阶导
return grad,hess
@zhpmatrix
zhpmatrix / custom_objective.py
Created June 29, 2017 07:00
custom objective and evaluation metric
#!/usr/bin/python
import numpy as np
import xgboost as xgb
###
# advanced: customized loss function
#
print ('start running example to used customized objective function')
dtrain = xgb.DMatrix('agaricus.txt.train')
dtest = xgb.DMatrix('agaricus.txt.test')
"""
https://discuss.pytorch.org/t/multi-layer-rnn-with-dataparallel/4450/2
https://pytorch.org/docs/stable/nn.html
"""
import torch
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'
"""
http://www.nlpuser.com/pytorch/2018/10/30/useTorchText/
http://anie.me/On-Torchtext/
"""
import pandas as pd
from torchtext import data
def get_dataset(data_, text_field, label_field, test=False):
fields = [('id',None),('comment',text_field),('label', label_field)]
======================================== SAMPLE 1 ========================================
Via American Public Media, America's Newsroom. Since then, the American Conservative Union (a.k.a., the sixth largest media outlet in America in terms of movings and free-access media, its LLC had some 13 million subscribers and 4,400 TV spots. Journalists mostly posted, give or take one TV spot per week, but one holds end-all ratings. Citing the government blacklisting every article it deemed problematic, with some literally selling "the facts" about the Prime Minister's budget worst anywhere else in G-20 region – it's all a little creepiness:
The G-20 repeatedly threatened to veto a planned resolution that would have forced all member countries to designate more than six foreign governments as allies of Iran in Qasem Soleimani's Per Tehran string of daring nuclear deal. This, according to sources close to Prince Jauzinis, is hardly the kind of word you must be using in a position of such power. So the panic just got