上药三品,神与气精

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


  • 首页

  • 关于

  • 分类

  • 标签

  • 归档

  • 搜索

深度学习的核心组件

发表于 2018-09-12 | 分类于 AI | 阅读次数:
字数统计: 210 | 阅读时长 ≈ 1

1.张量
2.基于张量的操作
3.计算图和优化
4.自动微分工具
5.BLAS/cuBLAS和cuDNN的扩展

  • 简单说 张量是n维矩阵的概括,可以简单认为是n维数组。(rgb位图是高 宽 通道数 的三维张量,2583203)
  • 作为扩展 100张图片 可以表示为4d张量 前面第一个是图像的id
  • 张量对象以张量形式存储数据,numpy.imread imsave将图像作为ndarrays 读取并将ndarrays存储为图像。
  • 神经网络可以被认为是在输入张量上执行一系列操作以给出输出,学习是通过纠正网络产生的输出和预期输出的误差来完成的,这些操作可能很简单,如矩阵乘法(sigmoid)或者更复杂,如卷积、池化或者LSTM

binary_search_tree

发表于 2018-09-11 | 分类于 algorithm | 阅读次数:
字数统计: 591 | 阅读时长 ≈ 3

对树节点 二叉搜索树的简单抽象

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
# -*- coding: utf-8 -*-


class TreeNode(object):
def __init__(self, key, value, parent=None, left_node=None, right_node=None, balance_factor=0):
self.key = key
self.payload = value
self.left_child = left_node
self.right_child = right_node
self.parent = parent
self.balance_factor = balance_factor

def has_left_child(self):
return self.left_child

def has_right_child(self):
return self.right_child

def is_left_child(self):
return self.parent and self.parent.left_child == self

def is_right_child(self):
return self.parent and self.parent.right_child == self

def is_root(self):
return not self.parent

def is_leaf(self):
return not (self.right_child or self.left_child)

def has_any_children(self):
return self.right_child or self.left_child

def has_both_children(self):
return self.right_child or self.left_child

def replace_node_data(self, key, value, lc, rc):
self.key = key
self.payload = value
self.left_child = lc
self.right_child = rc
if self.has_left_child():
self.left_child.parent = self
if self.has_right_child():
self.right_child.parent = self


class BinarySearchTree(object):
def __init__(self):
self.root = None
self.size = 0

def length(self):
return self.size

def __len__(self):
return self.size

# 中序遍历
def in_order(self, node):
if node.left_child:
self.inorder(node.left_child)
self.print_node(node)
if node.right_child:
self.inorder(node.right_child)

def level_order(self, node):
nodes = []
nodes.append(node)
while len(nodes) > 0:
current_node = nodes.pop(0)
self.print_node(current_node)
if current_node.left_child:
nodes.append(current_node.left_child)
if current_node.right_child:
nodes.append(current_node.right_child)

def print_node(self, node):
if node.parent:
print([node.key,
node.payload,
node.parent.key])
else:
print([node.key, node.payload])

# --------------------- 插入节点 ----------------
def put(self, key, value):
if self.root:
self._put(key, value, self.root)
else:
self.root = TreeNode(key, value)
self.size += 1

def _put(self, key, value, current_node):
if key < current_node.key:
if current_node.has_left_child():
self._put(key, value, current_node.left_child)
else:
current_node.left_child = TreeNode(key, value, parent=current_node)

else:
if current_node.has_right_child():
self._put(key, value, current_node.right_child)
else:
current_node.right_child = TreeNode(key, value, parent=current_node)
# --------------------- 插入节点 ----------------

def __setitem__(self, key, value):
self.put(key, value)

# --------------------- 删除节点 ----------------
def find_min(self): # Gets minimum node (leftmost leaf) in a subtree
current_node = self
while current_node.left_child:
current_node = current_node.left_child
return current_node

def replace_node_in_parent(self, new_value=None):
if self.parent:
if self == self.parent.left_child:
self.parent.left_child = new_value
else:
self.parent.right_child = new_value
if new_value:
new_value.parent = self.parent

def binary_tree_delete(self, key):
if key < self.key:
self.left_child.binary_tree_delete(key)
elif key > self.key:
self.right_child.binary_tree_delete(key)
else: # delete the key here
if self.left_child and self.right_child: # if both children are present
successor = self.right_child.find_min()
self.key = successor.key
successor.binary_tree_delete(successor.key)
elif self.left_child: # if the node has only a *left* child
self.replace_node_in_parent(self.left_child)
elif self.right_child: # if the node has only a *right* child
self.replace_node_in_parent(self.right_child)
else: # this node has no children
self.replace_node_in_parent(None)
# --------------------- 删除节点 ----------------

def get(self, key):
if self.root:
res = self._get(key, self.root)
if res:
return res.payload
else:
return None
else:
return None

def _get(self, key, current_node):
if not current_node:
return None
elif current_node.key == key:
return current_node
elif key < current_node.key:
return self._get(key, current_node.left_child)
else:
return self._get(key, current_node.right_child)


def run():
print('test bst')
mytree = BinarySearchTree()
mytree[3] = "red"
mytree[4] = "blue"
mytree[2] = "rat"
mytree[5] = "cat"
mytree[1] = "dog"

mytree.level_order(mytree.root)


if __name__ == '__main__':
run()

二叉搜索树的处理

发表于 2018-09-11 | 分类于 algorithm | 阅读次数:
字数统计: 76 | 阅读时长 ≈ 1

一种特殊的二叉树,它满足下面的性质:任何一个节点的key值都比它左子树上的节点的key值要大,但是比它右子树上的节点的key值要小。节点查找,插入,删除等操作的时间复杂度都是O(lgn)

ansible-自定义模块开发

发表于 2018-09-11 | 分类于 web | 阅读次数:
字数统计: 284 | 阅读时长 ≈ 1

Ansible就没有 ansible_private_ipv4_address 这样一个Facts,用来保存私网IP地址。 而我们恰恰就需要这样的一个Facts,因为我们有很多服务器的默认网卡并非是eth0,有的是bond0,eth1,em0,em1等,而公网IP地址与私网IP地址也并没有固定的绑定在某个网卡上,很多时候还是虚拟网卡。 还好,我们可以通过编写Ansible模块并自定义Facts来实现。

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
#!/usr/bin/python

import json
import commands
import re


def get_ansible_private_ipv4_address():
iprex = "(^192\.168)|(^10\.)|(^172\.1[6-9])|(^172\.2[0-9])|(^172\.3[0-1])"
output = commands.getoutput("""/sbin/ifconfig |grep "Link encap" |awk '{print $1}' |grep -wv 'lo'""")
nics = output.split('\n')
for i in nics:
ipaddr = commands.getoutput("""/sbin/ifconfig %s |grep -w "inet addr" |cut -d: -f2 | awk '{print $1}'""" % (i))
if re.match(iprex,ipaddr):
ansible_private_ipv4_address = ipaddr
return ansible_private_ipv4_address

def main():
global module
module = AnsibleModule(
argument_spec = dict(
get_facts=dict(default="yes", required=False),
),
supports_check_mode = True,
)

ansible_facts_dict = {
"changed" : False,
"ansible_facts": {
}
}

if module.params['get_facts'] == 'yes':
ansible_private_ipv4_address = get_ansible_private_ipv4_address()
ansible_facts_dict['ansible_facts']['ansible_private_ipv4_address'] = ansible_private_ipv4_address

print json.dumps(ansible_facts_dict)

from ansible.module_utils.basic import *
from ansible.module_utils.facts import *
main()

tensorflow-interview-problem

发表于 2018-09-10 | 阅读次数:
字数统计: 444 | 阅读时长 ≈ 2
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
import numpy as np 
import pandas as pd

import tensorflow as tf


NUM_DIGITS = 10


def binary_encode(i, num_digits):
return np.array([i >> d & 1 for d in range(num_digits)])


def fizz_buzz_encode(i):
if i % 15 == 0:
return np.array([0, 0, 0, 1])
elif i % 5 == 0:
return np.array([0, 0, 1, 0])
elif i % 3 == 0:
return np.array([0, 1, 0, 0])
else:
return np.array([1, 0, 0, 0])


train_X = np.array([binary_encode(i, NUM_DIGITS) for i in range(101, 2 ** NUM_DIGITS)])
train_y = np.array([fizz_buzz_encode(i) for i in range(101, 2 ** NUM_DIGITS)])


def init_weights(shape):
return tf.Variable(tf.random_normal(shape, stddev=0.01))


# Our model is a standard 1-hidden-layer multi-layer-perceptron with ReLU
# activation. The softmax (which turns arbitrary real-valued outputs into
# probabilities) gets applied in the cost function.
def model(X, w_h, w_o):
h = tf.nn.relu(tf.matmul(X, w_h))
return tf.matmul(h, w_o)

# Our variables. The input has width NUM_DIGITS, and the output has width 4.
X = tf.placeholder("float", [None, NUM_DIGITS])
Y = tf.placeholder("float", [None, 4])

# How many units in the hidden layer.
NUM_HIDDEN = 100

# Initialize the weights.
w_h = init_weights([NUM_DIGITS, NUM_HIDDEN])
w_o = init_weights([NUM_HIDDEN, 4])

# Predict y given x using the model.
py_x = model(X, w_h, w_o)

# We'll train our model by minimizing a cost function.
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))
# print("cost is: ", cost)
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

# And we'll make predictions by choosing the largest output.
predict_op = tf.argmax(py_x, 1)

# Finally, we need a way to turn a prediction (and an original number)
# into a fizz buzz output
def fizz_buzz(i, prediction):
return [str(i), "fizz", "buzz", "fizzbuzz"][prediction]

BATCH_SIZE = 128

# Launch the graph in a session
with tf.Session() as sess:
tf.initialize_all_variables().run()

# how many times for train?
for epoch in range(10000):
# Shuffle the data before each training iteration.
p = np.random.permutation(range(len(train_X)))
train_X, train_y = train_X[p], train_y[p]

# Train in batches of 128 inputs.
for start in range(0, len(train_X), BATCH_SIZE):
end = start + BATCH_SIZE
sess.run(train_op, feed_dict={X: train_X[start:end],
Y: train_y[start:end]})

# And print the current accuracy on the training data.
print(epoch, np.mean(np.argmax(train_y, axis=1) ==
sess.run(predict_op, feed_dict={X: train_X,
Y: train_y})))

# And now for some fizz buzz
numbers = np.arange(1, 101)
teX = np.transpose(binary_encode(numbers, NUM_DIGITS))
teY = sess.run(predict_op, feed_dict={X: teX})
output = np.vectorize(fizz_buzz)(numbers, teY)

print(output)
1…747576…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字