2017-07-30 52 views
1

我是一個新的theano-er ..我的代碼已成功運行,優化=無。但是,當mode = FAST_RUN時,我得到了幾次優化失敗,這讓我困擾了好幾天......儘管在這些失敗之後它仍然可以運行......但是它太慢了......我可以縮小這些失敗的範圍來掃描op ...錯誤信息在下面...任何人都可以有任何想法?TypeError:此操作的輸入的可廣播模式不正確。

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 

` ,這暗示有沒有幫助...

+0

你有一個(理想情況下很小)代碼片段來重現此錯誤? – Kh40tiK

+0

我已經發布了代碼作爲答案.. – annisamansa

+0

您可以編輯您的問題,而不是發佈答案。 – Kh40tiK

回答

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 
相關問題