上药三品,神与气精

曾因酒醉鞭名马 生怕情多累美人


  • 首页

  • 关于

  • 分类

  • 标签

  • 归档

  • 搜索

tensorflow foundation

发表于 2017-09-17 | 阅读次数:
字数统计: 707 | 阅读时长 ≈ 2

1. 基础架构

自底向上分为:

  • 网络层和设备层
  • 数据操作层
  • 图计算层
  • API 层
  • 应用层

1.1 网络通信层和设备管理层

网络通信层包括 gRPC 和 RDMA ,都是分布式计算时需要用到的。
设备管理层包括 tf 分别在 CPU GPU FPGA 等设备上的实现,也就是对上层提供了一个统一的接口,使得上层只需要处理卷积等逻辑,而不需要关心在硬件上的卷积的实现过程。

1.2 数据操作层

包括卷积函数、激活函数等操作。

1.3 图计算层

本地计算图和分布式计算图的实现

1.4 API 层

python实现和其他语言的实现

1.5 应用层

主要是训练相关类库和预测相关类库

2. 设计理念

2.1 图的定义和图的实现完全分开

符号式编程涉及很多的嵌入和优化,不容易理解和调试,但是运行速度有所提升。

2.2 涉及的运算全部放在图中,图的运行只是发生在会话 session 中。

下面是举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

import tensorflow as tf

a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
c = a * b

sess = tf.Session()

print sess.run(c)
sess.close()

# 当然 一般写法是这样
with tf.Session() as sess:
print sess.run(c)
  1. 编程模型

运行原理:

  • 输入input
  • 塑形reshape
  • Relu层
  • Logit层
  • Softmax
  • 交叉熵cross entropy
  • 梯度gradient
  • SGD 训练

从输入开始,经过塑形后,一层一层进行前向传播运算。Relu层里会有两个参数,在输出前使用Relu激活函数来做非线性处理。然后进入Logit层,学习两个参数。用softmax来计算输出结果中各个类别的概率分布。用交叉熵来度量两个概率分布之间的相似性。然后开始计算梯度,以及交叉墒后的结果。随后进入SGD训练,也就是反向传播的过程,从上往下计算每一层的参数,依次进行更新。

tensorflow 顾名思义就是指“张量的流动”,数据流图是由节点node和边edge组成的有向无环图。

3.1 边

张量具有数据属性 如tf.float32等。

3.2 节点

节点的运算操作有数学、数组等。详细代码在源码tensorflow/python/ops/目录下。

3.3 设备

设备device是指一块可以用来运算且拥有自己地址空间的硬件。

3.4 变量

变量variable是一种特殊的数据,在图中有固定的位置,不像张量那样可以流动。

3.5 内核

操作是对抽象操作的一个统称,而内核kernel则是能够运行在特定设备上的一种对操作的实现。

texas poker

发表于 2017-09-02 | 阅读次数:
字数统计: 1.2k | 阅读时长 ≈ 7

德州扑克比较大小的设计,设计全面还是有一些繁琐的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#! /usr/bin/env python
# _*_ coding:utf8 _*_

import random


def poker(hands):
"Return a list of winning hands: poker([hand,...]) => [hand,...]"
return allmax(hands, key=hand_rank)


def allmax(iterable, key=None):
"Return a list of all items equal to the max of the iterable."
iterable.sort(key=key,reverse=True)
result = [iterable[0]]
maxValue = key(iterable[0]) if key else iterable[0]
for value in iterable[1:]:
v = key(value) if key else value
if v == maxValue: result.append(value)
else: break
return result


def card_ranks(hand):
"Return a list of the ranks, sorted with higher first."
ranks = ['--23456789TJQKA'.index(r) for r, s in hand]
ranks.sort(reverse = True)
return [5, 4, 3, 2, 1] if (ranks == [14, 5, 4, 3, 2]) else ranks


# 同花色
def flush(hand):
"Return True if all the cards have the same suit."
suits = [s for r,s in hand]
return len(set(suits)) == 1


# 顺子
def straight(ranks):
"Return True if the ordered ranks form a 5-card straight."
return (max(ranks)-min(ranks) == 4) and len(set(ranks)) == 5


# 条子
def kind(n, ranks):
"""Return the first rank that this hand has exactly n-of-a-kind of.
Return None if there is no n-of-a-kind in the hand."""

for r in ranks:
if ranks.count(r) == n: return r
return None


# 两对
def two_pair(ranks):
"If there are two pair here, return the two ranks of the two pairs, else None."
pair = kind(2, ranks)
lowpair = kind(2, list(reversed(ranks)))
if pair and lowpair != pair:
return (pair, lowpair)
else:
return None


def hand_rank(hand):
"Return a value indicating the ranking of a hand."
ranks = card_ranks(hand)
if straight(ranks) and flush(hand):
return (8, max(ranks))
elif kind(4, ranks):
return (7, kind(4, ranks), kind(1, ranks))
elif kind(3, ranks) and kind(2, ranks):
return (6, kind(3, ranks), kind(2, ranks))
elif flush(hand):
return (5, ranks)
elif straight(ranks):
return (4, max(ranks))
elif kind(3, ranks):
return (3, kind(3, ranks), ranks)
elif two_pair(ranks):
return (2, two_pair(ranks), ranks)
elif kind(2, ranks):
return (1, kind(2, ranks), ranks)
else:
return (0, ranks)


def hand_rank_alt(hand):
"Return a value indicating how high the hand ranks."
# count is the count of each rank; ranks lists corresponding ranks
# E.g. '7 T 7 9 7' => counts = (3, 1, 1) ranks = (7, 10, 9)
groups = group(['--23456789TJQKA'.index(r) for r,s in hand])
counts, ranks = unzip(groups)
if ranks == (14, 5, 4, 3, 2): # Ace low straight
ranks = (5, 4, 3, 2, 1)
straight = len(ranks) == 5 and max(ranks) - min(ranks) == 4
flush = len(set([s for r,s in hand])) == 1
return (9 if (5,) == counts else
8 if straight and flush else
7 if (4, 1) == counts else
6 if (3, 2) == counts else
5 if flush else
4 if straight else
3 if (3, 1, 1) == counts else
2 if (2, 2, 1) == counts else
1 if (2, 1, 1, 1) == counts else
0), ranks


count_rankings = {(5,): 10, (4, 1): 7, (3, 2): 6, (3, 1, 1): 3, (2, 2, 1): 2,
(2, 1, 1, 1): 1, (1, 1, 1, 1, 1): 0}


def hand_rank_table(hand):
"Return a value indicating how high the hand ranks."
# count is the count of each rank; ranks lists corresponding ranks
# E.g. '7 T 7 9 7' => counts = (3, 1, 1) ranks = (7, 10, 9)
groups = group(['--23456789TJQKA'.index(r) for r,s in hand])
counts, ranks = unzip(groups)
if ranks == (14, 5, 4, 3, 2): # Ace low straight
ranks = (5, 4, 3, 2, 1)
straight = len(ranks) == 5 and max(ranks) - min(ranks) == 4
flush = len(set([s for r,s in hand])) == 1
return max(count_rankings[counts], 4*straight + 5*flush), ranks


def group(items):
"Return a list of [(count, x), ...], highest count first, then highest x first"
groups = [(items.count(x), x) for x in set(items)]
return sorted(groups, reverse = True)


def unzip(iterable):
"Return a list of tuples from a list of tuples : e.g. [(2, 9), (2, 7)] => [(2, 2), (9, 7)]"
return zip(*iterable)


mydeck = [r+s for r in '23456789TJQKA' for s in 'SHDC']


def deal(numhands, n=5, deck=mydeck):
random.shuffle(mydeck)
return [mydeck[n*i:n*(i+1)] for i in range(numhands)]


hand_names = ["Straight flush",
"Four of a kind",
"Full house",
"Flush",
"Straight",
"Three of a kind",
"Two pair",
"One pair",
"High card"]


def hand_percentages(n=700*1000):
counts = [0] * 9
for i in range(n/10):
for hand in deal(10):
ranking = hand_rank(hand)[0]
counts[ranking] += 1
for i in reversed(range(9)):
print "%15s: %6.3f %%" % (hand_names[i], 100.*counts[i]/n)


def test():
"Test cases for the functions in poker program."
sf1 = "6C 7C 8C 9C TC".split() # Straight Flush
sf2 = "6D 7D 8D 9D TD".split() # Straight Flush
fk = "9D 9H 9S 9C 7D".split() # Four of a Kind
fh = "TD TC TH 7C 7D".split() # Full House
tp = "5D 2C 2H 9H 5C".split() # Two Pair

# Testing allmax
assert allmax([2,4,7,5,1]) == [7]
assert allmax([2,4,7,5,7]) == [7,7]
assert allmax([2]) == [2]
assert allmax([0,0,0]) == [0,0,0]

# Testing card_ranks
assert card_ranks(sf1) == [10, 9, 8, 7, 6]
assert card_ranks(fk) == [9, 9, 9, 9, 7]
assert card_ranks(fh) == [10, 10, 10, 7, 7]

# Testing flush
assert flush([]) == False
assert flush(sf1) == True
assert flush(fh) == False

# Testing straight
assert straight(card_ranks(sf1)) == True
assert straight(card_ranks(fk)) == False

# Testing kind
assert kind(3, card_ranks(sf1)) == None
assert kind(4, card_ranks(fk)) == 9

# Tesing two pair
assert two_pair(card_ranks(sf1)) == None
assert two_pair(card_ranks(tp)) == (5,2)

# Testing group
assert group([2,3,4,6,2,1,9]) == [(2,2),(1,9),(1,6),(1,4),(1,3),(1,1)]
assert group([8,8,8,8]) == [(4,8)]
assert group([2,6,1]) == [(1,6),(1,2),(1,1)]

# Testing unzip
assert unzip([(2,2),(1,9),(1,6),(1,4),(1,3),(1,1)]) == [(2,1,1,1,1,1),(2,9,6,4,3,1)]
assert unzip([(1,6),(1,2),(1,1)]) == [(1,1,1),(6,2,1)]
assert unzip([(2, 9), (2, 7)]) == [(2, 2), (9, 7)]

# Testing hand rank
assert hand_rank(sf1) == (8,10)
assert hand_rank(fk) == (7,9,7)
assert hand_rank(fh) == (6,10,7)

# Testing hand rank alt
assert hand_rank_alt(sf1) == (8, (10,9,8,7,6))
assert hand_rank_alt(fk) == (7,(9,7))
assert hand_rank_alt(fh) == (6,(10,7))

# Testing hand rank table
assert hand_rank_table(sf1) == (9, (10,9,8,7,6))
assert hand_rank_table(fk) == (7,(9,7))
assert hand_rank_table(fh) == (6,(10,7))

# Testing poker
assert poker([sf1, fk, fh]) == [sf1]
assert poker([fk, fh]) == [fk]
assert poker([fh, fh]) == [fh, fh]
assert poker([fh]) == [fh]
assert poker([sf2] + 99*[fh]) == [sf2]
assert poker([sf1, sf2, fk, fh]) == [sf1, sf2]

return 'tests pass'

vim table cheatsheet

发表于 2017-08-27 | 阅读次数:
字数统计: 1.1k | 阅读时长 ≈ 4
命令 描述
进入 vim -
vim filename 打开或者新建文件,光标置于第一行行首
vim +n filename n行行首
vim + filename 最后一行行首
vim +/pattern filename 第一个与pattern匹配的串处
vim -r filename 恢复
vim filename … filename 打开多个
vim 配置 -
all 列出所有选项设置情况
term 设置终端类型
ignorance 忽略大小写
list 显示制表位和行尾标志
number 显示行号
report 显示由面向行的命令修改过的数目
terse 显示简短的警告信息
warn 显示no write信息
nomagic 搜索模式使用前面不带\的特殊字符
nowrapscan 禁止vi在搜索到达文件两端时,又从另一端开始
mesg 允许vi显示其他用户用write写到自己终端上的信息
:set number 显示行号 加no不显示
:set ruler 标尺 有no
:set hisearch 高亮显示查找到的单词
:syntax on 语法高亮
:set tabstop=8 tab大小为8
:set softtabstop=8 4: 4个空格,8:正常制表符,16:两个制表符
:set autoindent 自动缩进
:set cindent c语言格式自动缩进
移动光标 -
k nk 上
j nj 下
h nh 左
l nl 右
Space 光标右移一个字符
BackSpace 左移
Enter 下移动一行
w/W 右移动一个字至字首
b/B 左移
e/E 右移到字尾
) 句尾
( 句首
} 段落结尾
{ 开头
n$ n行尾
H 屏幕顶行
M 中间行
L 最后行
0 当前行首
^ 第一个非空字符上
$ 当前行尾
gg 第一行
G 最后一行
f 当前行的字符a上
F 相反
% 匹配的符号如()、[]、{}、<>等
nG n行上
G 最后一行
屏幕滚动 -
ctrl + u 向文件首翻半屏
ctrl + d 尾
ctrl + f 文件尾一屏
ctrl + b 首一屏
nz 将第n行滚动至屏幕顶部,不指定n行时将当前行滚动到屏幕顶部
插入文本 -
i 光标前
I 行首
a 光标后
A 行尾
o 当前行之下新开一行
O 之上
r 替换当前字符
R 替换当前字符以及以后的字符,直到按esc键
s 从当前光标位置处开始,以输入的文本替代指定数目的字符
S 删除指定数目的行,并以所输入的文本代替之
ncw/W 修改指定数目的字
nCC 修改指定数目的行
删除命令 -
x/X 删除一个字符,一个后一个前
dw 删除一个单词
dnw n个
dne 删除到单词尾
do 删除到行首
d$ 行尾
dd 删除当前行
ndd 当前行和后面n-1行
dnl 向右删除n个字母
dnh 左
dnj 下n行(n不含当前行)
dnk 上n行
cnw[word] 将n个word改变为word
C$ 改变到行尾
cc 改变整行
shift+j 删除行尾的换行符,下一行接上来了
复制粘贴 -
p 粘贴用x或者d删除的文本
ynw 复制n个单词
yy 复制一行
ynl n个字符
y$ 复制当前光标至行尾处
nyy 拷贝n行
撤销 -
u 撤销前一次的操作
shift+u 撤销对该行的所有操作
搜索以及替换 -
/pattern 光标开始处向文件尾搜索
?pattern 文件首
n 在同一个方向重复上一次搜索命令
N 反方向
cw newword 替换为newword
n 继续查找
. 执行替换
:s/p1/p2/g 将当前行中所有p1均用p2替代
:n1,n2 s/p1/p2/g n1到n2行
:g/p1/s/p2/g 文件中所有
:1,$ s/string1/string2/g 全文中替换
书签 -
m[a-z] 标记
`a 移动到标记a处
visual模式 -
v 进入模式
V 行的
ctrl+v 进入块操作
行方式命令 -
:n1,n2 co n3 n1到n2行拷贝到第n3行下
:n1,n2 m n3 移动
:n1,n2 d 删除
:n1,n2 wlcommand 作为command的输入并执行之
宏 -
q[a-z] q终止录宏
reg 显示当前所定义的所有宏
窗口操作 -
:split 分割一个
:split file.c 为另一个file.c分隔窗口
:nsplit file.c 并且指定行数
ctrl+w 在窗口中切换
:close 关闭当前窗口
文件及其他 -
:q 退出
:q! 强制
:e filename 打开文件并且进行编辑
:e! 放弃修改,并重载
:w 保存
:wq 存盘退出
:ZZ 保存退出
:!command 执行shell命令command
:r!command 输出结果放到当前行
:n1,n2 write temp.c
:read file.c 将文件file.c的内容放到当前光标所在的下面

make a forum with django

发表于 2017-08-24 | 阅读次数:
字数统计: 126 | 阅读时长 ≈ 1

每次想写点东西的时候,总是自己骗自己说,哎呀,哪有时间去搞这些东西。

决定还是一点点尝试写一个论坛系统出来。

Django这个框架非常庞大,可能在实际的开发过程中,我们用到的只是一小部分,比如创建model,view,然后根据这些写一些html的文件等。

再在页面调试的过程当中发现一些问题,对代码进行不断地修正。

wechat-delete-friends

发表于 2017-08-03 | 阅读次数:
字数统计: 66 | 阅读时长 ≈ 1

在终端中执行:

1
2
3
4
5
git  clone  https://github.com/0x5e/wechat-deleted-friends.git

cd wechat-deleted-friends

python wdf.py

然后按照提示操作就可以了.

完成之后手动删除生成的一个群聊, 不要在里面说话, 不说话好友们是不知情的.

1…102103104…109
John Cheung

John Cheung

improve your python skills

543 日志
33 分类
45 标签
RSS
GitHub Email
© 2020 John Cheung
本站访客数:
|
主题 — NexT.Pisces v5.1.4
博客全站共226.3k字