이 문제를 해결하여 Tensorflow로 손을 젖게하려고합니다 : https://www.kaggle.com/c/integer-sequence-learning.Tensorflow 손실이 내 RNN에서 분기 됨
이 내 작품은이 블로그 게시물을 기반으로합니다
- https://danijar.com/variable-sequence-lengths-in-tensorflow/
- https://gist.github.com/evanthebouncy/8e16148687e807a46e3f
완전한 작동 예를 - 내 데이터 - 여기에서 찾을 수 있습니다 : https://github.com/bottiger/Integer-Sequence-Learning은 예제를 실행하면 인쇄됩니다 많은 디버그 정보를 출력합니다. execute rnn-lstm-my.py를 실행하십시오. (tensorflow와 pandas 필요)
접근법은 꽤 간단합니다. 모든 열차 시퀀스를로드하고 길이를 벡터에 저장하고 변수 중 가장 긴 길이를 "max_length"라고 부릅니다. 내 훈련 데이터에서
나는 모양과 매트릭스의 모든 시퀀스, 0으로 채워을 저장
은 "train_solutions을"모든 시퀀스의 마지막 요소를 제거하고라는 벡터에 저장 : [n_seq, max_length].
시퀀스의 다음 숫자를 예측하려면 출력이 하나의 숫자 여야하고 입력은 시퀀스 여야합니다.
기본 숨겨진 단위가있는 BasicLSTMCell과 함께 RNN (tf.nn.rnn)을 셀로 사용합니다. 출력은 내 예측을 생성해야하는 기본 선형 모델 (xW + B)로 공급됩니다.
내 비용 함수는 단순히 내 모델의 예측 번호, 나는 비용과 같이 계산 :
cost = tf.nn.l2_loss(tf_result - prediction)
기본 사항의 치수는 코드가 실제로 실행되기 때문에 올바른 것 같다. 그러나 하나 또는 두 번의 반복 작업 만 수행하면 일부 NaN이 발생하기 시작하여 빠르게 확산되며 모든 것이 NaN이됩니다.
다음은 그래프를 정의하고 실행하는 코드의 중요한 부분입니다. 그러나 게시 된로드/준비 데이터는 생략했습니다. 그것에 대한 자세한 내용은 git repo를 참조하십시오.하지만 그 부분이 정확할 것이라고 확신합니다.
cell = tf.nn.rnn_cell.BasicLSTMCell(num_hidden, state_is_tuple=True)
num_inputs = tf.placeholder(tf.int32, name='NumInputs')
seq_length = tf.placeholder(tf.int32, shape=[batch_size], name='NumInputs')
# Define the input as a list (num elements = batch_size) of sequences
inputs = [tf.placeholder(tf.float32,shape=[1, max_length], name='InputData') for _ in range(batch_size)]
# Result should be 1xbatch_szie vector
result = tf.placeholder(tf.float32, shape=[batch_size, 1], name='OutputData')
tf_seq_length = tf.Print(seq_length, [seq_length, seq_length.get_shape()], 'SequenceLength: ')
outputs, states = tf.nn.rnn(cell, inputs, dtype=tf.float32)
# Print the output. The NaN first shows up here
outputs2 = tf.Print(outputs, [outputs], 'Last: ', name="Last", summarize=800)
# Define the model
tf_weight = tf.Variable(tf.truncated_normal([batch_size, num_hidden, frame_size]), name='Weight')
tf_bias = tf.Variable(tf.constant(0.1, shape=[batch_size]), name='Bias')
# Debug the model parameters
weight = tf.Print(tf_weight, [tf_weight, tf_weight.get_shape()], "Weight: ")
bias = tf.Print(tf_bias, [tf_bias, tf_bias.get_shape()], "bias: ")
# More debug info
print('bias: ', bias.get_shape())
print('weight: ', weight.get_shape())
print('targets ', result.get_shape())
print('RNN input ', type(inputs))
print('RNN input len()', len(inputs))
print('RNN input[0] ', inputs[0].get_shape())
# Calculate the prediction
tf_prediction = tf.batch_matmul(outputs2, weight) + bias
prediction = tf.Print(tf_prediction, [tf_prediction, tf_prediction.get_shape()], 'prediction: ')
tf_result = result
# Calculate the cost
cost = tf.nn.l2_loss(tf_result - prediction)
#optimizer = tf.train.AdamOptimizer()
learning_rate = 0.05
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
minimize = optimizer.minimize(cost)
mistakes = tf.not_equal(tf.argmax(result, 1), tf.argmax(prediction, 1))
error = tf.reduce_mean(tf.cast(mistakes, tf.float32))
init_op = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init_op)
no_of_batches = int(len(train_input))/batch_size
epoch = 1
val_dict = get_input_dict(val_input, val_output, train_length, inputs, batch_size)
for i in range(epoch):
ptr = 0
for j in range(no_of_batches):
print('eval w: ', weight.eval(session=sess))
# inputs batch
t_i = train_input[ptr:ptr+batch_size]
# output batch
t_o = train_output[ptr:ptr+batch_size]
# sequence lengths
t_l = train_length[ptr:ptr+batch_size]
sess.run(minimize,feed_dict=get_input_dict(t_i, t_o, t_l, inputs, batch_size))
ptr += batch_size
print("result: ", tf_result)
print("result len: ", tf_result.get_shape())
print("prediction: ", prediction)
print("prediction len: ", prediction.get_shape())
c_val = sess.run(error, feed_dict = val_dict)
print "Validation cost: {}, on Epoch {}".format(c_val,i)
print "Epoch ",str(i)
print('test input: ', type(test_input))
print('test output: ', type(test_output))
incorrect = sess.run(error,get_input_dict(test_input, test_output, test_length, inputs, batch_size))
sess.close()
그리고 여기에서 생성되는 출력 (의 첫 번째 줄)입니다. 나는 NaN를 참조 처음 여기 http://pastebin.com/TnFFNFrr은 (I 인해 신체 제한에 여기를 게시 할 수 없습니다)
: 나는/코어/커널/logging_ops을 tensorflow
당신은 NaN이 될 그 모든 것을 볼 수 있습니다 .cc : 79] 마지막 : [0 0.76159418 0 0 0 0 -0.76159418 0 -0.76159418 0 0 0.76159418 0.76159418 0 -0.76159418 0.76159418 0 0 0 0.76159418 0 0 0 nan nan nan nan 0 0 nan nan 1 0 nan 0 0.76159418 nan nan nan. 에서 76,159,418 -in -in -in -in -in -in -in -in -in -in -in -in -in -in -in -in -in -in -in -in -in -in -in -in에에에에에에에에에에에에에에에에에에에에 에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 - , 스플릿 -in -in -in -in -in -in -in -in -in -in -in의 중 중 중 중 중 중 중 중의 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서의 에있는에있는에있는에있는에있는에있는에있는에있는에있는에있는에서 -in -in -in -in -in -in -in -in -in -in -in -in에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 -에서 - -in의의의의의의의의의의의의의
]의의의의 나는 내 문제는 분명히 희망의. 미리 감사는