CS 20I : note 2
来源:互联网 发布:微信js分享demo 编辑:程序博客网 时间:2024/06/03 22:50
TensorFlow Ops
这是阅读 http://web.stanford.edu/class/cs20si/syllabus.html slides_02.pdf 的笔记.
- 基本操作
- Tensor 类型
- Project speed dating
- Placeholders and feeding inputs
- Lazy loading
第一个 TensorFlow 程序
import tensorflow as tfa = tf.constant(2)b = tf.constant(3)x = tf.add(a,b)with tf.Session() as sess: print(sess.run(x))
5
使用 TensorBoard 可视化
TensorFlow 是一个可视化的工具, 可以展示运算过程中的计算图等信息.
import tensorflow as tfa = tf.constant(2)b = tf.constant(3)x = tf.add(a,b)with tf.Session() as sess: # add this line to use TensorBoard. writer = tf.summary.FileWriter("./graphs",sess.graph) print(sess.run(x))writer.close() # close the writer when you're done using it
5
在本地文件,可以看到 graphs 目录中有一个文件.
启动 TensorBoard
打开 Anaconda Prompt, 激活到安装有 TensorFlow 的环境. 然后 cd 到 graphs 上层目录.
运行 tensorboard --logdir = graphs
, 然后在浏览器中输入 http://localhost:6006/ 即可进入 TensorBoard.
在网页中点击 Graph 就可以看到一些东西了.
注意到这里两个是显示 Const, Const_1 , 那么如何给它们改名字呢?
import tensorflow as tfa = tf.constant(2,name = "a")b = tf.constant(3,name="b")x = tf.add(a,b,name="add")with tf.Session() as sess: writer = tf.summary.FileWriter("./graphs",sess.graph) print(sess.run(x))
5
再通过 tensorboard 可以看到 graph 对应节点有名字了
constant 的更多例子
tf.constant(value, dtype = None, shape = None, name = "Const", verify_shape = Flase)
import tensorflow as tf# 注意 a,b 维度不同, 在运算时发生了 boardcasting# 更多关于 https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html# https://www.tensorflow.org/api_guides/python/math_ops#Arithmetic_Operatorsa = tf.constant([2,2],name="a")b = tf.constant([[0,1],[2,3]],name="b")x = tf.add(a,b,name="add")y = tf.multiply(a,b,name="mul")with tf.Session() as sess: x = sess.run(x) print(x) print('-----------------') y = sess.run(y) print(y)
[[2 3] [4 5]]-----------------[[0 2] [4 6]]
import tensorflow as tf# 生成指定维度的零 tensor# 一般定义: tf.zeros(shape, dtype=tf.float32, name=None)v1 = tf.zeros([3,4],tf.int32)# 值填充# tf.fill(dims, value, name = none)v2 = tf.fill([2,3],4)with tf.Session() as sess: print(sess.run(v1)) print(sess.run(v2))
[[0 0 0 0] [0 0 0 0] [0 0 0 0]][[4 4 4] [4 4 4]]
Variables
# 官方文档: https://www.tensorflow.org/programmers_guide/variables## 创建变量import tensorflow as tfa = tf.Variable(2, name = "scalar")b = tf.Variable(tf.truncated_normal([2,3]))# 初始化变量# 一次初始化所有变量init = tf.global_variables_initializer()with tf.Session() as sess: sess.run(init) print(sess.run(a)) print(sess.run(b))# provide name and shape#v1 = tf.get_variable("v1",[1,2,3])#print(v1)
2[[-0.89090335 -0.62117159 0.95811218] [ 1.0819943 0.74583757 -0.73409617]]
import tensorflow as tf# 变量初始值为 10w = tf.Variable(10)# 赋值为 100w.assign(100)with tf.Session() as sess: sess.run(w.initializer) print(w.eval()) # 但是结果仍然是 10, 为什么
10
import tensorflow as tf# 变量初始值为 10w = tf.Variable(10)# 赋值为 100assign_op = w.assign(100)with tf.Session() as sess: sess.run(assign_op) print(w.eval()) # 这时的结果就正确了
100
Placeholders (占位符)
TF 程序通常有两个阶段:
- assemble a graph
- use a session to execute operations in the graph.
import tensorflow as tf# 创建一个 placeholdera = tf.placeholder(tf.float32,shape=[3])b = tf.constant([5,5,5],tf.float32)c = a + bwith tf.Session() as sess: # 用字典来初始化 a print(sess.run(c,{a:[1,2,3]}))
[ 6. 7. 8.]
阅读全文
0 0
- CS 20I : note 2
- CS 20SI note
- 【CS 20SI】TensorFlow for Deep Learning Research Lecture note 2: TensorFlow Ops
- [翻译]斯坦福CS 20SI:基于Tensorflow的深度学习研究课程笔记,Lecture note 2: TensorFlow Ops
- Note:Useful websites about CS
- Harvard CS 50 video note
- DEPHI note I
- ASP.NET note I
- c# App NOTE-I
- C# WindForm Note-I
- C# Funciton_Method Note I
- C# control_base Note I
- SQL Note I
- C++ note I
- js note I
- c# primer note I
- .Net knowledge Note I
- SQL knowledge Note I
- 计蒜客-棋盘覆盖(循环输入)
- hdu1385Minimum Transport Cost
- 初探DOM(文档对象模型)
- 一个数组实现两个栈
- sonarqube代码审查问题汇总
- CS 20I : note 2
- LRU算法的小型缓存
- 新人新篇
- C++中方括号[]的作用总结
- matlab 求素数(三种方法)
- 基于centos 搭建 LAMP 环境,建立WordPress站点
- python正则表达式的使用方法
- 循环链表的简单使用
- 20170925_数据库查询结构