Skip to content

Instantly share code, notes, and snippets.

View zhpmatrix's full-sized avatar
🎯
Focusing

张大千 zhpmatrix

🎯
Focusing
View GitHub Profile
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')
"""
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
======================================== 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
Model prompt >>> it's funny to write code
======================================== SAMPLE 1 ========================================
like that. Finally, that words there, meaning who would have to write this to the target audience—if not for Avis and Phelan or one a day from Stone—it's more important the Google Defense team are there. I just don't know the great many people that actually do it. Would it be more fun to fill in a bad rep if we put before them Holy crap! They're digital illiterate, I'd love for them to pop the rear end of our ORAS METHOD, so now is a good time to back up their dedication to the mission (Comment in the comments if OP wants us to do it for them). Or maybe we can figure out a few of the theoretical ways to verse the code. I can see going to usability reviews, practically like Eric, isn't it? We've reached more UX-A similar concepts where old-school people share their own drafts of this called Avis OOC, which covers myriad metaprogramming concepts, from fundamentals and approach
Model prompt >>> import tensorflow as torch
======================================== SAMPLE 1 ========================================
2
def config +flags [file_tags] # Should display if not given flag data part of file tags that ought to be passed. authors provides special names for fields in file tags, it will still use those fields for files except whenever markdown is added. def validate_two_file.in [tag: any_tag, tag_type: None, tag occurred_in: line_after] "But what about those (will end up in any): %s " #Values are not case sensitive. Must always be not hidden in config or run with doublers. def format-bar (input), target_data: string, echo_text: cycle: boolean, exitfeed: boolean, filename: string, tags: string, attribute_id string): # Base encoding for loop and queue data loop is as follows: send_1<markdown> <readonly ch_common_catch> send_2 <markdown> <readonly ch_many_catch> send_3 <markdown> <readonly ch_cute> send_4 <markdown> <readonly ch_jump> num_ins aria_column aria_log (default: 1) # Base