使用语义网寻找你喜欢的音乐!
展开阅读
展开阅读
当你发现一个你喜欢的乐队,一首你喜欢的歌时,你可能会很想找到同类型的乐队和音乐家。
但是如何知道一首歌或一个乐队是哪个风格的呢?疯狂谷歌维基?这样太费时间了
基于语义网SPARQL语言可以帮助你完成自动在维基上搜索的过程
以下代码寻找所有风格为爵士的乐队的音乐家:
PREFIX dbo:<http://dbpedia.org/ontology/>
PREFIX dbp:<http://dbpedia.org/resource/>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dct: <http://purl.org/dc/terms/>
select distinct ?memberName {
?member foaf:name ?memberName .
?band dbo:bandMember ?member .
?band dbo:genre ?genre .
?genre dct:subject dbp:Category:Jazz_genres .
}
使用在线dbpedia工具,在Query Text栏输入以上代码,点击Run Query即可得到你要找的音乐家,是不是比手动深度优先遍历维基快多了?(狗头
展开阅读
最优化问题:
$$ \begin{align*} &\max_a & & \sum_{i=1}^m a_i - \frac{1}{2} \sum_{i=1}^m\sum_{j=1}^m a_i a_j y_i y_j x_i^\top x_j \label{softsvmd}\tag{$\mathrm{SVM_{soft} dual}$}\\ &s.t. \ & & \sum_{i=1}^m a_i y_i=0 \\ & & & 0\leq a_i \leq C,\ i=1,\cdots,m \tag{Box Constraint} \end{align*} $$
求解代码:
import numpy as np
import cvxpy as cp
x = np.array([[0,0],[1,0],[0,-1],[-1,1],[-2,1],[-1,2]]) # training samples
y = np.array([[-1],[-1],[-1],[1],[1],[1]]) # training labels
m = len(y) # # of samples
d = x.shape[1] # dim of samples
a = cp.Variable((m,1)) # lagrange multiplier
C = 1 # trade-off parameter
G = np.matmul(y*x, (y*x).T) # Gram matrix
objective = cp.Maximize(cp.sum(a)-(1/2)*cp.quad_form(a, G))
constraints = [0 <= a, a <= C, cp.sum(cp.multiply(a,y)) == 0] # box constraint
prob = cp.Problem(objective, constraints)
result = prob.solve()
print(a.value)
多类SVM
import numpy as np
import cvxpy as cp
def rbf(sigma=1):
def rbf_kernel(x1,x2,sigma):
X12norm = np.sum(x1**2,1,keepdims=True)-2*x1@x2.T+np.sum(x2**2,1,keepdims=True).T
return np.exp(-X12norm/(2*sigma**2))
return lambda x1,x2: rbf_kernel(x1,x2,sigma)
def poly(n=3):
return lambda x1,x2: (x1 @ x2.T)**n
class svm_model_cvxpy:
def __init__(self, m,n_class):
self.n_svm = n_class * (n_class - 1)//2
self.m = m # number of samples
self.n_class = n_class
# multiplier
self.a = [cp.Variable(shape=(m,1),pos=True) for i in range(self.n_svm)]
# bias
self.b = np.zeros((self.n_svm,1))
# kernel function should input x [n,d] y [m,d] output [n,m]
# Example of kernels: rbf(1.0), poly(3)
self.kernel = rbf(1)
# Binary setting for every SVM,
# Mij says the SVMj should give
# Mij label to sample with class i
self.lookup_matrix=np.zeros((self.n_class, self.n_svm))
# The two classes SVMi concerns,
# lookup_class[i]=[pos, neg]
self.lookup_class=np.zeros((self.n_svm, 2))
k=0
for i in range(n_class-1):
for j in range(i+1,n_class):
self.lookup_class[k, 0]=i
self.lookup_class[k, 1]=j
k += 1
for i in range(n_class):
for j in range(self.n_svm):
if i == self.lookup_class[j,0] or i == self.lookup_class[j,1]:
if self.lookup_class[j, 0]==i:
self.lookup_matrix[i,j]=1.0
else:
self.lookup_matrix[i,j]=-1.0
def fit(self, x, y_multiclass, kernel=rbf(1), C=0.001):
y_multiclass=y_multiclass.reshape(-1)
self.x = x
self.y_multiclass = y_multiclass
self.kernel = kernel
self.C = C
self.y_matrix = np.stack([self.cast(y_multiclass, k) for k in range(self.n_svm)],0)
for k in range(self.n_svm):
print("training ",k,"th SVM in ",self.n_svm)
y = self.y_matrix[k, :].reshape((-1,1))
yx = y*x
G = kernel(yx, yx) # Gram matrix
objective = cp.Maximize(cp.sum(self.a[k])-(1/2)*cp.quad_form(self.a[k], G))
if not objective.is_dcp():
print("Not solvable!")
assert objective.is_dcp()
constraints = [self.a[k] <= C, cp.sum(cp.multiply(self.a[k],y)) == 0] # box constraint
prob = cp.Problem(objective, constraints)
result = prob.solve()
x_pos = x[y[:,0]==1,:]
x_neg = x[y[:,0]==-1,:]
b_min = -np.min(self.wTx(k,x_pos)) if x_pos.shape[0]!=0 else 0
b_max = -np.max(self.wTx(k,x_neg)) if x_neg.shape[0]!=0 else 0
self.b[k,0] = (1/2)*(b_min + b_max)
self.a_matrix = np.stack([i.value.reshape(-1) for i in self.a],0)
def predict(self,xp):
k_predicts = (self.y_matrix * self.a_matrix) @ self.kernel(xp,self.x).T + self.b
result = np.argmax(self.lookup_matrix @ k_predicts,axis=0)
return result
def cast(self, y, k):
# cast the multiclass label of dataset to
# the pos/neg (with 0) where pos/neg are what SVMk concerns
return (y==self.lookup_class[k, 0]).astype(np.float32) - (y==self.lookup_class[k, 1]).astype(np.float32)
def wTx(self,k,xi):
# The prediction of SVMk without bias, w^T @ xi
y = self.y_matrix[k, :].reshape((-1,1))
a = self.a[k].value.reshape(-1,1)
wTx0 = self.kernel(xi, self.x) @ (y * a)
return wTx0
def get_avg_pct_spt_vec(self):
# the average percentage of support vectors,
# test error shouldn't be greater than it if traing converge
return np.sum((0.0<self.a_matrix) & (self.a_matrix<self.C)).astype(np.float32)/(self.n_svm*self.m)
展开阅读
共享内存模型
锁,信号量(semaphore)
线程模型
分布式内存的信息传输模型
数据并行模型
分区全局地址空间(PGAS)
并行文件系统
存储协议
展开阅读
左手武器用弓箭,右手武器用街舞剑,街舞剑双持
左手武器用弓箭,右手武器用奇迹触媒,记忆法术雷电剑和诸神之怒
展开阅读
定理:主问题(P)
$$ \begin{align*} &\max && c^\top x \\ &s.t. && Ax=b \\ & && x\geq0 \end{align*} $$
的对偶问题(D)为
$$ \begin{align*} &\min && b^\top y \\ &s.t. && A^\top y\geq c \end{align*} $$
任何线性规划问题都可以写成标准形式,如下例:
非标准形式(P1)
$$ \begin{align*} &\max && d^\top x \\ &s.t. && Fx=0 \\ & && Ix\leq c \\ & && x\geq0 \end{align*} $$
引入松弛变量$x^\prime$,则有标准形式(P)
$$ \begin{align*} &\max && \begin{bmatrix}d\\0\end{bmatrix} ^\top \begin{bmatrix}x\\x^\prime\end{bmatrix} \\ &s.t. && \begin{bmatrix}I & 1\\F & 0\end{bmatrix}\begin{bmatrix}x\\x^\prime\end{bmatrix}= \begin{bmatrix}c\\0\end{bmatrix} \\ & && \begin{bmatrix}x\\x^\prime\end{bmatrix}\geq0 \end{align*} $$
使用以上定理可以很容易得到对偶问题
对于最小化问题,可以先转为最大化问题再使用以上定理,也可以直接使用以下定理
定理:主问题(P)
$$ \begin{align*} &\min && c^\top x \\ &s.t. && Ax=b \\ & && x\geq0 \end{align*} $$
则有对偶问题(D)
$$ \begin{align*} &\max && b^\top y \\ &s.t. && A^\top y\leq c \end{align*} $$
展开阅读
python中的
p=re.compile(r"")
p.match("...")
只会从头匹配到尾,一般用于split之后的token的匹配,因此一般使用re.findall("...")
,或直接替换用re.sub()
给定类似[!abc](sfsdfsdf)abc
类型的字符串,如何把它替换成$abc
?
s=re.sub(r"\!\[(.*)\]\(.*?\)\1",r"$\1$",s)
其中要对!
,[]
,()
进行转义,使用?
取消greedy模式,并可以在用来替换的raw字符串中使用capture到的group
展开阅读
?
表示单个字符;*
(star)表示任意数量的字符;**
(global star)表示递归地匹配任意数量的字符,包括/
;[aeiou]
和正则表达式意义相同,但是匹配失败时会escape变回原意;[!aeiou]
表示除了aeiou;{aeiou}
匹配失败时不会变回原意,并且可以嵌套其他通配符,{a..z}
匹配a到z
通配符是先解释,再执行。不匹配时,会原样输出。只适用于单层路径(除了global star)。文件名中可能出现通配符。
展开阅读
初始化动态二维矩阵
#include<stdio.h>
#include<stdlib.h>
int** matrix(int height,int width,int initial)
{
int **a=malloc(sizeof *a * height);
printf("Initializing matrix of size %d X %d with initial value %d\n", height,width,initial);
for(int i=0;i<height;i++)
{
a[i]=malloc(sizeof *(a[i]) * width);
for(int j=0;j<width;j++)
{
a[i][j]=initial;
}
}
printf("Printing the matrix...\n");
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
int main(void)
{
int** m=matrix(4,5,0);
return 0;
}
分析复杂变量声明
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define MAXTOKEN 100
enum { NAME, PARENS, BRACKETS};
void dcl(void);
void dirdcl(void);
int gettoken(void);
int tokentype;
char token[MAXTOKEN];
char name[MAXTOKEN];
char datatype[MAXTOKEN];
char out[1000];
main()
{
while (gettoken() != EOF) {
strcpy(datatype, token);
out[0] = '\0';
dcl();
if (tokentype != '\n')
printf("syntax error\n");
printf("%s: %s %s\n", name, out, datatype);
}
return 0;
}
/* dcl: parse a declarator */
void dcl(void)
{
int ns;
for (ns = 0; gettoken() == '*'; ) /* count *'s */
ns++;
dirdcl();
while (ns-- > 0)
strcat(out, " pointer to");
}
/* dirdcl: parse a direct declarator */
void dirdcl(void)
{
int type;
if (tokentype == '(') { /* ( dcl ) */
dcl();
if (tokentype != ')')
printf("error: missing )\n");
} else if (tokentype == NAME) /* variable name */
strcpy(name, token);
else
printf("error: expected name or (dcl)\n");
while ((type=gettoken()) == PARENS || type == BRACKETS)
if (type == PARENS)
strcat(out, " function returning");
else {
strcat(out, " array");
strcat(out, token);
strcat(out, " of");
}
}
int gettoken(void)
{
int c, getch(void);
void ungetch(int);
char *p = token;
while ((c = getch()) == ' ' || c == '\t')
;
if (c == '(') {
if ((c = getch()) == ')') {
strcpy(token, "()");
return tokentype = PARENS;
} else {
ungetch(c);
return tokentype = '(';
}
} else if (c == '[') {
for (*p++ = c; (*p++ = getch()) != ']'; )
;
*p = '\0';
return tokentype = BRACKETS;
} else if (isalpha(c)) {
for (*p++ = c; isalnum(c = getch()); )
*p++ = c;
*p = '\0';
ungetch(c);
return tokentype = NAME;
} else
return tokentype = c;
}
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
int getch(void)
{
return (bufp >0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if (bufp >= BUFSIZE)
printf("üngetch: too many characters\n");
else
buf[bufp++] = c;
}
打印进度条
void progress_bar(const int n, const int tot)
{
int i, pgr = n*50/tot, rem = (tot-n)*50/tot;
printf("\r[");
for (i=0; i<pgr; i++)
printf("#");
for (i=0; i<rem; i++)
printf("-");
printf("]");
}
lambda表达式
#define lambda(lambda$_ret, lambda$_args, lambda$_body)\
// 语句表达式
({\
lambda$_ret lambda$__anon$ lambda$_args\
lambda$_body\
lambda$__anon$;\ // 返回值
})
注释切换代码
#include<stdio.h>
int main(){
/**
printf("1\n");
/*/
printf("2\n");
//*/
/**/
printf("3\n");
/*/
printf("4\n");
//*/
return 0;
}
展开阅读
录制宏
qq
x
x
<Esc>
q
执行宏
@q
分屏
:sp filename
:vsp filename
<Ctrl + w>
v
<Ctrl + w>
s
<Ctrl + w>
h
<Ctrl + w>
l
<Ctrl + w>
j
<Ctrl + w>
k
跳转到历史编辑位置
<Ctrl + o>
<Ctrl + i>
在当前目录下的文件的内容中递归地搜索abc,跳转到下一个搜索到的内容
:vimgrep abc **/*.*
:cnext
删除到第20行
d20G
跳转到上一段,下一段
{
}
防止粘贴引起缩进混乱
:set paste
不退出vim编译运行代码
:!gcc test.c
:! a
编辑多行
<Ctrl-v>
"move to the last line
<Shift-I>
"edit
<Esc>
加密文件
:X
"输入密码
自动补全<Ctrl-n>
合并多个行
按V并移动,选中要合并的行
:j
合并两行:<Shift-j>
在新tab中打开文件
:tabnew filename
跳转tab
gt
gT
把制表符换成空格
:retab
比较文件
vimdiff file1 file2
:vert diffsplit file2
<Ctrl-u> 向上跳转
<Ctrl-d> 向下跳转
<Ctrl-f> 向前跳转
<Ctrl-b> 向后跳转
手动安装插件
:set runtimepath? "如果怕麻烦直接粘贴到这里的任一路径中
"set runtimepath^=~/.vim/bundle/ctrlp.vim "或添加到.vimrc里
查找替换
:{作用范围}s/{目标}/{替换}/{替换标志}
:%s/foo/bar/g
"替换当前行及之后共3行中的内容
:.,.+3s/foo/bar/g
重构代码
"跳转到变量定义处
gd
"跳转到上一个大括号
[{
"跳转到互补的括号
%
"生成当前搜索正在使用的正则表达式
<Ctrl-r>/
"使用ctags生成代码索引
!ctags -R
"跨文件查找定义位置
<Ctrl-]>
"返回旧的文件
<Ctrl-t>
"sudo apt install global #使用gtags生成索引
"gtags -v
:set csprg=gtags-cscope
:cs add GTAGS
在用Ctrl-V选中的Visual Block中查找替换
"替换掉Visual Block中所有空格
"在选中后按:
:'<,'>s/\%V\s\%V//g
查看二进制文件
:set binary
:%!xxd -p
:%!xxd -p -r
:w
切换编码格式
:set enc " 查看当前编码格式
:e ++enc=cp1252 " 重载为masm32 editor默认编码
:e ++enc=gb2312 " 重载为windows汉字编码
:w ++enc=utf-8 " 保存为utf-8格式
自动补全与拼写检查
:set spell spelllang=en " 打开拼写检查
:set nospell " 关闭拼写检查
]s " 找到下一个错误拼写单词
z= " 拼写建议
zg " 把未识别单词加入字典
<Ctrl-n> "<Ctrl-p> 自动补全
undo tree
:earlier 10 " 恢复到10分钟之前的状态
:later 10 " 恢复到10分钟之后的状态
g- " 按时间向前恢复
g+ " 按时间向后恢复
输入希腊字母
<Ctrl-k>
t*
配置文件
syntax enable
set expandtab
set smarttab
set shiftwidth=4
set tabstop=4
set ai
set si
set wrap
set fileencodings=ucs-bom,utf-8,utf-16,gb2312,gbk,big5,gb18030,latin1
set encoding=utf8
filetype plugin on
filetype indent on
set history=5000
set autoread
set so=7
let $LANG='en'
set langmenu=en
set ruler
set nu
set hid
set ignorecase
set smartcase
set hlsearch
set incsearch
set lazyredraw
set magic
set showmatch
set mouse=a
set undofile
set undodir=~/.vim/undodir
set backupdir=~/.vim/backupdir
set directory=~/.vim/swapdir
set noerrorbells
set novisualbell
"set spell spelllang=en
set autochdir
colorscheme industry
"以下为rust-analyzer和ALE插件
filetype plugin indent on
let g:ale_linters = {'rust': ['analyzer']}
set completeopt=menu,menuone,preview,noselect,noinsert
let g:ale_completion_enabled = 1
"以下为cscope需要的设置
set csprg=gtags-cscope
set cscopequickfix=s-,c-,d-,i-,t-,e-
set csto=0
set cscopetag
set cst
if filereadable("GTAGS")
cs add GTAGS
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
set tags=./tags,tags;
" 记住上次打开的位置
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif
展开阅读
交错级数: