【问题标题】:Issue with Theano scan function - TypeError: Cannot convert Type TensorType(float64, 3D)Theano 扫描功能的问题 - TypeError: Cannot convert Type TensorType(float64, 3D)
【发布时间】:2018-08-11 18:22:36
【问题描述】:

我在使用 Theano 扫描功能和以下代码时遇到了一些问题:

def lstm_layer(tparams, options, trng, prefix='lstm'):

def _slice(_x, n, dim):
    if _x.ndim == 3:
        return _x[:, :, n * dim:(n + 1) * dim]
    return _x[:, n * dim:(n + 1) * dim]

def _step(sample_, h_, c_):
    theano.printing.debugprint(sample_,print_type=True)
    emb = tparams['Wemb'][sample_]
    x_ = tensor.dot(emb[None,:], tparams[_p(prefix, 'W')]) + tparams[_p(prefix, 'b')]
    preact = tensor.dot(h_, tparams[_p(prefix, 'U')])
    preact += x_

    i = tensor.nnet.sigmoid(_slice(preact, 0, options['dim_proj']))
    f = tensor.nnet.sigmoid(_slice(preact, 1, options['dim_proj']))
    o = tensor.nnet.sigmoid(_slice(preact, 2, options['dim_proj']))
    c = tensor.tanh(_slice(preact, 3, options['dim_proj']))

    c = f * c_ + i * c
    h = o * tensor.tanh(c)

    pred = tensor.nnet.softmax(tensor.dot(h, tparams['U']) + tparams['b'])
    rand = trng.multinomial(n=1, pvals=pred)
    sample = tensor.argmax(rand[0], axis=0)
    return sample, h, c

start = tensor.scalar('start', dtype='int64')
dim_proj = options['dim_proj']
nsteps = options['seq_length']
rval, updates = theano.scan(_step,
                            outputs_info=[start,
                                          tensor.alloc(numpy_floatX(0.),
                                                       1,
                                                       dim_proj),
                                          tensor.alloc(numpy_floatX(0.),
                                                       1,
                                                       dim_proj)],
                            name=_p(prefix, '_layers'),
                            n_steps=2)
return rval[0], start

如您所见,变量 start 是一个整数,在每次调用 step_ 后都会获得一个新值,我想在任意数量的步骤 n_steps 后获得其值的序列。如果我使用 n_steps = 1 运行代码,一切正常。但是,对于 n_steps > 1,我收到此错误:

TypeError:无法将类型 TensorType(float64, 3D)(变量 IncSubtensor{Set;:int64:}.0)转换为类型 TensorType(float64, (False, True, False))。您可以尝试手动将 IncSubtensor{Set;:int64:}.0 转换为 TensorType(float64, (False, True, False))。

我不知道它来自哪里,因为我的变量都不是 3D 张量(我已经检查了 theano.printing.debugprinting 并且 h 和 c 是预期的行并采样标量)。

你有什么线索吗?

谢谢

【问题讨论】:

  • 鉴于_slice 函数内的代码,您明确规定_x 是3D 的。您能否提供一些最小的可执行代码来说明问题,因为仅通过阅读代码可能很难帮助解决这些类型的错误。

标签: python theano


【解决方案1】:

实际上我已经找到了解决问题的方法。我改变了这个

def _slice(_x, n, dim):
if _x.ndim == 3:
    return _x[:, :, n * dim:(n + 1) * dim]
return _x[:, n * dim:(n + 1) * dim]

通过这个

    def _slice(_x, n, dim):
    if _x.ndim == 3:
        return _x[:, :, n * dim:(n + 1) * dim]
    if _x.ndim == 2:
        return _x[:, n * dim:(n + 1) * dim]
    return _x[n * dim:(n + 1) * dim]

还有这个

x_ = tensor.dot(emb[None,:], tparams[_p(prefix, 'W')]) + tparams[_p(prefix, 'b')]

通过这个

    x_ = tensor.dot(emb, tparams[_p(prefix, 'W')]) + tparams[_p(prefix, 'b')]

这使得x_, h_ and c_ theano 向量而不是之前的行并消除了错误(尽管我不确定为什么)。

当然我也更新了对scan的调用

    rval, updates = theano.scan(_step,
                            outputs_info=[start, tensor.alloc(numpy_floatX(0.),
                                                       dim_proj),
                                          tensor.alloc(numpy_floatX(0.),
                                                       dim_proj)],
                            name=_p(prefix, '_layers'),
                            n_steps=2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多