小行星

热爱平淡,向往未知


  • 首页

  • 分类

  • 归档

  • 关于

  • 阅读排行

  • 搜索

tensorflow自定义梯度

发表于 2018-11-Tue | 阅读次数:
1
2
3
4
5
6
7
opt = GradientDescentOptimizer(learning_rate=0.1)

grads_and_varas = opt.compute_gradients(loss, list_of_vars)

capped_grads_and_vars = [(fun(gv[0]), gv[1]) for gv in grads_and_vars]

opt.apply_gradients(capped_grads_and_vars)

tf.graph 初始化

发表于 2018-11-Tue | 阅读次数:
1
2
3
g = tf.Graph()
with g.as_default():
xxx
1
2
with tf.Graph().as_default() as g:
xxx

tensorflow scope理解

发表于 2018-11-Tue | 阅读次数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import tensorflow as tf
def scoping(fn, scope1, scope2, vals):
with fn(scope1):
a = tf.Variable(vals[0], name='a')
b = tf.get_variable('b', initializer=vals[1])
c = tf.constant(vals[2], name='c')
with fn(scope2):
d = tf.add(a * b, c, name='res')

print '\n '.join([scope1, a.name, b.name, c.name, d.name]), '\n'
return d

d1 = scoping(tf.variable_scope, 'scope_vars', 'res', [1, 2, 3])
d2 = scoping(tf.name_scope, 'scope_name', 'res', [1, 2, 3])

with tf.Session() as sess:
writer = tf.summary.FileWriter('logs', sess.graph)
sess.run(tf.global_variables_initializer())
print sess.run([d1, d2])
writer.close()

从上面的可以看出来,tf.variable_scope()为所有变量(不管你是怎么创建的)、操作(ops)、常量(constant)添加一个前缀,而tf.name_scope()会忽视使用tf.get_variable()创建的变量,因为它假设你知道你使用的变量位于哪个scope中。

tensorflow 图理解

发表于 2018-11-Tue | 阅读次数:
1
2
3
4
5
6
import tensorflow as tf

a = tf.constant(1, name="a")
b = tf.constant(2, name="b")
c = a + b
print(tf.get_default_graph().as_graph_def())
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
node {
name: "a"
op: "Const"
attr {
key: "dtype"
value {
type: DT_INT32
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_INT32
tensor_shape {
}
int_val: 1
}
}
}
}
node {
name: "b"
op: "Const"
attr {
key: "dtype"
value {
type: DT_INT32
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_INT32
tensor_shape {
}
int_val: 2
}
}
}
}
node {
name: "add"
op: "Add"
input: "a"
input: "b"
attr {
key: "T"
value {
type: DT_INT32
}
}
}
versions {
producer: 27
}

tensorflow tensor运算

发表于 2018-11-Tue | 阅读次数:
1
2
3
tf.multiply()
tf.negative()
tf.subtract()

clean code

发表于 2018-11-Mon | 阅读次数:

设计模式六大原则

开放封闭原则

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
public class BankProcess
{
public void Deposite(){} //存款
public void Withdraw(){} //取款
public void Transfer(){} //转账
}

public class BankStaff
{
private BankProcess bankpro = new BankProcess();
public void BankHandle(Client client)
{
switch (client .Type)
{
case "deposite": //存款
bankpro.Deposite();
break;
case "withdraw": //取款
bankpro.Withdraw();
break;
case "transfer": //转账
bankpro.Transfer();
break;
}
}
}

使用一个接口
//首先声明一个业务处理接口
public interface IBankProcess
{
void Process();
}
public class DeposiProcess:IBankProcess
{
public void Process() //办理存款业务
{
Console.WriteLine("Process Deposit");
}
}
public class WithDrawProcess:IBankProcess
{
public void Process() //办理取款业务
{
Console.WriteLine("Process WithDraw");
}
}
public class TransferProcess:IBankProcess
{
public void Process() //办理转账业务
{
Console .WriteLine ("Process Transfer");
}
}
public class BankStaff
{
private IBankProcess bankpro = null ;
public void BankHandle(Client client)
{
switch (client .Type)
{
case "Deposite": //存款
userProc =new WithDrawUser();
break;
case "WithDraw": //取款
userProc =new WithDrawUser();
break;
case "Transfer": //转账
userProc =new WithDrawUser();
break;
}
userProc.Process();
}
}

python中的使用

1
2
3
4
5
6
7
8
9
10
from abc import ABCMeta, abstractmethod

class IStream(metaclass=ABCMeta):
@abstractmethod
def read(self, maxbytes=-1):
pass

@abstractmethod
def write(self, data):
pass

https://python3-cookbook.readthedocs.io/zh_CN/latest/c08/p12_define_interface_or_abstract_base_class.html

单一职责

原因是,如果一个函数做两件事情,那么如果一个函数发生了变化,那么其他函数也可能被影响。

依赖倒置原则

体现一:高层模块不应该依赖低层模块。两个都应该依赖抽象。
大概就是定义一堆接口

迪米特法则

低耦合

里氏替换原则

普通 虚函数使用办法

###

变量命名

遵守信息学里面的墒最小原理。

  1. 不要加入类型说明,因为变量自身说明了类型
  2. 能搜索到,不要是其他变量的一个子字符串。
  3. 不要用一些只有自己能懂得奇怪的用法、
  4. 习惯一致。使用controller, manager, driver
  5. 一个单词不要用于多个目的
  6. 因为都是程序员读代码,所以使用一些专业术语
  7. 分解每一个很长的函数,变成多个小函数,然后每个函数的函数名都能直接看出意思
  8. 不要多此一举,如果是一个GSD的应用。那么就不要给每个变量前面都加一个GSD。

函数

  1. 短小, 更加短小
  2. 每个函数做一件事
  3. if else while 里面都是一个函数调用
  4. 不要传入标示参数,True/False, 分成2个函数

对象和数据结构

mac 突然没声音

发表于 2018-11-Mon | 阅读次数:
1
sudo killall coreaudiod

latex 画图配色

发表于 2018-11-Tue | 阅读次数:
1
2
blue: #0067b5
red: #d44918

plt latex

发表于 2018-11-Tue | 阅读次数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np
import matplotlib.pyplot as plt


# Example data
t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(4 * np.pi * t) + 2

plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.plot(t, s)

plt.xlabel(r'\textbf{time} (s)')
plt.ylabel(r'\textit{voltage} (mV)',fontsize=16)
plt.title(r"\TeX\ is Number "
r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
fontsize=16, color='gray')
# Make room for the ridiculously large title.
plt.subplots_adjust(top=0.8)

plt.savefig('tex_demo')
plt.show()

latex公式 nonumber

发表于 2018-11-Tue | 阅读次数:
1
\nonumber
1…678…59
fangyh

fangyh

最爱的是那苍穹之外的浩渺宇宙

588 日志
4 分类
66 标签
© 2020 fangyh
由 Hexo 强力驱动
|
主题 — NexT.Mist v5.1.3
|本站总访问量次