国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > 幫助中心 > 正文

TensorFlow MNIST手寫數(shù)據(jù)集的實現(xiàn)方法

2024-07-09 22:43:22
字體:
來源:轉載
供稿:網(wǎng)友

MNIST數(shù)據(jù)集介紹

MNIST數(shù)據(jù)集中包含了各種各樣的手寫數(shù)字圖片,數(shù)據(jù)集的官網(wǎng)是:http://yann.lecun.com/exdb/mnist/index.html,我們可以從這里下載數(shù)據(jù)集。使用如下的代碼對數(shù)據(jù)集進行加載:

from tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets('MNIST_data', one_hot=True)

運行上述代碼會自動下載數(shù)據(jù)集并將文件解壓在MNIST_data文件夾下面。代碼中的one_hot=True,表示將樣本的標簽轉化為one_hot編碼。

MNIST數(shù)據(jù)集中的圖片是28*28的,每張圖被轉化為一個行向量,長度是28*28=784,每一個值代表一個像素點。數(shù)據(jù)集中共有60000張手寫數(shù)據(jù)圖片,其中55000張訓練數(shù)據(jù),5000張測試數(shù)據(jù)。

在MNIST中,mnist.train.images是一個形狀為[55000, 784]的張量,其中的第一個維度是用來索引圖片,第二個維度圖片中的像素。MNIST數(shù)據(jù)集包含有三部分,訓練數(shù)據(jù)集,驗證數(shù)據(jù)集,測試數(shù)據(jù)集(mnist.validation)。

標簽是介于0-9之間的數(shù)字,用于描述圖片中的數(shù)字,轉化為one-hot向量即表示的數(shù)字對應的下標為1,其余的值為0。標簽的訓練數(shù)據(jù)是[55000,10]的數(shù)字矩陣。

下面定義了一個簡單的網(wǎng)絡對數(shù)據(jù)集進行訓練,代碼如下:

import tensorflow as tfimport numpy as npfrom tensorflow.examples.tutorials.mnist import input_dataimport matplotlib.pyplot as pltmnist = input_data.read_data_sets('MNIST_data', one_hot=True)tf.reset_default_graph()x = tf.placeholder(tf.float32, [None, 784])y = tf.placeholder(tf.float32, [None, 10])w = tf.Variable(tf.random_normal([784, 10]))b = tf.Variable(tf.zeros([10]))pred = tf.matmul(x, w) + bpred = tf.nn.softmax(pred)cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1))learning_rate = 0.01optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)training_epochs = 25batch_size = 100display_step = 1save_path = 'model/'saver = tf.train.Saver()with tf.Session() as sess:  sess.run(tf.global_variables_initializer())  for epoch in range(training_epochs):    avg_cost = 0    total_batch = int(mnist.train.num_examples/batch_size)    for i in range(total_batch):      batch_xs, batch_ys = mnist.train.next_batch(batch_size)      _, c = sess.run([optimizer, cost], feed_dict={x:batch_xs, y:batch_ys})      avg_cost += c / total_batch    if (epoch + 1) % display_step == 0:      print('epoch= ', epoch+1, ' cost= ', avg_cost)  print('finished')  correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))  accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))  print('accuracy: ', accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))  save = saver.save(sess, save_path=save_path+'mnist.cpkt')print(" starting 2nd session ...... ")with tf.Session() as sess:  sess.run(tf.global_variables_initializer())  saver.restore(sess, save_path=save_path+'mnist.cpkt')  correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))  accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))  print('accuracy: ', accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))  output = tf.argmax(pred, 1)  batch_xs, batch_ys = mnist.test.next_batch(2)  outputval= sess.run([output], feed_dict={x:batch_xs, y:batch_ys})  print(outputval)  im = batch_xs[0]  im = im.reshape(-1, 28)  plt.imshow(im, cmap='gray')  plt.show()  im = batch_xs[1]  im = im.reshape(-1, 28)  plt.imshow(im, cmap='gray')  plt.show()
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 蓝山县| 新晃| 东乡族自治县| 靖州| 靖宇县| 贡嘎县| 通榆县| 多伦县| 武威市| 宁化县| 汝州市| 大竹县| 商南县| 陆丰市| 盐亭县| 台江县| 蓬安县| 建水县| 汝城县| 青岛市| 临城县| 盐池县| 湘潭县| 乐亭县| 抚顺县| 沙坪坝区| 光山县| 措美县| 和平区| 安西县| 宜良县| 梁平县| 年辖:市辖区| 潮安县| 明水县| 新河县| 株洲市| 皋兰县| 伊宁县| 紫云| 乡宁县|