如何在MXnet中使用多输入数据预测或训练网络?

时间:2017-07-12 06:54:00

标签: python deep-learning mxnet

例如

  

假设变量是:

  • inputs_a = mx.sym.Variable(' inputs_a')
  • inputs_b = mx.sym.Variable(' inputs_b')
  

假设网络是:

  1. inputs_a [batch_size],100] - > FullyConnected(10) - > outputs_a [batch,10]
  2. inputs_b [batch_size],50] - > FullyConnected(10) - > outputs_b [batch, 10]
  3. 预测= outputs_a + outputs_b
  4.   

    假设数据(numpy)是:

    • data_a,其大小为[batch_size,100]
    • data_b,其大小为[batch_size,50]
    • data_label,其大小为[batch_size,10]
      

    我想知道如何构建模型?

    mod = mx.mod.Module(context=mx.gpu(), symbol=prediction,
                        data_names=['inputs_a', 'inputs_b'], label_names=['label'])
    
      

    我想知道如何构建数据呢?

    train_iter = mx.io.NDArrayIter(data=[data_a, data_b], label=data_label,batch_size=3,data_name=['inputs_a', 'inputs_b'],label_name='label')
    

    但是这种格式是错误的。我在MXNet的教程和API文档中找不到这种多输入数据演示。还有一些,MXNet演示的博客很少。 那么你能告诉我正确的方法吗?或者告诉我你的演示。谢谢!

1 个答案:

答案 0 :(得分:1)

NDArrayIter支持多个输入。以下代码片段有望捕获您想要执行的操作:

    import mxnet as mx

    X1 = mx.sym.Variable('X1')
    X2 = mx.sym.Variable('X2')
    Y = mx.sym.Variable('Y')

    fcX1 = mx.sym.FullyConnected(data=X1, num_hidden=10, name='fcX1')
    fcX2 = mx.sym.FullyConnected(data=X2, num_hidden=10, name='fcX2')

    prediction = fcX1 + fcX2

    graph = mx.sym.LinearRegressionOutput(data=prediction, label=Y, name='lro')

    model = mx.mod.Module(symbol=prediction, data_names=['X2', 'X1'], label_names=['Y'])

    # set x1, x2 and y to the training inputs corresponding to X1, X2 and Y
    data = {'X1' : x1, 'X2' : x2}
    label = {'Y' : y}
    data_iter = mx.io.NDArrayIter(data, label, batch_size)

    model.fit(data_iter, ...)