I'm trying to implement a simple logistic regression for image classification using the Cifar10 dataset. I'm only allowed to use TensorFlow 1.x for the training. (I am allowed to use Keras and other libraries for manipulating the data)
My problem is that the model I built does not learn ... All epochs give values of 0.1 in the accuracy of both the test and the train.
I think there is some problem with manipulating the data itself before I send to the model, I would be happy to help understand why the model is not learning.
code:
%tensorflow_version 1.x
import tensorflow as tf
import numpy as np
import keras
import cv2 as cv2
import matplotlib.pyplot as plt
from keras.utils import to_categorical
from keras.datasets import mnist, cifar10
def get_cifar10():
"""Retrieve the CIFAR dataset and process the data."""
# Set defaults.
nb_classes = 10
batch_size = 64
input_shape = (3072,)
# Get the data.
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.reshape(50000, 3072)
x_test = x_test.reshape(10000, 3072)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# x_train /= 255
# x_test /= 255
# convert class vectors to binary class matrices
y_train = to_categorical(y_train, nb_classes)
y_test = to_categorical(y_test, nb_classes)
return (nb_classes, batch_size, input_shape, x_train, x_test, y_train, y_test)
nb_classes, batch_size, input_shape, x_train, x_test, y_train, y_test = get_cifar10()
features = 3072
categories = nb_classes
x = tf.placeholder(tf.float32, [None, features])
y_ = tf.placeholder(tf.float32, [None, categories])
W = tf.Variable(tf.zeros([features,categories]))
b = tf.Variable(tf.zeros([categories]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
loss = -tf.reduce_mean(y_*tf.log(y))
update = tf.train.GradientDescentOptimizer(0.0001).minimize(loss)
correct_prediction = tf.equal(tf.argmax(y, 1),tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for epoch in range(0,1000):
sess.run(update, feed_dict = {x:x_train, y_:y_train}) #BGD
train_acc = sess.run(accuracy, feed_dict={x:x_train, y_:y_train})
test_acc = sess.run(accuracy, feed_dict={x:x_test, y_:y_test})
if(epoch % 10 == 0):
print ("epoch: %3d train_acc: %f test_acc: %f" % (epoch,train_acc, test_acc))
Running the model gives the following:
epoch: 0 train_acc: 0.099880 test_acc: 0.099900
epoch: 10 train_acc: 0.100000 test_acc: 0.100000
epoch: 20 train_acc: 0.100000 test_acc: 0.100000
epoch: 30 train_acc: 0.100000 test_acc: 0.100000
epoch: 40 train_acc: 0.100000 test_acc: 0.100000
epoch: 50 train_acc: 0.100000 test_acc: 0.100000
epoch: 60 train_acc: 0.100000 test_acc: 0.100000
epoch: 70 train_acc: 0.100000 test_acc: 0.100000
epoch: 80 train_acc: 0.100000 test_acc: 0.100000
epoch: 90 train_acc: 0.100000 test_acc: 0.100000
epoch: 100 train_acc: 0.100000 test_acc: 0.100000
epoch: 110 train_acc: 0.100000 test_acc: 0.100000
epoch: 120 train_acc: 0.100000 test_acc: 0.100000
epoch: 130 train_acc: 0.100000 test_acc: 0.100000
Thanks in advance!
So you got three problems
Uncomment these two lines:
You should normalize your input.
The loss is not the mean of the log losses, but only the sum (you are working with mutually exclusive classes)
Change your optimizer, or the learning rate. I have used Adam and the loss is now ok
The run on colab
output:
Obviously it is not the best to use a LogistRegressor on images. To obtain better and faster results it's better to use CNN