Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 1

Accuracy updation error in Tensorflow CNN

Greetings All,

I am a newbie to Tensorflow and am currently doing practice on CNN using Tensorflow in Python. As a part of my learning, I have written a program to classify handwritten digits using the MNIST dataset on handwritten digits. The training part is going well, but the accuracy is not being updated properly in the program.

The program returns the metrics like [None, Loss, Accuracy].

import warnings
import tensorflow as tf
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt
import numpy as np
from sklearn.preprocessing import OneHotEncoder

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0
train_images = train_images[:10000]
train_labels = train_labels[:10000]
train_images = train_images.reshape(-1,28,28,1)
train_labels = train_labels.reshape(-1,1)
test_images = test_images.reshape(-1,28,28,1)
test_labels = test_labels.reshape(-1,1)
train_images = train_images.astype('float32')
test_images = test_images.astype('float32')

del test_images
del test_labels

#one1 = OneHotEncoder()
#test_labels = one1.fit_transform(test_labels).toarray()
#test_images = tf.constant(test_images,dtype=tf.float32)
#test_labels = tf.constant(test_labels,dtype=tf.float32)

one = OneHotEncoder()
ygold = one.fit_transform(train_labels).toarray()
ygold = tf.constant(ygold,dtype=tf.float32)
x = tf.constant(train_images,dtype=tf.float32)

w_conv1 = tf.Variable(tf.zeros(shape=[5,5,1,32]))
b_conv1 = tf.Variable(tf.zeros(shape=[32]))
convolve1 = tf.nn.conv2d(x,w_conv1,strides=[1,1,1,1],padding='SAME') + b_conv1
convolve1 = tf.nn.relu(convolve1)
convolve1 = tf.nn.max_pool(convolve1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

w_conv2 = tf.Variable(tf.zeros(shape=[5,5,32,64]))
b_conv2 = tf.Variable(tf.zeros(shape=[64]))
convolve2 = tf.nn.conv2d(convolve1,w_conv2,strides=[1,2,2,1],padding='SAME') + b_conv2
convolve2 = tf.nn.relu(convolve2)
convolve2 = tf.nn.max_pool(convolve2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

fc_matrix = tf.reshape(convolve2,shape=[-1,4*4*64])

w_fc = tf.Variable(tf.zeros(dtype=tf.float32,shape=[1024,10]))
b_fc = tf.Variable(tf.zeros(dtype=tf.float32,shape=[10]))

hyp = tf.sigmoid(tf.matmul(fc_matrix,w_fc) + b_fc)
loss = tf.reduce_mean(-tf.reduce_sum(ygold * tf.log(hyp),reduction_indices=[1]))
trainm = tf.train.GradientDescentOptimizer(0.05).minimize(loss)
cp = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(hyp,1),tf.argmax(ygold,1)),dtype=tf.float32))

sess = tf.Session()

init = tf.global_variables_initializer()
for i in range(100):


Please suggest a solution.