人工智能实践课程-北京大学

  • 名称:人工智能实践课程-北京大学
  • 分类:人工智能  
  • 观看人数:加载中
  • 时间:2019/10/18 15:30:36

这是人工智能入门课,将用八次课帮你梳理人工智能概念、机器学习方法、深度学习框架。如果你还不知道什么是人工智能、机器学习、深度学习,欢迎进来学习交流。结课时,你将会用Python搭建人工神经网络,实现特定物体的识别。一起编码感受人工智能 机器学习 深度学习吧!

什么是机器学习

如果一个程序可在任务T上,随经验E的增加,效果P随之增加,则认为这个程序可以从经验中学习。

Tensorflow

基于Tensorflow的神经网络

张量表示数据,用计算图搭建神经网络,用会话执行计算图,优化线上的权重(参数),得到模型。

张量、计算图、会话和简单的Tensorflow例子

import tensorflow as tf
a = tf.constant([1.0, 2.0])b = tf.constant([3.0, 4.0])result = a + bprint(result)with tf.Session() as sess:
    print(sess.run(result))

结果为:

Tensor("add_3:0", shape=(2,), dtype=float32)[4. 6.]

再有

import tensorflow as tf
a = tf.constant([[1.0, 2.0]])b = tf.constant([[3.0], [4.0]])result = tf.matmul(a, b)print(result)with tf.Session() as sess:
    print(sess.run(result))

结果为:

Tensor("MatMul:0", shape=(1, 1), dtype=float32)[[11.]]

张量(tensor):多维数组(列表) 阶:张量的维数
计算图(Graph):搭建神经网络的计算过程,只搭建不运算
会话(Session):执行计算图中的节点运算

参数 tf.Variable

w = tf.Variable(tf.random_normal([2,3], stddev=2, mean=0, seed=1))

stdev 标准差; mean 均值; seed 随机数种子;
tf.random_normal 表示 正态分布
tf.truncated_normal() 去掉过大偏离点的正态分布
tf.random_uniform() 平均分布

变量的初始化

init_op = tf.global_variables_initializer()sess.run(init_op)

tf.placeholder占位,在sese.run中用feed_dict喂数据

x = tf.placeholder(tf.float32, shape=(1,2))sess.run(y, feed_dict={x:[[0.5,0.6]]})x = tf.placeholder(tf.float32, shape=(None,2))sess.run(y, feed_dict={x:[[0.1,0.2], [0.3,0.4], [0.5,0.6]]})

神经网络实现过程

  1. 准备数据集,提取特征,作为输入喂给神经网络

  2. 搭建NN结构,从输入到输出(先搭建计算图,再用会话执行)前向传播算法计算输出

  3. 大量特征数据喂给NN,迭代优化NN参数,反向传播算法优化参数训练模型

  4. 将训练好的模型投入实际使用

两层简单神经网络示例

import tensorflow as tf
x = tf.placeholder(tf.float32, shape=(None,2))w1 = tf.Variable(tf.random_normal([2,3], stddev=1, seed=1))w2 = tf.Variable(tf.random_normal([3,1], stddev=1, seed=1))a = tf.matmul(x,w1)y = tf.matmul(a,w2)with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    print(sess.run(y, feed_dict={x:[[0.7,0.5], [0.2,0.3], [0.3,0.4], [0.4,0.5]]}))
    print(sess.run(w1))
    print(sess.run(w2))