4. 神经网络
4.1. TensorFlow
4.1.1. TensorFlow 简介
由Google开发,可以先绘制计算结构图,神经网络中最好用的库之一
4.1.1.1. 安装
pip install tensorflow
4.1.1.2. 神经网络在干嘛
机器学习,其实就是让电脑尝试模拟已知的数据,不断改进拟合的参数,提高拟合度
模型通过学习数据,得到能表达数据的参数,然后对另外给的数据作出预测
4.1.2. Tensorflow 基础架构
4.1.2.1. 结构
向量流图(tensor flow graphs)
4.1.2.2. 例子
线性回归预测的是一个连续值,逻辑回归预测的是一个“是”或“否”的值
分类问题,最好使用交叉熵损失函数,其刻画的是实际输出(概率)与期望输出(概率)的距离
假设概率分布p为期望输出,概率分布q为实际输出,H(p,q)为交叉熵,则:
H
(
p
,
q
)
=
−
∑
x
p
(
x
)
l
o
g
q
(
x
)
H(p,q)=-\sum_x p(x) log q(x)
H(p,q)=−x∑p(x)logq(x)
线性回归
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('../dataset/Advertising.csv')
print(data.head())
# plt.figure()
# s1 = plt.scatter(data.TV, data.sales)
# s2 = plt.scatter(data.radio, data.sales)
# s3 = plt.scatter(data.newspaper, data.sales)
# plt.legend(handles=[s1, s2, s3], labels=['tv', 'radio', 'newspaper'])
# plt.show()
x = data.iloc[:, 1:-1]
y = data.iloc[:, -1]
model = tf.keras.Sequential([tf.keras.layers.Dense(10, input_shape=(3,), activation='relu'),
tf.keras.layers.Dense(1)])
model.summary()
model.compile(optimizer='adam',
loss='mse')
model.fit(x, y, epochs=1000)
test = data.iloc[:5, 1:-1]
print(data.iloc[:5, :])
print(model.predict(test))
逻辑回归
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('../dataset/credit-a.csv', header=None)
print(data.head())
print(data.iloc[:, -1].value_counts())
x = data.iloc[:, :-1]
y = data.iloc[:, -1].replace(-1, 0)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(4, input_shape=(15,), activation='relu'))
model.add(tf.keras.layers.Dense(4, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
model.summary()
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['acc'])
history = model.fit(x, y, epochs=100)
# l1 = plt.plot(history.epoch, history.history.get('loss'))
l2 = plt.plot(history.epoch, history.history.get('acc'))
# plt.legend(handles=[l1, l2], labels=['loss', 'acc'])
plt.show()
4.1.2.3. Session 会话控制
矩阵乘法:
tf.matmul(matrix1, matrix2)
# 类似于np中的dot
np.dot(matrix1, matrix2)
例子
import tensorflow as tf
# 不加这一句的话,tf.matmul会直接执行
tf.compat.v1.disable_eager_execution()
m1 = tf.constant([[3, 3]])
m2 = tf.constant([[2],
[2]])
product = tf.matmul(m1, m2)
print(product)
with tf.compat.v1.Session() as sess:
res = sess.run(product)
print(res)
4.1.2.4. Variable 变量
v2 版本默认开启 eager(热烈的、火热的,直接运算) 模式
v1 版本必须通过 session.run 才能真正启动运算
4.1.2.5. Placeholder
已过时
4.1.2.6. Activation Function 激活函数
激活函数主要为了解决日常生活中不能用线性函数所概况,且又不容易找到非线性函数的问题,它可以让线性函数弯曲;激活函数其实是一个非线性函数
linear function: 直线或平面方程
nonlinear function: 曲线或曲面方程
y = AF(wx+b)
常用激活函数:relu, sigmoid, tanh
4.1.2.7. tf 中的激活函数
激活函数位于 tf.keras.activations 或 tf.nn 中