This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from sklearn import datasets, cross_validation, metrics | |
import tensorflow as tf | |
from tensorflow.contrib import layers | |
from tensorflow.contrib import learn | |
random.seed(42) | |
# Load dataset and split it into train / test subsets. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> classifier = learn.DNNClassifier(hidden_units=[10, 20, 10], | |
... n_classes=2, | |
... feature_columns=learn.infer_real_valued_columns_from_input(X_train), | |
... optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.05)) | |
>>> classifier.fit(X_train, y_train, batch_size=128, steps=500) | |
>>> score = accuracy_score(classifier.predict(X_test), y_test) | |
>>> print("Accuracy: %f" % score) | |
Accuracy: 0.67597765363 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dnn_tanh(features, target): | |
target = tf.one_hot(target, 2, 1.0, 0.0) | |
logits = layers.stack(features, layers.fully_connected, [10, 20, 10], | |
activation_fn=tf.tanh) | |
prediction, loss = learn.models.logistic_regression(logits, target) | |
train_op = layers.optimize_loss(loss, | |
tf.contrib.framework.get_global_step(), optimizer='SGD', learning_rate=0.05) | |
return tf.argmax(prediction, dimension=1), loss, train_op | |
random.seed(42) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import pandas | |
import numpy as np | |
from sklearn import metrics, cross_validation | |
import tensorflow as tf | |
from tensorflow.contrib import layers | |
from tensorflow.contrib import learn | |
random.seed(42) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import pandas | |
from sklearn.cross_validation import train_test_split | |
from sklearn.linear_model import LogisticRegression | |
from sklearn.metrics import accuracy_score | |
from sklearn.preprocessing import LabelEncoder | |
from sklearn.utils import check_array | |
import tensorflow as tf | |
from tensorflow.contrib import layers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import numpy as np | |
import tensorflow as tf | |
from tensorflow.contrib import layers | |
GO_TOKEN = 0 | |
END_TOKEN = 1 | |
UNK_TOKEN = 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
import torch | |
from torch.autograd import Variable | |
import torch.nn as nn | |
from torch import optim | |
import torch.nn.functional as F | |
import utils |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TorchFold(object): | |
def __init__(self, versatible=False, cuda=False): | |
... | |
def add(self, op, *args): | |
... | |
def apply(self, nn, return_values): | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TreeLSTM(nn.Module): | |
def __init__(self, num_units): | |
super(TreeLSTM, self).__init__() | |
self.num_units = num_units | |
self.left = nn.Linear(num_units, 5 * num_units) | |
self.right = nn.Linear(num_units, 5 * num_units) | |
def forward(self, left_in, right_in): | |
lstm_in = self.left(left_in[0]) | |
lstm_in += self.right(right_in[0]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pytorch_tools import torchfold | |
def encode_tree_fold(fold, tree): | |
def encode_node(node): | |
if node.is_leaf(): | |
return fold.add('leaf', node.id).split(2) | |
else: | |
left_h, left_c = encode_node(node.left) | |
right_h, right_c = encode_node(node.right) | |
return fold.add('children', left_h, left_c, right_h, right_c).split(2) |
OlderNewer