This file contains 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
.fixed-menu { | |
position:fixed; | |
top:0; | |
padding:30px 0px 0px 0px; | |
z-index: 99999; | |
width: 100%; | |
display:block; | |
} | |
This file contains 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 numpy as np | |
# sigmoid function | |
def nonlin(x, deriv=False): | |
if(deriv==True): | |
return x*(1-x) | |
return 1/(1+np.exp(-x)) | |
# input dataset | |
X = np.array([ [0,0,1], |
This file contains 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
X = np.array([ [0,0,1],[0,1,1],[1,0,1],[1,1,1] ]) | |
y = np.array([[0,1,1,0]]).T | |
syn0 = 2*np.random.random((3,4)) - 1 | |
syn1 = 2*np.random.random((4,1)) - 1 | |
for j in xrange(60000): | |
l1 = 1/(1+np.exp(-(np.dot(X,syn0)))) | |
l2 = 1/(1+np.exp(-(np.dot(l1,syn1)))) | |
l2_delta = (y - l2)*(l2*(1-l2)) | |
l1_delta = l2_delta.dot(syn1.T) * (l1 * (1-l1)) | |
syn1 += l1.T.dot(l2_delta) |
This file contains 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 dual_encoder_model(hparams, mode, context, context_len, utterance, utterance_len, targets): | |
# Initialize embedidngs randomly or with pre-trained vectors if available | |
embeddings_W = get_embeddings(hparams) | |
# Embed the context and the utterance | |
context_embedded = tf.nn.embedding_lookup(embeddings_W, context, name="embed_context") | |
utterance_embedded = tf.nn.embedding_lookup(embeddings_W, utterance, name="embed_utterance") | |
# Build the RNN | |
with tf.variable_scope("rnn") as vs: | |
# We use an LSTM Cell | |
cell = tf.nn.rnn_cell.LSTMCell(hparams.rnn_dim, forget_bias=2.0, use_peepholes=True, state_is_tuple=True) |
This file contains 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
estimator = tf.contrib.learn.Estimator(model_fn=model_fn, model_dir=MODEL_DIR, config=tf.contrib.learn.RunConfig()) | |
input_fn_train = udc_inputs.create_input_fn(mode=tf.contrib.learn.ModeKeys.TRAIN, input_files=[TRAIN_FILE], batch_size=hparams.batch_size) | |
input_fn_eval = udc_inputs.create_input_fn(mode=tf.contrib.learn.ModeKeys.EVAL, input_files=[VALIDATION_FILE], batch_size=hparams.eval_batch_size, num_epochs=1) | |
eval_metrics = udc_metrics.create_evaluation_metrics() | |
# We need to subclass theis manually for now. The next TF version will | |
# have support ValidationMonitors with metrics built-in. | |
# It’s already on the master branch. | |
class EvaluationMonitor(tf.contrib.learn.monitors.EveryN): | |
def every_n_step_end(self, step, outputs): | |
self._estimator.evaluate(input_fn=input_fn_eval, metrics=eval_metrics, steps=None) |
This file contains 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 TFIDFPredictor: | |
def __init__(self): | |
self.vectorizer = TfidfVectorizer() | |
def train(self, data): | |
self.vectorizer.fit(np.append(data.Context.values,data.Utterance.values)) | |
def predict(self, context, utterances): | |
# Convert context and utterances into tfidf vector | |
vector_context = self.vectorizer.transform([context]) | |
vector_doc = self.vectorizer.transform(utterances) | |
# The dot product measures the similarity of the resulting vectors |
This file contains 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
# Random Predictor | |
def predict_random(context, utterances): | |
return np.random.choice(len(utterances), 10, replace=False) | |
# Evaluate Random predictor | |
y_random = [predict_random(test_df.Context[x], test_df.iloc[x,1:].values) for x in range(len(test_df))] | |
y_test = np.zeros(len(y_random)) | |
for n in [1, 2, 5, 10]: | |
print("Recall @ ({}, 10): {:g}".format(n, evaluate_recall(y_random, y_test, n))) |
This file contains 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 evaluate_recall(y, y_test, k=1): | |
num_examples = float(len(y)) | |
num_correct = 0 | |
for predictions, label in zip(y, y_test): | |
if label in predictions[:k]: | |
num_correct += 1 | |
return num_correct/num_examples |
NewerOlder