【问题标题】:How to use tf.train.shuffle_batch to train NN?如何使用 tf.train.shuffle_batch 训练 NN?
【发布时间】:2017-12-30 11:53:02
【问题描述】:

我已经用 tensorflow 网络训练了我的神经,并得到了一些我想减少的过拟合。我希望批量学习模型可以帮助我尝试测试这个想法。我找到了 tf.train.shuffle_batch() 并与之抗争,这可能会起作用。所以我尝试了,但没有奏效。 Tensorflow 的文档没有帮助。我发现one topic, but the example there 只打印数组。它很有希望用它来学习 NN,但在我的情况下,我没有将数据划分为 n 元素批次,而是将它们在附加维度上乘以 n 倍。

这是代码示例:

nnInput = tf.placeholder(tf.float32, [None, input_width], "network_input")
nnOutput = tf.placeholder(tf.float32, [None, output_width], "expected_labels")

batch_readings, batch_labels = tf.train.shuffle_batch(
    [
        tf.constant(train_readings), 
        tf.constant(train_labels)
    ],
    batch_size = 15,
    num_threads = 4,
    capacity = 500,
    min_after_dequeue = 250,
    allow_smaller_final_batch = True
)

sess.run(tf.global_variables_initializer())

for epoch in range(learning_steps):
    print("epoch:", epoch)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    print("Input data shapes:", train_readings.shape, train_labels.shape)
    for batch in range(10):
        x, y = sess.run([batch_readings, batch_labels])
        print("Batch shapes:", x.shape, y.shape)
        sess.run(train, feed_dict = {nnInput : x, nnOutput : y})
    coord.request_stop()
    coord.join(threads)

这是输出:

epoch: 0 
Input data shapes: (165, 60) (165, 1) 
Batch shapes: (15, 165, 60) (15, 165, 1)

错误列表的结尾是:

ValueError: Cannot feed value of shape (15, 165, 60) for Tensor 'network_input_1:0', which has shape '(?, 60)'

当我为 NN 提供 3D 数组时,这个结论并不令人惊讶,但是当我期望 x:(15, 60) 和 y:(15, 1) 时,为什么我会得到这样一个批次?为什么我会得到 x:(15, 165, 60) y:(15, 165, 1) 以及如何获得有用的批次?

我正在使用 tensorflow-gpu,但希望它也能正常工作,对吧?

【问题讨论】:

  • 这里有什么令人惊讶的地方? train_readings.shape(165, 60),而不是 (60,)。使用batch_size = 15,该函数生成(15, 165, 60) 张量。标签也一样。
  • 当我想将输入数据分成批次时,我希望输入数据的子集是随机的。因此,我期望有 165 个 60 个元素向量的案例,比如说,11 个批次,每个批次有 15 个向量。然后,划分输入。乘法输入令人惊讶。我不明白这一点。我见过的例子是从一个有 100 个数字的向量中给出 10 个向量,每个向量有 10 个数字。结果我有很大不同,代码不起作用,我不知道我能用它做什么。示例数据是data = np.arange(1, 100 + 1),所以它的形状是 (1, 101),结果是 10 个向量,每个向量有 10 个数字。不是 (10,1 ,101)。

标签: python python-3.x tensorflow artificial-intelligence tensorflow-gpu


【解决方案1】:

我在使用 tf.train.shuffle_batch 时遇到了同样的问题。解决方法是添加参数enqueue_many = True。默认是参数enqueue_many是False

tf.train.shuffle_batch 中的文档中所述 “如果 enqueue_many 为 True,则假定张量表示一批示例,其中第一维以示例为索引,并且张量的所有成员在第一维中应具有相同的大小。如果输入张量的形状为 [*, x , y, z],输出的形状为 [batch_size, x, y, z]。"

【讨论】:

    猜你喜欢
    • 2012-03-28
    • 2021-05-29
    • 2015-07-23
    • 2018-02-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    相关资源
    最近更新 更多