一、人工智能流派

人工智能:让机器具备人的思维和意识。

人工智能三学派:

  1. 行为主义:基于控制论,构建感知-动作控制系统。(控制论,如平衡、行走、避障等自适应控制系统。)
  2. 符号主义:基于算数逻辑表达式,求解问题时先把问题面熟为表达式,再求解表达式。(可用公式描述、实现理性思维,如专家系统。)
  3. 连接主义:仿生学,模仿神经元连接关系。(仿脑神经元连接,实现感性思维,如神经网络。)

用计算机仿出神经网络连接干系关系,让计算机具备感性思维:

  1. 准备数据:采集大量“特征/标签”数据
  2. 搭建网络:搭建神经网络结构
  3. 优化参数:训练网络获取最佳参数(反向传播算法)
  4. 应用网络:将网络保存为模型,输入新数据,输出分类或预测结果(前向传播)

注:反向传播:从后向前,逐层求损失函数对每层神经元参数的偏导数,迭代更新所有参数。

二、tensorflow基础知识

1.张量

神经网络属于 神经网络属于什么主义_随机数

注:有几个中括号就是几阶张量

 

创建一个张量(Tensor):

1 # 创建一个Tensor
2 tf.constant(张量内容,dtype=数据类型(可选))
3 
4 import tensorflow as tf
5 a = tf.tensorflow([1, 5], dtype=tf.int64)
6 print(a)
7 print(a.dtype)
8 print(a.shape)
运行结果:
tf.Tensor([1 5], shape=(2,), dtype=int64)
<dtype: 'int64'>
(2,)

tensorflow的数据类型:

  1. tf.int,  tf.float .....(tf.int 32, tf.float 32, tf.float 64)
  2. tf.bool  ( tf.constant([True, Flase]) )
  3. tf.string (tf.constant("Hello, world!"))
1 # 将numpy的数据类型转换为Tensor数据类型
2 tf.convert_to_tensor(数据名, dtype=数据类型(可选))
3 
4 import tensorflow as tf
5 import numpy as np
6 a = np.arange(0, 5)
7 b = tf.convert_to_tensor(a, dtype=tf.int64)
8 print(a)
9 print(b)

运行结果:
tf.Tensor(
[[0. 0. 0.]
 [0. 0. 0.]], shape=(2, 3), dtype=float32)

tf.Tensor([1. 1. 1. 1.], shape=(4,), dtype=float32)

tf.Tensor(
[[9 9]
 [9 9]], shape=(2, 2), dtype=int32)

注:

  维度:

    一维  直接写个数

    二维  用 [行, 列]

    多维  用 [n, m, j , k , ...]

2.随机生成初始化参数(符合正态分布)

  • 生成正态分布的随机数,默认均值为0,标准差为1

 

tf.random.normal(维度, mean=均值, stddev=标准差)

  • 生成截断式正态分布的随机数

tf.random.truncated_normal(维度,mean=均值,stddev=标准差)

1 # 随机生成初始化参数(符合正态分布)
2 import tensorflow as tf
3 d = tf.random.normal([2, 2], mean=0.5, stddev=1)
4 print(d)
5 
6 e = tf.random.truncated_normal([2, 2],mean=0.5, stddev=1)
7 print(e)

运行结果:
tf.Tensor(
[[0.75011015 0.53881025]
 [0.3881507  0.67407864]], shape=(2, 2), dtype=float32)
tf.Tensor(
[[-0.515918    0.556596  ]
 [ 0.03615129 -0.8332859 ]], shape=(2, 2), dtype=float32)

 

 

  • 生成均匀分布随机数  【minval,maxval)(前闭后开的区间)

tf.random.uniform(维度,minval=最小值,maxval=最大值)

 

f = tf.random.uniform([2, 2],minval=0, maxval=1)
print(f)

运行结果:
tf.Tensor(
[[0.59922206 0.6095402 ]
 [0.4024514  0.5356723 ]], shape=(2, 2), dtype=float32)

 3.常用函数

  • 强制tensor转换为该数据类型

tf.cast(张量名,dtype=数据类型)

  • 计算张量维度上元素的最小值

  tf.reduce_min(张量名)

  • 计算张量维度上元素的最大值

  tf.reduce_max(张量名)

理解axis:

神经网络属于 神经网络属于什么主义_神经网络属于_02

 

1 # 计算张量沿着指定维度的平均值
 2 # tf.reduce_mean(张量名,axis=操作轴)
 3 # 计算张量沿着指定维度的和
 4 # tf.reduce_sum(张量名,axis=操作轴)
 5 
 6 x = tf.constant([[1, 2, 3],
 7                  [2, 2, 3]])
 8 print(x)
 9 print(tf.reduce_mean(x))    # 对x求平均值
10 print(tf.reduce_sum(x,axis=1))  # 对每行求和

运行结果:
  tf.Tensor(
     [[1 2 3]
     [2 2 3]], shape=(2, 3), dtype=int32)
tf.Tensor(2, shape=(), dtype=int32)
tf.Tensor([6 7], shape=(2,), dtype=int32)

 

tf.Variable()  (变量)

  tf.Variable()将变量标记为“可训练”,被标记的变量会在反向传播中记录梯度信息。神经网络训练中,常用该函数标记待训练参数。

 

# tf.Variable(初始值)

w = tf.Variable(tf.random.normal([2, 2], mean=0, stddev=1))

 

Tensorflow中的数学运算

  • 对应元素的四则运算:tf.add,   tf.subtract,   tf.multiply,   tf.divide  (+    -    *     /)
  • 平方、次方与开方:tf.square,   tf.pow,   tf.sqrt
  • 矩阵乘: tf.matmul

神经网络属于 神经网络属于什么主义_随机数_03

 

 

神经网络属于 神经网络属于什么主义_数据类型_04

 

 

神经网络属于 神经网络属于什么主义_数据类型_05


特征与标签配对的函数:

神经网络属于 神经网络属于什么主义_随机数_06

1 # 特征与标签配对的函数
2 feature = tf.constant([12, 23, 10, 17])
3 labels = tf.constant([0, 1, 1 , 0])
4 dataset = tf.data.Dataset.from_tensor_slices((feature,labels))
5 print(dataset)
6 for element in dataset:
7     print(element)

运行结果:
  <TensorSliceDataset shapes: ((), ()), types: (tf.int32, tf.int32)>
(<tf.Tensor: id=9, shape=(), dtype=int32, numpy=12>, <tf.Tensor: id=10, shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: id=11, shape=(), dtype=int32, numpy=23>, <tf.Tensor: id=12, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=13, shape=(), dtype=int32, numpy=10>, <tf.Tensor: id=14, shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: id=15, shape=(), dtype=int32, numpy=17>, <tf.Tensor: id=16, shape=(), dtype=int32, numpy=0>)

 

 

指定函数对某个参数的求导运算:

神经网络属于 神经网络属于什么主义_神经网络属于_07

1 with tf.GradientTape() as tape:
2     w = tf.Variable(tf.constant(3.0))
3     loss = tf.pow(w,2)
4 grad = tape.gradient(loss,w)
5 print(grad)

 

枚举函数:

神经网络属于 神经网络属于什么主义_神经网络属于_08

 

 

tf.one_hot)

神经网络属于 神经网络属于什么主义_tensorflow_09

 

神经网络属于 神经网络属于什么主义_tensorflow_10

 

 

神经网络属于 神经网络属于什么主义_tensorflow_11

 

assign_sub()   (自动更新参数)

神经网络属于 神经网络属于什么主义_数据类型_12

 

tf.argmax()  返回张量沿着指定维度最大值的索引

神经网络属于 神经网络属于什么主义_神经网络属于_13

 

tf.where()函数

 

神经网络属于 神经网络属于什么主义_tensorflow_14

 

np.random.RandomState.rand()  返回一个【0, 1】之间的随机数

神经网络属于 神经网络属于什么主义_随机数_15

np.vstack()   将两个数组纵向叠加

神经网络属于 神经网络属于什么主义_随机数_16

生成网格形状

神经网络属于 神经网络属于什么主义_数据类型_17