[英]How to use tf.train.shuffle_batch to train NN?
我已經訓練了我用張量流網絡構建的神經,並且得到了一些我想減少的過擬合。 我希望分批學習該模型可以對我嘗試驗證該想法的廣告有所幫助。 我找到了tf.train.shuffle_batch()並與之抗爭了。 所以我嘗試了,但沒有成功。 Tensorflow的文檔無濟於事。 我找到了一個主題,但是那里的示例僅打印出數組。 用它來學習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)'
當我用3D數組填充NN時,結論並不令人驚訝,但是為什么當我期望x:(15,60)和y:(15,1)時為什么得到這樣的批處理? 為什么我得到x:(15,165,60)y:(15,165,1)以及如何獲得有用的批次?
我正在使用tensorflow-gpu,但希望它也能正常工作,對吧?
使用tf.train.shuffle_batch時遇到了相同的問題。 解決方案是添加參數enqueue_many = True 。 默認值為enqueue_many為False 。
如tf.train.shuffle_batch中的文檔中所述, “如果enqueue_many為True,則張量假定代表一批示例,其中第一個維度由example索引,並且所有張量成員在第一個維度中的大小均應相同如果輸入張量具有形狀[*,x,y,z],則輸出將具有形狀[batch_size,x,y,z]。”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.