Lasagne/Theano mnist 示例问题

Lasagne/Theano mnist example issue

我正在尝试将代码从 github here 稍微更改为读取更简单的二维数据的玩具示例。我的玩具数据集结构如下

x-coordinate, y-coordinate, class

一些示例数据点是

1,1
3,1
4,1
4,2
6,2
1,3

及其对应的类

0
1 
1
1
1
0

我能够读取数据并创建我的自定义 mlp。但是,当我尝试 运行 训练部分时,出现以下错误

(5, 2)
(5,)
Traceback (most recent call last):
  File "./t.py", line 78, in <module>
  train_err += train_fn(inputs, targets)
  File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 786, in __call__
allow_downcast=s.allow_downcast)
  File "/usr/local/lib/python2.7/dist-packages/theano/tensor/type.py", line 177, in filter
data.shape))
TypeError: ('Bad input argument to theano function with name "./t.py:67"  at index 0(0-based)', 'Wrong number of dimensions: expected 4, got 2 with shape (5, 2).')

这显然与我传递的数组的形状有关。但我似乎无法弄清楚的是,为什么我的情况与 mnist 数据集有任何不同,后者也是图像的二维数组。

我的全部代码如下。

def build_mlp(input_var=None):
    l_in = lasagne.layers.InputLayer(shape=(None,1,1,2),input_var = input_var)
    l_h1 = lasagne.layers.DropoutLayer(l_in,p = 0.2)
    l_hid1 = lasagne.layers.DenseLayer(
        l_h1,num_units = 10,
        nonlinearity = lasagne.nonlinearities.rectify,
        W = lasagne.init.GlorotUniform())
    l_h2 = lasagne.layers.DropoutLayer(l_hid1,p = 0.2)
    l_hid2 = lasagne.layers.DenseLayer(
        l_h2,num_units = 10,
        nonlinearity = lasagne.nonlinearities.rectify,
        W = lasagne.init.GlorotUniform())
    l_out = lasagne.layers.DenseLayer(
        l_hid2,num_units = 5,
        nonlinearity = lasagne.nonlinearities.softmax,
        W = lasagne.init.GlorotUniform())
    return l_out

def iterate_minibatches(inputs, targets, batchsize, shuffle=False):
    assert len(inputs) == len(targets)
    if shuffle:
        indices = np.arange(len(inputs))
        np.random.shuffle(indices)
    for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batchsize]
        else:
            excerpt = slice(start_idx, start_idx + batchsize)
        yield inputs[excerpt], targets[excerpt]


x_data = np.genfromtxt('a.csv',delimiter=',')
y_data = np.genfromtxt('b.csv',delimiter=',')

x_train, x_test, y_train, y_test = train_test_split(x_data,y_data,test_size = 0.33)

input_var = T.tensor4('inputs')
target_var = T.ivector('targets')

network = build_mlp(input_var)

prediction = lasagne.layers.get_output(network)
loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
loss = loss.mean()

params = lasagne.layers.get_all_params(network, trainable=True)
updates = lasagne.updates.nesterov_momentum(loss, params, learning_rate=0.01, momentum=0.4)

test_prediction = lasagne.layers.get_output(network, deterministic=True)
test_loss = lasagne.objectives.categorical_crossentropy(test_prediction,
                                                        target_var)
test_loss = test_loss.mean()

test_acc = T.mean(T.eq(T.argmax(test_prediction, axis=1), target_var),
                  dtype=theano.config.floatX)

train_fn = theano.function([input_var, target_var], loss, updates=updates)
val_fn = theano.function([input_var, target_var], [test_loss, test_acc])

num_epochs = 100
for epoch in range(num_epochs):
    train_err = 0
    start_time = time.time()
    for batch in iterate_minibatches(x_train, y_train, 5, shuffle=True):
        inputs, targets = batch
        print inputs.shape
        print targets.shape
        train_err += train_fn(inputs, targets)

    val_err = 0
    val_acc = 0
    val_batches = 0
    for batch in iterate_minibatches(x_train, y_train, 5, shuffle=False):
        inputs, targets = batch
        err, acc = val_fn(inputs, targets)
        val_err += err
        val_acc += acc
        val_batches += 1

    print 'Epoch %d of %d took {:%0.3f}s' % (epoch + 1, num_epochs, time.time() - start_time)
    print("  training loss:\t\t{:.6f}".format(train_err / train_batches))
    print("  validation loss:\t\t{:.6f}".format(val_err / val_batches))
    print("  validation accuracy:\t\t{:.2f} %".format(val_acc / val_batches * 100))

有人可以指点我在这里做什么吗?

您将 input_var 声明为 4d 张量,但错误消息表明您正在传递大小为 (5,2) 的数据矩阵作为输入。根据输入层的形状,这应该是 (5, 1, 1, 2)(假设 5 对应于小批量中训练示例的数量,而 2 对应于您的 x 和 y 坐标)。