Skip to content

Instantly share code, notes, and snippets.

View rish-16's full-sized avatar
🤖
backpropagating through memories

Rishabh Anand rish-16

🤖
backpropagating through memories
View GitHub Profile
@rish-16
rish-16 / generate.py
Last active May 13, 2019 10:26
Main event for generation of random numbers
def rand_int():
new_job = qk.execute(circ, backend, shots=1)
# The output bitstring consists of 3 collapsed Qubits (bits)
bitstring = new_job.results().get_counts()
bitstring = list(bitstring.keys()[0])
# Converting binary to Decimal integers
random_integer = int(bitstring, 2)
import qiskit as qk
# Load saved account from memory
qk.IBMQ.load_accounts()
n = 3
q = qk.QuantumRegister(n)
c = qk.ClassicalRegister(n)
circ = qk.QuantumCircuit(q, c)
a = rand_int()
print (a)
@rish-16
rish-16 / app.py
Created May 24, 2019 05:12
Our GPT-2 model server
#!/usr/bin/env python3
import os
from flask import Flask, request
from interactive_conditional_samples import interact_model
app = Flask(__name__)
@app.route('/', methods=['GET'])
def hello():
return "Hello from GPT-2 model server!"
@rish-16
rish-16 / interactive_conditional_samples.py
Created May 24, 2019 10:46
Generation function for the GPT-2 model
# Here, we have added a `raw_text` parameter
# From our Flask API we'll pass in the incoming text into this function
def interact_model(raw_text, model_name='117M', seed=None, nsamples=1, batch_size=1, length=None, temperature=1, top_k=40, models_dir='models/'):
models_dir = os.path.expanduser(os.path.expandvars(models_dir))
if batch_size is None:
batch_size = 1
assert nsamples % batch_size == 0
enc = encoder.get_encoder(model_name, models_dir)
hparams = model.default_hparams()
# Detect hardware
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection
except ValueError: # If TPU not found, resolve to GPU instead
tpu = None
gpus = tf.config.experimental.list_logical_devices("GPU")
# Select appropriate distribution strategy
if tpu:
tf.tpu.experimental.initialize_tpu_system(tpu)
resolver = tf.contrib.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
tf.contrib.distribute.initialize_tpu_system(resolver)
strategy = tf.contrib.distribute.TPUStrategy(resolver)
with strategy.scope():
"""
This essentailly takes our model and makes it
compatible to train on a TPU.
"""
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(512, input_size=[784,], activation='relu'))
model.add(tf.keras.layers.Dense(256, activation='relu'))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(64, activation='relu'))
history = model.fit(x_train,
y_train,
epochs=20,
steps_per_epoch=50
)
model.save_weights('./mnist_model.h5', overwrite=True)
# Detect hardware
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection
except ValueError: # If TPU not found
tpu = None