Press "Enter" to skip to content

## 什幺是神经网络？

`loss = y’ – f1(f2(f3(f4(x))))`

loss函数本身是数学公式表达的，因此对loss求各个系数的导数也是数学公式推导问题，因此网络中所有权重系数均可以得到调整，即向样本进一步拟合。

## 以tensorflow为例

`inputs=tf.keras.Input(shape=(2,),dtype=tf.float32,name=’input’)`

`dense1=tf.keras.layers.Dense(1,activation=None,name=’dense1′)  dense2=tf.keras.layers.Dense(2,activation=None,name=’dense2′)  dense3=tf.keras.layers.Dense(1,activation=None,name=’dense3′)`

```dense层的数学公式都是y=wx+b，不同神经元个数的区别是w和b系数的形状。
dense1有1个神经元，假设w=[ [0.3], [0.4] ]，b=0.2，输出y长相为[0.24]
dense2有2个神经元，假设w=[ [0.3, 0.6], [0.4, 0.12]]，b=[0.1, 0.35]，输出y长相为[0.1, 0.2]
w的行数与x的列数相等，w的列数与神经元的个数相等，这些w和b矩阵的初始化都由tensorflow自行判断生成。```

`outputs1=dense1(inputs)  outputs2=dense2(inputs)  outputs3=tf.keras.layers.concatenate([outputs1,outputs2],axis=1,name=’concat’)  outputs4=dense3(outputs3)`

```model=tf.keras.Model(inputs=inputs,outputs=outputs4)
model.summary()```

```Model: "functional_1"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input (InputLayer)              [(None, 2)]          0
__________________________________________________________________________________________________
dense1 (Dense)                  (None, 1)            3           input[0][0]
__________________________________________________________________________________________________
dense2 (Dense)                  (None, 2)            6           input[0][0]
__________________________________________________________________________________________________
concat (Concatenate)            (None, 3)            0           dense1[0][0]
dense2[0][0]
__________________________________________________________________________________________________
dense3 (Dense)                  (None, 1)            4           concat[0][0]
==================================================================================================
Total params: 13
Trainable params: 13
Non-trainable params: 0```

concat层没有需要学习的参数，因为K就是一个长度为3的[1,1,1]常量向量。

`tf.keras.utils.plot_model(model, “graph.png”, show_shapes=True)`

```x=tf.random.normal(shape=(10,2))  y=tf.random.normal(shape=(10,1))  print(x)  print(y)
tf.Tensor(  [[-0.8464397 -0.3152412 ]  [ 0.9817092 -0.57270414]  [ 0.86039394 0.57590604]  [-1.5055276 0.45981622]  [ 1.40179 1.0307338 ]  [ 0.5882102 2.671993 ]  [ 0.5666892 -0.33787787]  [ 0.36999676 0.5678155 ]  [ 2.131917 0.33147094]  [-0.23225114 0.84211487]], shape=(10, 2), dtype=float32)  tf.Tensor(  [[-0.9018226 ]  [-0.83541167]  [-0.70780784]  [ 0.43620512]  [-1.2712636 ]  [ 0.39236164]  [ 0.11044435]  [ 2.7505376 ]  [ 0.64985305]  [-1.4352192 ]], shape=(10, 1), dtype=float32)```

```pred_y = model(data)  print(pred_y)
tf.Tensor(  [[ 1.4824426 ]  [ 0.5423604 ]  [-1.0151732 ]  [ 2.2172787 ]  [-1.9427378 ]  [-1.8751484 ]  [ 2.486527 ]  [ 0.03885526]  [-0.62673503]  [ 0.23725389]], shape=(10, 1), dtype=float32)```

```loss_f = tf.keras.losses.MeanAbsoluteError()
with tf.GradientTape() as tape:
pred_y = model(data) # 模型计算
loss = loss_f(y, pred_y) # 计算误差
print(loss) # 打印损失
grads=tape.gradient(loss, model.trainable_variables) # 反向传播误差到各层, 对各权重系数求梯度
print(grads)```

`tf.Tensor(1.6826286, shape=(), dtype=float32)  [<tf.Tensor: shape=(2, 1), dtype=float32, numpy=  array([[ 0.37924558],  [-0.17607355]], dtype=float32)>, <tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>, <tf.Tensor: shape=(2, 2), dtype=float32, numpy=  array([[-0.28494048, 1.0743711 ],  [ 0.13229021, -0.4988016 ]], dtype=float32)>, <tf.Tensor: shape=(2,), dtype=float32, numpy=array([0., 0.], dtype=float32)>, <tf.Tensor: shape=(3, 1), dtype=float32, numpy=  array([[ 0.02184648],  [ 1.1978055 ],  [-0.9128995 ]], dtype=float32)>, <tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>]`

optimizer = tf.keras.optimizers.Adam(learning_rate=0.01)

optimizer.apply_gradients(zip(grads, model.trainable_variables)) # 将梯度应用到各个权重系数

```pred_y = model(data) # 使用学习后的模型再预测  print(pred_y)
tf.Tensor(  [[ 1.4320835 ]  [ 0.53110886]  [-0.9584385 ]他们说是  [ 2.1270993 ]  [-1.888143 ]  [-1.8089024 ]  [ 2.3986485 ]  [ 0.03384402]  [-0.5806757 ]  [ 0.22927463]], shape=(10, 1), dtype=float32)```

```loss = loss_f(y, pred_y)  print(loss)
tf.Tensor(1.6360016, shape=(), dtype=float32)```