@@ -0,0 +1,2695 @@
+#  Copyright 2015 Google Inc. All Rights Reserved.%0A#%0A#  Licensed under the Apache License, Version 2.0 (the %22License%22);%0A#  you may not use this file except in compliance with the License.%0A#  You may obtain a copy of the License at%0A#%0A#   http://www.apache.org/licenses/LICENSE-2.0%0A#%0A#  Unless required by applicable law or agreed to in writing, software%0A#  distributed under the License is distributed on an %22AS IS%22 BASIS,%0A#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A#  See the License for the specific language governing permissions and%0A#  limitations under the License.%0A%0Aimport csv%0Aimport numpy as np%0Afrom sklearn import metrics%0A%0Aimport tensorflow as tf%0Afrom tensorflow.models.rnn import rnn, rnn_cell%0Aimport skflow%0A%0A### Training data%0A%0A# Download dbpedia_csv.tar.gz from%0A# https://drive.google.com/folderview?id=0Bz8a_Dbh9Qhbfll6bVpmNUtUcFdjYmF2SEpmZUZUcVNiMUw1TWN6RDV3a0JHT3kxLVhVR2M%0A# Unpack: tar -xvf dbpedia_csv.tar.gz%0A%0Adef load_dataset(filename):%0A    target = %5B%5D%0A    data = %5B%5D%0A    reader = csv.reader(open(filename), delimiter=',')%0A    for line in reader:%0A        target.append(int(line%5B0%5D))%0A        data.append(line%5B1%5D)%0A    return data, np.array(target, np.float32)%0A%0AX_train, y_train = load_dataset('dbpedia_csv/train.csv')%0AX_test, y_test = load_dataset('dbpedia_csv/test.csv')%0A%0A### Process vocabulary%0A%0AMAX_DOCUMENT_LENGTH = 10%0A%0Avocab_processor = skflow.preprocessing.VocabularyProcessor(MAX_DOCUMENT_LENGTH)%0AX_train = np.array(list(vocab_processor.fit_transform(X_train)))%0AX_test = np.array(list(vocab_processor.transform(X_test)))%0A%0An_words = len(vocab_processor.vocabulary_)%0Aprint('Total words: %25d' %25 n_words)%0A%0A### Models%0A%0AEMBEDDING_SIZE = 50%0A%0Adef average_model(X, y):%0A    word_vectors = skflow.ops.categorical_variable(X, n_classes=n_words,%0A        embedding_size=EMBEDDING_SIZE, name='words')%0A    features = tf.reduce_max(word_vectors, reduction_indices=1)%0A    return skflow.models.logistic_regression(features, y)%0A%0Adef rnn_model(X, y):%0A    word_vectors = skflow.ops.categorical_variable(X, n_classes=n_words,%0A        embedding_size=EMBEDDING_SIZE, name='words')%0A    word_list = %5Btf.squeeze(w, %5B1%5D) for w in tf.split(1, MAX_DOCUMENT_LENGTH, word_vectors)%5D%0A    cell = rnn_cell.GRUCell(EMBEDDING_SIZE)%0A    _, encoding = rnn.rnn(cell, word_list, dtype=tf.float32)%0A    return skflow.models.logistic_regression(encoding%5B-1%5D, y)%0A%0Aclassifier = skflow.TensorFlowEstimator(model_fn=rnn_model, n_classes=15,%0A    steps=1000, continue_training=True)%0A%0A# Continuesly train for 1000 steps & predict on test set.%0Awhile True:%0A    classifier.fit(X_train, y_train)%0A    score = metrics.accuracy_score(classifier.predict(X_test), y_test)%0A    print(%22Accuracy: %25f%22 %25 score)%0A%0A

Work on #10, #5: Example of training RNN for DBPedia text classification problem (going from text description to one of 15 classes)