1

I am new to tensorflow, so this is how my code unfolds!

import tensorflow as tf
import tensorflow.contrib.learn as learn
mnist = learn.datasets.mnist.read_data_sets('MNIST-data',one_hot=True)
import numpy as np
M = tf.Variable(tf.zeros([784,10]))
B = tf.Variable(tf.zeros([10]))
image_holder = tf.placeholder(tf.float32,[None,784])
label_holder = tf.placeholder(tf.float32,[None,10])
predicted_value = tf.add(tf.matmul(image_holder,M),B)
loss= tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predicted_value , labels=label_holder))
learning_rate = 0.01
num_epochs = 1000
batch_size = 100
num_batches = int(mnist.train.num_examples/batch_size)
init = tf.global_variables_initializer()
optimizer =  tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
with tf.Session() as sess:
    sess.run(init)
    for _ in range(num_epochs):
        for each_batch in range(num_batches):
            current_image, current_image_label = mnist.train.next_batch(batch_size)
            optimizer_value,loss = sess.run([optimizer,loss],feed_dict={image_holder:current_image,label_holder:current_image_label})
        print ("The loss value is {} \n".format(loss))    

But the problem I am getting is, this strange error that says

'numpy.dtype' object has no attribute 'base_dtype'

I do not know what is wrong with the code which I think is absolutely correct. Any help regarding this issue ?

2 Answers 2

2

FIrst of all some comments:

  • Initialization of variables should always go at the end of the construction graph
  • Optimizer and train op should be separated; not necessary but it is a good practice.
  • Also when you run sess.run(variable) be sure not to name this as the same variable. That is, make sure you are not doing this: variable=sess.run(variable). Because you overwrite it.

The error here was the last one. So the code, once working, could be something like:

M = tf.Variable(tf.zeros([784,10]), dtype=tf.float32)
B = tf.Variable(tf.zeros([10]), dtype=tf.float32)

image_holder = tf.placeholder(tf.float32,[None,784])
label_holder = tf.placeholder(tf.float32,[None,10])
predicted_value = tf.add(tf.matmul(image_holder,M),B)
loss= tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predicted_value , labels=label_holder))
learning_rate = 0.01
num_epochs = 1000
batch_size = 100
num_batches = int(mnist.train.num_examples/batch_size)

optimizer =  tf.train.GradientDescentOptimizer(learning_rate)
train_op = optimizer.minimize(loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for _ in range(num_epochs):
        for each_batch in range(num_batches):
            current_image, current_image_label = mnist.train.next_batch(batch_size)

            optimizer_value,loss_value = sess.run([train_op,loss],feed_dict={image_holder:current_image,label_holder:current_image_label})
        print ("The loss value is {} \n".format(loss_value)) 

Hope this has helped

Sign up to request clarification or add additional context in comments.

Comments

0

More explicitly, you just overwrote your node loss by the value of 'loss' when you did sess.run([_, loss]) for the first time. So the second time of the for loop the session see a numpy value in the place of the original loss op

2 Comments

could you explain this to a beginner? I am having trouble following your explanation.
@moegizzle when you do loss = tf.reduce_mean(). It's just a node, there isn't value flow through. But when you do loss = session.run() you assign a value to loss. value loss ≠ node

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.