Tensorflow 速查表

这篇博客详述了TensorFlow的各个关键部分,包括Tensor、Operation、Optimizer、Graph的构建和执行、Layers以及Tensorboard的使用。介绍了变量、基本运算、损失函数、优化器、卷积层、池化层、模型评估、GPU使用等概念,并强调了Tensorboard在可视化学习中的作用。
摘要由CSDN通过智能技术生成

目录

Part I: Tensor

1 Variable:

2. 变量初始化

3. 保存变量

4. 恢复变量

Part II: Operation

1. 基本运算

2. 激活函数

3. 损失函数

4. 模型评估

Part III: Optimizer

Part IV: Graph

1. 构建图

2. 在一个会话中启动图

3. Fetch

4. Feed

Part V: Layers

1. 卷积层

2. 池化层

3. 密集连接层

4. dropout

5. 输出层

Part VI: Tensorboard 可视化学习

Part VI: GPU Usage


 

TensorFlow 是一个编程系统, 使用图来表示计算任务. 图中的节点被称之为 op (operation 的缩写). 一个 op获得 0 个或多个 Tensor , 执行计算, 产生 0 个或多个 Tensor . 每个 Tensor 是一个类型化的多维数组. 例如, 你可以将一小组图像集表示为一个四维浮点数数组, 这四个维度分别是 [batch, height, width, channels].

一个 TensorFlow 图描述了计算的过程. 为了进行计算, 图必须在 会话 里被启动. 会话 将图的 op 分发到诸如 CPU 或 GPU 之类的 设备 上, 同时提供执行 op 的方法. 这些方法执行后, 将产生的 tensor 返回. 在 Python 语言中, 返回的 tensor 是 numpy ndarray 对象; 在 C 和 C++ 语言中, 返回的 tensor 是 tensorflow::Tensor 实例.

Part I: Tensor

1 Variable:

# Create two variables.
X = tf.placeholder(tf.float32,shape=(None, n_inputs),name ="X")
y = tf.placeholder(tf.int64,shape=(None),name="y")

weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),
name="weights")
# use truncted_normal
weights = tf.Variable(tf.truncated_normal([IMAGE_PIXELS, hidden1_units], stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))),name='weights')

biases = tf.Variable(tf.zeros([200]), name="biases")

2. 变量初始化

# Add an op to initialize the variables.
init_op = tf.initialize_all_variables()

# Later, when launching the model
with tf.Session() as sess:
    # Run the init operation.
    sess.run(init_op)
    ...
    # Use the mod

由另一个变量初始化

# Create a variable with a random value.
weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),
name="weights")
# Create another variable with the same value as 'weights'.
w2 = tf.Variable(weights.initialized_value(), name="w2")
# Create another variable with twice the value of 'weights'
w_twice = tf.Variable(weights.initialized_value() * 0.2, name="w_twice")

3. 保存变量

# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add an op to initialize the variables.
init_op = tf.initialize_all_variables()
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
# Later, launch the model, initialize the variables, do some work, save the
# variables to disk.
with tf.Session() as sess:
    sess.run(init_op)
    # Do some work with the model.
    ..
    # Save the variables to disk.
    save_path = saver.save(sess, "/tmp/model.ckpt")
    print "Model saved in file: ", save_path

4. 恢复变量

# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
    # Restore variables from disk.
    saver.restore(sess, "/tmp/model.ckpt")
    print "Model restored."
    # Do some work with the model
    ...

 

Part II: Operation

1. 基本运算

y = tf.matmul(x,W)

 

2. 激活函数

softmax: 

y = tf.nn.softmax(tf.matmul(x,W) + b)

xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y,logits=logits)<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值