Tensorflow学习笔记(2)
2016-12-19 15:10
357 查看
关于TensorFlow的以下基础知识:
• 使用图(graphs)来表示计算.
TensorFlow是一个以图(graphs)来表示计算的编程系统,图中的节点被称之为op(operation的缩写).一个op获得零或多个张量(tensors)执行计算,产生零或多个张量。
张量是一个按类型划分的多维数组。例如,你可以将一小组图像集表示为一个四维浮点数数组,这四个维度分别是[batch,height,width,channels]。
• 在会话(Session)中执行图.
TensorFlow的图是一种对计算的抽象描述。在计算开始前,图必须在会话(Session())中被启动.会话将图的op分发到如CPU或GPU之类的设备(Devices())上,同时提供执行op的方法。这些方法执行后,将产生的张量(tensor)返回。
# Enter an interactive TensorFlow Session.
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])
# Initialize 'x' using the run() method of its initializer op.
x.initializer.run()
# Add an op to subtract 'a' from 'x'.
sub = tf.sub(x, a)
print(sub.eval())
# ==> [ − 2. − 1.]
Run it and print the result
# Close the Session when we're done.
sess.close()
• 使用张量(tensors)来代表数据.
TensorFlow程序使用tensor数据结构来代表所有的数据,计算图中,操作间传递的数据都是tensor.你可以把TensorFlow的张量看作是一个n维的数组或列表.一个tensor包含一个静态类型rank,和一个shape.
Tensor(张量)意味着N维数组,Flow(流)意味着基于数据流图的计算,TensorFlow即为张量从图的一端流动到另一端.
• 通过变量(Variables)维护状态.
变量维持了图执行过程中的状态信息.
通常会将一个统计模型中的参数表示为一组变量.例如,你可以将一个神经网络的权重作为某个变量存储在一个tensor中.在训练过程中,通过重复运行训练图,更新这个tensor.
import tensorflow as tf
#创建变量state,0初始化
state = tf.Variable(0, name="counter")
one = tf.constant(2)
#相加的操作
new_value = tf.add(state, one)
#赋值的操作,在session执行前不做该操作
update = tf.assign(state, new_value)
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init_op)
print(sess.run(state))
print('------------------')
for _ in range(3):
print(sess.run(update))
print(sess.run(new_value))
print(sess.run(state))
print('-+-+-+-+')
• 使用供给(feeds)和取回(fetches)将数据传入或传出任何操作.
取回:需要获取的多个tensor值,在op的一次运行中一起获得.
供给:feed使用一个tensor值临时替换一个操作的输出结果.你可以提供feed数据作为run()调用的参数.feed只在调用它的方法内有效,方法结束,feed就会消失.最常见的用
例是将某些特殊的操作指定为 "feed" 操作,标记的方法是使用tf.placeholder()为这些操作创建占位符.
import tensorflow as tf
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermed = tf.add(input2 , input3)
mul = tf.mul(input1 , intermed)
with tf.Session() as sess:
print(sess.run(intermed))
#这里是取回,一次性得到两个tensor值
result = sess.run([mul, intermed])
print(result)
#这里是供给,临时使用另外的值替代input2,input3来计算intermed
print(sess.run([intermed], feed_dict={input2:[7.], input3:[2.]}))
• 使用图(graphs)来表示计算.
TensorFlow是一个以图(graphs)来表示计算的编程系统,图中的节点被称之为op(operation的缩写).一个op获得零或多个张量(tensors)执行计算,产生零或多个张量。
张量是一个按类型划分的多维数组。例如,你可以将一小组图像集表示为一个四维浮点数数组,这四个维度分别是[batch,height,width,channels]。
• 在会话(Session)中执行图.
TensorFlow的图是一种对计算的抽象描述。在计算开始前,图必须在会话(Session())中被启动.会话将图的op分发到如CPU或GPU之类的设备(Devices())上,同时提供执行op的方法。这些方法执行后,将产生的张量(tensor)返回。
# Enter an interactive TensorFlow Session.
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])
# Initialize 'x' using the run() method of its initializer op.
x.initializer.run()
# Add an op to subtract 'a' from 'x'.
sub = tf.sub(x, a)
print(sub.eval())
# ==> [ − 2. − 1.]
Run it and print the result
# Close the Session when we're done.
sess.close()
• 使用张量(tensors)来代表数据.
TensorFlow程序使用tensor数据结构来代表所有的数据,计算图中,操作间传递的数据都是tensor.你可以把TensorFlow的张量看作是一个n维的数组或列表.一个tensor包含一个静态类型rank,和一个shape.
Tensor(张量)意味着N维数组,Flow(流)意味着基于数据流图的计算,TensorFlow即为张量从图的一端流动到另一端.
• 通过变量(Variables)维护状态.
变量维持了图执行过程中的状态信息.
通常会将一个统计模型中的参数表示为一组变量.例如,你可以将一个神经网络的权重作为某个变量存储在一个tensor中.在训练过程中,通过重复运行训练图,更新这个tensor.
import tensorflow as tf
#创建变量state,0初始化
state = tf.Variable(0, name="counter")
one = tf.constant(2)
#相加的操作
new_value = tf.add(state, one)
#赋值的操作,在session执行前不做该操作
update = tf.assign(state, new_value)
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init_op)
print(sess.run(state))
print('------------------')
for _ in range(3):
print(sess.run(update))
print(sess.run(new_value))
print(sess.run(state))
print('-+-+-+-+')
• 使用供给(feeds)和取回(fetches)将数据传入或传出任何操作.
取回:需要获取的多个tensor值,在op的一次运行中一起获得.
供给:feed使用一个tensor值临时替换一个操作的输出结果.你可以提供feed数据作为run()调用的参数.feed只在调用它的方法内有效,方法结束,feed就会消失.最常见的用
例是将某些特殊的操作指定为 "feed" 操作,标记的方法是使用tf.placeholder()为这些操作创建占位符.
import tensorflow as tf
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermed = tf.add(input2 , input3)
mul = tf.mul(input1 , intermed)
with tf.Session() as sess:
print(sess.run(intermed))
#这里是取回,一次性得到两个tensor值
result = sess.run([mul, intermed])
print(result)
#这里是供给,临时使用另外的值替代input2,input3来计算intermed
print(sess.run([intermed], feed_dict={input2:[7.], input3:[2.]}))
相关文章推荐
- tensorflow学习笔记(1)——基础(一)——安装和计算图
- TensorFlow学习笔记(十)tf搭建神经网络可视化结果
- TensorFlow学习笔记(二十一) tensorflow机器学习模型
- python tensorflow学习笔记(一)Hello World
- Tensorflow学习笔记(用哪学哪)tf.nn.dropout
- TensorFlow学习笔记(六)
- TensorFlow学习笔记3:线性回归
- Tensorflow学习笔记(一)------入门示例
- TensorFlow学习笔记(四)--activation function
- TensorFlow学习笔记(6) TensorFlow最佳实践样例程序
- 《TensorFlow学习笔记》卷积神经网络CNN实战-cifar10数据集(tensorboard可视化)
- TensorFlow学习笔记(更新中)
- TensorFlow学习笔记(五)
- TensorFlow学习笔记(4)----完整的工程示例:全连接前馈网络识别MNIST
- tensorflow学习笔记(五):变量保存与导入
- tensorflow学习笔记(十八):Multiple GPUs
- tensorflow学习笔记(二十八):collection
- TensorFlow学习笔记(二):TensorFlow入门
- TensorFlow学习笔记(一)入门
- TensorFlow学习笔记(1)--Mac安装TensorFlow