美文网首页
Tensorflow中一些有趣的问题

Tensorflow中一些有趣的问题

作者: cheerss | 来源:发表于2018-07-13 15:18 被阅读0次

1. 运行时warning:The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations。SSE全名Streaming SIMD Extensions,是英特尔公司针对单指令多数据做的一些拓展,可以加速CPU上某些运算,tensorflow的官方编译版本为了能兼容更多的CPU,编译时没有利用CPU这一特性,想要杀掉这个warning的方法是:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
# 或者export一下这个环境变量

2. 下面的代码可以看graph的定义:

with tf.Session() as sess:
  print(sess.graph.as_graph_def())

3. lazy loading trap

normal loading:

x = tf.Variable(10, name='x')
y = tf.Variable(20, name='y')
z = tf.add(x, y)        # create the node before executing the graph
writer = tf.summary.FileWriter('./graphs/normal_loading', tf.get_default_graph())
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for _ in range(10):
        sess.run(z)
writer.close()

对应的graph是:

lazy loading

x = tf.Variable(10, name='x')
y = tf.Variable(20, name='y')
writer = tf.summary.FileWriter('./graphs/lazy_loading', tf.get_default_graph())
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for _ in range(10):
        sess.run(tf.add(x, y)) # someone decides to be clever to save one line of code
writer.close()

对应的图是:

也就是相当于add的op被定义的十次,加入有的代码会执行几百万次的话。。。所以最好在使用前全部定义好,不要使用lazy loading

。。。待续

相关文章

网友评论

      本文标题:Tensorflow中一些有趣的问题

      本文链接:https://www.haomeiwen.com/subject/eeqjpftx.html