Skip to content

Instantly share code, notes, and snippets.

View ilblackdragon's full-sized avatar

Illia Polosukhin ilblackdragon

  • NEAR Protocol
  • San Francisco, CA
View GitHub Profile
@ilblackdragon
ilblackdragon / digits.py
Last active August 27, 2016 21:59
Scikit Flow - Digits example
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.
>>> 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
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)
@ilblackdragon
ilblackdragon / titanic_categorical1.py
Last active June 1, 2019 02:38
Titanic dataset - Categorical example
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)
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
@ilblackdragon
ilblackdragon / seq2seq.py
Last active May 22, 2022 21:42
Example of Seq2Seq with Attention using all the latest APIs
import logging
import numpy as np
import tensorflow as tf
from tensorflow.contrib import layers
GO_TOKEN = 0
END_TOKEN = 1
UNK_TOKEN = 2
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
@ilblackdragon
ilblackdragon / torchfold_api.py
Created September 6, 2017 20:56
TorchFold API
class TorchFold(object):
def __init__(self, versatible=False, cuda=False):
...
def add(self, op, *args):
...
def apply(self, nn, return_values):
...
@ilblackdragon
ilblackdragon / tree_lstm_regular.py
Last active September 6, 2017 23:16
TreeLSTM regular model
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])
@ilblackdragon
ilblackdragon / tree_lstm_folded.py
Last active September 6, 2017 23:17
Tree LSTM folded
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)