for 循环中的 MNIST 训练错误
MNIST training error in for loop
我仍在处理我的 MNIST 入门项目,现在我在 Training for Loop 中遇到了另一个问题:
Traceback (most recent call last):
File "C:\Users\uidj8441\Documents\PYTHON[=10=]_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'
看看我到目前为止的完整代码——目标是保存训练好的模型并将其重新加载到另一个文件中……首先我必须清除上面的错误:
## 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))
非常感谢!!!
mnist
模块似乎没有 train
class.Maybe 从 tensorflow 本身加载数据集?
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
这应该允许使用 next_batch()
我仍在处理我的 MNIST 入门项目,现在我在 Training for Loop 中遇到了另一个问题:
Traceback (most recent call last):
File "C:\Users\uidj8441\Documents\PYTHON[=10=]_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'
看看我到目前为止的完整代码——目标是保存训练好的模型并将其重新加载到另一个文件中……首先我必须清除上面的错误:
## 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))
非常感谢!!!
mnist
模块似乎没有 train
class.Maybe 从 tensorflow 本身加载数据集?
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
这应该允许使用 next_batch()