Python解析mnist出错的解决方法

Python解析mnist的方法:http://blog.csdn.net/lanchunhui/article/details/51244423#comments

同很多网友一样,博主在用Python运行时,常出现以下错误:

UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0x90 in position 614: ordinal not in range(128)
解决方法:在 pickle.load(f)中加入 encoding=’bytes’ ,即 pickle.load(f, encoding=’bytes’)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
MNIST是一个非常经典的手写数字数据集,常用于机器学习领域中的图像识别任务。在Python中,我们可以使用TensorFlow或者PyTorch等深度学习框架来调用MNIST数据集。 以下是使用TensorFlow调用MNIST数据集的示例代码: ``` python import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 下载并加载MNIST数据 mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # 构建神经网络模型 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) # 定义损失函数和优化器 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) # 训练模型 sess = tf.InteractiveSession() tf.global_variables_initializer().run() for _ in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) 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)) print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})) ``` 在上面的代码中,我们首先使用`input_data.read_data_sets()`函数加载MNIST数据集,其中`one_hot=True`表示将标签转换成One-hot编码。然后我们定义了一个简单的神经网络模型,使用交叉熵作为损失函数,使用梯度下降优化器进行训练。最后,我们通过计算准确率来评估模型的性能。 希望这个例子可以帮助你理解如何使用Python调用MNIST数据集进行机器学习任务。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值