如何根据python

时间:2015-07-20 07:40:37

标签: output convolution theano deep-learning

我在Theano教程here中有1万张图像被送到CNN。

在分类步骤中,我想将这些图像分类为40个类。因此,最后一层中的单位数量为40.我想从那里得到预测值。 Layer3调用此程序包中提供的“LogisticRegression”函数。我认为CNN进入'LogisticRegression'功能来评估预测值。如何获取这些值?
与图层相关的信息是:

 layer1 = LeNetConvPoolLayer(
    rng,
    input=layer0.output,
    image_shape=(batch_size, nkerns[0], 12, 12),
    filter_shape=(nkerns[1], nkerns[0], 5, 5),
    poolsize=(2, 2)
)

# the HiddenLayer being fully-connected, it operates on 2D matrices of
# shape (batch_size, num_pixels) (i.e matrix of rasterized images).
# This will generate a matrix of shape (batch_size, nkerns[1] * 4 * 4),
# or (500, 50 * 4 * 4) = (500, 800) with the default values.
layer2_input = layer1.output.flatten(2)

# construct a fully-connected sigmoidal layer
layer2 = HiddenLayer(
    rng,
    input=layer2_input,
    n_in=nkerns[1] * 4 * 4,
    n_out=500,
    activation=T.tanh
)

# classify the values of the fully-connected sigmoidal layer
layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=40)

我认为,如果我们可以从'layer3'获得输出,那就太棒了。我想要这个值,因为我想要计算每个类中有多少样本被准确预测。 有没有帮助的机构

1 个答案:

答案 0 :(得分:1)

我对Theano不太熟悉,但您无法访问layer0的原因是因为在训练convolutional_mlp时,您不仅要保存layer3,还必须保存所有图层。 (或者,您可以选择保存每个图层中的参数并重新创建它们。)

例如,在while循环中,您可以添加以下内容:

with gzip.open('./testing/model.pkl.gz', 'w') as f:
    cPickle.dump([layer0_input, layer0, layer1, layer2_input, layer2, layer3], f)

然后您可以执行以下操作作为预测功能。

  def predict(model='./testing/model.pkl.gz', 
            testset='./testing/testset.pkl.gz',
            batch_size=5):

      """ Load a trained model and use it to predict labels.

      :type model: Layers to accept inputs and produce outputs.
      """

      # Load the saved model.
      classifiers = cPickle.load(gzip.open(model))

      # Pick out the individual layer
      layer0_input = classifiers[0]
      layer0 = classifiers[1]
      layer1 = classifiers[2]
      layer2_input = classifiers[3]
      layer2 = classifiers[4]
      layer3 = classifiers[5]

      # Apply it to our test set
      testsets = load_data(testset)
      test_set_x = testsets.get_value()

      # compile a predictor function
      index = T.lscalar()

      predict_model = theano.function(
          [layer0_input],
          layer3.y_pred,
      )

      predicted_values = predict_model(
          test_set_x[:batch_size].reshape((batch_size, 1, 28, 23))
      )

      print('Prediction complete.')
      return predicted_values

令人讨厌的是,通过本教程的设计,您必须传递与您训练过的相同大小的批次。在这里,我只做了一批,但你想要遍历test_set_x中的所有批次。