2017-08-07 25 views
1
wordsList = np.load('training_data/wordsList.npy') 
wordsList = wordsList.tolist() #Originally loaded as numpy array 
wordsList = [word.decode('UTF-8') for word in wordsList] #Encode words as UTF-8 
wordVectors = np.load('training_data/wordVectors.npy') 

加載一些positiveFiles和negativeFiles可變如何使用tensorflow中的python3預測LSTM模型中的情緒?

with tf.device('/gpu:0'): 
    ids = np.zeros((numFiles, maxSeqLength), dtype='int32') 
    fileCounter = 0 
    for pf in positiveFiles: 
     with open(pf, "r") as f: 
      indexCounter = 0 
      line=f.readline() 
      cleanedLine = cleanSentences(line) 
      split = cleanedLine.split() 
      for word in split: 
       try: 
        ids[fileCounter][indexCounter] = wordsList.index(word) 
       except ValueError: 
        ids[fileCounter][indexCounter] = 399999 #Vector for unkown words 
       #print('value :' + str(ids)) 
       indexCounter = indexCounter + 1 
       if indexCounter >= maxSeqLength: 
        break 
      fileCounter = fileCounter + 1 

    for nf in negativeFiles: 
     with open(nf, "r") as f: 
      indexCounter = 0 
      line=f.readline() 
      cleanedLine = cleanSentences(line) 
      split = cleanedLine.split() 
      for word in split: 
       try: 
        ids[fileCounter][indexCounter] = wordsList.index(word) 
       except ValueError: 
        ids[fileCounter][indexCounter] = 399999 #Vector for unkown words 
       # print('value :' + str(ids)) 
       indexCounter = indexCounter + 1 
       if indexCounter >= maxSeqLength: 
        break 
      fileCounter = fileCounter + 1 
    #Pass into embedding function and see if it evaluates. 

np.save('idsMatrix', ids) 

batchSize = 24 

培訓和測試方法

def getTrainBatch(): 
    labels = [] 
    arr = np.zeros([batchSize, maxSeqLength]) 
    for i in range(batchSize): 
     if (i % 2 == 0): 
      num = randint(1,11499) 
      labels.append([1,0]) 
     else: 
      num = randint(13499,24999) 
      labels.append([0,1]) 
     arr[i] = ids[num-1:num] 
    return arr, labels 

def getTestBatch(): 
    labels = [] 
    arr = np.zeros([batchSize, maxSeqLength]) 
    for i in range(batchSize): 
     num = randint(11499,13499) 
     if (num <= 12499): 
      labels.append([1,0]) 
     else: 
      labels.append([0,1]) 
     arr[i] = ids[num-1:num] 
    return arr, labels 

with tf.device('/gpu:0'): 
    batchSize = 24 
    lstmUnits = 64 
    numClasses = 2 
    iterations = 100000 

    tf.reset_default_graph() 

    labels = tf.placeholder(tf.float32, [batchSize, numClasses]) 
    input_data = tf.placeholder(tf.int32, [batchSize, maxSeqLength]) 

    data = tf.Variable(tf.zeros([batchSize, maxSeqLength, numDimensions]), dtype=tf.float32) 
    data = tf.nn.embedding_lookup(wordVectors, input_data) 

    lstmCell = tf.contrib.rnn.BasicLSTMCell(lstmUnits) 
    lstmCell = tf.contrib.rnn.DropoutWrapper(cell=lstmCell, output_keep_prob=0.75) 
    value, _ = tf.nn.dynamic_rnn(lstmCell, data, dtype=tf.float32) 

with tf.device('/gpu:0'): 
    weight = tf.Variable(tf.truncated_normal([lstmUnits, numClasses])) 
    bias = tf.Variable(tf.constant(0.1, shape=[numClasses])) 
    value = tf.transpose(value, [1, 0, 2]) 
    last = tf.gather(value, int(value.get_shape()[0]) - 1) 
    prediction = (tf.matmul(last, weight) + bias) 

correctPred = tf.equal(tf.argmax(prediction,1), tf.argmax(labels,1)) 
accuracy = tf.reduce_mean(tf.cast(correctPred, tf.float32)) 

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=labels)) 
optimizer = tf.train.AdamOptimizer().minimize(loss) 

sess = tf.InteractiveSession() 
saver = tf.train.Saver() 
sess.run(tf.global_variables_initializer()) 

with tf.device('/gpu:0'): 
    for i in range(iterations): 
     nextBatch, nextBatchLabels = getTrainBatch(); 
     sess.run(optimizer, {input_data: nextBatch, labels: nextBatchLabels}) 

iterations = 10 
for i in range(iterations): 
    nextBatch, nextBatchLabels = getTestBatch(); 
    sess.run(accuracy, {input_data: nextBatch, labels: nextBatchLabels}) 

在這裏,我想預測的1或0的形式輸出給定的句子。 從檢查點通過這個加載此文件後,我想測試句子是Positive(1)還是Negative(0)。

new_saver = tf.train.import_meta_graph('models/pretrained....') 
new_saver.restore(sess, tf.train.latest_checkpoint('models/./')) 

請幫忙。

回答

0

對輸入和輸出使用命名,然後從圖中檢索張量進行預測;我建議一些必要的變化和附加代碼來獲取預測會

... 
input_data = tf.placeholder(tf.int32, [batchSize, maxSeqLength], name='inputs') 
... 
prediction = (tf.matmul(last, weight) + bias) 
# you may use softmax if you want probabilities for prediction, but not for calculating the loss 
# prediction = tf.nn.softmax(prediction) 
prediction = tf.identity(prediction, name='prediction') 
... 
with tf.device('/gpu:0'): 
    for i in range(iterations): 
     nextBatch, nextBatchLabels = getTrainBatch(); 
     sess.run(optimizer, {input_data: nextBatch, labels: nextBatchLabels} 
    saver.save(sess, 'model') 

代碼恢復:在這裏使用的相對/絕對路徑model.meta和模型

new_saver = tf.train.import_meta_graph('/path/to/model.meta') 
new_saver.restore(sess, '/path/to/model') 
with tf.Session() as sess: 
    g = tf.get_default_graph() 
    inputs = g.get_tensor_by_name('inputs:0') 
    prediction = g.get_tensor_by_name('prediction:0') 
    prediction_ = sess.run(prediction, {inputs: your_inputs}) 
相關問題