tensorflow 2.0 深度学习(第二部分 part1)

逃离我推掉我的手 2023-07-02 04:25 16阅读 0赞

" class="reference-link">20191009191333910.png

日萌社

人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新)


tensorflow 2.0 深度学习(第一部分 part1)

tensorflow 2.0 深度学习(第一部分 part2)

tensorflow 2.0 深度学习(第一部分 part3)

tensorflow 2.0 深度学习(第二部分 part1)

tensorflow 2.0 深度学习(第二部分 part2)

tensorflow 2.0 深度学习(第二部分 part3)

tensorflow 2.0 深度学习 (第三部分 卷积神经网络 part1)

tensorflow 2.0 深度学习 (第三部分 卷积神经网络 part2)

tensorflow 2.0 深度学习(第四部分 循环神经网络)

tensorflow 2.0 深度学习(第五部分 GAN生成神经网络 part1)

tensorflow 2.0 深度学习(第五部分 GAN生成神经网络 part2)

tensorflow 2.0 深度学习(第六部分 强化学习)


神经网络


" class="reference-link">感知机watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 1


全连接层

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 2

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 3

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 4

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 5

  1. import tensorflow as tf
  2. from tensorflow import keras
  3. x = tf.random.normal([2, 3])
  4. model = keras.Sequential([
  5. keras.layers.Dense(2, activation='relu'),
  6. keras.layers.Dense(2, activation='relu'),
  7. keras.layers.Dense(2)
  8. ])
  9. #通过 build 函数完成内部张量的创建,其中None为任意的 batch 数量,3为输入特征长度
  10. model.build(input_shape=[None, 3])
  11. model.summary()
  12. for p in model.trainable_variables:
  13. print(p.name, p.shape)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 6

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 7

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 8


前向传播、反向传播、误差函数

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 9

  1. import tensorflow as tf
  2. from tensorflow import keras
  3. from tensorflow.keras import layers
  4. from tensorflow.keras import datasets
  5. import os
  6. x = tf.random.normal([2,28*28]) #[2,784]
  7. w1 = tf.Variable(tf.random.truncated_normal([784, 256], stddev=0.1))
  8. b1 = tf.Variable(tf.zeros([256]))
  9. o1 = tf.matmul(x,w1) + b1 #[2,784]@[784, 256]+[256]
  10. o1
  11. x = tf.random.normal([4,28*28])#[4,784]
  12. fc1 = layers.Dense(256, activation=tf.nn.relu) #[4,784]@[784,256]+[256]= [b, 784] => [b, 256]
  13. fc2 = layers.Dense(128, activation=tf.nn.relu) #[4,256]@[256,128]+[128]= [b, 256] => [b, 128]
  14. fc3 = layers.Dense(64, activation=tf.nn.relu) #[4,128]@[128,64]+[64]= [b, 128] => [b, 64]
  15. fc4 = layers.Dense(10, activation=None) #[4,64]@[64,10]+[10]= [b, 64] => [b, 10]
  16. h1 = fc1(x)
  17. h2 = fc2(h1)
  18. h3 = fc3(h2)
  19. h4 = fc4(h3)
  20. model = layers.Sequential([
  21. layers.Dense(256, activation=tf.nn.relu) ,
  22. layers.Dense(128, activation=tf.nn.relu) ,
  23. layers.Dense(64, activation=tf.nn.relu) ,
  24. layers.Dense(10, activation=None) ,
  25. ])
  26. out = model(x)# 256*784+256 + 128*256+128 + 64*128+64 + 10*64+10
  27. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
  28. # x: [60k, 28, 28],
  29. # y: [60k]
  30. (x, y), _ = datasets.mnist.load_data()
  31. # x: [0~255] => [0~1.]
  32. x = tf.convert_to_tensor(x, dtype=tf.float32) / 255. #标准化/归一化
  33. y = tf.convert_to_tensor(y, dtype=tf.int32)
  34. print(x.shape, y.shape, x.dtype, y.dtype)
  35. print(tf.reduce_min(x), tf.reduce_max(x))
  36. print(tf.reduce_min(y), tf.reduce_max(y))
  37. train_db = tf.data.Dataset.from_tensor_slices((x,y)).batch(128) #batch构建批量大小
  38. train_iter = iter(train_db)#iter返回生成器对象
  39. sample = next(train_iter)#next调用生成器返回第一个批量大小的数据
  40. print('batch:', sample[0].shape, sample[1].shape)
  41. # [b, 784] => [b, 256] => [b, 128] => [b, 10]
  42. # [dim_in, dim_out], [dim_out] 第N层权重[输入神经元节点数, 输出神经元节点数]、偏置[输出神经元节点数]
  43. # 隐藏层1张量
  44. w1 = tf.Variable(tf.random.truncated_normal([784, 256], stddev=0.1))
  45. b1 = tf.Variable(tf.zeros([256]))
  46. # 隐藏层2张量
  47. w2 = tf.Variable(tf.random.truncated_normal([256, 128], stddev=0.1))
  48. b2 = tf.Variable(tf.zeros([128]))
  49. # 隐藏层3张量
  50. w3 = tf.Variable(tf.random.truncated_normal([128, 64], stddev=0.1))
  51. b3 = tf.Variable(tf.zeros([64]))
  52. # 输出层张量
  53. w4 = tf.Variable(tf.random.truncated_normal([64, 10], stddev=0.1))
  54. b4 = tf.Variable(tf.zeros([10]))
  55. lr = 1e-3 #学习率
  56. #构建epoch训练次数
  57. for epoch in range(10): # iterate db for 10
  58. #遍历生成器对象,每次获取每个批量大小的数据
  59. for step, (x, y) in enumerate(train_db): # for every batch
  60. # x:[128, 28, 28]
  61. # y: [128]
  62. # [b, 28, 28] => [b, 28*28] 展平为 (批量大小,行*列)
  63. x = tf.reshape(x, [-1, 28*28])
  64. #构建梯度记录环境
  65. with tf.GradientTape() as tape: # tf.Variable
  66. # x: [b, 28*28]
  67. # 隐藏层1前向计算,[b, 28*28] => [b, 256]
  68. h1 = x@w1 + tf.broadcast_to(b1, [x.shape[0], 256])
  69. h1 = tf.nn.relu(h1)
  70. # 隐藏层2前向计算,[b, 256] => [b, 128]
  71. h2 = h1@w2 + b2
  72. h2 = tf.nn.relu(h2)
  73. # 隐藏层3前向计算,[b, 128] => [b, 64]
  74. h3 = h2@w3 + b3
  75. h3 = tf.nn.relu(h3)
  76. # 输出层前向计算,[b, 64] => [b, 10]
  77. h4 = h3@w4 + b4
  78. out = h4
  79. # compute loss
  80. # out: [b, 10]
  81. # y: [b] => [b, 10] 真实标签one-hot化
  82. y_onehot = tf.one_hot(y, depth=10)
  83. # mse = mean(sum(y-out)^2)
  84. # [b, 10] 均方差mse = mean(sum(y-out)^2) 预测值与真实值之差的平方的平均值
  85. loss = tf.square(y_onehot - out)
  86. # mean: scalar
  87. loss = tf.reduce_mean(loss)
  88. #1.求导,tape.gradient(y,[参数θ])求参数θ相对于y的梯度信息
  89. # dy_dw = tape.gradient(y, [w])
  90. #2.通过tape.gradient(loss,[参数θ])函数求得网络参数θ的梯度信息
  91. # grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])
  92. # 根据loss对模型所有参数求导 tape.gradient(loss, model.trainable_variables)
  93. # compute gradients
  94. grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3, w4, b4])
  95. # print(grads)
  96. # w1 = w1 - lr * w1_grad 优化器规则,根据 模型参数θ = θ - lr * grad 更新网络参数
  97. w1.assign_sub(lr * grads[0])
  98. b1.assign_sub(lr * grads[1])
  99. w2.assign_sub(lr * grads[2])
  100. b2.assign_sub(lr * grads[3])
  101. w3.assign_sub(lr * grads[4])
  102. b3.assign_sub(lr * grads[5])
  103. w4.assign_sub(lr * grads[6])
  104. b4.assign_sub(lr * grads[7])
  105. if step % 100 == 0:
  106. print(epoch, step, 'loss:', float(loss))

#


激活函数(非线性函数)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 10

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 11

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 12

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 13

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 14

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 15


" class="reference-link">输出层设计watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 16

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 17

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 18

  1. import tensorflow as tf
  2. help(tf.losses.categorical_crossentropy)
  3. 查看categorical_crossentropy函数的默认参数列表和使用方法介绍
  4. 其中形参默认为from_logits=False,网络预测值y_pred 表示必须为经过了 Softmax函数的输出值。
  5. from_logits 设置为 True 时,网络预测值y_pred 表示必须为还没经过 Softmax 函数的变量 z
  6. from_logits=True 标志位将softmax激活函数实现在损失函数中,便不需要手动添加softmax损失函数,提升数值计算稳定性。
  7. Help on function categorical_crossentropy in module tensorflow.python.keras.losses:
  8. categorical_crossentropy(y_true, y_pred, from_logits=False, label_smoothing=0)
  9. Computes the categorical crossentropy loss.
  10. Args:
  11. y_true: tensor of true targets.
  12. y_pred: tensor of predicted targets.
  13. from_logits: Whether `y_pred` is expected to be a logits tensor. By default,
  14. we assume that `y_pred` encodes a probability distribution.

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 19


" class="reference-link">loss误差函数watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 20

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 21

  1. import tensorflow as tf
  2. x=tf.random.normal([1,3])
  3. w=tf.ones([3,2])
  4. b=tf.ones([2])
  5. y = tf.constant([0, 1])
  6. with tf.GradientTape() as tape:
  7. tape.watch([w, b])
  8. logits = tf.sigmoid(x@w+b) #非线性激活函数sigmoid(线性函数x@w+b)
  9. loss = tf.reduce_mean(tf.losses.MSE(y, logits)) #MSE均方差损失函数(y真实值-预测值)
  10. #通过loss对模型参数θ的梯度
  11. grads = tape.gradient(loss, [w, b])
  12. print('w grad:', grads[0])
  13. print('b grad:', grads[1])
  14. import tensorflow as tf
  15. tf.random.set_seed(4323) #随机种子
  16. x=tf.random.normal([1,3])
  17. w=tf.random.normal([3,2])
  18. b=tf.random.normal([2])
  19. y = tf.constant([0, 1])
  20. with tf.GradientTape() as tape:
  21. tape.watch([w, b])
  22. logits = (x@w+b) #线性函数x@w+b
  23. #from_logits默认为from_logits=False,网络预测值y_pred 表示必须为经过了 Softmax函数的输出值。
  24. #当 from_logits 设置为 True 时,网络预测值y_pred 表示必须为还没经过 Softmax 函数的变量z。
  25. loss = tf.reduce_mean(tf.losses.categorical_crossentropy(y, logits, from_logits=True))
  26. #通过loss对模型参数θ的梯度
  27. grads = tape.gradient(loss, [w, b])
  28. print('w grad:', grads[0])
  29. print('b grad:', grads[1])
  30. import tensorflow as tf
  31. from tensorflow import keras
  32. from tensorflow.keras import datasets, layers
  33. import os
  34. a = tf.random.normal([4,35,8]) # 模拟成绩册A
  35. b = tf.random.normal([6,35,8]) # 模拟成绩册B
  36. tf.concat([a,b],axis=0) # 合并成绩册 TensorShape([10, 35, 8])
  37. x = tf.random.normal([2,784])
  38. w1 = tf.Variable(tf.random.truncated_normal([784, 256], stddev=0.1))
  39. b1 = tf.Variable(tf.zeros([256]))
  40. o1 = tf.matmul(x,w1) + b1 #[2,784]@[784, 256]+[256]=[2, 256]
  41. o1 = tf.nn.relu(o1)
  42. o1 #TensorShape([2, 256])
  43. x = tf.random.normal([4,28*28])
  44. # 创建全连接层,指定输出节点数和激活函数
  45. fc = layers.Dense(512, activation=tf.nn.relu)
  46. h1 = fc(x) # 通过fc类完成一次全连接层的计算
  47. vars(fc)
  48. x = tf.random.normal([4,4])
  49. # 创建全连接层,指定输出节点数和激活函数
  50. fc = layers.Dense(3, activation=tf.nn.relu)
  51. h1 = fc(x) # 通过fc类完成一次全连接层的计算
  52. fc.non_trainable_variables
  53. embedding = layers.Embedding(10000, 100) #词汇表单词数量为10000,每个单词的维度为100
  54. x = tf.ones([25000,80])
  55. embedding(x)
  56. z = tf.random.normal([2,10]) # 构造输出层的输出
  57. y_onehot = tf.constant([1,3]) # 构造真实值
  58. y_onehot = tf.one_hot(y_onehot, depth=10) # 真实值one-hot编码 TensorShape([2, 10])
  59. # from_logits=True表示 输出层未使用Softmax函数,故from_logits设置为True
  60. loss = keras.losses.categorical_crossentropy(y_onehot,z,from_logits=True)
  61. loss = tf.reduce_mean(loss) # 计算平均交叉熵损失
  62. loss
  63. # from_logits=True表示 输出层未使用Softmax函数,故from_logits设置为True
  64. criteon = keras.losses.CategoricalCrossentropy(from_logits=True) # 计算平均交叉熵损失
  65. loss = criteon(y_onehot,z) # 计算损失
  66. loss
  67. import tensorflow as tf
  68. y = tf.constant([1, 2, 3, 0, 2])
  69. y = tf.one_hot(y, depth=4) #TensorShape([5, 4])
  70. y = tf.cast(y, dtype=tf.float32)
  71. out = tf.random.normal([5, 4])
  72. # mse = mean(sum(y-out)^2) 均方差mse = mean(sum(y-out)^2) 预测值与真实值之差的平方的平均值
  73. loss1 = tf.reduce_mean(tf.square(y-out))
  74. #tf.norm(a)默认为执行L2范数tf.norm(a. ord=2),等同于tf.sqrt(tf.reduce_sum(tf.square(a)))
  75. loss2 = tf.square(tf.norm(y-out)) / (5*4)
  76. # MSE 函数返回的是每个样本的均方差,需要在样本数量上再次平均来获得 batch 的均方差
  77. # loss = keras.losses.MSE(y_onehot, o) # 计算均方差
  78. # loss = tf.reduce_mean(loss) # 计算 batch 均方差
  79. loss3 = tf.reduce_mean(tf.losses.MSE(y, out))
  80. print(loss1) #tf.Tensor(0.96629584, shape=(), dtype=float32)
  81. print(loss2) #tf.Tensor(0.9662959, shape=(), dtype=float32)
  82. print(loss3) #tf.Tensor(0.96629584, shape=(), dtype=float32)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 22

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 23

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 24

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 25

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 26

  1. import tensorflow as tf
  2. help(tf.losses.categorical_crossentropy)
  3. 查看categorical_crossentropy函数的默认参数列表和使用方法介绍
  4. 其中形参默认为from_logits=False,网络预测值y_pred 表示必须为经过了 Softmax函数的输出值。
  5. from_logits 设置为 True 时,网络预测值y_pred 表示必须为还没经过 Softmax 函数的变量 z
  6. from_logits=True 标志位将softmax激活函数实现在损失函数中,便不需要手动添加softmax损失函数,提升数值计算稳定性。
  7. Help on function categorical_crossentropy in module tensorflow.python.keras.losses:
  8. categorical_crossentropy(y_true, y_pred, from_logits=False, label_smoothing=0)
  9. Computes the categorical crossentropy loss.
  10. Args:
  11. y_true: tensor of true targets.
  12. y_pred: tensor of predicted targets.
  13. from_logits: Whether `y_pred` is expected to be a logits tensor. By default,
  14. we assume that `y_pred` encodes a probability distribution.

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 27


神经网络类型

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 28


油耗预测实战

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 29

  1. #统计数据
  2. sns.pairplot(train_dataset[["Cylinders", "Displacement", "Weight", "MPG"]], diag_kind="kde")
  3. #查看训练集的输入X的统计数据
  4. train_stats = train_dataset.describe()
  5. # MPG Cylinders Displacement Horsepower ... Model Year USA Europe Japan
  6. #count 314.000000 314.000000 314.000000 314.000000 ... 314.000000 314.000000 314.000000 314.000000
  7. #mean 23.310510 5.477707 195.318471 104.869427 ... 75.898089 0.624204 0.178344 0.197452
  8. #std 7.728652 1.699788 104.331589 38.096214 ... 3.675642 0.485101 0.383413 0.398712
  9. #min 10.000000 3.000000 68.000000 46.000000 ... 70.000000 0.000000 0.000000 0.000000
  10. #25% 17.000000 4.000000 105.500000 76.250000 ... 73.000000 0.000000 0.000000 0.000000
  11. #50% 22.000000 4.000000 151.000000 94.500000 ... 76.000000 1.000000 0.000000 0.000000
  12. #75% 28.950000 8.000000 265.750000 128.000000 ... 79.000000 1.000000 0.000000 0.000000
  13. #max 46.600000 8.000000 455.000000 225.000000 ... 82.000000 1.000000 1.000000 1.000000

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 30

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9henVzYS5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70 31

  1. from __future__ import absolute_import, division, print_function, unicode_literals
  2. import pathlib
  3. import os
  4. import matplotlib.pyplot as plt
  5. import pandas as pd
  6. import seaborn as sns
  7. import tensorflow as tf
  8. from tensorflow import keras
  9. from tensorflow.keras import layers, losses
  10. print(tf.__version__) #2.0.0
  11. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
  12. # 在线下载汽车效能数据集
  13. dataset_path = keras.utils.get_file("auto-mpg.data", "http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data")
  14. # 效能(公里数每加仑),气缸数,排量,马力,重量
  15. # 加速度,型号年份,产地
  16. column_names = ['MPG','Cylinders','Displacement','Horsepower','Weight',
  17. 'Acceleration', 'Model Year', 'Origin']
  18. raw_dataset = pd.read_csv(dataset_path, names=column_names,
  19. na_values = "?", comment='\t',
  20. sep=" ", skipinitialspace=True)
  21. # MPG Cylinders Displacement Horsepower Weight Acceleration Model Year Origin
  22. #0 18.0 8 307.0 130.0 3504.0 12.0 70 1
  23. #1 15.0 8 350.0 165.0 3693.0 11.5 70 1
  24. #2 18.0 8 318.0 150.0 3436.0 11.0 70 1
  25. #3 16.0 8 304.0 150.0 3433.0 12.0 70 1
  26. #4 17.0 8 302.0 140.0 3449.0 10.5 70 1
  27. dataset = raw_dataset.copy()
  28. # 查看部分数据
  29. dataset.tail()
  30. dataset.head()
  31. dataset
  32. # 统计空白数据,并清除
  33. dataset.isna().sum()
  34. dataset = dataset.dropna()
  35. dataset.isna().sum()
  36. dataset #[392 rows x 8 columns]
  37. # 处理类别型数据,其中origin列代表了类别1,2,3,分布代表产地:美国、欧洲、日本
  38. # 其弹出这一列
  39. origin = dataset.pop('Origin')
  40. # 根据origin列来写入新列
  41. dataset['USA'] = (origin == 1)*1.0
  42. dataset['Europe'] = (origin == 2)*1.0
  43. dataset['Japan'] = (origin == 3)*1.0
  44. dataset.tail()
  45. # MPG Cylinders Displacement Horsepower Weight Acceleration Model Year USA Europe Japan
  46. #0 18.0 8 307.0 130.0 3504.0 12.0 70 1.0 0.0 0.0
  47. #1 15.0 8 350.0 165.0 3693.0 11.5 70 1.0 0.0 0.0
  48. #2 18.0 8 318.0 150.0 3436.0 11.0 70 1.0 0.0 0.0
  49. #3 16.0 8 304.0 150.0 3433.0 12.0 70 1.0 0.0 0.0
  50. #4 17.0 8 302.0 140.0 3449.0 10.5 70 1.0 0.0 0.0
  51. # 切分为训练集和测试集
  52. train_dataset = dataset.sample(frac=0.8,random_state=0) #[314 rows x 10 columns]
  53. test_dataset = dataset.drop(train_dataset.index) #[78 rows x 10 columns]
  54. #统计数据
  55. sns.pairplot(train_dataset[["Cylinders", "Displacement", "Weight", "MPG"]], diag_kind="kde")
  56. #查看训练集的输入X的统计数据
  57. train_stats = train_dataset.describe()
  58. # MPG Cylinders Displacement Horsepower ... Model Year USA Europe Japan
  59. #count 314.000000 314.000000 314.000000 314.000000 ... 314.000000 314.000000 314.000000 314.000000
  60. #mean 23.310510 5.477707 195.318471 104.869427 ... 75.898089 0.624204 0.178344 0.197452
  61. #std 7.728652 1.699788 104.331589 38.096214 ... 3.675642 0.485101 0.383413 0.398712
  62. #min 10.000000 3.000000 68.000000 46.000000 ... 70.000000 0.000000 0.000000 0.000000
  63. #25% 17.000000 4.000000 105.500000 76.250000 ... 73.000000 0.000000 0.000000 0.000000
  64. #50% 22.000000 4.000000 151.000000 94.500000 ... 76.000000 1.000000 0.000000 0.000000
  65. #75% 28.950000 8.000000 265.750000 128.000000 ... 79.000000 1.000000 0.000000 0.000000
  66. #max 46.600000 8.000000 455.000000 225.000000 ... 82.000000 1.000000 1.000000 1.000000
  67. train_stats.pop("MPG")
  68. #train_stats['列值字段名']:该方式为通过使用['列值字段名']访问的是列值,不能直接使用['行值字段名']访问行值。
  69. #如要使用行值字段名访问行值的话,必须先transpose()反转张量的行和列,那么本来的行值字段名和行值都变成了列值字段名和列值,
  70. #本来的列值字段名和列值都变成了行值字段名和行值,那么现在便可以使用['原来的行值字段名']访问原来的行值。
  71. #(8, 9) 表示8行统计指标值,9列特征值 => (9, 8) 表示9行特征值,8列统计指标值
  72. train_stats = train_stats.transpose()
  73. train_stats #train_stats['列值字段名']:可以使用train_stats['count'],不可以使用train_stats['Cylinders']
  74. # count mean std min 25% 50% 75% max
  75. #Cylinders 314.0 5.477707 1.699788 3.0 4.00 4.0 8.00 8.0
  76. #Displacement 314.0 195.318471 104.331589 68.0 105.50 151.0 265.75 455.0
  77. #Horsepower 314.0 104.869427 38.096214 46.0 76.25 94.5 128.00 225.0
  78. #Weight 314.0 2990.251592 843.898596 1649.0 2256.50 2822.5 3608.00 5140.0
  79. #Acceleration 314.0 15.559236 2.789230 8.0 13.80 15.5 17.20 24.8
  80. #Model Year 314.0 75.898089 3.675642 70.0 73.00 76.0 79.00 82.0
  81. #USA 314.0 0.624204 0.485101 0.0 0.00 1.0 1.00 1.0
  82. #Europe 314.0 0.178344 0.383413 0.0 0.00 0.0 0.00 1.0
  83. #Japan 314.0 0.197452 0.398712 0.0 0.00 0.0 0.00 1.0
  84. # 移动MPG油耗效能这一列为真实标签Y
  85. train_labels = train_dataset.pop('MPG') #(314,)
  86. test_labels = test_dataset.pop('MPG') #(78,)
  87. # 标准化数据
  88. def norm(x):
  89. return (x - train_stats['mean']) / train_stats['std']
  90. #>>> train_stats['mean'].shape
  91. #(9,)
  92. #>>> train_stats['mean']
  93. #Cylinders 5.477707
  94. #Displacement 195.318471
  95. #Horsepower 104.869427
  96. #Weight 2990.251592
  97. #Acceleration 15.559236
  98. #Model Year 75.898089
  99. #USA 0.624204
  100. #Europe 0.178344
  101. #Japan 0.197452
  102. #Name: mean, dtype: float64
  103. #>>> train_stats['std'].shape
  104. #(9,)
  105. #>>> train_stats['std']
  106. #Cylinders 1.699788
  107. #Displacement 104.331589
  108. #Horsepower 38.096214
  109. #Weight 843.898596
  110. #Acceleration 2.789230
  111. #Model Year 3.675642
  112. #USA 0.485101
  113. #Europe 0.383413
  114. #Japan 0.398712
  115. #Name: std, dtype: float64
  116. normed_train_data = norm(train_dataset)
  117. normed_test_data = norm(test_dataset)
  118. print(normed_train_data.shape,train_labels.shape) #(314, 9) (314,)
  119. print(normed_test_data.shape, test_labels.shape) #(78, 9) (78,)
  120. class Network(keras.Model):
  121. # 回归网络
  122. def __init__(self):
  123. super(Network, self).__init__()
  124. # 创建3个全连接层
  125. self.fc1 = layers.Dense(64, activation='relu')
  126. self.fc2 = layers.Dense(64, activation='relu')
  127. self.fc3 = layers.Dense(1)
  128. def call(self, inputs, training=None, mask=None):
  129. # 依次通过3个全连接层
  130. x = self.fc1(inputs)
  131. x = self.fc2(x)
  132. x = self.fc3(x)
  133. return x
  134. model = Network()
  135. model.build(input_shape=(None, 9))
  136. model.summary()
  137. optimizer = tf.keras.optimizers.RMSprop(0.001)
  138. train_db = tf.data.Dataset.from_tensor_slices((normed_train_data.values, train_labels.values))
  139. train_db = train_db.shuffle(100).batch(32)
  140. # 未训练时测试
  141. # example_batch = normed_train_data[:10]
  142. # example_result = model.predict(example_batch)
  143. # example_result
  144. train_mae_losses = [] #MAE
  145. test_mae_losses = []
  146. #构建epoch训练次数
  147. for epoch in range(200):
  148. #遍历生成器对象,每次获取每个批量大小的数据
  149. for step, (x,y) in enumerate(train_db):
  150. #构建梯度记录环境
  151. with tf.GradientTape() as tape:
  152. out = model(x)
  153. loss = tf.reduce_mean(losses.MSE(y, out)) #MSE
  154. mae_loss = tf.reduce_mean(losses.MAE(y, out)) #MAE
  155. if step % 10 == 0:
  156. print(epoch, step, float(loss))
  157. #1.求导,tape.gradient(y,[参数θ])求参数θ相对于y的梯度信息
  158. # dy_dw = tape.gradient(y, [w])
  159. #2.通过tape.gradient(loss,[参数θ])函数求得网络参数θ的梯度信息
  160. # grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])
  161. # 根据loss对模型所有参数求导 tape.gradient(loss, model.trainable_variables)
  162. grads = tape.gradient(loss, model.trainable_variables)
  163. #优化器规则,根据 模型参数θ = θ - lr * grad 更新网络参数
  164. optimizer.apply_gradients(zip(grads, model.trainable_variables))
  165. train_mae_losses.append(float(mae_loss))
  166. out = model(tf.constant(normed_test_data.values))
  167. test_mae_losses.append(tf.reduce_mean(losses.MAE(test_labels, out)))
  168. plt.figure()
  169. plt.xlabel('Epoch')
  170. plt.ylabel('MAE')
  171. plt.plot(train_mae_losses, label='Train')
  172. plt.plot(test_mae_losses, label='Test')
  173. plt.legend()
  174. # plt.ylim([0,10])
  175. plt.legend()
  176. plt.savefig('auto.svg')
  177. plt.show()

发表评论

表情:
评论列表 (有 0 条评论,16人围观)

还没有评论,来说两句吧...

相关阅读