【问题标题】:sess.run() causing training to be slowsess.run() 导致训练变慢
【发布时间】:2017-10-27 07:50:30
【问题描述】:

我正在训练一个 CNN,我相信我对 sess.run() 的使用导致我的训练非常缓慢。

本质上,我使用的是mnist 数据集...

from tensorflow.examples.tutorials.mnist import input_data
...
...
features = input_data.read_data_sets("/tmp/data/", one_hot=True)

问题是,CNN 的第一层必须接受[batch_size, 28, 28, 1] 形式的图像,这意味着我必须在将每个图像输入到 CNN 之前对其进行转换。

我使用我的脚本执行以下操作...

x = tf.placeholder(tf.float32, [None, 28, 28, 1])
y = tf.placeholder(tf.float32, [None, 10])  
...
...
with tf.Session() as sess:

    for epoch in range(25):

        total_batch = int(features.train.num_examples/500)

        avg_cost = 0

        for i in range(total_batch):

            batch_xs, batch_ys = features.train.next_batch(10)

            # Notice this line.
            _, c = sess.run([train_op, loss], feed_dict={x:sess.run(tf.reshape(batch_xs, [10, 28, 28, 1])), y:batch_ys})

            avg_cost += c / total_batch

        if (epoch + 1) % 1 == 0:
            print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost))

注意注释行。我正在从训练集中获取第一批,并且正在将其重塑为正确的格式[batch_size, 28, 28, 1]。我每次都必须打电话给sess.run(),我相信这就是训练如此缓慢的原因。

我该如何防止这种情况发生。我尝试使用numpy 在另一个脚本中重新格式化数据,但它仍然给我带来了问题,因为我无法在不运行sess.run() 的情况下提供numpy 数组。有人可以告诉我如何在培训课程之外格式化数据吗?也许我可以在另一个脚本中格式化数据并将其加载到包含我的 CNN 的脚本中?

【问题讨论】:

    标签: python numpy machine-learning tensorflow


    【解决方案1】:

    您绝对不应该在每次迭代时在新操作上使用内部 sess.run()(尽管我不确定它真的会减慢您的速度)。您应该执行以下操作之一:

    • 有一个与您的输入相同形状的占位符,例如[None, 28*28*1],后跟 tf.reshape([None, 28, 28, 1]),位于网络的开头(而不是 tf.placeholder([None, 28, 28, 1])

    • 保留您的神经网络,并使用 numpy reshape 而不是 tensorflow 重新格式化:_, c = sess.run([train_op, loss], feed_dict={x:batch_xs.reshape( [-1, 28, 28, 1]), y:batch_ys})

    如果你只写_, c = sess.run([train_op, loss], feed_dict={x:tf.reshape(batch_xs, [10, 28, 28, 1]), y:batch_ys}),它可能也有效,但你不应该那样做,因为它会在每次迭代时在你的图中创建一个新的操作。

    【讨论】:

    • 我也不确定它会减慢多少网络,但我实施了第二种方法,训练似乎更快。
    【解决方案2】:

    您可以做的另一件事是在开始时重塑所有输入,然后将其提供给占位符。

    import math
    import numpy as np
    x = tf.placeholder(tf.float32, [None, 28, 28, 1])
    y = tf.placeholder(tf.float32, [None, 10])  
    ...
    ...
    with tf.Session() as sess:
        X_train=mnist.train.images.reshape(-1,28,28,1)
        y_train=mnist.train.labels
        train_indicies = np.arange(X_train.shape[0])
        num_epochs = 25 // number of epochs
        batch_size = 50
        total_batch = int(math.ceil(X_train.shape[0]/batch_size))
        for epoch in range(25):
            for i in np.arange(total_batch):
            start_idx = (i*batch_size)%X_train.shape[0]
            idx = train_indicies[start_idx:start_idx+batch_size]
            _, c = sess.run([train_op, loss], feed_dict={x:X_train[idx,:], y:y_train[idx]})
            avg_cost += c / total_batch
    
        if (epoch + 1) % 1 == 0:
            print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost))
    

    由于我们将无法使用 mnist.train.next_batch,我们将需要手动计算和递增索引。

    希望这可行:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 2021-07-13
      相关资源
      最近更新 更多