Reputation: 651
I'm a newbie in Tensorflow and trying to do MNIST example in python 3.6.
I built up the code to see how errors changes during # of iterations, but it gives me an error on feed_dict
inside of sess.run
.
Here is my code as below,
import tensorflow as tf
import numpy as np
import functools
import sys
sys.path.append('./utils')
from mnist import MNIST
def lazy_property(function):
attribute = '_cache_' + function.__name__
@property
@functools.wraps(function)
def decorator(self):
if not hasattr(self, attribute):
setattr(self, attribute, function(self))
return getattr(self, attribute)
return decorator
class Model:
def __init__(self, image, label):
self.image = image
self.label = label
self.logits
self.prediction
self.optimize
self.error
@lazy_property
def logits(self):
weight = tf.Variable(tf.zeros([img_size_flat, num_classes]))
#print (img_size_flat)
biases = tf.Variable(tf.zeros([num_classes]))
#print (num_classes)
equation = tf.matmul(self.image, weight) + biases
return equation
@lazy_property
def prediction(self):
return tf.nn.softmax(self.logits)
@lazy_property
def optimize(self):
cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.logits, labels=self.label)
cost = tf.reduce_mean(cross_entropy)
return tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(cost)
@lazy_property
def error(self):
y_pred_cls = tf.argmax(self.prediction, axis=1)
mistakes = tf.not_equal(y_true_cls, y_pred_cls)
#print(mistakes)
error_from_acc = tf.reduce_mean(tf.cast(mistakes, tf.float32))
return error_from_acc
batch_size = 100
num_steps = 1000
tf.reset_default_graph()
data = MNIST(data_dir="data/MNIST/")
img_size_flat = data.img_size_flat
img_shape = data.img_shape
num_classes = data.num_classes
image = tf.placeholder(tf.float32, [None, img_size_flat])
label = tf.placeholder(tf.float32, [None, num_classes])
y_true_cls = tf.placeholder(tf.int64, [None])
#print (y_true_cls)
model = Model(image, label)
with tf.Session() as session:
session.run(tf.global_variables_initializer())
for step in range(num_steps):
error = session.run(model.error, {x: data.x_test, y_true: data.y_test}) # Gives me an error message from HERE!!!!
x_batch, y_true_batch, _ = data.random_batch(batch_size=batch_size)
session.run(model.optimize, {x: x_batch, y_true: y_true_batch})
if (step % 100 == 0):
print("Error rate @ iter %d : %f" % (step, error))
What did I do wrong?
Where should I put that feed_dict ({x: data.x_test, y_true: data.y_test}
and {x: x_batch, y_true: y_true_batch}
)?
Also, is there anywhere I did wrong in the code?
Please help me out here..
Thanks.
Upvotes: 0
Views: 67
Reputation: 10709
You have defined your placeholders like this:
image = tf.placeholder(tf.float32, [None, img_size_flat])
label = tf.placeholder(tf.float32, [None, num_classes])
, but then you are passing x
and y_true
as placeholders into session.run
:
session.run(model.error, {x: data.x_test, y_true: data.y_test})
Therefore you need to replace x
and y_true
by image
and label
and you should be fine:
session.run(model.error, {image : data.x_test, label : data.y_test})
Upvotes: 1