저는 바닐라 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 : ( 'Bad input argument to theano function with name"train.py:94 "at index 0 (0-based)",'Wrong number of dimension : expected 0, got 1 with shape (1 ,). ') "
이제 이것은 (predict_model에서) 다음 줄에 해당합니다.
givens={ x: voc[index] }
몇 시간을 보낸 후에도 다음과 같은 경우 차원 불일치가 발생할 수있는 방법을 이해할 수 없습니다.
train_set_x has shape: (42, 4, 109)
voc has shape: (109, 1, 109)
내가 train_set_x [인덱스], 내가 무엇입니까 할 때 (4 109) ' X '텐서 형 fmatrix의 저장할 수있는 (이것은에서 일어나는 것입니다 train_model 하지만) 나는 휘발성 유기 화합물 (VOC) [인덱스], 내가 무엇입니까 수행 할 때 (1, 109) , 이것은 또한 행렬이지만 ' x '는 이것을 가질 수 없습니다. 왜? !
어떤 도움이라도 대단히 감사하겠습니다.
감사 !
오류 메시지는 predict_model
대체가 givens
발생 하는 특정 줄이 아니라 라는 이름의 전체 Theano 함수의 정의를 나타냅니다 .
문제 는 스칼라 대신 길이 1predict_model
의 벡터 인 인수로 호출되는 것 같습니다 . 에서 샘플링 된 초기 값 은 실제로 스칼라이지만의 출력 은 스칼라가 아닌 벡터 라고 추측합니다 .seed
randint
p
predict_model(seed)
이 경우, 당신도 반환 할 수 rnn.y[0]
있는 predict_model
, 또는 교체 seed = p
로 seed = p[0]
를 통해 루프 j
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다