Skip to content

Instantly share code, notes, and snippets.

@daTokenizer
Last active June 2, 2022 13:27
Show Gist options
  • Save daTokenizer/9e5b38f0baae026657d58c6744f50aea to your computer and use it in GitHub Desktop.
Save daTokenizer/9e5b38f0baae026657d58c6744f50aea to your computer and use it in GitHub Desktop.
hyperdemo
print("Still Alive!")
print("this should never work
import hyperplane
def test_get_secret():
not_so_secret_key = "demo_key"
secret_value = hyperplane.get_secret(not_so_secret_key)
return f'my secret value (for the key:{not_so_secret_key}) is {secret_value}'
def test_get_env_param():
env_param_name = "ENVVAR_TEST"
env_param_value = hyperplane.get_env_param(env_param_name)
return f'my environment includes a parameter called {env_param_name} with the value {env_param_value}'
def test_get_user_id():
user_id = hyperplane.get_user_id()
return f'was ran by user {user_id}'
def test_get_job_id():
job_id = hyperplane.get_job_id()
return f'I was Job ID: {job_id}'
def test_reporting(*messages):
combined_analytics_message = "\n".join(messages)
if not hyperplane.report(analytics_str=combined_analytics_message):
test_stderr("Failed to use reporting API for some reason")
def test_stderr(message):
import sys
print(message, file=sys.stderr)
def test_output_methods(base_parameters_message):
out_file_name1 = "demo_file1.txt"
out_file_name2 = "demo_file2.txt"
file1_message = f'this message was also printed to the file {out_file_name1}'
file2_message = f'this message was printed to the file {out_file_name2}'
file_post_message = f" under the OUTPUT_FILES_BASE_DIR (./{hyperplane.OUTPUT_FILES_DIR}) dir"
test_reporting("this repost will only show up for a short while")
# print first message to stdout and both files
print("first:", base_parameters_message)
hyperplane.print_to_file(out_file_name1, base_parameters_message, "\n")
hyperplane.print_to_file(out_file_name2, base_parameters_message, "\n")
# send all messages to the analytics API
test_reporting(base_parameters_message, file1_message, f", and {file2_message}.")
# print the end of the massage to the appropriate file (and to stdout)
hyperplane.print_to_file(out_file_name1, file1_message)
print(f"also: {file1_message},")
hyperplane.print_to_file(out_file_name2, file2_message)
print(f"second: {file2_message},")
print(f"both {file_post_message}.")
hyperplane.print_to_file(out_file_name1, f" {file_post_message}", "\n")
hyperplane.print_to_file(out_file_name2, f" {file_post_message}", "\n")
# demonstrate printing to STDERR as well
test_stderr("If there were an issue, we would have printed it here..")
def say_hi():
job_id_test_response = test_get_job_id()
user_id_test_response = test_get_user_id()
env_param_test_response = test_get_env_param()
secret_test_response = test_get_secret()
base_parameters_message = f'Hi there, ' \
f'{job_id_test_response}, ' \
f'{user_id_test_response}, ' \
f'{env_param_test_response}, ' \
f'and {secret_test_response}.\n'
test_output_methods(base_parameters_message)
if __name__ == '__main__':
say_hi()
# Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>
# License: BSD 3 clause
# Standard scientific Python imports
#import matplotlib.pyplot as plt
# Import datasets, classifiers and performance metrics
from sklearn import datasets, svm, metrics
from sklearn.model_selection import train_test_split
import numpy as np
import sys
import sklearn
import json
digits = datasets.load_digits()
# flatten the images
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))
# Create a classifier: a support vector classifier
clf = svm.SVC(gamma=0.001)
# Split data into 50% train and 50% test subsets
X_train, X_test, y_train, y_test = train_test_split(
data, digits.target, test_size=0.5, shuffle=False
)
# Learn the digits on the train subset
clf.fit(X_train, y_train)
# Predict the value of the digit on the test subset
predicted = clf.predict(X_test)
data = {}
data["python"] = sys.version
data["predictions"] = predicted.tolist()
data["sklearn_version"] = sklearn.__version__
print(data)
from datetime import datetime
file_name = sys.argv[0]+"_"+str(datetime.now())+".txt"
np.savetxt(file_name, predicted[:10])
# Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>
# License: BSD 3 clause
# Standard scientific Python imports
#import matplotlib.pyplot as plt
# Import datasets, classifiers and performance metrics
from sklearn import datasets, svm, metrics
from sklearn.model_selection import train_test_split
import numpy as np
import sys
import sklearn
import json
digits = datasets.load_digits()
# flatten the images
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))
# Create a classifier: a support vector classifier
clf = svm.SVC(gamma=0.001)
# Split data into 50% train and 50% test subsets
X_train, X_test, y_train, y_test = train_test_split(
data, digits.target, test_size=0.5, shuffle=False
)
# Learn the digits on the train subset
clf.fit(X_train, y_train)
# Predict the value of the digit on the test subset
predicted = clf.predict(X_test)
data = {}
data["python"] = sys.version
data["predictions"] = predicted[:100].tolist()
data["sklearn_version"] = sklearn.__version__
data["script"] = sys.argv[0]
print(data)
from datetime import datetime
file_name = sys.argv[0]+"_"+str(datetime.now())+".json"
with open(file_name, "w") as f:
json.dump(data, f)
import pprint, sys
pprint.pprint(sys.modules)
# -*- coding: utf-8 -*-
import torch
import math
dtype = torch.float
device = torch.device("cpu")
device = torch.device("cuda:0") # Uncomment this to run on GPU
# Create random input and output data
x = torch.linspace(-math.pi, math.pi, 2000, device=device, dtype=dtype)
y = torch.sin(x)
# Randomly initialize weights
a = torch.randn((), device=device, dtype=dtype)
b = torch.randn((), device=device, dtype=dtype)
c = torch.randn((), device=device, dtype=dtype)
d = torch.randn((), device=device, dtype=dtype)
learning_rate = 1e-6
for t in range(2000):
# Forward pass: compute predicted y
y_pred = a + b * x + c * x ** 2 + d * x ** 3
# Compute and print loss
loss = (y_pred - y).pow(2).sum().item()
if t % 100 == 99:
print(t, loss)
# Backprop to compute gradients of a, b, c, d with respect to loss
grad_y_pred = 2.0 * (y_pred - y)
grad_a = grad_y_pred.sum()
grad_b = (grad_y_pred * x).sum()
grad_c = (grad_y_pred * x ** 2).sum()
grad_d = (grad_y_pred * x ** 3).sum()
# Update weights using gradient descent
a -= learning_rate * grad_a
b -= learning_rate * grad_b
c -= learning_rate * grad_c
d -= learning_rate * grad_d
print(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3')
# -*- coding: utf-8 -*-
import torch
import math
dtype = torch.float
device = torch.device("cpu")
# device = torch.device("cuda:0") # Uncomment this to run on GPU
# Create random input and output data
x = torch.linspace(-math.pi, math.pi, 2000, device=device, dtype=dtype)
y = torch.sin(x)
# Randomly initialize weights
a = torch.randn((), device=device, dtype=dtype)
b = torch.randn((), device=device, dtype=dtype)
c = torch.randn((), device=device, dtype=dtype)
d = torch.randn((), device=device, dtype=dtype)
learning_rate = 1e-6
for t in range(2000):
# Forward pass: compute predicted y
y_pred = a + b * x + c * x ** 2 + d * x ** 3
# Compute and print loss
loss = (y_pred - y).pow(2).sum().item()
if t % 100 == 99:
print(t, loss)
# Backprop to compute gradients of a, b, c, d with respect to loss
grad_y_pred = 2.0 * (y_pred - y)
grad_a = grad_y_pred.sum()
grad_b = (grad_y_pred * x).sum()
grad_c = (grad_y_pred * x ** 2).sum()
grad_d = (grad_y_pred * x ** 3).sum()
# Update weights using gradient descent
a -= learning_rate * grad_a
b -= learning_rate * grad_b
c -= learning_rate * grad_c
d -= learning_rate * grad_d
print(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3')
python<=3.7
scikit-learn==0.24.2
scikit-learn>=0.24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment