def test_predict_functions_honor_device(digits, func): X_train, X_test, y_train, y_test = digits mod = TorchShallowNeuralClassifier(max_iter=2) mod.fit(X_train, y_train) prediction_func = getattr(mod, func) with pytest.raises(RuntimeError): prediction_func(X_test, device="FAKE_DEVICE")
def test_hidden_activation_in_graph(digits): X_train, X_test, y_train, y_test = digits mod = TorchShallowNeuralClassifier(max_iter=1, hidden_activation=nn.ReLU()) mod.fit(X_train, y_train) mod_hidden_activation = mod.hidden_activation.__class__ graph_activation_class = mod.model[1].__class__ assert mod_hidden_activation == graph_activation_class
def test_model_graph_dimensions(digits, attr, layer_index, weight_dim): X_train, X_test, y_train, y_test = digits mod = TorchShallowNeuralClassifier(max_iter=1) mod.fit(X_train, y_train) mod_attr_val = getattr(mod, attr) graph_dim = mod.model[layer_index].weight.shape[weight_dim] assert mod_attr_val == graph_dim
def test_simple_example_params(digits, param, expected): X_train, X_test, y_train, y_test = digits mod = TorchShallowNeuralClassifier(**{param: expected}) mod.fit(X_train, y_train) preds = mod.predict(X_test) acc = accuracy_score(y_test, preds) if not (param in ["max_iter", "batch_size"] and expected <= 1): assert acc >= 0.86
def test_predict_restores_device(digits, func): X_train, X_test, y_train, y_test = digits mod = TorchShallowNeuralClassifier(max_iter=2) mod.fit(X_train, y_train) current_device = mod.device assert current_device != torch.device("cpu:0") prediction_func = getattr(mod, func) prediction_func(X_test, device="cpu:0") assert mod.device == current_device
def test_model(XOR): """Just makes sure that this code will run; it doesn't check that it is creating good models. """ X, y = XOR model = TorchShallowNeuralClassifier(hidden_dim=4, hidden_activation=nn.ReLU(), max_iter=100, eta=0.01) model.fit(X, y) model.predict(X) model.predict_proba(X)
def fit_basic_rnn(X, y, hidden_dim, max_iter, hidden_activation, eta): if hidden_dim is None: hidden_dim = 50 if max_iter is None: max_iter = 100 if hidden_activation is None: hidden_activation = nn.Tanh() if eta is None: eta = 0.01 mod = TorchShallowNeuralClassifier(hidden_dim = hidden_dim, max_iter = max_iter, hidden_activation = hidden_activation, eta = eta) mod.fit(X, y) return mod
def test_save_load(XOR): X, y = XOR mod = TorchShallowNeuralClassifier(hidden_dim=4, hidden_activation=nn.ReLU(), max_iter=100, eta=0.01) mod.fit(X, y) mod.predict(X) with tempfile.NamedTemporaryFile(mode='wb') as f: name = f.name mod.to_pickle(name) mod2 = TorchShallowNeuralClassifier.from_pickle(name) mod2.predict(X) mod2.fit(X, y)
def test_optimizer_keywords(XOR, expected): X, y = XOR mod = TorchShallowNeuralClassifier(amsgrad=expected) mod.fit(X, y) assert mod.amsgrad == expected assert mod.optimizer.param_groups[0]['amsgrad'] == expected
def fit_hf_shallow_network(X, y): mod = TorchShallowNeuralClassifier( max_iter=100, hidden_dim=300) mod.fit(X, y) return mod
def fit_nn_classifier(X, y): mod = TorchShallowNeuralClassifier(hidden_dim=50, max_iter=100) mod.fit(X, y) return mod
class Shallow_Neural_Classifier: ''' Modified torch shallow neural classifier wrapper class for initial fitting and then fine tuning of weights. ''' def __init__(self, sent140_train_X, sent140_dev_X, sent140_train_Y, sent140_dev_Y, emoji_train_X, emoji_dev_X, emoji_test_X, emoji_train_Y, emoji_dev_Y, emoji_test_Y, emojiless_train_X, emojiless_dev_X, emojiless_test_X, emojiless_train_Y, emojiless_dev_Y, emojiless_test_Y, testing): ''' Pass in initial data for fitting to constructor. Later adding passing logisitic regression parameters into constructor. ''' self.testing = testing self.sent140_train_X = sent140_train_X self.sent140_train_Y = sent140_train_Y self.sent140_dev_X = sent140_dev_X self.sent140_dev_Y = sent140_dev_Y self.emoji_train_X = emoji_train_X self.emoji_train_Y = emoji_train_Y self.emoji_dev_X = emoji_dev_X self.emoji_dev_Y = emoji_dev_Y if self.testing: self.emoji_test_X = emoji_test_X self.emoji_test_Y = emoji_test_Y self.emojiless_train_X = emojiless_train_X self.emojiless_train_Y = emojiless_train_Y self.emojiless_dev_X = emojiless_dev_X self.emojiless_dev_Y = emojiless_dev_Y if self.testing: self.emojiless_test_X = emojiless_test_X self.emojiless_test_Y = emojiless_test_Y # pass in model parameters for to constructor? def run_sent140(self): ''' Trained on sent140, predict on emoji Report score on sent 140 too, just because it's intersting ''' # model self.model_sent140 = TorchShallowNeuralClassifier() # train self.model_sent140.fit(self.sent140_train_X, self.sent140_train_Y) # test on sent140 sent140_train_preds = self.model_sent140.predict(self.sent140_train_X) sent140_dev_preds = self.model_sent140.predict(self.sent140_dev_X) # test on emoji emoji_train_preds = self.model_sent140.predict(self.emoji_train_X) emoji_dev_preds = self.model_sent140.predict(self.emoji_dev_X) if self.testing: emoji_test_preds = self.model_sent140.predict(self.emoji_test_X) else: emoji_test_preds = None return (sent140_train_preds, sent140_dev_preds, emoji_train_preds, emoji_dev_preds, emoji_test_preds) def run_sent140_emojiless(self): ''' Trained on sent140, fine-tuned on emojiless, predict on emoji Report score on sent 140 too, just because it's intersting ''' # model self.model_sent140_emojiless = TorchShallowNeuralClassifier() # train # combine features combined_train_X = np.vstack((self.sent140_train_X, self.emojiless_train_X)) combined_train_Y = self.sent140_train_Y + self.emojiless_train_Y self.model_sent140_emojiless.fit(combined_train_X, combined_train_Y) # test on sent140 sent140_train_preds = self.model_sent140_emojiless.predict(self.sent140_train_X) sent140_dev_preds = self.model_sent140_emojiless.predict(self.sent140_dev_X) # test on emoji emoji_train_preds = self.model_sent140_emojiless.predict(self.emoji_train_X) emoji_dev_preds = self.model_sent140_emojiless.predict(self.emoji_dev_X) if self.testing: emoji_test_preds = self.model_sent140_emojiless.predict(self.emoji_test_X) else: emoji_test_preds = None return (sent140_train_preds, sent140_dev_preds, emoji_train_preds, emoji_dev_preds, emoji_test_preds) def run_sent140_emoji(self): ''' Trained on sent140, fine-tuned on emoji, predict on emoji Report score on sent 140 too, just because it's intersting ''' # model self.model_sent140_emoji = TorchShallowNeuralClassifier() # train combined_train_X = np.vstack((self.sent140_train_X, self.emoji_train_X)) combined_train_Y = self.sent140_train_Y + self.emoji_train_Y self.model_sent140_emoji.fit(combined_train_X, combined_train_Y) # test on sent140 sent140_train_preds = self.model_sent140_emoji.predict(self.sent140_train_X) sent140_dev_preds = self.model_sent140_emoji.predict(self.sent140_dev_X) # test on emoji emoji_train_preds = self.model_sent140_emoji.predict(self.emoji_train_X) emoji_dev_preds = self.model_sent140_emoji.predict(self.emoji_dev_X) if self.testing: emoji_test_preds = self.model_sent140_emoji.predict(self.emoji_test_X) else: emoji_test_preds = None return (sent140_train_preds, sent140_dev_preds, emoji_train_preds, emoji_dev_preds, emoji_test_preds)