Skip to content

Instantly share code, notes, and snippets.

View WillKoehrsen's full-sized avatar
🌆
building

Will Koehrsen WillKoehrsen

🌆
building
View GitHub Profile
history = model.fit(X_train, y_train,
batch_size=2048, epochs=150,
callbacks=callbacks,
validation_data=(X_valid, y_valid))
from keras.callbacks import EarlyStopping, ModelCheckpoint
# Create callbacks
callbacks = [EarlyStopping(monitor='val_loss', patience=5),
ModelCheckpoint('../models/model.h5'), save_best_only=True,
save_weights_only=False)]
# Load in embeddings
glove_vectors = '/home/ubuntu/.keras/datasets/glove.6B.100d.txt'
glove = np.loadtxt(glove_vectors, dtype='str', comments=None)
# Extract the vectors and words
vectors = glove[:, 1:].astype('float')
words = glove[:, 0]
# Create lookup of words to vectors
word_lookup = {word: vector for word, vector in zip(words, vectors)}
from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout, Masking, Embedding
model = Sequential()
# Embedding layer
model.add(
Embedding(input_dim=num_words,
input_length = training_length,
output_dim=100,
features = []
labels = []
training_length = 50
# Iterate through the sequences of tokens
for seq in sequences:
# Create multiple training examples from each sequence
for i in range(training_length, len(seq)):
from tpot import TPOTClassifier
from sklearn.model_selection import TimeSeriesSplit
# Cross validation object
tss = TimeSeriesSplit(n_splits = 3)
# Make the tpot search model
tpot_pipeline = TPOTClassifier(generations = 10, population_size = 10,
cv = tss, scoring = 'f1')
# Find best model
from sklearn.ensemble import RandomForestClassifier
# Create the model with 100 trees and default hyperparameters
model = RandomForestClassifier(n_estimators=100)
# Fit on training data
model.fit(train, train_labels)
# Make predictions on holdout test data
predictions = model.predict(test)
import featuretools as ft
# Primitives for deep feature synthesis
trans_primitives = ['weekend', 'cum_sum', 'day', 'month', 'diff', 'time_since_previous']
agg_primitives = ['sum', 'time_since_last', 'avg_time_between', 'all', 'mode',
'num_unique', 'min', 'last', 'mean', 'percent_true',
'max', 'std', 'count']
# Perform deep feature synthesis
feature_matrix, feature_names = ft.dfs(entityset=es,
import numpy as np
import random
random.seed(100)
def generate_batch(pairs, n_positive = 50, negative_ratio = 1.0):
"""Generate batches of samples for training.
Random select positive samples
from pairs and randomly select negatives."""
# Create empty array to hold batch
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
book (InputLayer) (None, 1) 0
__________________________________________________________________________________________________
link (InputLayer) (None, 1) 0
__________________________________________________________________________________________________
book_embedding (Embedding) (None, 1, 50) 1851000 book[0][0]
__________________________________________________________________________________________________