{"id":14958790,"url":"https://github.com/cookeem/tensorflow_learning_notes","last_synced_at":"2025-04-07T05:11:28.785Z","repository":{"id":139312469,"uuid":"103598590","full_name":"cookeem/TensorFlow_learning_notes","owner":"cookeem","description":"tensorflow学习笔记，来源于电子书：《Tensorflow实战Google深度学习框架》","archived":false,"fork":false,"pushed_at":"2017-10-12T07:36:24.000Z","size":7370,"stargazers_count":391,"open_issues_count":2,"forks_count":138,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-07T05:11:13.392Z","etag":null,"topics":["deep-learning","tensorflow","tensorflow-tutorials"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cookeem.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-15T01:17:07.000Z","updated_at":"2025-01-26T14:33:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"7e568242-9f2a-4510-9e6a-9309dab5b982","html_url":"https://github.com/cookeem/TensorFlow_learning_notes","commit_stats":{"total_commits":57,"total_committers":1,"mean_commits":57.0,"dds":0.0,"last_synced_commit":"187a93bba93b74a44641c9d3f0990adb5236fdbc"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cookeem%2FTensorFlow_learning_notes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cookeem%2FTensorFlow_learning_notes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cookeem%2FTensorFlow_learning_notes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cookeem%2FTensorFlow_learning_notes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cookeem","download_url":"https://codeload.github.com/cookeem/TensorFlow_learning_notes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247595335,"owners_count":20963943,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","tensorflow","tensorflow-tutorials"],"created_at":"2024-09-24T13:18:17.013Z","updated_at":"2025-04-07T05:11:28.755Z","avatar_url":"https://github.com/cookeem.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tensorflow学习笔记\n\n- 学习内容来源于：[caicloud / tensorflow-tutorial](https://github.com/caicloud/tensorflow-tutorial)\n\n- 电子书：《Tensorflow实战Google深度学习框架》\n\n![Tensorflow实战Google深度学习框架](images/cover.JPEG)\n\n对样例中python2.x代码以及注释进行补充和修正，修正为python3.x代码。代码使用[jupyter/jupyter](https://github.com/jupyter/jupyter)进行交互演示，运行以下命令演示：\n\n```sh\ngit clone https://github.com/cookeem/TensorFlow_learning_notes\ncd TensorFlow_learning_notes\njupyter notebook\n```\n\n## 目录：\n1. [第3章 TensorFlow入门](Chapter03)\n1. [第4章 深层神经网络](Chapter04)\n1. [第5章 MNIST数字识别问题](Chapter05)\n1. [第6章 图像识别与卷积神经网络](Chapter06)\n1. [第7章 图像数据处理](Chapter07)\n1. [第8章 循环神经网络](Chapter08)\n1. [第9章 TensorBoard可视化](Chapter09)\n1. [第10章 TensorFlow计算加速](Chapter10)\n\n## 学习笔记：\n\n### 1、多层：使用多层权重，例如多层全连接方式\n\u003e 以下定义了三个隐藏层的全连接方式的神经网络\n\u003e 样例代码：\n\n```python\nimport tensorflow as tf\n\nl1 = tf.matmul(x, w1)\nl2 = tf.matmul(l1, w2)\ny = tf.matmul(l2,w3)\n```\n\n### 2、激活层：引入激活函数，让每一层去线性化\n\u003e 激活函数有多种，例如常用的：\n\u003e tf.nn.relu\n\u003e tf.nn.tanh\n\u003e tf.nn.sigmoid\n\u003e tf.nn.elu\n\u003e 样例代码：\n\n```python\nimport tensorflow as tf\n\na = tf.nn.relu(tf.matmul(x, w1) + biase1)\ny = tf.nn.relu(tf.matmul(a, w2) + biase2)\n```\n\n### 3、损失函数：\n\u003e 经典损失函数，交叉熵（cross entropy）\n\u003e 用于计算预测结果矩阵Y和实际结果矩阵Y_之间的距离\n\u003e 样例代码：\n\n```python\nimport tensorflow as tf\n\ncross_entropy = -tf.reduce_mean(y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)))\n```\n\n```python\nimport tensorflow as tf\n\nv = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])\ntf.reduce_mean(tf.clip_by_value(v, 0.0, 10.0))\n```\n\n\u003e 对于分类问题，通常把交叉熵与softmax回归一起使用\n\n```python\nimport tensorflow as tf\n\ncross_entropy = tf.nn.softmax_cross_entropy_with_logits(y, y_)\n```\n\n\u003e 对于回归问题，通常使用mse（均方误差函数）计算损失函数\n\n```python\nimport tensorflow as tf\n\nmse_loss = tf.reduce_mean(tf.square(y_ - y))\n\n# 与以下函数计算结果完全一致\ndataset_size = 1000\nmse_loss = tf.reduce_sum(tf.pow(y_ - y, 2)) / dataset_size\n```\n\n\u003e 自定义条件化的损失函数\n\n```python\nimport tensorflow as tf\n\nloss_less = 10\nloss_more = 1\nloss = tf.reduce_sum(tf.where(tf.greater(y, y_), (y - y_) * loss_more, (y_ - y) * loss_less))\ntrain_step = tf.train.AdamOptimizer(0.001).minimize(loss)\n```\n\n### 4、神经网络优化算法，训练优化器\n\u003e 一般优化器的目标是优化权重W和偏差biases，最小化损失函数的结果\n\u003e 以下优化器会不断优化W和biases\n\n```python\nimport tensorflow as tf\n\nLEARNING_RATE = 0.001\nmse_loss = tf.reduce_mean(tf.square(y_ - y))\ntrain_op = tf.train.AdamOptimizer(LEARNING_RATE).minimize(mse_loss)\n```\n\n### 5、优化学习率LEARNING_RATE\n\u003e 学习率设置过大可能导致无法收敛，学习率设置过小可能导致收敛过慢\n\n```python\nimport tensorflow as tf\n\nglobal_step = tf.Variable(0)\nlearning_rate = tf.train.exponential_decay(\n    learning_rate=0.1, \n    global_step=global_step, \n    decay_steps=100, \n    decay_rate=0.96, \n    staircase=True, \n    name=None\n)\ntrain_op = tf.train.AdamOptimizer(learning_rate).minimize(loss, global_step=global_step)\n```\n\n### 6、过拟合问题（正则化）\n\u003e 避免训练出来的模型过分复杂，即模型记住了所有数据（包括噪声引起的误差）\n\u003e 因此需要引入正则化函数叠加的方式，避免模型出现过拟合\n\n```python\nimport tensorflow as tf\n\nv_lambda = 0.001\nw = tf.Variable(tf.random_normal([2, 1], stddev=1, seed=1))\ny = tf.matmul(x, w)\nmse_loss = tf.reduce_mean(tf.square(y_ - y) + tf.contrib.layers.l2_regularizer(v_lambda)(w))\n```\n\n### 7、滑动平均模型\n\u003e 用于控制模型的变化速度，可以控制权重W以及偏差biases\n\u003e 例如：avg_class.average(w) avg_class.average(biases)\n\n```python\nimport tensorflow as tf\n\nv1 = tf.Variable(0, dtype=tf.float32)\nstep = tf.Variable(0, trainable=False)\nema = tf.train.ExponentialMovingAverage(decay=0.99, num_updates=step)\n# 每一次操作的时候，列表变量[v1]都会被更新\nmaintain_averages_op = ema.apply([v1]) \n\nwith tf.Session() as sess:\n    \n    # 初始化\n    init_op = tf.global_variables_initializer()\n    sess.run(init_op)\n    print(sess.run([v1, ema.average(v1)]))\n    \n    # 更新step和v1的取值\n    sess.run(tf.assign(step, 10000))  \n    sess.run(tf.assign(v1, 10))\n    sess.run(maintain_averages_op)\n    print(sess.run([v1, ema.average(v1)]))\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcookeem%2Ftensorflow_learning_notes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcookeem%2Ftensorflow_learning_notes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcookeem%2Ftensorflow_learning_notes/lists"}