2016-07-30 22 views
2

Ich versuche ein einfaches Programm mit TensorFlow zu schreiben, um die nächste Zahl in einer Sequenz vorherzusagen.Nächste Zahl in einem Muster vorhersagen

Ich bin nicht in TensorFlow erlebt so statt von Grund begann ich mit diesem Handbuch: http://monik.in/a-noobs-guide-to-implementing-rnn-lstm-using-tensorflow/

jedoch im Gegensatz zur Implementierung in den oberen Link Ich möchte das Problem als Klassifikationsproblem nicht behandeln - wo ich nur n mögliche Ergebnisse habe - aber nur einen einzelnen Wert für eine Sequenz berechnen.

Ich habe versucht, den Code zu modifizieren, mein Problem zu passen:

import numpy as np 
import random 
from random import shuffle 
import tensorflow as tf 

NUM_EXAMPLES = 10000 

train_input = ['{0:020b}'.format(i) for i in range(2**20)] 
shuffle(train_input) 
train_input = [map(int,i) for i in train_input] 
ti = [] 
for i in train_input: 
    temp_list = [] 
    for j in i: 
      temp_list.append([j]) 
    ti.append(np.array(temp_list)) 
train_input = ti 

train_output = [] 
for i in train_input: 
    count = 0 
    for j in i: 
     if j[0] == 1: 
      count+=1 
    #temp_list = ([0]*21) 
    #temp_list[count]=1 
    #train_output.append(temp_list) 
    train_output.append(count) 

test_input = train_input[NUM_EXAMPLES:] 
test_output = train_output[NUM_EXAMPLES:] 
train_input = train_input[:NUM_EXAMPLES] 
train_output = train_output[:NUM_EXAMPLES] 

print "test and training data loaded" 


target = tf.placeholder(tf.float32, [None, 1]) 
data = tf.placeholder(tf.float32, [None, 20,1]) #Number of examples, number of input, dimension of each input 
#target = tf.placeholder(tf.float32, [None, 1]) 

#print('target shape: ', target.get_shape()) 
#print('shape[0]', target.get_shape()[1]) 
#print('int(shape) ', int(target.get_shape()[1])) 

num_hidden = 24 
cell = tf.nn.rnn_cell.LSTMCell(num_hidden) 
val, _ = tf.nn.dynamic_rnn(cell, data, dtype=tf.float32) 
val = tf.transpose(val, [1, 0, 2]) 

print('val shape, ', val.get_shape()) 

last = tf.gather(val, int(val.get_shape()[0]) - 1) 

weight = tf.Variable(tf.truncated_normal([num_hidden, int(target.get_shape()[1])])) 
bias = tf.Variable(tf.constant(0.1, shape=[target.get_shape()[1]])) 

#prediction = tf.nn.softmax(tf.matmul(last, weight) + bias) 
prediction = tf.matmul(last, weight) + bias 

cross_entropy = -tf.reduce_sum(target - prediction) 
optimizer = tf.train.AdamOptimizer() 
minimize = optimizer.minimize(cross_entropy) 

mistakes = tf.not_equal(tf.argmax(target, 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) 

batch_size = 100 
no_of_batches = int(len(train_input))/batch_size 
epoch = 500 

for i in range(epoch): 
    ptr = 0 
    for j in range(no_of_batches): 
     inp, out = train_input[ptr:ptr+batch_size], train_output[ptr:ptr+batch_size] 
     ptr+=batch_size 
     sess.run(minimize,{data: inp, target: out}) 
    print "Epoch ",str(i) 

incorrect = sess.run(error,{data: test_input, target: test_output}) 

#print sess.run(prediction,{data: [[[1],[0],[0],[1],[1],[0],[1],[1],[1],[0],[1],[0],[0],[1],[1],[0],[1],[1],[1],[0]]]}) 
#print('Epoch {:2d} error {:3.1f}%'.format(i + 1, 100 * incorrect)) 

sess.close() 

Es ist immer noch in Entwicklung befindet, da die Eingabe als auch die Kreuzentropie Berechnung Schein ist.

Mein Hauptproblem ist jedoch, dass der Code überhaupt nicht kompiliert.

ich diesen Fehler:

ValueError: Cannot feed value of shape (100,) for Tensor u'Placeholder:0', which has shape '(?, 1)'

Die Zahl 100 kommt aus dem „batch_size“ und kommt von der Tatsache, dass meine Vorhersage eine eindimensionale Zahl (1?). Ich habe jedoch keine Ahnung, wo das Problem in meinem Code ist?

Kann jemand mir helfen, die Maße zusammenzubringen, um zusammenzupassen?

+0

@Silverfish Sie könnte Recht haben. Wissen Sie, wo Sie eine Frage wie diesen - Stapelüberlauf? – Markus

+0

Sie können die Frage hier für die Migration zu SO "markieren", aber es wäre eine gute Idee, sicherzustellen, dass Ihr Beispiel zuerst * reproduzierbar * und * minimal * ist. Erwarten Sie nicht, dass Benutzer unnötigen Code debuggen (dh, der nicht mit dem zugrunde liegenden Problem zusammenhängt), aber schneiden Sie nicht so viel Code aus, dass das, was übrig ist, nicht in sich abgeschlossen ist und nicht ausgeführt werden kann. – Silverfish

Antwort

0

Dieser Fehler bedeutet, dass Ihr targets Platzhalter etwas mit der falschen Form zugeführt wird. Um es zu beheben, ich glaube, Sie so etwas wie test_output.reshape([-1, 1])

0

Um die Platzhalter Form fixieren neu gestalten sollte, ändern Sie Ihren Code

for i in range(epoch): 
    ptr = 0 
    for j in range(no_of_batches): 
     inp = train_input[ptr:ptr+batch_size] 
     out = train_output[ptr:ptr+batch_size] 
     ptr+=batch_size 
     out = np.reshape(out, (100,1)) #reshape 
     sess.run(minimize,{data: inp, target: out}) 
    print ("Epoch ",str(i)) 
test_output = np.reshape(test_output, (1038576,1)) #reshape 
incorrect = sess.run(error,{data: test_input, target: test_output})