Skip to content

Instantly share code, notes, and snippets.

View parse.py
import re
import nltk
def get_verb_phrases(t):
verb_phrases = []
num_children = len(t)
num_VP = sum(1 if t[i].label() == "VP" else 0 for i in range(0, num_children))
View memory_eval.py
import torch
from transformers import *
import sys, logging
print('cuda available? ', torch.cuda.is_available())
print('how many gpus?', torch.cuda.device_count())
logging.root.handlers = []
logging.basicConfig(level="INFO", format='%(asctime)s:%(levelname)s: %(message)s', stream=sys.stdout)
View so_answer.py
import tensorflow as tf
vocabulary_size = 10000
embedding_size = 64
rnn_size = 64
batch_size = 512
# download dataset
(train_data, train_labels), (test_data, test_labels) = tf.keras.datasets.imdb.load_data(num_words=vocabulary_size)
View estimator_9.py
NUM_GPUS = 4
strategy = tf.contrib.distribute.MirroredStrategy(num_gpus=NUM_GPUS)
config = tf.estimator.RunConfig(train_distribute=strategy)
estimator = tf.estimator.Estimator(model, config=config)
View estimator_8.py
# Instantiate a Keras inception v3 model.
keras_inception_v3 = tf.keras.applications.inception_v3.InceptionV3(weights=None)
keras_inception_v3.compile(optimizer=tf.keras.optimizers.SGD(lr=0.0001, momentum=0.9),
loss='categorical_crossentropy',
metric='accuracy')
est_inception_v3 = tf.keras.estimator.model_to_estimator(keras_model=keras_inception_v3)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
View estimator_7.py
model_estimator = tf.estimator.Estimator(model_fn=model_fn, model_dir='./tmp/')
model_estimator.train(train_input_fn, max_steps=5000)
result = model_estimator.evaluate(test_input_fn)
print(result)
View estimator_6.py
def neural_net_model(inputs, mode):
with tf.variable_scope('ConvModel'):
inputs = inputs / 255
input_layer = tf.reshape(inputs, [-1, 28, 28, 1])
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=20,
kernel_size=[5, 5],
padding='valid',
activation=tf.nn.relu)
View estimator_5.py
def model_fn(features, labels, mode):
logits = neural_net_model(features, mode)
class_prediction = tf.argmax(logits, axis=-1)
preds = class_prediction
loss = None
train_op = None
eval_metric_ops = {}
if mode in (tf.estimator.ModeKeys.EVAL, tf.estimator.ModeKeys.TRAIN):
View estimator_4.py
def model_fn(features, labels, mode, params, config)
View estimator__2.py
iris = datasets.load_iris()
X = iris.data[:, :2]
Y = iris.target
clf = MLPClassifier(solver='lbfgs', hidden_layer_sizes=(10,10))
# Create an instance of Logistic Regression Classifier and fit the data.
clf.fit(X, Y)
# Prediction phase