使用烤宽面条获得输出分类

Getting output classification with lasagne

使用 Lasagne/Theano

获取输出分类

我正在将我的代码从纯 Theano 迁移到 Lasagne。 我从教程中获得了这段特定代码,以获取特定数据的预测结果,我将生成一个 csv 文件以发送给 kaggle。 但是对于烤宽面条,它不起作用。 我尝试了几种方法,但它们都出错了。

如果有人能帮我找出问题所在,我会很高兴!

我把整个代码粘贴在这里: http://pastebin.com/e7ry3280

test_data  = np.loadtxt("../inputData/test.csv", dtype=np.uint8, delimiter=',', skiprows=1)

# The inputs are vectors now, we reshape them to monochrome 2D images,
# following the shape convention: (examples, channels, rows, columns)
data = data.reshape(-1, 1, 28, 28)
test_data = test_data.reshape(-1, 1, 28, 28)

index = T.lscalar()  # index to a [mini]batch
preds = []
for it in range(len(test_data)):
        test_data = test_data[it]
        N = len(test_data)
        # print "N : ", N
        test_data = theano.shared(np.asarray(test_data, dtype=theano.config.floatX))

        test_labels = T.cast(theano.shared(np.asarray(np.zeros(batch_size), dtype=theano.config.floatX)),'uint8')

        ###target_var
        #y = T.ivector('y')  # the labels are presented as 1D vector of [int] labels
        #index = T.lscalar()  # index to a [mini]batch

        ppm = theano.function([index],lasagne.layers.get_output(network, deterministic=True),
                              givens={
                                  input_var: test_data[index * batch_size: (index + 1) * batch_size],
                                  target_var: test_labels
                              }, on_unused_input='warn')

        p = [ppm(ii) for ii in range(N // batch_size)]

        p = np.array(p).reshape((N, 10))
        print (p)
        p = np.argmax(p, axis=1)
        p = p.astype(int)
        preds.append(p)

subm = np.empty((len(preds), 2))
subm[:, 0] = np.arange(1, len(preds) + 1)
subm[:, 1] = preds

np.savetxt('submission.csv', subm, fmt='%d', delimiter=',',header='ImageId,Label', comments='')

return preds

代码在以 ppm = theano.function...:

开头的行上失败

TypeError: Cannot convert Type TensorType(float32, 3D) (of Variable Subtensor{int64:int64:}.0) into Type TensorType(float32, 4D). You can try to manually convert Subtensor{int64:int64:}.0 into a TensorType(float32, 4D).

我只是想将测试数据输入到 CNN 并将结果保存到 CSV 文件中。我该怎么做?我知道我必须使用小批量,因为整个测试数据不适合 GPU。

正如错误消息和 Daniel Renshaw 在评论中指出的那样,问题是 test_datainput_var 之间的维度不匹配。在循环的第一行,你写:

test_data = test_data[it]

这会将 4D 数组 test_data 转换为具有相同名称的 3D 数组(这就是为什么不建议对不同类型使用相同的变量名:))。之后,将其封装在一个不改变维度的共享变量中,然后将其切片以将其分配给 input_var,这同样不会改变维度。

如果我理解你的代码,我认为你应该删除第一行。这样 test_data 仍然是示例列表,您可以将其切片以进行批处理。