I am still working on my MNIST getting started Project and now I ve got another issue in the Training for Loop:
Traceback (most recent call last):
File "C:\Users\uidj8441\Documents\PYTHON\0_projects\aa\train_mnist_model\train
_mnist_model\train_mnist_model.py", line 58, in <module>
batch_xs, batch_ys = mndata.train.next_batch(100)
# every loop iteration: hundred images are trained
AttributeError: 'MNIST' object has no attribute 'train'
See my complete code so far-goal is to save the trained model and reload it in an other file... first I have to clear the error from above:
## skript loads MNIST dataset and saves the model in a file
#### libaries
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import time
import tensorflow as tf
from mnist import MNIST
import random
from PIL import Image, ImageOps
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' #deactivate warnings
#### set and print working folder
os.chdir('C:\\Users\\uidj8441\\Documents\\PYTHON\\0_projects\\aa\\train_mnist_model\\train_mnist_model')
print('working folder:\n\n',os.getcwd(),'\n')
#### load dataset from idx1 / idx3 files
mndata = MNIST('C:\\Users\\uidj8441\\Documents\\PYTHON\\0_projects\\aa\\train_mnist_model\\train_mnist_model\\')
images, labels =mndata.load_training()
#images_train, labels_train =mndata.load_training()
#images_test, labels_test =mndata.load_testing()
#### display random / explicit image
print('\nLoading random image and display\n')
index=random.randrange(0,len(images))
print('Random image with index',index,'is a:',labels[index])
print(mndata.display(images[index]))
img_num=8
print('\n Chosen image with index',img_num, 'is a:',labels[img_num])
print(mndata.display(images[img_num]))
################################
# # # # # # Training # # # # # #
#### create the model
x = tf.placeholder(tf.float32, [None, 784]) #x=image , None=any length (variable size)
W = tf.Variable(tf.zeros([784, 10])) #W=weigths variable -> Tensor full of zeros
b = tf.Variable(tf.zeros([10])) #b=bias variable -> Tensor full of zeros
y = tf.nn.softmax(tf.matmul(x, W) + b) #y=label , implement the model: softmax(x*W+b)
#### Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10]) # y_= placeholder for correct answers
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) # cross_entrop: determine the loss/cost of the model
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) # optimization algo->make Grad.Desc. with learning rate of 0.5
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
#### --> Begin Training
for _ in range(100): # (n) is number of training steps
batch_xs, batch_ys = mndata.train.next_batch(100) # every loop iteration: hundred images are trained
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
Thanks a lot !!!