在 chainer 中使用数组作为 MNIST 数据的标签
Using arrays as labels on MNIST data in chainer
python 模块 chainer has an introduction where it uses its neural network to recognize handwritten digits from the MNIST database。
假设特定的手写数字 D.png
被标记为 3
。我已经习惯了以数组形式出现的标签,如下所示:
label = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
但是,chainer
标签改为整数:
label = 3
数组标签对我来说更直观,因为输出预测也是一个数组。在不处理图像的神经网络中,我希望可以灵活地将标签指定为特定数组。
我直接从链接器介绍中包含了下面的代码。如果您解析 train
或 test
数据集,请注意所有标签都是整数而不是浮点数。
我如何 运行 training/test 以数组而不是整数作为标签的数据?
import numpy as np
import chainer
from chainer import cuda, Function, gradient_check, report, training, utils, Variable
from chainer import datasets, iterators, optimizers, serializers
from chainer import Link, Chain, ChainList
import chainer.functions as F
import chainer.links as L
from chainer.training import extensions
class MLP(Chain):
def __init__(self, n_units, n_out):
super(MLP, self).__init__()
with self.init_scope():
# the size of the inputs to each layer will be inferred
self.l1 = L.Linear(None, n_units) # n_in -> n_units
self.l2 = L.Linear(None, n_units) # n_units -> n_units
self.l3 = L.Linear(None, n_out) # n_units -> n_out
def __call__(self, x):
h1 = F.relu(self.l1(x))
h2 = F.relu(self.l2(h1))
y = self.l3(h2)
return y
train, test = datasets.get_mnist()
train_iter = iterators.SerialIterator(train, batch_size=100, shuffle=True)
test_iter = iterators.SerialIterator(test, batch_size=100, repeat=False, shuffle=False)
model = L.Classifier(MLP(100, 10)) # the input size, 784, is inferred
optimizer = optimizers.SGD()
optimizer.setup(model)
updater = training.StandardUpdater(train_iter, optimizer)
trainer = training.Trainer(updater, (20, 'epoch'), out='result')
trainer.extend(extensions.Evaluator(test_iter, model))
trainer.extend(extensions.LogReport())
trainer.extend(extensions.PrintReport(['epoch', 'main/accuracy', 'validation/main/accuracy']))
trainer.extend(extensions.ProgressBar())
trainer.run()
分类器接受包含图像或其他数据的元组作为数组 (float32) 和标签作为 int。这是链接器及其工作方式的约定。
如果你打印你的标签,你会看到你得到一个 dtype 为 int 的数组。 image/non-image 数据和标签都将在数组中,但数据类型分别为 float 和 int。
所以回答你的问题:你的标签本身是数组格式,dtype int(对于标签来说应该是这样)。
如果您希望标签为 0 和 1 而不是 1 到 10,请使用 One Hot Encoding(https://blog.cambridgespark.com/robust-one-hot-encoding-in-python-3e29bfcec77e)。
python 模块 chainer has an introduction where it uses its neural network to recognize handwritten digits from the MNIST database。
假设特定的手写数字 D.png
被标记为 3
。我已经习惯了以数组形式出现的标签,如下所示:
label = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
但是,chainer
标签改为整数:
label = 3
数组标签对我来说更直观,因为输出预测也是一个数组。在不处理图像的神经网络中,我希望可以灵活地将标签指定为特定数组。
我直接从链接器介绍中包含了下面的代码。如果您解析 train
或 test
数据集,请注意所有标签都是整数而不是浮点数。
我如何 运行 training/test 以数组而不是整数作为标签的数据?
import numpy as np
import chainer
from chainer import cuda, Function, gradient_check, report, training, utils, Variable
from chainer import datasets, iterators, optimizers, serializers
from chainer import Link, Chain, ChainList
import chainer.functions as F
import chainer.links as L
from chainer.training import extensions
class MLP(Chain):
def __init__(self, n_units, n_out):
super(MLP, self).__init__()
with self.init_scope():
# the size of the inputs to each layer will be inferred
self.l1 = L.Linear(None, n_units) # n_in -> n_units
self.l2 = L.Linear(None, n_units) # n_units -> n_units
self.l3 = L.Linear(None, n_out) # n_units -> n_out
def __call__(self, x):
h1 = F.relu(self.l1(x))
h2 = F.relu(self.l2(h1))
y = self.l3(h2)
return y
train, test = datasets.get_mnist()
train_iter = iterators.SerialIterator(train, batch_size=100, shuffle=True)
test_iter = iterators.SerialIterator(test, batch_size=100, repeat=False, shuffle=False)
model = L.Classifier(MLP(100, 10)) # the input size, 784, is inferred
optimizer = optimizers.SGD()
optimizer.setup(model)
updater = training.StandardUpdater(train_iter, optimizer)
trainer = training.Trainer(updater, (20, 'epoch'), out='result')
trainer.extend(extensions.Evaluator(test_iter, model))
trainer.extend(extensions.LogReport())
trainer.extend(extensions.PrintReport(['epoch', 'main/accuracy', 'validation/main/accuracy']))
trainer.extend(extensions.ProgressBar())
trainer.run()
分类器接受包含图像或其他数据的元组作为数组 (float32) 和标签作为 int。这是链接器及其工作方式的约定。 如果你打印你的标签,你会看到你得到一个 dtype 为 int 的数组。 image/non-image 数据和标签都将在数组中,但数据类型分别为 float 和 int。
所以回答你的问题:你的标签本身是数组格式,dtype int(对于标签来说应该是这样)。
如果您希望标签为 0 和 1 而不是 1 到 10,请使用 One Hot Encoding(https://blog.cambridgespark.com/robust-one-hot-encoding-in-python-3e29bfcec77e)。