I'm beginning with tensorflow and Machine Learning , but I have a book very useful about it. I would like to implement mini-batch gradient descent. I followed exactly what they said but it didn't work.
It's explained as : "Finally, in the execution phase, fetch the mini-batches one by one,then provide the value of X and Y via the feed_dict parameter when evaluating a node that depends on either of them."
I'm using Jupyter notebook
, tensorflow 1.3.0.
Here's what I tried :
n_epochs=1000
learning_rate=0.0001
#X=tf.constant(housing_data_plus_bias,dtype=tf.float32,name="X")
X=tf.placeholder(tf.float32,shape=(None,n+1),name="X")
Y=tf.placeholder(tf.float32,shape=(None,1),name="Y")
batch_size=100
n_batches=int(np.ceil(m/batch_size))
#Y=tf.constant(housing.target.reshape(-1,1),dtype=tf.float32,name="Y")
theta=tf.Variable(tf.random_uniform([n+1,1],-1.0,1.0),name="theta")
y_pred=tf.matmul(X,theta,name="predictions") #eq 1.4
error=y_pred - Y
mse=tf.reduce_mean(tf.square(error),name="mse") #eq 1.5
#gradients=tf.gradients(mse,[theta])[0]
gradients= (2/(m*mse) ) * tf.matmul(tf.transpose(X),error)
training_op = tf.assign(theta,theta - learning_rate * gradients)
def fetch_batch(epoch,batch_index,batch_size):
[...] #Load DATA FROM DISK (SEE NOTEBOOK)
return X_batch, Y_batch
init=tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(n_epochs):
for batch_index in range(n_batches):
X_batch,Y_batch=fetch_batch(epoch,batch_index,batch_size)
sess.run(training_op,feed_dict={X:X_batch,Y:Y_batch})
best_theta=theta.eval()
print(best_theta)
and here's the error :
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-41-f199ccce6734> in <module>
27 for epoch in range(n_epochs):
28 for batch_index in range(n_batches):
---> 29 X_batch,Y_batch=fetch_batch(epoch,batch_index,batch_size)
30 sess.run(training_op,feed_dict={X:X_batch,Y:Y_batch})
31
<ipython-input-41-f199ccce6734> in fetch_batch(epoch, batch_index, batch_size)
19 def fetch_batch(epoch,batch_index,batch_size):
20 [...]
---> 21 return X_batch, Y_batch
22
23 init=tf.global_variables_initializer()
NameError: name 'X_batch' is not defined
So my question is , what should I do with that [...] , is it a real way to load data from disk or should I replace it by something ?
batch_index
– mrksfetch_batch()
function). – bruno desthuilliers