维数错误:预期为0,形状为1(1,)

时间:2016-02-19 13:48:36

标签: theano recurrent-neural-network language-model

我正在使用vanilla rnn进行单词级语言建模,我能够训练模型,但由于一些奇怪的原因,我无法从模型中获得任何样本/预测;这是代码的相关部分:

train_set_x, train_set_y, voc = load_data(dataset, vocab, vocab_enc)  # just load all data as shared variables
index = T.lscalar('index')
x = T.fmatrix('x')
y = T.ivector('y')
n_x = len(vocab)
n_h = 100
n_y = len(vocab)

rnn = Rnn(input=x, input_dim=n_x, hidden_dim=n_h, output_dim=n_y)

cost = rnn.negative_log_likelihood(y)

updates = get_optimizer(optimizer, cost, rnn.params, learning_rate)

train_model = theano.function(
    inputs=[index],
    outputs=cost,
    givens={
        x: train_set_x[index],
        y: train_set_y[index]
    },
    updates=updates
)

predict_model = theano.function(
    inputs=[index],
    outputs=rnn.y,
    givens={
        x: voc[index]
    }
)

sampling_freq = 2
sample_length = 10
n_train_examples = train_set_x.get_value(borrow=True).shape[0]
train_cost = 0.
for i in xrange(n_train_examples):
    train_cost += train_model(i)
    train_cost /= n_train_examples

    if i % sampling_freq == 0:
       # sample from the model     
       seed = randint(0, len(vocab)-1)
       idxes = []
       for j in xrange(sample_length):
           p = predict_model(seed)
           seed = p
           idxes.append(p)
           # sample = ''.join(ix_to_words[ix] for ix in idxes)
           # print(sample)

我收到错误:“TypeError :('索引0(基于0)的名称为”train.py:94“的theano函数的错误输入参数','维数错误:预期为0,得到1有形状(1,)。')“

现在这对应于以下行(在predict_model中):

 givens={   x: voc[index]   }

即使花了好几个小时,我也无法理解在以下情况下是否存在尺寸不匹配:

train_set_x has shape: (42, 4, 109)
voc has shape: (109, 1, 109)

当我做train_set_x [index]时,我得到(4,109) x ”fmatrix类型的张量可以容纳(这是发生在 train_model )但是当我做voc [index]时,我得到(1,109),这也是一个矩阵,但是“ x ”不能抱这个,为什么? !

非常感谢任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:2)

错误消息指的是名为predict_model的整个Theano函数的定义,而不是givens替换发生的特定行。

问题似乎是predict_model使用参数调用,该参数是长度为1 向量而不是标量。从seed采样的初始randint实际上是一个标量,但我猜测p的输出predict_model(seed)是一个向量而不是标量。

在这种情况下,您可以在rnn.y[0]中返回predict_model,或在seed = p的循环中将seed = p[0]替换为j