2017-09-07 10 views
-1

私はテンソルフローとこの分野の初心者ですが、私はCS231nクラスのAndrej Karpathyの講義をすべて見てきましたので、私はそのコードを理解しています。テンソルフローで予測する

だから、これはコード(ない鉱山)である:(私は私が作成しようとしています最後に書いた部分のために受け入れることがRNNのスパム分類器だし、それは素晴らしい働いてhttps://github.com/nfmcclure/tensorflow_cookbook/tree/master/09_Recurrent_Neural_Networks/02_Implementing_RNN_for_Spam_Prediction

# Implementing an RNN in TensorFlow 
# ---------------------------------- 
# 
# We implement an RNN in TensorFlow to predict spam/ham from texts 
# 
# https://github.com/nfmcclure/tensorflow_cookbook/blob/master/09_Recurrent_Neural_Networks/02_Implementing_RNN_for_Spam_Prediction/02_implementing_rnn.py 

import os 
import re 
import io 
import glob 
import requests 
import numpy as np 
import matplotlib.pyplot as plt 
import tensorflow as tf 
from zipfile import ZipFile 
from tensorflow.python.framework import ops 

ops.reset_default_graph() 

# Start a graph 
sess = tf.Session() 

# Set RNN parameters 
epochs = 20 
batch_size = 250 
max_sequence_length = 25 
rnn_size = 10 
embedding_size = 50 
min_word_frequency = 10 
learning_rate = 0.0005 
dropout_keep_prob = tf.placeholder(tf.float32) 

# Download or open data 
data_dir = 'temp' 
data_file = 'text_data.txt' 
if not os.path.exists(data_dir): 
    os.makedirs(data_dir) 

if not os.path.isfile(os.path.join(data_dir, data_file)): 
    zip_url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/00228/smsspamcollection.zip' 
    r = requests.get(zip_url) 
    z = ZipFile(io.BytesIO(r.content)) 
    file = z.read('SMSSpamCollection') 
    # Format Data 
    text_data = file.decode() 
    text_data = text_data.encode('ascii', errors='ignore') 
    text_data = text_data.decode().split('\n') 

    # Save data to text file 
    with open(os.path.join(data_dir, data_file), 'w') as file_conn: 
     for text in text_data: 
      file_conn.write("{}\n".format(text)) 
else: 
    # Open data from text file 
    text_data = [] 
    with open(os.path.join(data_dir, data_file), 'r') as file_conn: 
     for row in file_conn: 
      text_data.append(row) 
    text_data = text_data[:-1] 

text_data = [x.split('\t') for x in text_data if len(x) >= 1] 
text_data = [x for x in text_data if len(x) > 1] 
print([list(x) for x in zip(*text_data)]) 
[text_data_target, text_data_train] = [list(x) for x in zip(*text_data)] 


# Create a text cleaning function 
def clean_text(text_string): 
    text_string = re.sub(r'([^\s\w]|_|[0-9])+', '', text_string) 
    text_string = " ".join(text_string.split()) 
    text_string = text_string.lower() 
    return (text_string) 


# Clean texts 
text_data_train = [clean_text(x) for x in text_data_train] 

# Change texts into numeric vectors 
vocab_processor = tf.contrib.learn.preprocessing.VocabularyProcessor(max_sequence_length, 
                    min_frequency=min_word_frequency) 
text_processed = np.array(list(vocab_processor.fit_transform(text_data_train))) 

# Shuffle and split data 
text_processed = np.array(text_processed) 
text_data_target = np.array([1 if x == 'ham' else 0 for x in text_data_target]) 
shuffled_ix = np.random.permutation(np.arange(len(text_data_target))) 
x_shuffled = text_processed[shuffled_ix] 
y_shuffled = text_data_target[shuffled_ix] 

# Split train/test set 
ix_cutoff = int(len(y_shuffled) * 0.80) 
x_train, x_test = x_shuffled[:ix_cutoff], x_shuffled[ix_cutoff:] 
y_train, y_test = y_shuffled[:ix_cutoff], y_shuffled[ix_cutoff:] 
vocab_size = len(vocab_processor.vocabulary_) 
print("Vocabulary Size: {:d}".format(vocab_size)) 
print("80-20 Train Test split: {:d} -- {:d}".format(len(y_train), len(y_test))) 

# Create placeholders 
x_data = tf.placeholder(tf.int32, [None, max_sequence_length]) 
y_output = tf.placeholder(tf.int32, [None]) 

# Create embedding 
embedding_mat = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0)) 
embedding_output = tf.nn.embedding_lookup(embedding_mat, x_data) 
# embedding_output_expanded = tf.expand_dims(embedding_output, -1) 

# Define the RNN cell 
# tensorflow change >= 1.0, rnn is put into tensorflow.contrib directory. Prior version not test. 
if tf.__version__[0] >= '1': 
    cell = tf.contrib.rnn.BasicRNNCell(num_units=rnn_size) 
else: 
    cell = tf.nn.rnn_cell.BasicRNNCell(num_units=rnn_size) 

output, state = tf.nn.dynamic_rnn(cell, embedding_output, dtype=tf.float32) 
output = tf.nn.dropout(output, dropout_keep_prob) 

# Get output of RNN sequence 
output = tf.transpose(output, [1, 0, 2]) 
last = tf.gather(output, int(output.get_shape()[0]) - 1) 

weight = tf.Variable(tf.truncated_normal([rnn_size, 2], stddev=0.1)) 
bias = tf.Variable(tf.constant(0.1, shape=[2])) 
logits_out = tf.matmul(last, weight) + bias 

# Loss function 
losses = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits_out, 
                 labels=y_output) # logits=float32, labels=int32 
loss = tf.reduce_mean(losses) 

accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(logits_out, 1), tf.cast(y_output, tf.int64)), tf.float32)) 

optimizer = tf.train.RMSPropOptimizer(learning_rate) 
train_step = optimizer.minimize(loss) 

init = tf.global_variables_initializer() 
sess.run(init) 

train_loss = [] 
test_loss = [] 
train_accuracy = [] 
test_accuracy = [] 
# Start training 
for epoch in range(epochs): 

    # Shuffle training data 
    shuffled_ix = np.random.permutation(np.arange(len(x_train))) 
    x_train = x_train[shuffled_ix] 
    y_train = y_train[shuffled_ix] 
    num_batches = int(len(x_train)/batch_size) + 1 
    # TO DO CALCULATE GENERATIONS ExACTLY 
    for i in range(num_batches): 
     # Select train data 
     min_ix = i * batch_size 
     max_ix = np.min([len(x_train), ((i + 1) * batch_size)]) 
     x_train_batch = x_train[min_ix:max_ix] 
     y_train_batch = y_train[min_ix:max_ix] 

     # Run train step 
     train_dict = {x_data: x_train_batch, y_output: y_train_batch, dropout_keep_prob: 0.5} 
     sess.run(train_step, feed_dict=train_dict) 

    # Run loss and accuracy for training 
    temp_train_loss, temp_train_acc = sess.run([loss, accuracy], feed_dict=train_dict) 
    train_loss.append(temp_train_loss) 
    train_accuracy.append(temp_train_acc) 

    # Run Eval Step 
    test_dict = {x_data: x_test, y_output: y_test, dropout_keep_prob: 1.0} 
    temp_test_loss, temp_test_acc = sess.run([loss, accuracy], feed_dict=test_dict) 
    test_loss.append(temp_test_loss) 
    test_accuracy.append(temp_test_acc) 
    print('Epoch: {}, Test Loss: {:.2}, Test Acc: {:.2}'.format(epoch + 1, temp_test_loss, temp_test_acc)) 


# Plot loss over time 
epoch_seq = np.arange(1, epochs + 1) 
plt.plot(epoch_seq, train_loss, 'k--', label='Train Set') 
plt.plot(epoch_seq, test_loss, 'r-', label='Test Set') 
plt.title('Softmax Loss') 
plt.xlabel('Epochs') 
plt.ylabel('Softmax Loss') 
plt.legend(loc='upper left') 
plt.show() 

# Plot accuracy over time 
plt.plot(epoch_seq, train_accuracy, 'k--', label='Train Set') 
plt.plot(epoch_seq, test_accuracy, 'r-', label='Test Set') 
plt.title('Test Accuracy') 
plt.xlabel('Epochs') 
plt.ylabel('Accuracy') 
plt.legend(loc='upper left') 
plt.show() 

def findFiles(path): return glob.glob(path) 

pred_array = "words" 

pred_num = np.array(list(vocab_processor.fit_transform(pred_array))) 
print(pred_num) 

pred_output = tf.placeholder(tf.float32,[1,len(pred_array),max_sequence_length]) 

feed_dict = {pred_output: [pred_num]} 
classification = sess.run(losses, feed_dict) 
print(classification) 

予測)。私はちょうどこれに予測関数を作成する方法を理解したいんだ

、そのようなもの:

def predict(text): # text is a string (my mail) 
    # Doing prediction stuff 
    return (top result) # ham or spam 

最後の数行は、私の最後の試みは私に次のエラーを与えているされています tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=<unknown>, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]私はちょうど初心者が正しくありませんよので...

はまた、私はMaking predictions with a TensorFlow modelを使って何かをしようとした、と私はまたhttps://www.tensorflow.org/serving/serving_basicを読んで、私が試したすべてのものが失敗しましたanationsは歓迎されていますが、私はそれをコードする方法を知っているかどうか分からないので、コードの回答も投稿してください。

(Python 3.6 btw)

ありがとうございました!

+0

それがどのように失敗しました:

だからあなたのような何かを試してみてください?最小限の例を提供できますか? – Harald

+0

さて、私は何をしているのか分かりません...しかし、今はInvalidArgumentError(上記のトレースバック参照):dtype floatを使ってプレースホルダテンソル 'Placeholder'の値を入力する必要があります \t [[Node: =プレースホルダ[dtype = DT_FLOAT、shape = 、_device = "/ job:localhost/replica:0/task:0/cpu:0"]()]] @Harald – yon

+0

私はいくつかのものを修正しましたが、同じエラーは、何が起こっているのかまだ分かりません。 @ハラルド助けてください? – yon

答えて

0

あなたは、元のコードは、トレーニングとテストのステップをどうするかを見て、特に彼らはtrain_dicttest_dictの設定方法、あなたは彼らがグラフでplaceholderとして定義されテンソルのそれぞれに値を養うことがわかります。基本的に、プレースホルダーは、ネットワークに何を求めているかに関わらず、何らかの価値を与えられる必要があります。ネットワークからの予測を探しているので、予想される出力を提供する必要はないかもしれませんが、入力データx_datadropout_keep_probの値を入力する必要があります。予測にはdropout_keep_prob=1.0にする必要があります。

また、の予測も、ネットワークの損失ではありません。この損失は、基本的には、ネットワークの出力が期待どおりのものであるかを測るものですが、新しいデータのために何かを予測しようとしているので、ネットワークが何を言っているかを実際に見たいだけです。直接logits_outを使用してこれを行うことができます。あるいは、あなたのクラスを確率分布に変換するopを追加することができます。どちらの方法でも、ディストリビューションを見れば、ネットワークがデータを各カテゴリにどれほど分類できるかを知ることができます。また、このベクトルの最大値をネットワークの最良の推測値にすることもできます。

prediction = tf.nn.softmax(logits_out) 
feed_dict = {x_data: your_input_data, dropout_keep_prob: 1.0} 
pred = sess.run(prediction, feed_dict) 
best_guess = np.argmax(pred) # highest-rated class 
関連する問題