I am currently facing an issue with the next_batch function. When I try to implement it with the code I have below, I get this error.
AttributeError: 'str' object has no attribute 'nextBatch'
I am suspecting that the issue is stemming from the next_batch function. I have tried to create my own implementation of the function from the tensorflow example. I have based my code for this Logistic Regression model on the example below.
I have based the code for the next_batch function on a previous StackOverflow post.
how to implement tensorflow's next_batch for own data
Thanks for the help in advance.
from __future__ import print_function
import tensorflow as tf
import os as os
from LogRegModel import csvToTrainTF, csvtoTestTF
dir_path = os.path.dirname(os.path.realpath(__file__))
filename1 = dir_path + "/TrainData2.csv"
filename2 = dir_path + "/TestData2.csv"
filenameTrain = 'TrainData2.tfrecords'
filenameTest = 'TestData2.tfrecords'
trainData = csvToTrainTF(filename1, filenameTrain)
testData = csvtoTestTF(filename2, filenameTest)
learning_rate = 0.01
training_epochs = 20
batch_size = 32
display_step = 1
x = tf.placeholder(tf.float32, [None, 9])
y = tf.placeholder(tf.float32, [None, 2])
W = tf.Variable(tf.zeros([9,2]))
b = tf.Variable(tf.zeros([2]))
pred = tf.nn.softmax(tf.matmul(x,W) + b)
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices = 1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
start = 0
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(992/batch_size)
for i in range(total_batch):
batch_xs, batch_ys = trainData.nextBatch(batch_size) # Error is occurring in the train.next_batch call? Could you please tell me how to write out the function?
_, c = sess.run([optimizer, cost], feed_dict={x: batch_xs, y: batch_ys})
avg_cost += c/total_batch
if (epoch+1) % display_step == 0:
print("Epoch:", '%40d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost))
print("Optimization Finished")
# Test model
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
# Calculate accuracy
#I am not sure if this is the correct way to print out the accuracy. Could you please check this?
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Accuracy:", accuracy.eval({x: trainData[0:9], y: trainData[10]}))
Above is the code for the main function Below is code for next_batch
def next_batch(self,batch_size,shuffle = True):
start = self._index_in_epoch
if start == 0 and self._epochs_completed == 0:
idx = np.arange(0, self._num_examples) # get all possible indexes
np.random.shuffle(idx) # shuffle indexe
self._data = self.data[idx] # get list of `num` random samples
# go to the next batch
if start + batch_size > self._num_examples:
self._epochs_completed += 1
rest_num_examples = self._num_examples - start
data_rest_part = self.data[start:self._num_examples]
idx0 = np.arange(0, self._num_examples) # get all possible indexes
np.random.shuffle(idx0) # shuffle indexes
self._data = self.data[idx0] # get list of `num` random samples
start = 0
self._index_in_epoch = batch_size - rest_num_examples #avoid the case where the #sample != integar times of batch_size
end = self._index_in_epoch
data_new_part = self._data[start:end]
return np.concatenate((data_rest_part, data_new_part), axis=0)
else:
self._index_in_epoch += batch_size
end = self._index_in_epoch
return self._data[start:end]