TypeError:此op的输入的可广播模式不正确。

时间:2017-07-30 03:05:56

标签: deep-learning theano

我是一个新的theano-er ..我的代码已经成功运行,优化=无。但是当使用mode = FAST_RUN时,我得到了几次优化失败,这让我困扰了好几天......虽然它仍然可以在这些失败之后运行......但它太慢了......我可以缩小这些无法扫描操作...错误信息在下面..有没有人有任何想法?

ERROR (theano.gof.opt): SeqOptimizer apply <theano.gpuarray.opt.GraphToGPU object at 0xb788f2d0>
ERROR (theano.gof.opt): Traceback:
ERROR (theano.gof.opt): Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/theano/gof/opt.py", line 241, in apply
    sub_prof = optimizer.optimize(fgraph)
  File "/usr/lib/python2.7/site-packages/theano/gof/opt.py", line 87, in optimize
    ret = self.apply(fgraph, *args, **kwargs)
  File "/usr/lib/python2.7/site-packages/theano/gpuarray/opt.py", line 392, in apply
    outputs = new_ops(*[mapping[i] for i in node.inputs], return_list=True)
  File "/usr/lib/python2.7/site-packages/theano/gof/op.py", line 615, in __call__
    node = self.make_node(*inputs, **kwargs)
  File "/usr/lib/python2.7/site-packages/theano/gpuarray/elemwise.py", line 419, in make_node
    res = DimShuffle.make_node(self, input)
  File "/usr/lib/python2.7/site-packages/theano/tensor/elemwise.py", line 202, in make_node
    % (self.input_broadcastable, ib)))
TypeError: The broadcastable pattern of the input is incorrect for this op. Expected (True, False, False, False, True, False), got (False, False, False, False, False, False).

ERROR (theano.gof.opt): Optimization failure due to: local_gpua_reshape
ERROR (theano.gof.opt): node: Reshape{6}(Elemwise{add,no_inplace}.0, HostFromGpu(gpuarray).0)
ERROR (theano.gof.opt): TRACEBACK:
ERROR (theano.gof.opt): Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/theano/gof/opt.py", line 2036, in process_node
    remove=remove)
  File "/usr/lib/python2.7/site-packages/theano/gof/toolbox.py", line 569, in replace_all_validate_remove
    chk = fgraph.replace_all_validate(replacements, reason)
  File "/usr/lib/python2.7/site-packages/theano/gof/toolbox.py", line 518, in replace_all_validate
    fgraph.replace(r, new_r, reason=reason, verbose=False)
  File "/usr/lib/python2.7/site-packages/theano/gof/fg.py", line 486, in replace
    ". The type of the replacement must be the same.", old, new)
BadOptimization: BadOptimization Error 
  Variable: id 4166841488 HostFromGpu(gpuarray).0
  Op HostFromGpu(gpuarray)(GpuReshape{6}.0)
  Value Type: <type 'NoneType'>
  Old Value:  None
  New Value:  None
  Reason:  local_gpua_reshape. The type of the replacement must be the same.
  Old Graph:
  Reshape{6} [id A] <TensorType(float32, (True, False, False, False, True, False))> ''   
   |Elemwise{add,no_inplace} [id B] <TensorType(float32, 3D)> ''   
   | |Elemwise{mul} [id C] <TensorType(float32, 3D)> ''   
   | | |Elemwise{add,no_inplace} [id D] <TensorType(float32, (True, False, False))> ''   
   | | | |Elemwise{add,no_inplace} [id E] <TensorType(float32, (True, False, False))> ''   
   | | | | |Elemwise{mul} [id F] <TensorType(float32, (True, False, False))> ''   
   | | | | |Elemwise{mul} [id G] <TensorType(float32, (True, False, False))> ''   
   | | | |InplaceDimShuffle{x,0,1} [id H] <TensorType(float32, (True, False, False))> ''   
   | | |   |Dot22 [id I] <TensorType(float32, matrix)> '' 
   | | |Elemwise{true_div,no_inplace} [id J] <TensorType(float32, (False, False, True))> ''   
   | |   |InplaceDimShuffle{0,1,x} [id K] <TensorType(float32, (False, False, True))> ''   
   | |   | |Elemwise{exp,no_inplace} [id L] <TensorType(float32, matrix)> ''   
   | |   |InplaceDimShuffle{x,0,x} [id M] <TensorType(float32, (True, False, True))> ''   
   | |     |Sum{axis=[0], acc_dtype=float64} [id N] <TensorType(float32, vector)> ''   
   | |Elemwise{mul} [id O] <TensorType(float32, 3D)> ''   
   |   |Reshape{3} [id P] <TensorType(float32, 3D)> ''  
   |   | |Dot22 [id Q] <TensorType(float32, matrix)> ''   
   |   | | |Reshape{2} [id R] <TensorType(float32, matrix)> ''   
   |   | | |HostFromGpu(gpuarray) [id S] <TensorType(float32, matrix)> ''   
   |   | |HostFromGpu(gpuarray) [id T] <TensorType(int64, vector)> ''   
   |   |   |<GpuArrayType<None>(int64, vector)> [id U] <GpuArrayType<None>(int64, vector)>
   |   |HostFromGpu(gpuarray) [id V] <TensorType(float32, (True, True, True))> ''   
   |     |<GpuArrayType<None>(float32, (True, True, True))> [id W] <GpuArrayType<None>(float32, (True, True, True))>
   |HostFromGpu(gpuarray) [id X] <TensorType(int64, vector)> ''   
     |<GpuArrayType<None>(int64, vector)> [id Y] <GpuArrayType<None>(int64, vector)>

  New Graph:
  HostFromGpu(gpuarray) [id Z] <TensorType(float32, 6D)> ''   
   |GpuReshape{6} [id BA] <GpuArrayType<None>(float32, 6D)> ''   
     |GpuFromHost<None> [id BB] <GpuArrayType<None>(float32, 3D)> ''
     | |Elemwise{add,no_inplace} [id B] <TensorType(float32, 3D)> ''   
     |HostFromGpu(gpuarray) [id X] <TensorType(int64, vector)> ''   


Hint: relax the tolerance by setting tensor.cmp_sloppy=1
  or even tensor.cmp_sloppy=2 for less-strict comparison

` 这个提示没有帮助...

1 个答案:

答案 0 :(得分:0)

原始代码正常。我的增量代码是这样的:

def sample_subspace(ctx, next_state_0, next_state_1, tparams, options, maxlen, k):
    # init sample with -1
    sample = -1 * tensor.ones((k, maxlen)).astype('int64')
    sample_scores = tensor.zeros((k)).astype('float32')
    alignment = tensor.zeros((k, maxlen, maxlen)).astype('float32')
    # sample the first k words
    next_w = -1 * tensor.ones((1, 1)).astype('int64')

    next_state_1 = tensor.alloc(0., 1, next_state_0.shape[-1])
    proj, proj_1, next_probs = decode_one_word(next_w, ctx, next_state_0, next_state_1, tparams, options)
    new_next_state_0 = tensor.tile(proj[0], (k, 1))
    new_next_state_1 = tensor.tile(proj_1[0], (k, 1))
    hyp_scores = tensor.zeros(1).astype('float32')
    cand_scores = hyp_scores[:, None] - tensor.log(next_probs)
    cand_flat = cand_scores.flatten()
    ranks_flat = cand_flat.argsort()[:k]
    scores_flat = cand_flat.sort()[:k]
    dec_alphas = proj[2]

    sample = tensor.set_subtensor(sample[:,0], ranks_flat)
    sample_scores = tensor.inc_subtensor(sample_scores[:], scores_flat)
    padx0 = tensor.alloc(0., maxlen)
    padx0 = tensor.set_subtensor(padx0[:dec_alphas[0].shape[0]], dec_alphas[0][:])
    alignment = tensor.set_subtensor(alignment[:,0], tensor.tile(padx0[None,:], (k, 1)))

    new_next_w = ranks_flat[None, :]

    def _one_step(next_w, next_state_0, next_state_1, score=None, alignment=None):
        ctx_r = tensor.tile(ctx, (1, k, 1))
        proj, proj_1, next_probs = decode_one_word(next_w, ctx_r, next_state_0, next_state_1, tparams, options)
        new_next_state_0 = proj[0]
        new_next_state_1 = proj_1[0]
        hyp_scores = tensor.zeros(1).astype('float32')
        cand_scores = hyp_scores[:, None] - tensor.log(next_probs)
        new_next_w = tensor.argmin(cand_scores, axis=1, keepdims=True).dimshuffle(1,0)
        dec_alphas = proj[2]
        score = tensor.min(cand_scores, axis=0, keepdims=True)
        alignment = dec_alphas
        return new_next_w, new_next_state_0, new_next_state_1, score, alignment

    results, updates = theano.scan(fn=_one_step,
                                   outputs_info=[new_next_w, new_next_state_0, new_next_state_1, None, None],
                                   n_steps=maxlen - 1)

    sample = tensor.set_subtensor(sample[:, 1:], results[0].dimshuffle(2,0,1)[:,:,0])
    #sample = theano.printing.Print('samples')(sample)
    sample_scores = tensor.inc_subtensor(sample_scores[:], results[3].sum(0)[:,0])
    padx = tensor.alloc(0., k, maxlen-1, maxlen)
    padx = tensor.set_subtensor(padx[:, :, :results[4].shape[2]], results[4].dimshuffle(1,0,2))
    alignment = tensor.set_subtensor(alignment[:,1:], padx)

    # decode k sequences
    return sample, sample_scores, alignment