我正在尝试使用 PyBrain 对 mnist 数据进行分类。
下面是训练代码:
def train_net(self):
print("Build network")
net = buildNetwork(784, 30, 10, bias=True, hiddenclass=TanhLayer, outclass=SoftmaxLayer)
back_trainer = BackpropTrainer(net, learningrate=1)
training_dataset = self.get_training_dataset()
print("Begin training")
time0 = time()
err = back_trainer.trainUntilConvergence(dataset=training_dataset, maxEpochs=300, verbose=True)
print("Training time is " + str(round(time()-time0, 3)) + " seconds.")
return net, err
def get_training_dataset(self):
print("Reading training images")
features_train = self.read_images("train-images.idx3-ubyte")
print("Reading training labels")
labels_train = self.read_labels("train-labels.idx1-ubyte")
# view_image(features_train[10])
print("Begin reshaping training features")
features_train = self.reshape_features(features_train)
print("Create training dataset")
training_dataset = ClassificationDataSet(784, 10)
for i in range(len(features_train)):
result = [0]*10
result[labels_train[i]] = 1
training_dataset.addSample(features_train[i], result)
training_dataset._convertToOneOfMany()
return training_dataset
当我在测试数据集上激活网络时,结果如下所示:
[ 3.72885642e-25 4.62573440e-64 2.32150541e-31 9.42499004e-16
1.33256639e-39 2.30439387e-17 5.16602624e-94 1.00000000e+00
1.83860601e-27 1.22969684e-22]
其中 argmax 值表示类。对于给定的列表 argmax 是 7。
但为什么?当我准备数据集时,您可以看到result[labels_train[i]] = 1
我需要相应的神经元给我 1 和其他必须为零的位置。所以我期待[0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
。
我读过 _convertToOneOfMany 函数可以产生这样的输出。所以我添加了它,但没有任何改变。我做错了什么?