https://blog.csdn.net/weixin_44517301/article/details/89605496

https://blog.csdn.net/weixin_44517301/article/details/90176513?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

https://blog.csdn.net/weixin_44517301/article/details/96325200?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

课堂纪要.py day01_deeplearning.py 深度学习day01.pdf

1 machine_learn VS deeping learn

三天 - 图1
三天 - 图2

1.1.1 特征提取

三天 - 图3

1.1.2 数据量 和 计算性能要求

三天 - 图4
三天 - 图5

1.1.3 算法代表

三天 - 图6

1.2 应用场景

三天 - 图7

1.3 tensorflow 总体框架

三天 - 图8
三天 - 图9

1.3.3 tensorflow install

三天 - 图10
用Tensorflow实现加法运算演示数据流图(需要开启会话)

  1. import tensorflow as tf
  2. def tensorflow_demo():
  3. # Tensorflow实现加法
  4. a=tf.constant(2)
  5. b=tf.constant(3)
  6. c=a+b
  7. print("Tensorflow加法运算的结果:\n",c)
  8. #开启会话
  9. with tf.Session() as sess:
  10. c_t=sess.run(c)
  11. print("c_t:\n",c_t)
  12. if __name__ == '__main__':
  13. tensorflow_demo()

三天 - 图11

2 Tensorflow 结构分析

三天 - 图12
三天 - 图13

2.2 图 与 TensorBoard

三天 - 图14

2.2.1 什么是图结构

三天 - 图15

2.2.2 图 相关操作

三天 - 图16
三天 - 图17

  1. import tensorflow as tf
  2. def tensorflow_demo():
  3. # Tensorflow实现加法
  4. a=tf.constant(2)
  5. b=tf.constant(3)
  6. #c=a+b(不提倡直接使用符合运算)
  7. c=tf.add(a,b)
  8. print("Tensorflow加法运算的结果:\n",c)
  9. # 查看默认图
  10. # 方法1:调用方法
  11. default_g = tf.get_default_graph()
  12. print("default_g:\n", default_g)
  13. # 方法2:查看属性
  14. print("a的图属性:\n", a.graph)
  15. print("c的图属性:\n", c.graph)
  16. #开启会话
  17. with tf.Session() as sess:
  18. c_t=sess.run(c)
  19. print("c_t:\n",c_t)
  20. print("sess的图属性:\n",sess.graph)
  21. return None
  22. if __name__ == '__main__':
  23. tensorflow_demo()

三天 - 图18

2.2.3 创建图

三天 - 图19

  1. import tensorflow as tf
  2. def tensorflow_demo():
  3. new_g = tf.get_default_graph()
  4. with new_g.as_default():
  5. a_new = tf.constant(20)
  6. b_new = tf.constant(30)
  7. c_new = a_new+b_new
  8. print("c_new:\n",c_new)
  9. #这时就不能用默认的sesstion了
  10. #开启new_g的会话
  11. with tf.Session(graph=new_g) as new_sess:
  12. c_new_value = new_sess.run(c_new)
  13. print("c_new_value:\n",c_new_value)
  14. print("new_sess的图属性:\n",new_sess.graph)
  15. return None
  16. if __name__ == '__main__':
  17. tensorflow_demo()

2.2.4 TensorBoard: 可视化

三天 - 图20
三天 - 图21
三天 - 图22
三天 - 图23
三天 - 图24

OP

三天 - 图25
三天 - 图26
三天 - 图27
三天 - 图28

2.3 session

三天 - 图29

2.3.1 session

三天 - 图30
三天 - 图31三天 - 图32
三天 - 图33
三天 - 图34
三天 - 图35
三天 - 图36
三天 - 图37

2.4 张量

三天 - 图38

2.4.1 tensor

三天 - 图39
三天 - 图40

三天 - 图41
三天 - 图42

2.4.2 创建 张量

三天 - 图43
三天 - 图44

三天 - 图45

2.4.3 张量的变换

三天 - 图46三天 - 图47
三天 - 图48
三天 - 图49
三天 - 图50
三天 - 图51
三天 - 图52
三天 - 图53

2.4.4 张量的数学运算

三天 - 图54

2.5 变量

三天 - 图55

2.5.1 创建变量

三天 - 图56

三天 - 图57

2.5.2 tf.variable 修改变量的命名空间

三天 - 图58
创建变量、变量的初始化、修改命名空间代码:

  1. import tensorflow as tf
  2. import os
  3. os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
  4. def variable_demo():
  5. """
  6. 变量的演示
  7. :return:
  8. """
  9. # 创建变量
  10. with tf.variable_scope("my_scope"):
  11. a = tf.Variable(initial_value=50)
  12. b = tf.Variable(initial_value=40)
  13. with tf.variable_scope("your_scope"):
  14. c = tf.add(a, b)
  15. print("a:\n", a)
  16. print("b:\n", b)
  17. print("c:\n", c)
  18. # 初始化变量
  19. init = tf.global_variables_initializer()
  20. # 开启会话
  21. with tf.Session() as sess:
  22. # 运行初始化
  23. sess.run(init)
  24. a_value, b_value, c_value = sess.run([a, b, c])
  25. print("a_value:\n", a_value)
  26. print("b_value:\n", b_value)
  27. print("c_value:\n", c_value)
  28. return None
  29. if __name__ == '__main__':
  30. variable_demo()

三天 - 图59

2.6 高级API

三天 - 图60

2.6.1 其他基础 API

三天 - 图61三天 - 图62

2.6.2 高级API

三天 - 图63
三天 - 图64
三天 - 图65

2.7 实现线性回归

三天 - 图66
三天 - 图67

2.7.1 线性回归原理复习

三天 - 图68
三天 - 图69
3 步骤
三天 - 图70

  1. import tensorflow as tf
  2. def linear_regression():
  3. """
  4. 自实现一个线性回归
  5. :return:
  6. """
  7. # 1)准备数据
  8. X = tf.random_normal(shape=[100, 1])
  9. y_true = tf.matmul(X, [[0.8]]) + 0.7
  10. # 2)构造模型
  11. # 定义模型参数 用 变量
  12. weights = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]))
  13. bias = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]))
  14. y_predict = tf.matmul(X, weights) + bias
  15. # 3)构造损失函数
  16. error = tf.reduce_mean(tf.square(y_predict - y_true))
  17. # 4)优化损失
  18. optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(error)
  19. # 显式地初始化变量
  20. init = tf.global_variables_initializer()
  21. # 开启会话
  22. with tf.Session() as sess:
  23. # 初始化变量
  24. sess.run(init)
  25. # 查看初始化模型参数之后的值
  26. print("训练前模型参数为:权重%f,偏置%f,损失为%f" % (weights.eval(), bias.eval(), error.eval()))
  27. #开始训练
  28. for i in range(100):
  29. sess.run(optimizer)
  30. print("第%d次训练后模型参数为:权重%f,偏置%f,损失为%f" % (i+1, weights.eval(), bias.eval(), error.eval()))
  31. return None
  32. if __name__ == '__main__':
  33. linear_regression()

三天 - 图71

三天 - 图72
三天 - 图73
三天 - 图74
三天 - 图75

三天 - 图76

  1. import tensorflow as tf
  2. import os
  3. os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
  4. def linear_regression():
  5. """
  6. 自实现一个线性回归
  7. :return:
  8. """
  9. with tf.variable_scope("prepare_data"):
  10. # 1)准备数据
  11. X = tf.random_normal(shape=[100, 1], name="feature")
  12. y_true = tf.matmul(X, [[0.8]]) + 0.7
  13. with tf.variable_scope("create_model"):
  14. # 2)构造模型
  15. # 定义模型参数 用 变量
  16. weights = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]), name="Weights")
  17. bias = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]), name="Bias")
  18. y_predict = tf.matmul(X, weights) + bias
  19. with tf.variable_scope("loss_function"):
  20. # 3)构造损失函数
  21. error = tf.reduce_mean(tf.square(y_predict - y_true))
  22. with tf.variable_scope("optimizer"):
  23. # 4)优化损失
  24. optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(error)
  25. # 2_收集变量
  26. tf.summary.scalar("error", error)
  27. tf.summary.histogram("weights", weights)
  28. tf.summary.histogram("bias", bias)
  29. # 3_合并变量
  30. merged = tf.summary.merge_all()
  31. # 创建Saver对象
  32. saver = tf.train.Saver()
  33. # 显式地初始化变量
  34. init = tf.global_variables_initializer()
  35. # 开启会话
  36. with tf.Session() as sess:
  37. # 初始化变量
  38. sess.run(init)
  39. # 1_创建事件文件
  40. file_writer = tf.summary.FileWriter("./tmp/linear", graph=sess.graph)
  41. # 查看初始化模型参数之后的值
  42. print("训练前模型参数为:权重%f,偏置%f,损失为%f" % (weights.eval(), bias.eval(), error.eval()))
  43. # #开始训练
  44. # for i in range(100):
  45. # sess.run(optimizer)
  46. # print("第%d次训练后模型参数为:权重%f,偏置%f,损失为%f" % (i+1, weights.eval(), bias.eval(), error.eval()))
  47. #
  48. # # 运行合并变量操作
  49. # summary = sess.run(merged)
  50. # # 将每次迭代后的变量写入事件文件
  51. # file_writer.add_summary(summary, i)
  52. #
  53. # # 保存模型
  54. # if i % 10 ==0:
  55. # saver.save(sess, "./tmp/model/my_linear.ckpt")
  56. # 加载模型
  57. if os.path.exists("./tmp/model/checkpoint"):
  58. saver.restore(sess, "./tmp/model/my_linear.ckpt")
  59. print("训练后模型参数为:权重%f,偏置%f,损失为%f" % (weights.eval(), bias.eval(), error.eval()))
  60. return None
  61. if __name__ == '__main__':
  62. linear_regression()

三天 - 图77三天 - 图78

  1. import tensorflow as tf
  2. # 1)定义命令行参数
  3. tf.app.flags.DEFINE_integer("max_step", 100, "训练模型的步数")
  4. tf.app.flags.DEFINE_string("model_dir", "Unknown", "模型保存的路径+模型名字")
  5. # 2)简化变量名
  6. FLAGS = tf.app.flags.FLAGS
  7. def command_demo():
  8. """
  9. 命令行参数演示
  10. :return:
  11. """
  12. print("max_step:\n", FLAGS.max_step)
  13. print("model_dir:\n", FLAGS.model_dir)
  14. return None
  15. if __name__ == '__main__':
  16. command_demo()

三天 - 图79

三天 - 图80

在主函数中直接使用
三天 - 图81

就可以调用main(argv),功能是显示路径
三天 - 图82