Deep learning TypeError:输入的可广播模式对此操作不正确。

Deep learning TypeError:输入的可广播模式对此操作不正确。,deep-learning,theano,Deep Learning,Theano,我是新来的西亚诺人。。我的代码已成功运行,优化=无。但是当使用mode=FAST\u RUN时,我遇到了几次优化失败,这让我困扰了好几天。。。虽然它仍然可以在这些失败后运行…但是它太慢了…我可以缩小这些失败的范围来扫描操作…错误信息如下…有人有什么想法吗 ERROR (theano.gof.opt): SeqOptimizer apply <theano.gpuarray.opt.GraphToGPU object at 0xb788f2d0> ERROR (theano.gof.o

我是新来的西亚诺人。。我的代码已成功运行,优化=无。但是当使用mode=FAST\u 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
错误(theano.gof.opt):SeqOptimizer应用
错误(theano.gof.opt):回溯:
错误(theano.gof.opt):回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.7/site packages/theano/gof/opt.py”,第241行,在apply中
sub_prof=优化器.optimize(fgraph)
文件“/usr/lib/python2.7/site packages/theano/gof/opt.py”,第87行,在优化中
ret=self.apply(fgraph,*args,**kwargs)
文件“/usr/lib/python2.7/site packages/theano/gpuarray/opt.py”,第392行,在apply中
输出=新的_操作(*[i在node.inputs中映射[i],返回_list=True)
文件“/usr/lib/python2.7/site packages/theano/gof/op.py”,第615行,在调用中__
节点=self.make_节点(*输入,**kwargs)
文件“/usr/lib/python2.7/site packages/theano/gpuarray/elemwise.py”,第419行,在make_节点中
res=DimShuffle.make_节点(自身,输入)
文件“/usr/lib/python2.7/site packages/theano/tensor/elemwise.py”,第202行,在make_节点中
%(self.input_broadcastable,ib)))
TypeError:输入的可广播模式对于此操作不正确。预期为(真、假、假、假、真、假)、GET(假、假、假、假、假)。
错误(theano.gof.opt):由于以下原因导致优化失败:局部\gpua\U整形
错误(theano.gof.opt):节点:重塑{6}(Elemwise{add,no_inplace}.0,HostFromGpu(gpuarray).0)
错误(theano.gof.opt):回溯:
错误(theano.gof.opt):回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.7/site packages/theano/gof/opt.py”,第2036行,进程中节点
移除=移除)
文件“/usr/lib/python2.7/site packages/theano/gof/toolbox.py”,第569行,在replace\u all\u validate\u remove中
chk=fgraph.replace\u all\u validate(替换,原因)
文件“/usr/lib/python2.7/site packages/theano/gof/toolbox.py”,第518行,在replace\u all\u validate中
fgraph.replace(r,new_r,reason=reason,verbose=False)
文件“/usr/lib/python2.7/site packages/theano/gof/fg.py”,第486行,替换为
“。更换的类型必须相同。”,旧的,新的)
BadOptimization:BadOptimization错误
变量:id 4166841488 HostFromGpu(gpuarray)。0
ophostfromgpu(gpuarray)(GpuReshape{6}.0)
值类型:
旧值:无
新值:无
原因:局部整形。更换的类型必须相同。
旧图:
重塑{6}[id A]''
|Elemwise{add,no_in place}[id B]''
|| Elemwise{mul}[id C]'
|| | Elemwise{add,no_inplace}[id D]'
|| | | Elemwise{add,no|in place}[id E]'
|| | | | Elemwise{mul}[id F]'
|| | | | Elemwise{mul}[id G]'
|| | | InplaceDimShuffle{x,0,1}[IDH]'
|| | | Dot22[id I]“”
|| | Elemwise{true_div,no_inplace}[id J]'
|| | InplaceDimShuffle{0,1,x}[IDK]'
|| | | Elemwise{exp,no_in place}[id L]“”
|| | InplaceDimShuffle{x,0,x}[id M]'
|| | Sum{axis=[0],acc_dtype=float64}[id N]'
|| Elemwise{mul}[id O]'
||重塑{3}[id P]''
|| | Dot22[id Q]“”
|| | |重塑{2}[id R]'
|| | | HostFromGpu(gpuarray)[id S]“”
|| | HostFromGpu(gpuarray)[id T]“”
|| |[id U]
|| HostFromGpu(gpuarray)[id V]“”
||[id W]
|HostFromGpu(gpuarray)[id X]“”
|[id Y]
新图表:
HostFromGpu(gpuarray)[id Z]“”
|GpuReshape{6}[id-BA]''
|GpuFromHost[id BB]“”
|| Elemwise{add,no_inplace}[id B]'
|HostFromGpu(gpuarray)[id X]“”
提示:通过设置tensor.cmp\u sloppy=1放松公差
甚至是张量.cmp_sloppy=2,用于不太严格的比较
`
这个提示没有任何帮助…

原始代码工作正常。我的增量代码如下所示:

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

原始代码工作正常。我的增量代码如下所示:

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

您是否有(理想情况下较小的)代码段来重现此错误?我已将代码作为答案发布。您可以编辑问题而不是发布答案。您是否有(理想情况下较小的)代码段来重现此错误?我已将代码作为答案发布。您可以编辑问题而不是发布答案。