【发布时间】:2019-04-09 16:52:08
【问题描述】:
我正在训练模型。但是,当我应用教程中的代码时:batch_x, batch_y = mnist.train.next_batch(50)。它表明 TensorFlow 模型中没有属性“train”。我知道这是过时的代码,我尝试转换为新版本的 TensorFlow。但是,我找不到可以与上述代码行执行相同操作的匹配代码。我敢打赌有一种方法,但我想不出一个解决方案。
我找到了一个要求我使用tf.data.Dataset.batch(batch_size) 的方法。
我尝试了以下方法,但它们都不起作用。
a. batch_x, batch_y = mnist.train.next_batch(50)
b. batch_x, batch_y = tf.data.Dataset.batch(batch_size)
c. batch_x, batch_y = tf.data.Dataset.batch(50)
d. batch_x, batch_y = mnist.batch(50)
with tf.Session() as sess:
#FIrst, run vars_initializer to initialize all variables
sess.run(vars_initializer)
for i in range(steps):
#Each batch: 50 images
batch_x, batch_y = mnist.train.next_batch(50)
#Train the model
#Dropout keep_prob (% to keep): 0.5 --> 50% will be dropped out
sess.run(cnn_trainer, feed_dict={x: batch_x, y_true: batch_y, hold_prob: 0.5})
#Test the model: at each 100th step
#Run this block of code for each 100 times of training, each time run a batch
if i % 100 == 0:
print('ON STEP: {}'.format(i))
print('ACCURACY: ')
#Compare to find matches of y_pred and y_true
matches = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1))
#Cast the matches from integers to tf.float32
#Calculate the accuracy using the mean of matches
acc = tf.reduce_mean(tf.cast(matches, tf.float32))
#Test the model at each 100th step
#Using test dataset
#Dropout: NONE because of test, not training.
test_accuracy = sess.run(acc, feed_dict = {x:mnist.test.images, y_true:mnist.test.labels, hold_prob:1.0})
print(test_accuracy)
print('\n')
【问题讨论】:
-
您想从 MNIST 数据集中获取批次?
-
@Shubham Panchal,是的,我正在尝试获取 batch_x 和 batch_y。
-
请正确格式化您的代码。
标签: python-3.x tensorflow