import numpy as np import snnfunctions as sf import read_mnist as rm a = np.load("E:/design project/wenquxing22a-master/sw/swSim-mnist/scripts/weight/standard_weights.npy")#np.load("./data/active-learning-weight-labels.npz") X_test, test_label = rm.load_mnist('E:/design project/wenquxing22a-master/sw/swSim-mnist/scripts',kind='t10k') images = np.load("E:/design project/wenquxing22a-master/sw/swSim-mnist/scripts/result date/nomalizedDataTest.npy") neurons = np.zeros(len(a), dtype=np.int64) iterations = 100 vleak = 12 vth = 37 acc = 0 #n = 1 for (testimg, p) in zip(range(10000), range(1, 10001)): sf.processBar(p, 10000, "Testing") neuSpike = np.zeros(len(a), dtype= np.int64) frequence = np.zeros(len(a), dtype=np.int64) poisson_input_test = [] poisson_input_test = np.asarray(sf.PoissonEncoder(iterations, poisson_input_test, images[testimg])) for test in poisson_input_test: for j in range(len(neurons)): vin = np.sum(test.reshape(784,) & a[j])#['weight'][j]) (neurons[j], neuSpike[j]) = sf.neuCharge(neurons[j], vin, vleak, vth) frequence += neuSpike result = np.where(frequence == np.max(frequence)) for i in range(len(result)): if (result[i] == test_label[testimg]).any(): acc += 1 break else: continue print("") print("done!") print(acc / 10000.00 * 100, "%")
时间: 2024-04-04 19:29:12 浏览: 26
这段代码使用了numpy、snnfunctions和read_mnist三个模块,并且需要预先安装这些模块。其中numpy用于处理多维数组,snnfunctions是自定义的神经元模型,read_mnist用于读取MNIST数据集。代码的主要作用是利用已经训练好的神经元权重进行手写数字的识别,并输出准确率。代码中的具体流程如下:
1.使用numpy模块的load函数读取已经训练好的神经元权重。
2.使用read_mnist模块中的load_mnist函数读取测试集数据。
3.使用numpy模块的load函数读取已经归一化的测试集数据。
4.初始化神经元的输出值和脉冲计数器。
5.使用自定义的Poisson编码器对测试集数据进行编码,并计算每一个神经元的输入电流。
6.根据神经元模型计算每一个神经元的输出值和脉冲计数器。
7.统计每一个神经元的脉冲个数,找到脉冲最多的神经元。
8.将脉冲最多的神经元的输出结果与实际标签进行比较,计算准确率。
9.输出准确率。
需要注意的是,这段代码没有经过完整的测试,可能存在一些错误。
相关问题
import time start_time = datetime.datetime.now() print("start preprocessing..................") n = 10000 (X_train, train_labels) = rm.load_mnist('E:\design project\wenquxing22a-master\sw\swSim-mnist\scripts', kind='train') otsu_imgs = [] images = [] for (k, j) in zip(range(n),range(1, n + 1)): o = sf.OTSU(X_train[k].reshape([28,28])) otsu_imgs.append(o.Otsu()) sf.processBar(j, n, "Otsuing") print("") # image normalization for (img, i) in zip(otsu_imgs, range(1, n + 1)): images.append(sf.deskew(img / 255.0)) sf.processBar(i, n, "Normalization") print("") images = np.asarray(images) np.save("E:/design project/wenquxing22a-master/sw/swSim-mnist/scripts/result date/nomalizedData_after_OTSU.npy", images) np.save("E:/design project/wenquxing22a-master/sw/swSim-mnist/scripts/result date/label.npy", train_labels) otsu_imgs_test = [] X_test, test_label = rm.load_mnist(r'E:\design project\wenquxing22a-master\sw\swSim-mnist\scripts',kind='t10k') testimages = [] for (k, j) in zip(range(n),range(1, n + 1)): o = sf.OTSU(X_test[k].reshape([28,28])) otsu_imgs_test.append(o.Otsu()) sf.processBar(j, n, "Otsuing") print("") # image normalization for img in otsu_imgs_test: testimages.append(sf.deskew(img / 255.0)) #soft_test = sf.imgSoft(testimages, soft_test, 0.80) #soft_test = np.asarray(soft_test) np.save("E:/design project/wenquxing22a-master/sw/swSim-mnist/scripts/result date/nomalizedDataTest.npy", testimages) np.save("E:/design project/wenquxing22a-master/sw/swSim-mnist/scripts/result date//label_test.npy",test_label) end_time = datetime.datetime.now() print("done!") print("running time: ", end_time - start_time)
这段代码是用来对 MNIST 数据集进行预处理的。首先,通过调用 `rm.load_mnist()` 函数加载 MNIST 数据集,然后使用 Otsu 算法进行图像二值化处理,接着对图像进行归一化处理(即去除图像中的旋转和偏移)。最后,将处理好的图像数据和对应的标签保存到 Numpy 数组中。其中,函数的输入参数包括数据集的路径和数据集的类型(训练集或测试集),预处理后的图像数据和标签数据会分别保存到不同的文件中。这段代码可以用于对 MNIST 数据集进行预处理,以便在训练和测试模型时使用。
import numpy as np x = np.load("E:\\pythonProject3\\netsim\\sim3_subject_10.npz.") x.files
()There seems to be an error in the code. The correct code to load the file and check the available variables would be:
```
import numpy as np
# load the file
x = np.load("E:\\pythonProject3\\netsim\\sim3_subject_10.npz")
# check the available variables
print(x.files)
```
This should return the list of available variables in the file.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)