神经网络概述

当前,随着AI潮的风起云涌,尤其是今年,也被称之为“人工智能元年”,伴随着各类企业的“智慧”项目,使得机器学习,尤其是深度学习(Deep Learning,简称DL)在算法领域可谓是大红大紫。要学习深度学习,那么首先要熟悉神经网络(Neural Networks,简称NN)的一些基本概念。当然,这里所说的神经网络不是生物学的神经网络,我们将其称之为人工神经网络(Artificial Neural Networks,简称ANN)貌似更为合理。神经网络最早是人工智能领域的一种算法或者说是模型,目前神经网络已经发展成为一类多学科交叉的学科领域,它也随着深度学习取得的进展重新受到重视和推崇。

继续阅读全文 »

机器学习算法常用性能指标

机器学习算法常用性能指标总结

考虑一个二分问题,即将实例分成正类(positive)或负类(negative)。对一个二分问题来说,会出现四种情况。如果一个实例是正类并且也被 预测成正类,即为真正类(True positive),如果实例是负类被预测成正类,称之为假正类(False positive)。相应地,如果实例是负类被预测成负类,称之为真负类(True negative),正类被预测成负类则为假负类(false negative)。

  • TP:正确肯定的数目;
  • FN:漏报,没有正确找到的匹配的数目;
  • FP:误报,给出的匹配是不正确的;
  • TN:正确拒绝的非匹配对数;

列联表如下表所示,1代表正类,0代表负类:
| | 预测1 | 预测0 |
|-|-|-|
| 实际1| True Positive(TP)| False Negative(FN)|
| 实际0|False Positive(FP)|True Negative(TN)|

继续阅读全文 »

最大似然估计总结

最大似然估计总结

最大似然估计方法(Maximum Likelihood Estimate,MLE)也称为极大概似估计或最大似然估计,是求估计的另一种方法,最大概似是1821年首先由德国数学家高斯(C. F. Gauss)提出,但是这个方法通常被归功于英国的统计学家罗纳德·费希尔(R. A. Fisher)。

作用

在已知试验结果(即是样本)的情况下,用来估计满足这些样本分布的参数,把可能性最大的那个参数 θ 作为真实 θ* 的参数估计。说的通俗一点啊,最大似然估计,就是利用已知的样本结果,反推最有可能(最大概率)导致这样结果的参数值(模型已知,参数未知)

继续阅读全文 »

Tensorboard 实例

Tensorboard 实例

构建数据流图

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
import tensorflow as tf
graph = tf.Graph()
with graph.as_default():
with tf.name_scope("variables"):
# 记录数据流图运行次数的 Variable 对象
global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step")
# 追踪该模型的所有输出随时间的累加和的 Variable 对象
total_output = tf.Variable(0.0, dtype=tf.float32, trainable=False, name="total_output")
with tf.name_scope("transformation"):
# 独立的输入层
with tf.name_scope("input"):
# 创建输出占用符, 用于接收一个向量
a = tf.placeholder(tf.float32, shape=[None], name="input_placeholder_a")
# 独立的中间层
with tf.name_scope("intermediate_layer"):
b = tf.reduce_prod(a, name="product_b")
c = tf.reduce_sum(a, name="sum_c")
# 独立的输出层
with tf.name_scope("output"):
output = tf.add(b, c, name="output")
with tf.name_scope("update"):
# 用最新的输出更新 Variable 对象 total_output
update_total = total_output.assign_add(output)
# 将前面的 Variable 对象 global_step 曾1,只要数据流图运行,该操作需要进行
increment_step = global_step.assign_add(1)
with tf.name_scope("summaries"):
avg = tf.div(update_total, tf.cast(increment_step, tf.float32), name="average")
# 为输出节点创建汇总数据
tf.summary.scalar('output_summary', output)
tf.summary.scalar('total_summary', update_total)
tf.summary.scalar('average_summary', avg)
with tf.name_scope("global_ops"):
# 初始化Op
init = tf.initialize_all_variables()
# 将所有汇总数据合并到一个Op中
merged_summaries = tf.summary.merge_all()

运行数据流图

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
# 用明确定义的 Graph 对象启动一个会话
sess = tf.Session(graph=graph)
# 开启一个 summary.FileWriter 对象, 保存汇总数据
writer = tf.summary.FileWriter('./improved_graph', graph)
# 初始化 Variable 对象
sess.run(init)
# 为方便代码复用,定义辅助函数 run_graph()
# 用以输入张量以运行数据流图,并保存汇总数据
def run_graph(input_tensor):
feed_dict = {a: input_tensor}
_, step, summary = sess.run([output, increment_step, merged_summaries], feed_dict=feed_dict)
writer.add_summary(summary, global_step=step)
# 用不同的输入运行该数据流图
run_graph([2, 8])
run_graph([3, 1, 3, 3])
run_graph([8])
run_graph([1, 2, 3])
run_graph([11, 4])
run_graph([4, 1])
run_graph([7, 3, 1])
run_graph([6, 3])
run_graph([0, 2])
run_graph([4, 5, 6])
# 将汇总数据写入磁盘
writer.flush()
# 关闭 summary.FileWriter 对象
writer.close()
# 关闭 Session 对象
sess.close()

TensorBoard Graph 效果:

scalar 将显示汇总 summaries:

TensorFlow学习——TensorFlow Core

Tensorflow core

tf.train API

  • optimizers —— 优化器

TensorFlow 提供了优化器来缓慢地更改每个变量,从而最大程度的降低损耗函数。
eg. gradient descent:

1
2
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

训练线性回归模型完整的代码:

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
# 导入tensorflow
import tensorflow as tf
# 模型参数
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
# 模型输入和输出
x = tf.placeholder(tf.float32)
# 定义线性模型
linear_model = W*x + b
y = tf.placeholder(tf.float32)
# 损耗
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# 优化器:梯度下降
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# 训练集数据
x_train = [1, 2, 3, 4]
y_train = [0, -1, -2, -3]
# 训练循环
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
sess.run(train, {x: x_train, y: y_train})
# 计算训练的准确度
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))

tf.estimator

tf.estimator 作为 TensorFlow 高级库,简化了机器学习的机制,其中包括:

  • 运行训练循环
  • 运行评估循环
  • 管理数据集

tf.estimator 定义了徐福哦常见的模型