我正在使用 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) fmatrix 类型的“ x ”张量可以保持(这是在train_model中发生的情况)但是当我做 voc[index] 时,我得到(1, 109),这也是一个矩阵,但' x '不能容纳这个,为什么?!
任何帮助都感激不尽。
谢谢 !