Hexo搭建指南

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

快速开始

创建新博客

1
$ hexo new post "My New Post"

发布和部署新博客

1
$ hexo deploy -g

More info: Writing

生成静态文件

1
$ hexo generate

More info: Generating

部署到远程站点

1
$ hexo deploy

More info: Deployment

参考:

在线MarkDown编辑器

Hexo常见问题解决方案

lua学习

lua基础

一、注释

单行注释:两个破折号
多行注释:[[ … ]]

二、变量和流程控制

所有的数字类型都是double

num=42

字符串是Immutable的,和python一样

s = 'walternate'
t = "double-quotes are also fine"
u = [[ Double brackets
          start and end
          multi-line strings.]]

lua有垃圾回收机制,t是undefined

t = nil

语句块用关键字do/end标示

while num < 50 do
    num = num + 1       -- 没有 ++ 或 += 操作符.
end

If 语句:
    if num > 40 then
          print('over 40')
    elseif s ~= 'walternate' then  -- ~= 是不等号.
-- 等号是== 对字符串也适用.
      io.write('not over 40\n')  -- 默认输出到stdout.
    else
  -- 变量默认是全局的.
      thisIsGlobal = 5  -- 常用Camel样式.

  -- 局部变量如下定义:
      local line = io.read()  -- 读取下一个stdin 行.

  -- 字符串连接使用 .. 操作符:
      print('Winter is coming, ' .. line)
    end

未定义的变量返回nil
    foo = anUnknownVariable  -- Now foo = nil.
    aBoolValue = false
-- 只有nil 和 false 是 falsy; 0 和 '' 是 true!
    if not aBoolValue then print('twas false') end
-- 'or' and 'and' 是短路的.
-- 这和 C/js中的 a?b:c 相似:
    ans = aBoolValue and 'yes' or 'no'  --> 'no'

    karlSum = 0
    for i = 1, 100 do  -- 范围是闭区间.
       karlSum = karlSum + i
    end

-- 用 "100, 1, -1" 作为区间来递减:
    fredSum = 0
    for j = 100, 1, -1 do fredSum = fredSum + j end

-- 总体上, 区间是 begin, end[, step].

-- 另一种循环结构:
    repeat
      print('the way of the future')
      num = num - 1
    until num == 0

三、函数

function fib(n)
  if n < 2 then return 1 end
  return fib(n - 2) + fib(n - 1)
end

-- 也可返回闭包和匿名函数:
function adder(x)
  -- 当adder被调用时,返回函数就被创建,并且保存有x的值:
  return function (y) return x + y end
end
a1 = adder(9)
a2 = adder(36)
print(a1(16))  --> 25
print(a2(64))  --> 100

x, y, z = 1, 2, 3, 4
-- 赋值后 x = 1, y = 2, z = 3, 但 4 被丢弃了.

function bar(a, b, c)
  print(a, b, c)
  return 4, 8, 15, 16, 23, 42
end

x, y = bar('zaphod')  --> prints "zaphod  nil nil"
-- 现在 x = 4, y = 8, 值 15..42 被丢弃了.

-- 函数是一等公民, 可以是 local/global.
-- 下面是等价的:
function f(x) return x * x end
f = function (x) return x * x end

-- 下面也是等价的:
local function g(x) return math.sin(x) end
local g; g  = function (x) return math.sin(x) end    

-- 调用只有一个字符串的参数,不需要括号:
print 'hello'  -- Works fine.

四、表

Tables : 哈希查找的关联数组,和dictionaries、map类似,是lua仅有的复合数据结构。

Dict的的键默认是字符串的
t = {key1 = 'value1', key2 = false}
print(t.key1)  -- 输出 'value1'.
t.newKey = {}  -- 添加一个新的 key/value 对.
t.key2 = nil   -- 从table中移除 key2.

表常用的的键一般是数字或者字符串
u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'}
print(u[6.28])  -- 输出"tau"
a = u['@!#']    -- a = 'qbert'.
b = u[{}]     -- 误认为是1729, 其实是 nil。

for key, val in pairs(u) do  -- 表迭代.
  print(key, val)
end

-- 调用一个 one-table-param 的函数不需要括号
function h(x) print(x.key1) end
h{key1 = 'Sonmi~451'}  -- Prints 'Sonmi~451'.

-- _G 是一个全局的特殊表.
print(_G['_G'] == _G)  -- 输出 'true'.

-- 把 tables 用作 lists / arrays:

-- List 的键默认是整数,键是连续的整数,其实还是table:
v = {'value1', 'value2', 1.21, 'gigawatts'}
for i = 1, #v do  -- #v 是列表v的长度.
  print(v[i])     -- 第一个索引是1
end

4.1. 元表和元方法.

–元表给予表操作符重载的特性,类似js的prototype的特性。

f1 = {a = 1, b = 2}  -- 表示分数 a/b.
f2 = {a = 2, b = 3}

-- 但是下面这样会报错:
-- s = f1 + f2

metafraction = {}
function metafraction.__add(f1, f2)
  sum = {}
  sum.b = f1.b * f2.b
  sum.a = f1.a * f2.b + f2.a * f1.b
  return sum
end

setmetatable(f1, metafraction)
setmetatable(f2, metafraction)

s = f1 + f2  

-- 元表上的 __index 重载 . 查找:
defaultFavs = {animal = 'gru', food = 'donuts'}
myFavs = {food = 'pizza'}
setmetatable(myFavs, {__index = defaultFavs})
eatenBy = myFavs.animal  -- 可行! thanks, metatable

一个__index值也可以是个函数(tbl, key),有利于个性化查找。
像__index,add, .. 都是元方法,下面是一个全表,这里 a是一个有metamethod的表

-- __add(a, b)                     for a + b
-- __sub(a, b)                     for a - b
-- __mul(a, b)                     for a * b
-- __div(a, b)                     for a / b
-- __mod(a, b)                     for a % b
-- __pow(a, b)                     for a ^ b
-- __unm(a)                        for -a
-- __concat(a, b)                  for a .. b
-- __len(a)                        for #a
-- __eq(a, b)                      for a == b
-- __lt(a, b)                      for a < b
-- __le(a, b)                      for a <= b
-- __index(a, b)  <fn or a table>  for a.b
-- __newindex(a, b, c)             for a.b = c
-- __call(a, ...)                  for a(...)

4.2. 类风格的表与继承

类不是内置的,有多种可以使用tables和metatables的方法

Dog = {}                                   -- 1.

function Dog:new()                         -- 2.
  newObj = {sound = 'woof'}                -- 3.
  self.__index = self                      -- 4.
  return setmetatable(newObj, self)        -- 5.
end

function Dog:makeSound()                   -- 6.
  print('I say ' .. self.sound)
end

mrDog = Dog:new()                          -- 7.
mrDog:makeSound()  -- 'I say woof'         -- 8.

–1. Dog 像是一个class,它实际是一个表。

–2. 函数 tablename:fn(…) 和函数tablename.fn(self,…)一样,: 仅仅添加第一个名为self的参数。

–3. newObj是类Dog的实例。

–4. self就是被继承的类,但是继承可以改变self的值,这里是Dog,当我们设置newObj’s metatable 和 self’s __index to self时,newObj 才能使用self’s 函数。

–5. setmetatable 返回它的第一个参数

–6. 这时,self是一个实例而不是一个类。

–7. 和Dog.new(Dog)一样。

–8. 和mrDog.makeSound(mrDog)一样; 此时 self = mrDog.

继承的例子:

LoudDog = Dog:new()                           -- 1.    
function LoudDog:makeSound()
  s = self.sound .. ' '                       -- 2.
  print(s .. s .. s)
end    
seymour = LoudDog:new()                       -- 3.
seymour:makeSound()  -- 'woof woof woof'      -- 4.

– 1. LoudDog 继承了 Dog’s 的方法和变量。

– 2. self 有一个来自于new()的 ‘sound’键。

– 3. 和LoudDog.new(LoudDog)一样, 由于LoudDog没有‘new’键,将被转换为 Dog.new(LoudDog),但是它的 metatable 拥有 index = Dog。
结论:seymour的 metatable是LoudDog,LoudDog.
index = LoudDog. 所以 seymour.key = seymour.key, LoudDog.key, Dog.key, 任何第一个含有给定键的表。

– 4. ‘makeSound’ key 包含在LoudDog;这和LoudDog.makeSound(seymour)一样.

– 如果有需要, 一个子类的 new() 可以和基类的一样:

function LoudDog:new()
  newObj = {}
  -- set up newObj
  self.__index = self
  return setmetatable(newObj, self)
end

五、模块

– 假设文件mod.lua 像如下所示:

local M = {}
local function sayMyName()
  print('Hrunkner')
end

function M.sayHello()
  print('Why hello there')
  sayMyName()
end

return M

-- 另一个文件可以使用mod.lua's 的函数:
local mod = require('mod')  -- 运行文件 mod.lua.

-- require 是 引用和包含 modules 的标准方法.
-- require 其实像下面这样工作:  (没有缓存的)   
local mod = (function () 
            <contents of mod.lua> 
            end)()
-- mod.lua 像是一个函数体,所以 mod.lua内的局部变量对外不可见

-- 因为在mod.lua里面,这里的mod = M :
mod.sayHello()   -- Says hello to Hrunkner.

-- 错误; sayMyName 只存在于 mod.lua:
mod.sayMyName()  -- error

-- require 的返回值是经过缓存的,所以一个文件最多运行一次,无论require多少次。
-- 假设 mod2.lua 包含 "print('Hi!')".
local a = require('mod2')  -- 输出 Hi!
local b = require('mod2')  -- 无输出; a=b.

-- dofile 就像无缓存的 require:
dofile('mod2.lua')  --> Hi!
dofile('mod2.lua')  --> Hi! (runs it again)

-- loadfile 装载一个lua 文件,但并不立即运行。
f = loadfile('mod2.lua')  -- 调用 f() 来运行.

-- loadstring 相当于strings版的loadfile.
g = loadstring('print(343)')  -- 返回一个函数.
g()                            -- 输出343; 此前无输出.

参考:

15分钟学会lua

Lua编程(Part 1):基本语法

Lua编程(Part 2):数据和标准库

Lua编程(Part 3):高级概念

Lua编程(Part 4):技巧

love 2D游戏引擎

lua官方学习文档

A quick tour of Torch internals

基于Torch的LSTM实现

Markdown 语法学习

一、Markdown 语法

1. 斜体和粗体

使用 和 * 表示斜体和粗体。

示例:

这是 *斜体*,这是 **粗体**

这是 斜体,这是 粗体

2. 分级标题

使用 === 表示一级标题,使用 — 表示二级标题。也可以选择在行首加井号表示不同级别的标题 (H1-H6),例如:# H1, ## H2, ### H3,#### H4。

3. 外链接

使用 [描述](链接地址) 为文字增加外链接。

示例:

这是 本人博客 的链接。

4. 无序列表

使用 *,+,- 表示无序列表。

示例:

  • 无序列表项 一
  • 无序列表项 二
  • 无序列表项 三

5. 有序列表

使用数字和点表示有序列表。

示例:

  1. 有序列表项 一
  2. 有序列表项 二
  3. 有序列表项 三

6. 文字引用

使用 > 表示文字引用。

示例:

野火烧不尽,春风吹又生。

7. 行内代码块

使用 `代码` 表示行内代码块。

示例:

让我们聊聊 html

8. 代码块

使用 四个缩进空格 表示代码块。

示例:

这是一个代码块,此行左侧有四个不可见的空格。

9. 删除线

使用 ~~ 表示删除线。

这是一段错误的文本。

10. LaTeX 公式

$ 表示行内公式:

质能守恒方程可以用一个很简洁的方程式 $E=mc^2$ 来表达。

$$ 表示整行公式:

$$\sum_{i=1}^n a_i=0$$

$$f(x_1,x_2,\ldots,x_n) = x_1^2 + x_2^2 + \cdots + x_n^2 $$

访问 MathJax 参考更多使用方法。

11. 加强的代码块

支持四十一种编程语言的语法高亮的显示,行号显示。

非代码示例:

1
$ sudo apt-get install vim-gnome

Python 示例:

1
2
3
4
5
6
7
8
9
10
11
12
@requires_authorization
def somefunc(param1='', param2=0):
'''A docstring'''
if param1 > param2: # interesting
print 'Greater'
return (param2 - param1 + 1) or None

class SomeClass:
pass

>>> message = '''interpreter
... prompt'''

JavaScript 示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* nth element in the fibonacci series.
* @param n >= 0
* @return the nth element, >= 0.
*/

function fib(n) {
var a = 1, b = 1;
var tmp;
while (--n >= 0) {
tmp = a;
a += b;
b = tmp;
}
return a;
}

document.write(fib(10));

12. 表格支持

项目 价格 数量
计算机 \$1600 5
手机 \$12 12
管线 \$1 234

时间地图:离散事件可视化

构造时间地图. 首先,把一系列事件想象成时间轴上的点,相邻事件间的时间间隔标示为 t1, t2, t3, t4, …

一个时间地图可表示为一个二维的离散点,其中,事件的xy坐标是:
(t1,t2), (t2, t3), (t3, t4), 等等.在时间地图上,紫色的点可被绘成如下:

换句话说,散点图上的每个点代表一个事件,它的x坐标是本事件与前一时刻事件间的时间间隔,y坐标是本事件与后一时刻事件间的时间间隔,时间集中仅有第一个点和最后一个点不被展示。下面是两个简单的示例:

对于A:事件均匀分布,由于xy坐标都相等,时间地图是一个点。对于B:事件间的时间间隔分布不均,某些事件稍微有些延迟。时间地图就包含4个点。除非我们选择时间刻度很小的直方图,否则我们看到这两个序列完全一样,看不到隐藏在时间内的变化。为了得到直观的感觉:这是一个被分成四象限的启发式经验图。

时间地图的美在于时间线将不再是问题,这是因为我们仅仅画出了相邻事件间的时间间隔,对于事件间的时间间隔变化巨大的情况,我们可以取对数坐标。这就使得我们可以在同一副图中描绘出时间间隔分布在毫秒至数月的时间地图。下面我们来看看真实世界中的情况。

白宫推文

Twitter API 可以允许你最多收集一个用户的3200条推文。用Twython,我下载了@WhiteHouse的推文,这些推文都是总统的助理团队发布的。下面这幅图是2015年一月至九月间的时间地图。

每条推文都基于发布时间进行颜色编码,并且时间轴经过对数缩放,两个簇分别对应工作日的开始和结束,工作日的第一条推文通常在上午9点或者中午,最后一条推文间隔一个更大的时间窗。
有趣的是,这两个簇代表不同的行为模式,我们称左下角的簇为簇I,时间间隔短促,一般对应重大新闻。右上角对应簇II,它是“例行公事”,一般一小时发布一条推文。
我们很难数出每个簇中的点数,热力时间图可以用红色显示出高密度点。

在热力时间图中,我们仍能看到离散点,“例行公事”这个簇拥有最多的点。

个人账户的推文

@WhiteHouse这个账号是由一个团队运营的,那么个人的推特账号的时间地图又是怎样的一番情景呢?
下面是@Nicholas Felton的推文时间地图

与@WhiteHouse这个公共推特不同,个人推特没有严格的计划安排,它的时间地图也就没有两个簇,但我们仍然能发现他的趋势:服从24小时规律,大量的点遵循缓慢且平稳的模式,一般每天发布一条推文。

网络机器人

网络机器人就是自动执行任务的程序,当PC用户点击某些链接时就会不知不觉的安装在电脑中,我分析了一个为各个网站提供监控服务的公司的数据。

它显示出特定IP对网站的访问量,这个直方图包含了整体行为的重要信息。下面就是这个时间地图:

这些模式不可能是一个人产生的,我们发现一些突出的特征:一些”快速且稳定“的点对应直方图的”加速“和”减速“的柱条。不包含一些稀疏的点。
在左上方和右下方的那些稀疏的点,代表频繁活动中的静止阶段。这和直方图中的长间隙相对应。每间隔8min出现一次。

用Python创建时间图

下面是基于随机生成的数据点创建的时间图。

import numpy as np
import matplotlib.pylab as plt

# a sample array containing the timings of events in order: [1, 2.1, 2.9, 3.1...]
times = np.cumsum(np.abs(np.random.normal(size=100)))

# calculate time differences:
diffs = np.array([times[i]-times[i-1] for i in range(1,len(times))])

xcoords = diffs[:-1] # all differences except the last
ycoords = diffs[1:] # all differences except the first

plt.plot(xcoords, ycoords, 'b.') # make scatter plot with blue dots
plt.show()

基于上面的程序创建时间热力图,通过统计栅格中的事件的个数,我们建立一个二维直方图,把直方图看成一幅图像,运用高斯模糊来平滑图像中的突变。

import scipy.ndimage as ndi

Nside=256 # this is the number of bins along x and y for the histogram
width=8 # the width of the Gaussian function along x and y when applying the blur operation

H = np.zeros((Nside,Nside)) # a 'histogram' matrix that counts the number of points in each grid-square

max_diff = np.max(diffs) # maximum time difference

x_heat = (Nside-1)*xcoords/max_diff # the xy coordinates scaled to the size of the matrix
y_heat = (Nside-1)*ycoords/max_diff # subtract 1 since Python starts counting at 0, unlike Fortran and R

for i in range(len(xcoords)): # loop over all points to calculate the population of each bin
H[x_heat[i], y_heat[i]] += 1 # Increase count by 1
# here, the integer part of x/y_heat[i] is automatically taken

H = ndi.gaussian_filter(H,width) # apply Gaussian blur
H = np.transpose(H) # so that the orientation is the same as the scatter plot

plt.imshow(H, origin='lower') # display H as an image
plt.show()

可以在我的github下载对应的python程序。
Time Map上可以找到详细原文说明。

结论

时间图可以揭示潜在的模式,它和直方图一起各自从不同方面反映数据的特征。Picasso在一副画作中通过多个视角全面展示一个物体的内容。类似的,时间图在多重时间尺度上提取内在隐式结构,在数据驱动的今天,它是潜力巨大的工具。

更多的TimeMap应用如下:

原始博客:Time Maps: Visualizing Discrete Events Across Many Timescales

https://rpubs.com/vadimus/bmore_tow_time_maps