tensorflow中的批处理问题

tensorflow中的批处理问题,tensorflow,training-data,Tensorflow,Training Data,我刚从咖啡馆转到tensorflow。我在tensorflow中有一个非常初始的示例,它没有批处理。我打算用迷你批,但我被卡住了。它似乎需要批处理、队列和协调。我不知道如何使用它们 如果您能在我的代码中解释我如何使用批处理,我将不胜感激 import tensorflow as tf import numpy as np import scipy.io as sio import h5py sess = tf.InteractiveSession() train_mat = h5py.Fil

我刚从咖啡馆转到tensorflow。我在tensorflow中有一个非常初始的示例,它没有批处理。我打算用迷你批,但我被卡住了。它似乎需要批处理、队列和协调。我不知道如何使用它们

如果您能在我的代码中解释我如何使用批处理,我将不胜感激

import tensorflow as tf
import numpy as np
import scipy.io as sio
import h5py

sess = tf.InteractiveSession()

train_mat = h5py.File('Basket_train_data_binary.mat')
test_mat = h5py.File('Basket_test_data_binary.mat')

train_mat = train_mat["binary_train"].value
test_mat = test_mat["binary_test"].value

Train = np.transpose(train_mat)
Test = np.transpose(test_mat)

# import the data

# placeholders, which are the training data
x = tf.placeholder(tf.float32, shape=[None, 42])
y_ = tf.placeholder(tf.float32, shape=[None])


nnodes = 10
# define the variables
W1 = tf.Variable(tf.zeros([43,nnodes]))
b1 = tf.Variable(tf.zeros([nnodes]))

W2 = tf.Variable(tf.zeros([nnodes,1]))
b2 = tf.Variable(tf.zeros([1]))

# initilize the variables
sess.run(tf.initialize_all_variables())

# placeholders, which are the training data                                                                                                      
x = tf.placeholder(tf.float32, shape=[None, 43])
y_ = tf.placeholder(tf.float32, shape=[None])


nnodes = 10
# define the variables                                                                                                                           
W1 = tf.Variable(tf.zeros([43,nnodes]))
b1 = tf.Variable(tf.zeros([nnodes]))

W2 = tf.Variable(tf.zeros([nnodes,1]))
b2 = tf.Variable(tf.zeros([1]))

# Passing global_step to minimize() will increment it at each step.
global_step = tf.Variable(0, trainable=False)

# initilize the variables                                                                                                                       
sess.run(tf.initialize_all_variables())

# prediction function (just one layer)                                                                                                           
layer1 = tf.nn.sigmoid(tf.matmul(x,W1) + b1)
y = tf.matmul(layer1,W2) + b2

# cost function 
cost_function = tf.reduce_sum(tf.square(y_ - y))

alpha = 2
l2regularization = tf.reduce_sum(tf.square(W1)) +             tf.reduce_sum(tf.square(b1)) +tf.reduce_sum(tf.square(W2)) + tf.reduce_sum(tf.square(b2))
loss = cost_function + alpha*l2regularization

# define the learning_rate and its decaying procedure.
decay_rate = 0.00005
starter_learning_rate = 0.0000009
learning_rate = tf.train.exponential_decay(starter_learning_rate,     global_step,10000, decay_rate, staircase=True)


# define the training paramters and model, gradient model and feeding the function
train_step =     tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)

# Train the Model for 1000 times. by defining the batch number we determine that it is sgd
bth_sz = 100
for i in range(2):
  train_step.run(feed_dict={x:Train[1:100,0:43] , y_:Train[1:100,43]})

print "y"
sess.run([tf.Print(y,[y])],feed_dict={x:Train[0:100,0:43] ,     y_:Train[1:100,43]})
print "y_"
sess.run([tf.Print(y_,[y_])],feed_dict={x:Train[0:100,0:43] , y_:Train[1:100,43]})
print "W1" 
sess.run([tf.Print(W1,[W1])],feed_dict={x:Train[0:100,0:43] ,     y_:Train[1:100,43]})
print "W2" 
sess.run([tf.Print(W2,[W2])],feed_dict={x:Train[0:100,0:43] ,     y_:Train[1:100,43]})
print "b1" 
sess.run([tf.Print(b1,[b1])],feed_dict={x:Train[0:100,0:43] , y_:Train[1:100,43]})

# evaluation
# it returns 1, if both y and y_ are equal. 
correct_prediction = tf.reduce_sum(tf.square(y_ - y))

# calculate the accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# print tset loss 
print(accuracy.eval(feed_dict={x: Test[:,0:43], y_: Test[:,43]}))

# print training loss 
print sess.run(cost_function,feed_dict={x: Train[:,0:43], y_: Train[:,43]})
现在,我正在使用
x:Train[1:100,0:43]
选择前100条记录。我也不想自己编写小批量选择

提前感谢,,
Afshin

使用队列非常容易,但不幸的是,从(对我来说)来看,它并不明显。因此,下面我提供了使用队列使用批次进行培训的示例:

# capacity - upper bound on the nu,ber fo elements
queue = tf.FIFOQueue(capacity, [tf.float64, tf.float64])

# here we create placeholder for data and op that enqueues them
enq_x, enq_y = tf.placeholder(tf.float64), tf.placeholder(tf.float64)
enqueue_op = queue.enqueue_many([enq_x, enq_y])

# here we dequeue data from queue for further usage
bth_sz = 100
x, y_ = queue.dequeue_many(bth_sz)

# here you initialize your variables and loss for training that use x and y_ ...

# Note that you can enqueue data any size. For example if 
# you have big data set you can divide it into several parts
# and enqueue each part in different threads
sess.run(enqueue_op, feed_dict={enq_x: Train[,0:43], enq_y: Train[,43]})

for _ in range(2):
    # Note that you can make btch_sz as placeholder and provide it through feed_dict here
    sess.run(train_step)
我希望这是有帮助的

已编辑:enq_y-占位符而非常量

已编辑

Train = np.random.rand(100, 44)

tf.reset_default_graph()
# capacity - upper bound on the nu,ber fo elements
queue = tf.FIFOQueue(500, [tf.float64, tf.float64], shapes=[[43], []])

# here we create placeholder for data and op that enqueues them
enq_x, enq_y = tf.placeholder(tf.float64, shape=[None, 43]), tf.placeholder(tf.float64, shape=[None])
enqueue_op = queue.enqueue_many([enq_x, enq_y])

bth_sz = tf.placeholder(tf.int32)
x, y_ = queue.dequeue_many(bth_sz)

# here you initialize your variables and loss for training that use x and y_
# ...

# Note that you can enqueue data any size. For example if you have big data set you can divide it
# and enqueue each part in different threads
with tf.Session() as sess:
    sess.run(enqueue_op, feed_dict={enq_x: Train[:,0:43], enq_y: Train[:,43]})

    sess.run([x, y_], feed_dict={bth_sz: 10})

这很容易使用队列,但不幸的是,从(对我来说)来看,它并不明显。因此,下面我提供了使用队列使用批次进行培训的示例:

# capacity - upper bound on the nu,ber fo elements
queue = tf.FIFOQueue(capacity, [tf.float64, tf.float64])

# here we create placeholder for data and op that enqueues them
enq_x, enq_y = tf.placeholder(tf.float64), tf.placeholder(tf.float64)
enqueue_op = queue.enqueue_many([enq_x, enq_y])

# here we dequeue data from queue for further usage
bth_sz = 100
x, y_ = queue.dequeue_many(bth_sz)

# here you initialize your variables and loss for training that use x and y_ ...

# Note that you can enqueue data any size. For example if 
# you have big data set you can divide it into several parts
# and enqueue each part in different threads
sess.run(enqueue_op, feed_dict={enq_x: Train[,0:43], enq_y: Train[,43]})

for _ in range(2):
    # Note that you can make btch_sz as placeholder and provide it through feed_dict here
    sess.run(train_step)
我希望这是有帮助的

已编辑:enq_y-占位符而非常量

已编辑

Train = np.random.rand(100, 44)

tf.reset_default_graph()
# capacity - upper bound on the nu,ber fo elements
queue = tf.FIFOQueue(500, [tf.float64, tf.float64], shapes=[[43], []])

# here we create placeholder for data and op that enqueues them
enq_x, enq_y = tf.placeholder(tf.float64, shape=[None, 43]), tf.placeholder(tf.float64, shape=[None])
enqueue_op = queue.enqueue_many([enq_x, enq_y])

bth_sz = tf.placeholder(tf.int32)
x, y_ = queue.dequeue_many(bth_sz)

# here you initialize your variables and loss for training that use x and y_
# ...

# Note that you can enqueue data any size. For example if you have big data set you can divide it
# and enqueue each part in different threads
with tf.Session() as sess:
    sess.run(enqueue_op, feed_dict={enq_x: Train[:,0:43], enq_y: Train[:,43]})

    sess.run([x, y_], feed_dict={bth_sz: 10})

我得到了这个错误:
TypeError:feed的值不能是tf.Tensor对象。可接受的提要值包括Python标量、字符串、列表或numpy ndarray。
我认为这是因为也应该有一个sess.run用于出列。不是吗?在哪一部分代码错误发生?我添加代码,这对我来说很好,试着使用它。如果错误再次发生,请告诉我,并提供您使用的完整代码。我得到了以下错误:
TypeError:feed的值不能是tf.Tensor对象。可接受的提要值包括Python标量、字符串、列表或numpy ndarray。
我认为这是因为也应该有一个sess.run用于出列。不是吗?在哪一部分代码错误发生?我添加代码,这对我来说很好,试着使用它。如果错误再次发生,请告诉我,并提供您使用的完整代码。