DTA随笔1

run_experiments.py的预处理部分

  1. os.environ['PYTHONHASHSEED'] = '0'

(1)如果该环境变量被设定为 random ,相当于 -R 命令行参数。Python 会用一个随机的种子来生成 str/bytes/datetime 对象的 hash 值。

(2)如果该环境变量被设定为一个数字,它就被当作一个固定的种子来生成 str/bytes/datetime对象的hash值。

在深度学习模型训练中,为了在同样的数据集上获得可复现的训练结果,通常把该值设定为一个固定值


  1. session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)

使用服务器时,碰巧服务器没有安装GPU或者GPU都被占满了。可是,服务器有很多CPU都是空闲的,其实,把这些CPU都充分利用起来,也可以有不错的训练效果。

with tf.Session(config=tf.ConfigProto(
  device_count={"CPU":12},
  inter_op_parallelism_threads=1,
  intra_op_parallelism_threads=1,
  gpu_options=gpu_options,
)) as sess:

在Session定义时,ConfigProto中可以尝试指定下面三个参数
(1)device_count, 告诉tf Session使用CPU数量上限,如果你的CPU数量较多,可以适当加大这个值。
(2)inter_op_parallelism_threadsintra_op_parallelism_threads告诉session操作的线程并行程度,如果值越小,线程的复用就越少,越可能使用较多的CPU核数。如果值为0,TF会自动选择一个合适的值


  1. sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)

(1)tf.get_default_graph()
功能:获取当前默认计算图。
例子:

import tensorflow as tf

a=tf.constant([1.0,2.0],name="a")
b=tf.constant([3.0,4.0],name="b")
c=a+b

print(c.graph)
print(tf.get_default_graph())

结果:
tensorflow.python.framework.ops.Graph object at 0x000001E4F528CB70
tensorflow.python.framework.ops.Graph object at 0x000001E4F528CB70

(2)tensorflow graph(图)和session(会话)
tensorflow的tf.Graph静态的图(对比eager execution),它表示模型的骨架,定义图不需要输入数据也不会执行运算

图的创建和使用

# 创建图
g_1 = tf.Graph()

# 在命名空间中构建图
with g_1.as_default():
    pass

通过添加tf.Operation和tf.Tensor来构建tf.Graph

a = tf.constant(42.0, name='const1')
b = tf.constant(2.0, name='cosnt2')
c = tf.add(a, b, name='add1')

tf.constant(42.0, name=‘const1’)创建名为const1的Operation并将该const1的结果作为返回值返回,其中const1用于生成值为42的scalar Tensor

tf.add(a, b, name=‘add1’)创建名为add1的Operation并返回其结果,其中add1接收2个Tensor作为输入

TensorFlow 使用tf.Session类来表示客户端程序(通常为 Python 程序,但也提供了其他语言的类似接口)与 C++ 运行时之间的连接

通过session执行图

with tf.Session(graph=g_1) as sess:
    x = tf.placeholder(tf.float32, shape=[3])
    y = tf.square(x)
    fetch = [y]
    feed_dict = {
        x: [1.0, 2.0, 3.0]
    }
    print(sess.run(fetch, feed_dict))  # => "[1.0, 4.0, 9.0]"

使用tensorboard显示图

with tf.Session() as sess:
  writer = tf.summary.FileWriter("/tmp/log/...", sess.graph)

  # Perform your computation...
  for i in range(1000):
    sess.run(train_op)
    # ...

  writer.close()

在终端中输入tensorboard --logdir='path’以启动tensorboard

使用多个图进行编程

g_1 = tf.Graph()
with g_1.as_default():
  # Operations created in this scope will be added to `g_1`.
  c = tf.constant("Node in g_1")

  # Sessions created in this scope will run operations from `g_1`.
  sess_1 = tf.Session()

g_2 = tf.Graph()
with g_2.as_default():
  # Operations created in this scope will be added to `g_2`.
  d = tf.constant("Node in g_2")

# Alternatively, you can pass a graph when constructing a session
# `sess_2` will run operations from `g_2`.
sess_2 = tf.Session(graph=g_2)

# Get the default graph.
g = tf.get_default_graph()

  1. K.set_session(sess)

Keras作为tensorflow精简接口,在tensorflow中调用Keras层:
让我们以一个简单的例子开始:MNIST数字分类。

我们将以Keras的全连接层堆叠构造一个TensorFlow的分类器

import tensorflow as tf
sess = tf.Session()

from keras import backend as K
K.set_session(sess)

然后,我们开始用tensorflow构建模型

img = tf.placeholder(tf.float32, shape=(None, 784))

用Keras可以加速模型的定义过程:

from keras.layers import Dense

# Keras layers can be called on TensorFlow tensors:
x = Dense(128, activation='relu')(img)  # fully-connected layer with 128 units and ReLU activation
x = Dense(128, activation='relu')(x)
preds = Dense(10, activation='softmax')(x)  # output layer with 10 units and a softmax activation

定义标签的占位符和损失函数

labels = tf.placeholder(tf.float32, shape=(None, 10))

from keras.objectives import categorical_crossentropy
loss = tf.reduce_mean(categorical_crossentropy(labels, preds))

然后,我们可以用tensorflow的优化器来训练模型

from tensorflow.examples.tutorials.mnist import input_data
mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True)

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
with sess.as_default():
    for i in range(100):
        batch = mnist_data.train.next_batch(50)
        train_step.run(feed_dict={img: batch[0],
                                  labels: batch[1]})

最后我们来评估一下模型性能:

from keras.metrics import categorical_accuracy as accuracy

acc_value = accuracy(labels, preds)
with sess.as_default():
    print acc_value.eval(feed_dict={img: mnist_data.test.images,
                                    labels: mnist_data.test.labels})

我们只是将Keras作为生成从tensor到tensor的函数(op)的快捷方法而已,优化过程完全采用的原生tensorflow的优化器,而不是Keras优化器,我们压根不需要Keras的Model。

关于原生TensorFlow和Keras的优化器的一点注记:Keras的优化器要比TensorFlow的优化器快大概5-10%。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值