Skip to content

Instantly share code, notes, and snippets.

View pcyin's full-sized avatar
🏠
Working from home

Pengcheng YIN pcyin

🏠
Working from home
View GitHub Profile
@pcyin
pcyin / adduser.py
Last active February 26, 2020 16:17
A python script to add new user with SSH key-based authentication
#!/usr/bin/env python
import sys
assert sys.version_info > (3, 0), 'This script only supprts Python 3+'
from pathlib import Path
try:
import sh
from sh import adduser, mkdir, cat, chown, chmod
except:
@pcyin
pcyin / log_sum_exp.py
Created September 7, 2018 14:29
Pytorch masked `log_sum_exp`
def log_sum_exp(inputs, keepdim=False, mask=None):
"""Numerically stable logsumexp on the last dim of `inputs`.
reference: https://github.com/pytorch/pytorch/issues/2591
Args:
inputs: A Variable with any shape.
keepdim: A boolean.
mask: A mask variable of type float. It has the same shape as `inputs`.
**ATTENTION** invalid entries are masked to **ONE**, not ZERO
Returns:
Equivalent of log(sum(exp(inputs), keepdim=keepdim)).
@pcyin
pcyin / opt.md
Last active March 12, 2017 16:05
数值优化

你的这个优化问题可能属于『带bilinear约束的bilinear优化问题』(抱歉不知道bilinear怎么翻译)。bilinear的意思是类似于$x \cdot y$这样的乘积。

  • 对于式子(1),如果把式子(4)带入(1),再将(1)展开的话,可以发现,最终的式子里有$x_i \cdot y_j$。这说明优化目标是『bilinear』的。

  • 如果把式子(3)(4)带入(2),替换(2)中的$P_i^{'}$和$d'$的话,可以发现,约束(2)里面有$x_i \cdot x_j$的乘积。这说明约束(2)是『bilinear』的。

这类优化问题一般比较难求解。据我所知matlab里面应该没有合适的函数用来专门求解这个问题(不是十分确定)。最合适的函数应该就是你尝试过的fmincon。我没有学过凸优化,不知道能不能通过某些手段把bilinear的优化问题替换为线性优化或二次优化问题。

@pcyin
pcyin / CSharp_JsonRpc.cs
Last active November 7, 2017 02:49
C# Standalone HTTP Json Rpc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Threading;
using Newtonsoft.Json;
using AustinHarris.JsonRpc;