CS20 Class_1 Operations

Operations

数据流图,TensorFlow将计算的定义与其执行分开,通过 G r a p h s Graphs Graphs S e s s i o n Session Session来完成。
先定义一个 G r a p h s Graphs Graphs,然后使用 S e s s i o n Session Session中的相关的操作

Basic operations

import tensorflow as tf
a = tf.add(3, 5)
print(a)
Tensor("Add:0", shape=(), dtype=int32)
a = tf.add(3, 5)
with tf.Session() as sess:
    print(sess.run(a))
x = 2
y = 3
add_op = tf.add(x, y)
mul_op = tf.multiply(x, y)
useless = tf.multiply(x, add_op)
pow_op = tf.pow(add_op, mul_op)
with tf.Session() as sess:
    #z = sess.run(pow_op)
    z, not_useless = sess.run([pow_op, useless])
    
tf.Session.run(fetches,
   	 feed_dict=None,
 	 options=None,
     run_metadata=None)

使用特定的设备

with tf.device('/cpu:0'):
    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    c = tf.matmul(a, b)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(sess.run(c))

S e s s i o n Session Session会有一些常见的配置,最常用的是 l o g _ d e v i c e _ p l a c e m e n t log\_device\_placement log_device_placement a l l o w _ s o f t _ p l a c e m e n t allow\_soft\_placement allow_soft_placement
为了避免出现你指定的设备不存在这种情况, 你可以在创建的 S e s s i o n Session Session 里把参数 a l l o w _ s o f t _ p l a c e m e n t allow\_soft\_placement allow_soft_placement设置为 T r u e True True, 这样 T e n s o r F l o w TensorFlow TensorFlow 会自动选择一个存在并且支持的设备来运行 o p e r a t i o n operation operation
l o g _ d e v i c e _ p l a c e m e n t log\_device\_placement log_device_placement 将该属性设置为 T r u e True True 的时候,程序日志中将记录计算图中每个节点是在哪个设备中进行计算的。

config = tf.ConfigProto(log_device_placement=True,
                        allow_soft_placement=True)
sess = tf.Session(config=config)

创建不同的图

g = tf.Graph()
with g.as_default():
	x = tf.add(3, 5)
sess = tf.Session(graph=g)
with tf.Session() as sess:
	sess.run(x)

如果是处理默认的图

g = tf.get_default_graph()

显示log的输出级别

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
#1 代表这是默认的显示等级,显示所有信息
#2 代表只显示 warning 和 Error
#3 代表只显示 Error

TensorBoard显示

如果可视化,需要把log日志输出

writer = tf.summary.FileWriter([logdir], [graph])
import tensorflow as tf
a = tf.constant(2,name = "a")
b = tf.constant(3, name = "b")
x = tf.add(a, b, name = "add")
writer = tf.summary.FileWriter('./graphs', tf.get_default_graph())
with tf.Session() as sess:
	# writer = tf.summary.FileWriter('./graphs', sess.graph) 
	# if you prefer creating your writer using session's graph
	print(sess.run(x))
writer.close()
python3 [my_program.py] 
tensorboard --logdir="./graphs/lazy_loading" --port 6006 #6006 or any port you want

这时打开浏览器,推荐使用google浏览器,http://localhost:6006/

Base op

tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False)
# constant of 1d tensor (vector)
a = tf.constant([2, 2], name="vector")
# constant of 2x2 tensor (matrix)
b = tf.constant([[0, 1], [2, 3]], name="matrix")
tf.zeros(shape, dtype=tf.float32, name=None)
# create a tensor of shape and all elements are zeros
tf.zeros([2, 3], tf.int32) ==> [[0, 0, 0], [0, 0, 0]]
# input_tensor [[0, 1], [2, 3], [4, 5]]
tf.zeros_like(input_tensor) ==> [[0, 0], [0, 0], [0, 0]]
tf.ones(shape, dtype=tf.float32, name=None)
# create a tensor of shape and all elements are ones
tf.ones([2, 3], tf.int32) ==> [[1, 1, 1], [1, 1, 1]]
# input_tensor is [[0, 1], [2, 3], [4, 5]]
tf.ones_like(input_tensor) ==> [[1, 1], [1, 1], [1, 1]]
tf.fill(dims, value, name=None) 
# create a tensor filled with a scalar value.
tf.fill([2, 3], 8) ==> [[8, 8, 8], [8, 8, 8]]
tf.lin_space(start, stop, num, name=None)
# create a sequence of num evenly-spaced values are generated beginning at start.
# If num > 1, the values in the sequence increase by (stop - start) / (num - 1), so that the last one is exactly stop.
# comparable to but slightly different from numpy.linspace

tf.lin_space(10.0, 13.0, 4, name="linspace") ==> [10.0 11.0 12.0 13.0]

tf.range([start], limit=None, delta=1, dtype=None, name='range')
# create a sequence of numbers that begins at start and extends by increments of delta up to but not including limit
# 'start' is 3, 'limit' is 18, 'delta' is 3
tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]
# 'start' is 3, 'limit' is 1,  'delta' is -0.5
tf.range(start, limit, delta) ==> [3, 2.5, 2, 1.5]
# 'limit' is 5
tf.range(limit) ==> [0, 1, 2, 3, 4]
Note that unlike NumPy or Python sequences, TensorFlow sequences are not iterable.
tf.ones([2, 2], np.float32) 	# ⇒ [[1.0 1.0], [1.0 1.0]]
For  tf.Session.run(fetches): if the requested fetch is a Tensor , output will be a NumPy ndarray.
sess = tf.Session()
a = tf.zeros([2, 3], np.int32)
print(type(a))  			# ⇒ <class 'tensorflow.python.framework.ops.Tensor'>
a = sess.run(a)  <<<< Avoid doing this. Use a_out = sess.run(a)
print(type(a))  			# ⇒ <class 'numpy.ndarray'>
Only use constants for primitive types.
Use variables or readers for more data that requires more memory
# create variables with tf.Variable
s = tf.Variable(2, name="scalar") 
m = tf.Variable([[0, 1], [2, 3]], name="matrix") 
W = tf.Variable(tf.zeros([784,10]))

tf.Variable holds several ops:
x = tf.Variable(...) 
x.initializer # init op
x.value() # read op
x.eval() # 读取里面的值
x.assign(...) # write op
x.assign_add(...) # and more

变量的创建方式建议使用下面的方式

# create variables with tf.get_variable
s = tf.get_variable("scalar", initializer=tf.constant(2)) 
m = tf.get_variable("matrix", initializer=tf.constant([[0, 1], [2, 3]]))
W = tf.get_variable("big_matrix", shape=(784, 10), initializer=tf.zeros_initializer())
tf.get_variable(
    name,
    shape=None,
    dtype=None,
    initializer=None,
    regularizer=None,
    trainable=True,
    collections=None,
    caching_device=None,
    partitioner=None,
    validate_shape=True,
    use_resource=None,
    custom_getter=None,
    constraint=None
)

变量使用之前需要进行初始化

The easiest way is initializing all variables at once:
with tf.Session() as sess:
	sess.run(tf.global_variables_initializer())
Initialize only a subset of variables:
with tf.Session() as sess:
	sess.run(tf.variables_initializer([a, b]))
Initialize a single variable
W = tf.Variable(tf.zeros([784,10]))
with tf.Session() as sess:
	sess.run(W.initializer)

Initializer is an op. You need to execute it within the context of a session
# W is a random 700 x 100 variable object
W = tf.Variable(tf.truncated_normal([700, 10]))
with tf.Session() as sess:
	sess.run(W.initializer)
	print(W.eval())				# Similar to print(sess.run(W))
W = tf.Variable(10)
W.assign(100)
with tf.Session() as sess:
	sess.run(W.initializer)
	print(W.eval()) 				# >> 10   why ?
#W.assign(100) creates an assign op. That op needs to be executed in a session to take effect.
W = tf.Variable(10)
assign_op = W.assign(100)
with tf.Session() as sess:
	sess.run(W.initializer)
	sess.run(assign_op)
	print(W.eval()) 				# >> 100
	
my_var = tf.Variable(10)
With tf.Session() as sess:
	sess.run(my_var.initializer)
	# increment by 10 
	sess.run(my_var.assign_add(10)) # >> 20
# decrement by 2 
sess.run(my_var.assign_sub(2)) # >> 18

T e n s o r f l o w Tensorflow Tensorflow S e s s i o n Session Session维持变量的分离

W = tf.Variable(10)
sess1 = tf.Session()
sess2 = tf.Session()
sess1.run(W.initializer)
sess2.run(W.initializer)
print(sess1.run(W.assign_add(10)))		# >> 20
print(sess2.run(W.assign_sub(2)))		# >> 8
print(sess1.run(W.assign_add(100)))		# >> 120
print(sess2.run(W.assign_sub(50)))		# >> -42
sess1.close()
sess2.close()

C o n t r o l D e p e n d e n c i e s Control Dependencies ControlDependencies

tf.Graph.control_dependencies(control_inputs)
# defines which ops should be run first
# your graph g have 5 ops: a, b, c, d, e
g = tf.get_default_graph()
with g.control_dependencies([a, b, c]):
	# 'd' and 'e' will only run after 'a', 'b', and 'c' have executed.
	d = ...
	e =

占位符

# create a placeholder for a vector of 3 elements, type tf.float32
a = tf.placeholder(tf.float32, shape=[3])
b = tf.constant([5, 5, 5], tf.float32)
# use the placeholder as you would a constant or a variable
c = a + b  # short for tf.add(a, b)
with tf.Session() as sess:
	print(sess.run(c, feed_dict={a: [1, 2, 3]})) 	# the tensor a is the key, not the string ‘a’
# >> [6, 7, 8]

tf.placeholder(dtype, shape=None, name=None)
shape=None means that tensor of any shape will be accepted as value for placeholder.
shape=None is easy to construct graphs, but nightmarish for debugging
You have to do it one at a time
with tf.Session() as sess:
	for a_value in list_of_values_for_a:
	print(sess.run(c, {a: a_value}))

The trap of lazy loading

x = tf.Variable(10, name='x')
y = tf.Variable(20, name='y')
z = tf.add(x, y)
with tf.Session() as sess:
	sess.run(tf.global_variables_initializer())
	writer = tf.summary.FileWriter('graphs/normal_loading', sess.graph)
	for _ in range(10):
		sess.run(z)
	writer.close()
	
x = tf.Variable(10, name='x')
y = tf.Variable(20, name='y')
with tf.Session() as sess:
	sess.run(tf.global_variables_initializer())
	writer = tf.summary.FileWriter('graphs/lazy_loading', sess.graph)
	for _ in range(10):
		sess.run(tf.add(x, y))
	print(tf.get_default_graph().as_graph_def()) 
	writer.close()

可以看到图片如下
在这里插入图片描述
上面的图片是normal_loading,下面的是lazy_loading。
normal_loading中定义了x和y变量,同时定义了 x + y x+y x+y的操作 z z z,无论在Seession中做多少次加法只会有一个节点,而在lazy_loading中多次执行 x + y x+y x+y的操作,会产生多个节点,每个节点都是 x + y x+y x+y,这个就是lazy_loading带来的问题,会严重影响图的读的速度
解决办法
1、把ops的计算和运行的定义分开
2、Use Python property to ensure function is also loaded once the first time it is called*
第二个解决办法具体可参考参考文献4

参考文献:
1、https://web.stanford.edu/class/cs20si/syllabus.html
2、http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/using_gpu.html
3、https://www.jianshu.com/p/2da98dcbca77
4、https://danijar.com/structuring-your-tensorflow-models/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: cs_matlab_code 是一个指代一段 MATLAB 代码的术语。"cs" 是压缩感知(Compressed Sensing)的缩写,压缩感知是一种信号处理技术,用于高效地采集和恢复信号。"matlab_code" 意味着该代码是用 MATLAB 编程语言编写的。 在 cs_matlab_code 中,应该包含有关压缩感知的相关算法和实现。这段代码可能首先会使用稀疏表示(sparse representation)的方法对信号进行表示。稀疏表示是压缩感知的核心概念之一,它利用信号在适当的字典(dictionary)下可以以较少的非零系数表示的特性。 接下来,cs_matlab_code 可能会实现通过采用较少数量的测量(measurements)或传感器来获取信号的压缩表示。这些测量是用来捕获信号中的主要信息,并利用压缩感知算法来恢复最接近原信号的稀疏表示。 最后,cs_matlab_code 可能还包含信号的恢复过程。这一过程会利用经过适当优化的算法,基于上述测量结果,通过优化算法计算出信号的稀疏表示,并尽可能准确地恢复原始信号。 通过使用 cs_matlab_code,我们可以在压缩感知信号采集和恢复方面进行实验和研究。这样的代码对于信号处理、图像处理、通信和传感领域的研究人员和工程师来说非常有用,因为它可以提供一种高效的信号恢复方法,同时减少了数据采集和传输的负担。 总而言之,cs_matlab_code 是一个实现压缩感知算法的 MATLAB 代码,可用于稀疏信号采集和恢复。它将压缩感知的概念与 MATLAB 的计算能力相结合,为信号处理和相关领域的研究者和从业者提供了一种强大的工具。 ### 回答2: CS代表计算机科学,MATLAB是一种通用的科学和工程计算软件。CS_MATLAB_CODE指的是使用MATLAB语言编写的计算机科学代码。 借助MATLAB,计算机科学家和工程师可以进行各种计算和数据分析,开发和实现各种算法和模型。通过编写CS_MATLAB_CODE,我们可以进行图像处理、信号处理、模式识别、数据分析、机器学习、计算机视觉等多个领域的研究和开发。 编写CS_MATLAB_CODE可以极大地简化科学计算和数据分析的复杂度,加速算法的开发和测试过程。MATLAB提供了丰富的函数库和工具箱,包括线性代数、数据可视化、优化算法、统计分析等功能,为编写高效且可靠的CS_MATLAB_CODE提供了有力的支持。 在编写CS_MATLAB_CODE时,我们可以使用MATLAB的函数、数据结构、控制流程等语法来实现我们的算法和模型。我们可以定义变量、矩阵,进行数值计算和矩阵运算,实现条件判断和循环结构等。 编写CS_MATLAB_CODE时,我们还可以通过可视化界面进行交互式编程,方便地查看和分析数据结果,并进行动态的参数调整和算法优化。 总之,CS_MATLAB_CODE是指使用MATLAB语言编写的计算机科学代码,它为计算机科学家和工程师提供了强大的工具和平台,来进行科学计算、数据分析和算法开发。通过编写CS_MATLAB_CODE,我们可以更加高效地实现各种复杂的计算任务,推动科学研究和应用的发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值