import minist的时候报错:
No module named ‘mnist’
部分代码:
import mnist
import numpy as np
from conv import Conv3x3
from maxpool import MaxPool2
from softmax import Softmax
# 训练集文件
#train_images_idx3_ubyte_file = '../../data/mnist/bin/train-images.idx3-ubyte'
# 训练集标签文件
#train_labels_idx1_ubyte_file = '../../data/mnist/bin/train-labels.idx1-ubyte'
# 测试集文件
test_images_idx3_ubyte_file = r't10k-images.idx3-ubyte'
# 测试集标签文件
test_labels_idx1_ubyte_file = r't10k-labels.idx1-ubyte'
#我们只使用前1K测试示例(全部是10K个)。为了时间的利益。如果你想的话,可以随意改变
test_images = mnist.test_images()[:1000]
test_labels = mnist.test_labels()[:1000]
conv = Conv3x3(8) # 28x28x1 -> 26x26x8
pool = MaxPool2() # 26x26x8 -> 13x13x8
softmax = Softmax(13 * 13 * 8, 10) # 13x13x8 -> 10
def forward(image, label):
#完成CNN的前向传递并计算准确度和交叉熵损失。
#图像是二维numpy数组。标签是数字
#我们将图像从[0,255]转换为[-0.5,0.5]以使其更容易。一起工作。这是标准做法。
out = conv.forward((image / 255) - 0.5)
out = pool.forward(out)
out = softmax.forward(out)
# 计算交叉熵损失和精度。np.log() 是自然日志
loss = -np.log(out[label])
acc = 1 if np.argmax(out) == label else 0
return out, loss, acc
print('MNIST CNN 初始化!')
loss = 0
num_correct = 0
for i, (im, label) in enumerate(zip(test_images, test_labels)):
# 向前传递
_, l, acc = forward(im, label)
loss += l
num_correct += acc
# 每100步打印一次统计数据
if i % 100 == 99:
print('[Step %d] Past 100 steps: Average Loss %.3f | Accuracy: %d%%'%(i + 1, loss/100, num_correct) )
loss = 0
num_correct = 0
整体代码链接:
https://blog.csdn.net/zimiao552147572/article/details/92801266
初次怀疑是不是被删了,因为都是tensorflow在使用minist。
import没有错,错的是确实有这个包,需要pip导入
pip install mnist(不要打错mnist)
希望能帮助我,谢谢!