我只是从caffe切换到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条记录。我也不想自己编写小批量选择。
提前致谢, 阿夫欣
答案 0 :(得分:0)
使用队列非常容易,但不幸的是tutorial(对我而言)并不明显。所以下面我提供了使用队列来使用批次进行培训的示例:
# 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 - 占位符代替常量
<强> EDITED 强>:
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})