用tensorflow在mnist数据集上做测试

用tensorflow在mnist数据集上做测试

方法:softmax regression

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)
输出:
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
print(mnist.train.images.shape, mnist.train.labels.shape)
print(mnist.test.images.shape, mnist.test.labels.shape)
print(mnist.validation.images.shape, mnist.validation.labels.shape)
(55000, 784) (55000, 10)
(10000, 784) (10000, 10)
(5000, 784) (5000, 10)

placeholder 第一个参数表示类型, 第二个参数中None代表不限条输入,784表示每条输入是一个784维向量

import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W)+b)

用交叉熵计算loss

先定义一个placeholder, 输入是真实的label, 用来计算cross-entropy.
1. tf.reduce_mean 对每个 batch 数据求均值
2. tf.reduce_sum 求和

y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices = [1]))

优化算法

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

注意

tf.global_variables_initializer() 是 tensorflow1.0.0 之后开始使用
tf.initialize_all_variables() 是 tensorflow1.0.0 之前的用法

tf.global_variables_initializer().run()

每次从训练集中抽出100个样本, 构成一个mini-batch, 并feed给placeholder, 然后调用train_step 对这些样本进行训练。
优点:
使用一小部分样本进行训练成为随机梯度下降(SGD), 与每次使用全部样本的传统的梯度下降对应。
如果每次训练都是用全部样本,计算量太大,有时也不容易跳出局部最优,因此,对于大部分机器学习问题,我们都只是用一小部分数据进行随机梯度下降, 这种做法绝大多数时候回避全体样本训练的收敛速度很多

for i in range(1000):

    batch_xs, batch_ys = mnist.train.next_batch(100)
    train_step.run({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))
print(accuracy.eval({x: mnist.test.images, y_:mnist.test.labels}))
0.9169

流程共分为4部分:

  1. 定义算法公式
  2. 定义loss, 选定优化器,并指定优化器优化loss,
  3. 迭代对数据进行训练,
  4. 在测试集或验证集上对数据进行评估
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorFlow 中,可以使用 `tf.keras.datasets` 模块来读取 MNIST 数据集。以下是一个读取 MNIST 数据集的示例: ```python import tensorflow as tf # 使用 tf.keras.datasets 中的 load_data 函数读取 MNIST 数据集 (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() # 对数据进行预处理 x_train = x_train / 255.0 x_test = x_test / 255.0 # 将数据转换为张量 x_train = tf.convert_to_tensor(x_train, dtype=tf.float32) y_train = tf.convert_to_tensor(y_train, dtype=tf.int32) x_test = tf.convert_to_tensor(x_test, dtype=tf.float32) y_test = tf.convert_to_tensor(y_test, dtype=tf.int32) # 创建数据集 train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)) test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test)) # 打乱数据并分成 batch train_dataset = train_dataset.shuffle(buffer_size=10000) train_dataset = train_dataset.batch(32) test_dataset = test_dataset.batch(32) # 迭代数据 for x, y in train_dataset: # 训练模型 pass ``` 这个例子中,我们使用 `tf.keras.datasets.mnist.load_data` 函数来读取 MNIST 数据集,并将数据集拆分为训练集和测试集。然后,我们对数据进行预处理,将像素值缩放到 0 到 1 之间。 接着,我们将数据转换为 TensorFlow 张量,并使用 `tf.data.Dataset.from_tensor_slices` 函数将数据集转换为 TensorFlow 数据集。最后,我们使用 `shuffle` 和 `batch` 函数对数据进行随机打乱和分批次。 最后,我们可以在模型训练时使用 `for` 循环迭代数据集中的每一个批次,并对模型进行训练。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值