markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
7.Set up the FeatureUnion with the desired features, then fit according to the train reviews and transform the train reviews | #Make a FeatureUnion object with the desired features then fit to train reviews
comb_features = yml.make_featureunion()
comb_features.fit(train_reviews)
train_features = comb_features.transform(train_reviews)
train_lsi = yml.get_lsi_features(train_reviews, lsi, topics, dictionary)
train_features = sparse.hstack((t... | machine_learning/User_Sample_test_draft_ed.ipynb | georgetown-analytics/yelp-classification | mit |
Ahora vamos a elegir diferentes grados de aproximación a la imagen original usando un ciclo "For": | for i in range(5, 85, 15):
matreconstruida = np.matrix(U[:, :i]) * np.diag(S[:i]) * np.matrix(V[:i, :])
plt.imshow(matreconstruida, cmap='gray')
title = "Aproximación de grado k = %s" % i
plt.title(title)
plt.show() | Alumnos/Victor Quintero/Tarea 2.ipynb | mauriciogtec/PropedeuticoDataScience2017 | mit |
¿Qué tiene que ver este proyecto con compresión de imágenes?
R= Al descomponer la imagen nos estamos quedando únicamente con información relevante de la misma, por lo que podemos reconstruirla posteriormente utilizando únicamente un porcentaje de los vectores de U, Sigma y V, dependiendo de la fidelidad de la imagen qu... | from copy import copy, deepcopy
def pseudoinversa(A):
U, S, V = np.linalg.svd(A)
m, n = A.shape
D = np.empty([m,n])
D = D * 0
for k in range (n):
D[k,k] = 1
S = D * S # Vuelvo a S una matriz diagonal mXn
pseudo = deepcopy(S)
for i in range (n): #C... | Alumnos/Victor Quintero/Tarea 2.ipynb | mauriciogtec/PropedeuticoDataScience2017 | mit |
Ejemplo para ver que la función resuelve de manera correcta el sistema de ecuaciones: | A = np.array([[2, 1, 3], [4, -1, 3], [-2, 5, 5]])
b = np.array([[17],[31],[-5]])
resuelve(A,b) | Alumnos/Victor Quintero/Tarea 2.ipynb | mauriciogtec/PropedeuticoDataScience2017 | mit |
Jugar con la función donde b puede tomar distintos valores y A=[[1,1],[0,0]]: | A = np.array([[1,1],[0,0]])
b= np.array([[5],[0]])
resuelve(A,b) | Alumnos/Victor Quintero/Tarea 2.ipynb | mauriciogtec/PropedeuticoDataScience2017 | mit |
a) Si b esta en la imagen de A (La imagen es [x,0]) devuelve la solución al sistema de manera correcta. Si b no esta en la imagen (ej. b= [1,1]) devuelve la solución al sistema considerando la imagen, que es la solución más cercana, en el ejemplo b=[1,1] devuelve la solución al sistema considerando b=[1,0].
b) ¿La solu... | A = np.array([[1,1],[0,1e-32]])
b= np.array([[5],[0]])
resuelve(A,b) | Alumnos/Victor Quintero/Tarea 2.ipynb | mauriciogtec/PropedeuticoDataScience2017 | mit |
Ejercicio 3: Ajuste de mínimos cuadrados | import pandas as pd
z = pd.read_csv("https://raw.githubusercontent.com/mauriciogtec/PropedeuticoDataScience2017/master/Tarea/study_vs_sat.csv",index_col = False)
m, n = z.shape
SX= z.iloc[0][0]
SY = z.iloc[0][1]
SXX = z.iloc[0][0] **2
SYY = z.iloc[0][1] **2
SXY = z.iloc[0][0] * z.iloc[0][1]
for i in range (1,m):
... | Alumnos/Victor Quintero/Tarea 2.ipynb | mauriciogtec/PropedeuticoDataScience2017 | mit |
<li> ¿Cuál es el gradiente de la función que se quiere optimizar? R= El Vector [1, Study_hours]
Programar una función que reciba los valores alpha, beta y el vector Study_hours y devuelva un vector array de numpy de predicciones alpha + beta * Study_hours_i, con un vaor por cada individuo. | def sat_score(Alpha,Beta,Study_hours):
m, = Study_hours.shape
Satscore= [0]
for i in range (m-1):
Satscore += [0]
Satscore = np.array([Satscore])
Satscore= Satscore.transpose()
for j in range (m):
Satscore[j,0]= Alpha + Beta * Study_hours[j]
return Satscore
... | Alumnos/Victor Quintero/Tarea 2.ipynb | mauriciogtec/PropedeuticoDataScience2017 | mit |
<li><strong>(Avanzado)</strong> Usen la libreria <code>matplotlib</code> par visualizar las predicciones con alpha y beta solución contra los valores reales de sat_score. | SS= z.iloc[:,1]
g1 = (SH,SS)
g2 = (SH,sat_s)
data = (g1, g2)
colors = ("green", "red")
groups = ("Real", "Forecast: Alpha + Beta * Study_Hours")
fig, ax = plt.subplots()
for data, color, group in zip(data, colors, groups):
x, y = data
ax.scatter(x, y, alpha=0.8, c=color, edgecolors='none', s=30, label=grou... | Alumnos/Victor Quintero/Tarea 2.ipynb | mauriciogtec/PropedeuticoDataScience2017 | mit |
<li> Definan un numpy array X de dos columnas, la primera con unos en todas sus entradas y la segunda y la segunda con la variable Study_hours. Observen que <code>X*[alpha,beta]</code> nos devuelve <code>alpha + beta*study_hours_i</code> en cada entrada y que entonces el problema se vuelve <code>sat_score ~ X*[alpha,be... | x=[1.]
y= [z.iloc[0,0]]
for i in range (19):
x += [1]
y += [z.iloc[i+1,0]]
X = np.array([x,y])
X = X.transpose()
alpha = 353.164879499
beta = 25.3264677779
ab=np.array([[alpha],[beta]])
R = np.dot(X,ab)
R
| Alumnos/Victor Quintero/Tarea 2.ipynb | mauriciogtec/PropedeuticoDataScience2017 | mit |
<li>Calculen la pseudoinversa X^+ de X y computen <code>(X^+)*sat_score</code> para obtener alpha y beta soluciones.</li> | Xpseudo= pseudoinversa(X)
Sscore= z.iloc[:,1]
ab=np.dot(Xpseudo,Sscore)
ab | Alumnos/Victor Quintero/Tarea 2.ipynb | mauriciogtec/PropedeuticoDataScience2017 | mit |
<li>Comparen la solución anterior con la de la fórmula directa de solución exacta <code>(alpha,beta)=(X^t*X)^(-1)*X^t*study_hours</code>.</li> | SH= z.iloc[:,0]
Sscore= z.iloc[:,1]
XT = X.transpose()
XT2 = np.dot(XT,X)
XTI = np.linalg.inv(XT2)
w= np.dot(XTI,XT)
ab = np.dot(w,Sscore)
ab
| Alumnos/Victor Quintero/Tarea 2.ipynb | mauriciogtec/PropedeuticoDataScience2017 | mit |
The first step in any data analysis is acquiring and munging the data
Our starting data set can be found here:
http://jakecoltman.com in the pyData post
It is designed to be roughly similar to the output from DCM's path to conversion
Download the file and transform it into something with the columns:
id,lifetime,age... | ####Data munging here
###Parametric Bayes
#Shout out to Cam Davidson-Pilon
## Example fully worked model using toy data
## Adapted from http://blog.yhat.com/posts/estimating-user-lifetimes-with-pymc.html
## Note that we've made some corrections
N = 2500
##Generate some random data
lifetime = pm.rweibull( 2, 5, si... | Basic Presentation.ipynb | JakeColtman/BayesianSurvivalAnalysis | mit |
Problems:
1 - Try to fit your data from section 1
2 - Use the results to plot the distribution of the median
Note that the media of a Weibull distribution is:
$$β(log 2)^{1/α}$$ | #### Fit to your data here
#### Plot the distribution of the median | Basic Presentation.ipynb | JakeColtman/BayesianSurvivalAnalysis | mit |
Problems:
4 - Try adjusting the number of samples for burning and thinnning
5 - Try adjusting the prior and see how it affects the estimate | #### Adjust burn and thin, both paramters of the mcmc sample function
#### Narrow and broaden prior | Basic Presentation.ipynb | JakeColtman/BayesianSurvivalAnalysis | mit |
Problems:
7 - Try testing whether the median is greater than a different values | #### Hypothesis testing | Basic Presentation.ipynb | JakeColtman/BayesianSurvivalAnalysis | mit |
If we want to look at covariates, we need a new approach.
We'll use Cox proprtional hazards, a very popular regression model.
To fit in python we use the module lifelines:
http://lifelines.readthedocs.io/en/latest/ | ### Fit a cox proprtional hazards model | Basic Presentation.ipynb | JakeColtman/BayesianSurvivalAnalysis | mit |
Once we've fit the data, we need to do something useful with it. Try to do the following things:
1 - Plot the baseline survival function
2 - Predict the functions for a particular set of features
3 - Plot the survival function for two different set of features
4 - For your results in part 3 caculate how much more l... | #### Plot baseline hazard function
#### Predict
#### Plot survival functions for different covariates
#### Plot some odds | Basic Presentation.ipynb | JakeColtman/BayesianSurvivalAnalysis | mit |
Model selection
Difficult to do with classic tools (here)
Problem:
1 - Calculate the BMA coefficient values
2 - Try running with different priors | #### BMA Coefficient values
#### Different priors | Basic Presentation.ipynb | JakeColtman/BayesianSurvivalAnalysis | mit |
List all Positions for an Account. | r = positions.PositionList(accountID=accountID)
client.request(r)
print(r.response) | Oanda v20 REST-oandapyV20/06.00 Position Management.ipynb | anthonyng2/FX-Trading-with-Python-and-Oanda | mit |
List all open Positions for an Account. | r = positions.OpenPositions(accountID=accountID)
client.request(r) | Oanda v20 REST-oandapyV20/06.00 Position Management.ipynb | anthonyng2/FX-Trading-with-Python-and-Oanda | mit |
Get the details of a single instrument’s position in an Account | instrument = "AUD_USD"
r = positions.PositionDetails(accountID=accountID, instrument=instrument)
client.request(r) | Oanda v20 REST-oandapyV20/06.00 Position Management.ipynb | anthonyng2/FX-Trading-with-Python-and-Oanda | mit |
Closeout the open Position regarding instrument in an Account. | data = {
"longUnits": "ALL"
}
r = positions.PositionClose(accountID=accountID,
instrument=instrument,
data=data)
client.request(r) | Oanda v20 REST-oandapyV20/06.00 Position Management.ipynb | anthonyng2/FX-Trading-with-Python-and-Oanda | mit |
Define resources
To execute parsl we need to first define a set of resources on which the apps can run. Here we use a pool of threads. | workers = ThreadPoolExecutor(max_workers=4)
# We pass the workers to the DataFlowKernel which will execute our Apps over the workers.
dfk = DataFlowKernel(executors=[workers]) | Bash-Tutorial.ipynb | Parsl/parsl_demos | apache-2.0 |
Defining Bash Apps
To demonstrate how to run apps written as Bash scripts, we use two mock science applications: simulate.sh and stats.sh. The simulation.sh script serves as a trivial proxy for any more complex scientific simulation application. It generates and prints a set of one or more random integers in the range ... | @App('bash', dfk)
def simulate(sim_steps=1, sim_range=100, sim_values=5, outputs=[], stdout=None, stderr=None):
# The bash app function requires that the bash script is returned from the function as a
# string. Positional and Keyword args to the fn() are formatted into the cmd_line string
# All arguments t... | Bash-Tutorial.ipynb | Parsl/parsl_demos | apache-2.0 |
Running Bash Apps
Now that we've defined an app, let's run 10 parallel instances of it using a for loop. Each run will write 100 random numbers, each between 0 and 99, to the output file.
In order to track files created by Bash apps, a list of data futures (one for each file in the outputs[] list) is made available as ... | simulated_results = []
# Launch 10 parallel runs of simulate() and put the futures in a list
for sim_index in range(10):
sim_fut = simulate(sim_steps=1,
sim_range=100,
sim_values=100,
outputs = ['stdout.{0}.txt'.format(sim_index)], ... | Bash-Tutorial.ipynb | Parsl/parsl_demos | apache-2.0 |
Handling Futures
The variable "simulated_results" contains a list of AppFutures, each corresponding to a running bash app.
Now let's print the status of the 10 jobs by checking if the app futures are done.
Note: you can re-run this step until all the jobs complete (all status are True) or go on, as a later step will bl... | print ([i.done() for i in simulated_results]) | Bash-Tutorial.ipynb | Parsl/parsl_demos | apache-2.0 |
Retrieving Results
Each of the Apps return one DataFuture. Here we put all of these (data futures of file outputs) together into a list (simulation_outputs). This is done by iterating over each of the AppFutures and taking the first and only DataFuture in it's outputs list. | # Grab just the data futures for the output files from each simulation
simulation_outputs = [i.outputs[0] for i in simulated_results] | Bash-Tutorial.ipynb | Parsl/parsl_demos | apache-2.0 |
Defining a Second Bash App
We now explore how Parsl can be used to block on results. Let's define another app, analyze(), that calls stats.sh to find the average of the numbers in a set of files. | @App('bash', dfk)
def analyze(inputs=[], stdout=None, stderr=None):
# Here we compose the commandline for stats.sh that take a list of filenames as arguments
# Since we want a space separated list, rather than a python list (e.g: ['x.txt', 'y.txt'])
# we create a string by joining the filenames of each item... | Bash-Tutorial.ipynb | Parsl/parsl_demos | apache-2.0 |
Blocking on Results
We call analyze with the list of data futures as inputs. This will block until all the simulate runs have completed and the data futures have 'resolved'. Finally, we print the result when it is ready. | results = analyze(inputs=simulation_outputs,
stdout='analyze.out',
stderr='analyze.err')
results.result()
with open('analyze.out', 'r') as f:
print(f.read()) | Bash-Tutorial.ipynb | Parsl/parsl_demos | apache-2.0 |
Association (USES-A)
Passing object of class 1 as an argument of class 2 methods | class A():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def addNums():
self.b + self.c
class B():
def __init__(self, d, e):
self.d = d
self.e = e
def addAllNums(self, arg):
x = self.d + self.e + arg.b + arg.c
ret... | tutorials/python/Ipython files/py basics/OOPS basics.ipynb | vivekec/datascience | gpl-3.0 |
Composition (PART-OF)
Object of class 1 is defined inside the constructor of class 2 | class A():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def addNums():
self.b + self.c
class B():
def __init__(self, d, e):
self.d = d
self.e = e
self.objA = A("hi", 2, 6)
def addAllNums(self):
x = self.d + self.e + self... | tutorials/python/Ipython files/py basics/OOPS basics.ipynb | vivekec/datascience | gpl-3.0 |
Inheritance (IS-A) | class A():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def addNums():
self.b + self.c
class B(A):
def __init__(self, a, b, c, d, e):
# A.__init__(self, a, b, c)
super().__init__(a, b, c)
self.d = d
self.e = e
d... | tutorials/python/Ipython files/py basics/OOPS basics.ipynb | vivekec/datascience | gpl-3.0 |
Function overriding | class A():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def addNums(self):
return self.b * self.c
class B(A):
def __init__(self, a, b, c, d, e):
super().__init__(a, b, c)
self.d = d
self.e = e
def addNums(self):
... | tutorials/python/Ipython files/py basics/OOPS basics.ipynb | vivekec/datascience | gpl-3.0 |
There is no function overloading
Gives no error but only last defined function is executed | class A():
def f1(self, x):
return x
def f1(self, x, y):
return x, y
objA = A()
objA.f1(8,5)
# objA.f1(8) # Gives error
# How to work with function overloading
class A():
def f1(self,name = None):
if name is None:
return 5
else:
return name
objA... | tutorials/python/Ipython files/py basics/OOPS basics.ipynb | vivekec/datascience | gpl-3.0 |
Open a Terminal through Jupyter Notebook
(Menu Bar -> Terminal -> New Terminal)
Create Queue and Feed Tensorflow Graph
Run the Next Cell to Display the Code | %%bash
cat /root/src/main/python/queue/tensorflow_hdfs.py | oreilly.ml/high-performance-tensorflow/notebooks/02_Feed_Queue_HDFS.ipynb | shareactorIO/pipeline | apache-2.0 |
I'll write another function to grab batches out of the arrays made by split data. Here each batch will be a sliding window on these arrays with size batch_size X num_steps. For example, if we want our network to train on a sequence of 100 characters, num_steps = 100. For the next batch, we'll shift this window the next... | def get_batch(arrs, num_steps):
batch_size, slice_size = arrs[0].shape
n_batches = int(slice_size/num_steps)
for b in range(n_batches):
yield [x[:, b*num_steps: (b+1)*num_steps] for x in arrs]
def build_rnn(num_classes, batch_size=50, num_steps=50, lstm_size=128, num_layers=2,
le... | tensorboard/Anna_KaRNNa_Summaries.ipynb | hvillanua/deep-learning | mit |
Training
Time for training which is is pretty straightforward. Here I pass in some data, and get an LSTM state back. Then I pass that state back in to the network so the next batch can continue the state from the previous batch. And every so often (set by save_every_n) I calculate the validation loss and save a checkpo... | !mkdir -p checkpoints/anna
epochs = 10
save_every_n = 100
train_x, train_y, val_x, val_y = split_data(chars, batch_size, num_steps)
model = build_rnn(len(vocab),
batch_size=batch_size,
num_steps=num_steps,
learning_rate=learning_rate,
lstm_size=... | tensorboard/Anna_KaRNNa_Summaries.ipynb | hvillanua/deep-learning | mit |
Constants | _DEFAULT_IMAGE_SIZE = 224
_NUM_CHANNELS = 3
_LABEL_CLASSES = 1001
RESNET_SIZE = 50 # We're loading a resnet-50 saved model.
# Model directory
MODEL_DIR='resnet_model_checkpoints'
VIS_DIR='visualization'
# RIEMANN STEPS is the number of steps in a Riemann Sum.
# This is used to compute an approximate the integral of... | jupyter/resnet_model_understanding.ipynb | google-aai/tf-serving-k8s-tutorial | apache-2.0 |
Download model checkpoint
The next step is to load the researcher's saved checkpoint into our estimator. We will download it from
http://download.tensorflow.org/models/official/resnet50_2017_11_30.tar.gz using the following commands. | import urllib.request
urllib.request.urlretrieve("http://download.tensorflow.org/models/official/resnet50_2017_11_30.tar.gz ", "resnet.tar.gz")
#unzip the file into a directory called resnet
call(["mkdir", MODEL_DIR])
call(["tar", "-zxvf", "resnet.tar.gz", "-C", MODEL_DIR])
# Make sure you see model checkpoint files... | jupyter/resnet_model_understanding.ipynb | google-aai/tf-serving-k8s-tutorial | apache-2.0 |
Import the Model Architecture
In order to reconstruct the Resnet neural network used to train the Imagenet model, we need to load the architecture pieces. During the setup step, we checked out https://github.com/tensorflow/models/tree/v1.4.0/official/resnet. We can now load functions and constants from resnet_model.py ... | %run ../models/official/resnet/resnet_model.py #TODO: modify directory based on where you git cloned the TF models. | jupyter/resnet_model_understanding.ipynb | google-aai/tf-serving-k8s-tutorial | apache-2.0 |
Image preprocessing functions
Note that preprocessing functions are called during training as well (see https://github.com/tensorflow/models/blob/master/official/resnet/imagenet_main.py and https://github.com/tensorflow/models/blob/master/official/resnet/vgg_preprocessing.py), so we will need to extract relevant logic ... | def preprocess_images(images):
"""Preprocesses the image by subtracting out the mean from all channels.
Args:
image: A 4D `Tensor` representing a batch of images.
Returns:
image pixels normalized to be between -0.5 and 0.5
"""
return tf.to_float(images) / 255 - 0.5 | jupyter/resnet_model_understanding.ipynb | google-aai/tf-serving-k8s-tutorial | apache-2.0 |
Resnet Model Functions
We are going to create two estimators here since we need to run two model predictions.
The first prediction computes the top labels for the image by returning the argmax_k top logits.
The second prediction returns a sequence of gradients along the straightline path from a purely grey image ... | def resnet_model_fn(features, labels, mode):
"""Our model_fn for ResNet to be used with our Estimator."""
# Preprocess images as necessary for resnet
features = preprocess_images(features['images'])
# This network must be IDENTICAL to that used to train.
network = imagenet_resnet_v2(RESNET_SIZE, _LABEL_CLAS... | jupyter/resnet_model_understanding.ipynb | google-aai/tf-serving-k8s-tutorial | apache-2.0 |
Gradients Model Function
The Gradients model function takes as input a single image (a 4d tensor of dimension [1, 244, 244, 3]) and expands it to a series of images (tensor dimension [RIEMANN_STEPS + 1, 244, 244, 3]), where each image is simply a "fractional" image, with image 0 being pure gray to image RIEMANN_STEPS b... | def gradients_model_fn(features, labels, mode):
"""Our model_fn for ResNet to be used with our Estimator."""
# Supply the most likely class from features dict to determine which logit function
# to use gradients along the
most_likely_class = features['most_likely_class']
# Features here is a 4d tens... | jupyter/resnet_model_understanding.ipynb | google-aai/tf-serving-k8s-tutorial | apache-2.0 |
Estimators
Load in the model_fn using the checkpoints from MODEL_DIR. This will initialize our weights which we will then use to run backpropagation to find integrated gradients. | # Load this model into our estimator
resnet_estimator = tf.estimator.Estimator(
model_fn=resnet_model_fn, # Call our generate_model_fn to create model function
model_dir=MODEL_DIR, # Where to look for model checkpoints
#config not needed
)
gradients_estimator = tf.estimator.Estimator(
model_fn=gradients_mode... | jupyter/resnet_model_understanding.ipynb | google-aai/tf-serving-k8s-tutorial | apache-2.0 |
Create properly sized image in numpy
Load whatever image you would like (local or url), and resize to 224 x 224 x 3 using opencv2. | def resize_and_pad_image(img, output_image_dim):
"""Resize the image to make it IMAGE_DIM x IMAGE_DIM pixels in size.
If an image is not square, it will pad the top/bottom or left/right
with black pixels to ensure the image is square.
Args:
img: the input 3-color image
output_image_dim: resized and pa... | jupyter/resnet_model_understanding.ipynb | google-aai/tf-serving-k8s-tutorial | apache-2.0 |
Prediction Input Function
Since we are analyzing the model using the estimator api, we need to provide an input function for prediction. Fortunately, there are built-in input functions that can read from numpy arrays, e.g. tf.estimator.inputs.numpy_input_fn. | label_predictions = resnet_estimator.predict(
tf.estimator.inputs.numpy_input_fn(
x={'images': feature},
shuffle=False
)
)
label_dict = next(label_predictions)
# Print out probabilities and class names
classval = label_dict['classes']
probsval = label_dict['probabilities']
labels = []
with op... | jupyter/resnet_model_understanding.ipynb | google-aai/tf-serving-k8s-tutorial | apache-2.0 |
Computing Gradients
Run the gradients estimator to retrieve a generator of metrics and gradient pictures, and pickle the images. | # make the visualization directory
IMAGE_DIR = os.path.join(VIS_DIR, IMAGE_NAME)
call(['mkdir', '-p', IMAGE_DIR])
# Get one of the top classes. 0 picks out the best, 1 picks out second best, etc...
best_label = label_dict['classes'][0]
# Compute gradients with respect to this class
gradient_predictions = gradients_e... | jupyter/resnet_model_understanding.ipynb | google-aai/tf-serving-k8s-tutorial | apache-2.0 |
Visualization
If you simply want to play around with visualization, unpickle the result from above so you do not have to rerun prediction again. The following visualizes the gradients with different amplification of pixels, and prints their derivatives and logits as well to view where the biggest differentiators lie. Y... | AMPLIFICATION = 2.0
INTERPOLATION = 'none'
gradients_and_logits = pickle.load(open(os.path.join(IMAGE_DIR, 'gradients_and_logits.pkl'), "rb" ))
for i in range(0, RIEMANN_STEPS + 1):
gradient_map = pickle.load(open(os.path.join(IMAGE_DIR, 'path_gradient_' + str(i) + '.pkl'), "rb" ))
min_grad = np.ndarray.min(gr... | jupyter/resnet_model_understanding.ipynb | google-aai/tf-serving-k8s-tutorial | apache-2.0 |
Plot the Integrated Gradient
When integrating over all gradients along the path, the result is an image that captures larger signals from pixels with the large gradients. Is the integrated gradient a clear representation of what it is trying to detect? | AMPLIFICATION = 2.0
INTERPOLATION = 'none'
# Plot the integrated gradients
int_gradients = pickle.load(open(os.path.join(IMAGE_DIR, 'int_gradients.pkl'), "rb" ))
min_grad = np.ndarray.min(int_gradients)
max_grad = np.ndarray.max(int_gradients)
median_grad = np.median(int_gradients)
plt.figure(figsize=(15,15))
plt.subp... | jupyter/resnet_model_understanding.ipynb | google-aai/tf-serving-k8s-tutorial | apache-2.0 |
Plot the integrated gradients for each channel
We can also visualize individual pixel contributions from different RGB channels.
Can you think of any other visualization ideas to try out? | AMPLIFICATION = 2.0
INTERPOLATION = 'none'
# Show red-green-blue channels for integrated gradients
for channel in range(0, 3):
gradient_channel = int_gradients[:,:,channel]
min_grad = np.ndarray.min(gradient_channel)
max_grad = np.ndarray.max(gradient_channel)
median_grad = np.median(gradient_channel)
... | jupyter/resnet_model_understanding.ipynb | google-aai/tf-serving-k8s-tutorial | apache-2.0 |
Load capacity curves
In order to use this methodology, it is necessary to provide one (or a group) of capacity curves, defined according to the format described in the RMTK manual. In case multiple capacity curves are input, a spectral shape also needs to be defined.
Please provide the location of the file containing ... | capacity_curves_file = "../../../../../../rmtk_data/capacity_curves_Vb-dfloor.csv"
input_spectrum = "../../../../../../rmtk_data/FEMAP965spectrum.txt"
capacity_curves = utils.read_capacity_curves(capacity_curves_file)
Sa_ratios = utils.get_spectral_ratios(capacity_curves, input_spectrum)
utils.plot_capacity_curves(cap... | notebooks/vulnerability/derivation_fragility/R_mu_T_dispersion/SPO2IDA/spo2ida.ipynb | GEMScienceTools/rmtk | agpl-3.0 |
Idealise pushover curves
In order to use this methodology the pushover curves need to be idealised. Please choose an idealised shape using the parameter idealised_type. The valid options for this methodology are "bilinear" and "quadrilinear". Idealised curves can also be directly provided as input by setting the field ... | idealised_type = "quadrilinear"
idealised_capacity = utils.idealisation(idealised_type, capacity_curves)
utils.plot_idealised_capacity(idealised_capacity, capacity_curves, idealised_type) | notebooks/vulnerability/derivation_fragility/R_mu_T_dispersion/SPO2IDA/spo2ida.ipynb | GEMScienceTools/rmtk | agpl-3.0 |
Load damage state thresholds
Please provide the path to your damage model file using the parameter damage_model_file in the cell below. Currently only interstorey drift damage model type is supported. | damage_model_file = "../../../../../../rmtk_data/damage_model_ISD.csv"
damage_model = utils.read_damage_model(damage_model_file) | notebooks/vulnerability/derivation_fragility/R_mu_T_dispersion/SPO2IDA/spo2ida.ipynb | GEMScienceTools/rmtk | agpl-3.0 |
Calculate fragility functions
The damage threshold dispersion is calculated and integrated with the record-to-record dispersion through Monte Carlo simulations. Please enter the number of Monte Carlo samples to be performed using the parameter montecarlo_samples in the cell below. | montecarlo_samples = 50
fragility_model = SPO2IDA_procedure.calculate_fragility(capacity_curves, idealised_capacity, damage_model, montecarlo_samples, Sa_ratios, 1) | notebooks/vulnerability/derivation_fragility/R_mu_T_dispersion/SPO2IDA/spo2ida.ipynb | GEMScienceTools/rmtk | agpl-3.0 |
Plot fragility functions
The following parameters need to be defined in the cell below in order to plot the lognormal CDF fragility curves obtained above:
* minIML and maxIML: These parameters define the limits of the intensity measure level for plotting the functions | minIML, maxIML = 0.01, 2
utils.plot_fragility_model(fragility_model, minIML, maxIML)
print fragility_model | notebooks/vulnerability/derivation_fragility/R_mu_T_dispersion/SPO2IDA/spo2ida.ipynb | GEMScienceTools/rmtk | agpl-3.0 |
Save fragility functions
The derived parametric fragility functions can be saved to a file in either CSV format or in the NRML format that is used by all OpenQuake input models. The following parameters need to be defined in the cell below in order to save the lognormal CDF fragility curves obtained above:
1. taxonomy:... | taxonomy = "RC"
minIML, maxIML = 0.01, 2.00
output_type = "csv"
output_path = "../../../../../../rmtk_data/output/"
utils.save_mean_fragility(taxonomy, fragility_model, minIML, maxIML, output_type, output_path) | notebooks/vulnerability/derivation_fragility/R_mu_T_dispersion/SPO2IDA/spo2ida.ipynb | GEMScienceTools/rmtk | agpl-3.0 |
Step 2: Initialize Webcam and HDMI Out | # monitor configuration: 640*480 @ 60Hz
Mode = VideoMode(640,480,24)
hdmi_out = base.video.hdmi_out
hdmi_out.configure(Mode,PIXEL_BGR)
hdmi_out.start()
# monitor (output) frame buffer size
frame_out_w = 1920
frame_out_h = 1080
# camera (input) configuration
frame_in_w = 640
frame_in_h = 480
# initialize camera from O... | boards/Pynq-Z1/base/notebooks/video/opencv_face_detect_webcam.ipynb | cathalmccabe/PYNQ | bsd-3-clause |
Step 3: Show input frame on HDMI output | # Capture webcam image
import numpy as np
ret, frame_vga = videoIn.read()
# Display webcam image via HDMI Out
if (ret):
outframe = hdmi_out.newframe()
outframe[0:480,0:640,:] = frame_vga[0:480,0:640,:]
hdmi_out.writeframe(outframe)
else:
raise RuntimeError("Failed to read from camera.") | boards/Pynq-Z1/base/notebooks/video/opencv_face_detect_webcam.ipynb | cathalmccabe/PYNQ | bsd-3-clause |
Step 4: Now use matplotlib to show image inside notebook | # Output webcam image as JPEG
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
plt.imshow(frame_vga[:,:,[2,1,0]])
plt.show() | boards/Pynq-Z1/base/notebooks/video/opencv_face_detect_webcam.ipynb | cathalmccabe/PYNQ | bsd-3-clause |
Step 5: Apply the face detection to the input | import cv2
np_frame = frame_vga
face_cascade = cv2.CascadeClassifier(
'/home/xilinx/jupyter_notebooks/base/video/data/'
'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(
'/home/xilinx/jupyter_notebooks/base/video/data/'
'haarcascade_eye.xml')
gray = cv2.cvtColor(np_frame, cv... | boards/Pynq-Z1/base/notebooks/video/opencv_face_detect_webcam.ipynb | cathalmccabe/PYNQ | bsd-3-clause |
Step 6: Show results on HDMI output | # Output OpenCV results via HDMI
outframe[0:480,0:640,:] = frame_vga[0:480,0:640,:]
hdmi_out.writeframe(outframe) | boards/Pynq-Z1/base/notebooks/video/opencv_face_detect_webcam.ipynb | cathalmccabe/PYNQ | bsd-3-clause |
Step 7: Now use matplotlib to show image inside notebook | # Output OpenCV results via matplotlib
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
plt.imshow(np_frame[:,:,[2,1,0]])
plt.show() | boards/Pynq-Z1/base/notebooks/video/opencv_face_detect_webcam.ipynb | cathalmccabe/PYNQ | bsd-3-clause |
Step 8: Release camera and HDMI | videoIn.release()
hdmi_out.stop()
del hdmi_out | boards/Pynq-Z1/base/notebooks/video/opencv_face_detect_webcam.ipynb | cathalmccabe/PYNQ | bsd-3-clause |
2. Read in the hanford.csv file | df = pd.read_csv("data/hanford.csv")
df | class6/donow/Skinner_Barnaby_DoNow_6.ipynb | ledeprogram/algorithms | gpl-3.0 |
3. Calculate the basic descriptive statistics on the data | df['Exposure'].mean()
df['Exposure'].describe() | class6/donow/Skinner_Barnaby_DoNow_6.ipynb | ledeprogram/algorithms | gpl-3.0 |
4. Calculate the coefficient of correlation (r) and generate the scatter plot. Does there seem to be a correlation worthy of investigation? | df.corr()
df.plot(kind='scatter', x='Mortality', y='Exposure') | class6/donow/Skinner_Barnaby_DoNow_6.ipynb | ledeprogram/algorithms | gpl-3.0 |
5. Create a linear regression model based on the available data to predict the mortality rate given a level of exposure | lm = smf.ols(formula='Mortality~Exposure',data=df).fit()
lm.params
intercept, Exposure = lm.params
Mortality = Exposure*10+intercept
Mortality | class6/donow/Skinner_Barnaby_DoNow_6.ipynb | ledeprogram/algorithms | gpl-3.0 |
6. Plot the linear regression line on the scatter plot of values. Calculate the r^2 (coefficient of determination)
7. Predict the mortality rate (Cancer per 100,000 man years) given an index of exposure = 10 | intercept, Exposure = lm.params
Mortality = Exposure*10+intercept
Mortality | class6/donow/Skinner_Barnaby_DoNow_6.ipynb | ledeprogram/algorithms | gpl-3.0 |
映画レビューのテキスト分類
<table class="tfo-notebook-buttons" align="left">
<td>
<a target="_blank" href="https://colab.research.google.com/github/tensorflow/docs-l10n/blob/master/site/ja/r1/tutorials/keras/basic_text_classification.ipynb"><img src="https://www.tensorflow.org/images/colab_logo_32px.png" />Run in Google Colab... | # keras.datasets.imdb is broken in 1.13 and 1.14, by np 1.16.3
!pip install tf_nightly
import tensorflow.compat.v1 as tf
from tensorflow import keras
import numpy as np
print(tf.__version__) | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
IMDB datasetのダウンロード
IMDBデータセットは、TensorFlowにパッケージ化されています。それは前処理済みのものであり、(単語の連なりである)レビューが、整数の配列に変換されています。そこでは整数が辞書中の特定の単語を表します。
次のコードは、IMDBデータセットをあなたのパソコンにダウンロードします。(すでにダウンロードしていれば、キャッシュされたコピーを使用します) | imdb = keras.datasets.imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000) | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
num_words=10000という引数は、訓練データ中に出てくる単語のうち、最も頻繁に出現する10,000個を保持するためのものです。データサイズを管理可能にするため、稀にしか出現しない単語は破棄されます。
データを調べる
データの形式を理解するために少し時間を割いてみましょう。このデータセットは前処理済みで、サンプルそれぞれが、映画レビューの中の単語を表す整数の配列になっています。ラベルはそれぞれ、0または1の整数値で、0が否定的レビュー、1が肯定的なレビューを示しています。 | print("Training entries: {}, labels: {}".format(len(train_data), len(train_labels))) | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
レビューのテキストは複数の整数に変換されており、それぞれの整数が辞書の中の特定の単語を表します。最初のレビューがどのようなものか見てみましょう。 | print(train_data[0]) | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
映画のレビューはそれぞれ長さが異なっていることでしょう。次のコードで、最初と2つ目のレビューの単語の数を見てみます。ニューラルネットワークへの入力は同じ長さでなければならないため、後ほどその問題を解決する必要があります。 | len(train_data[0]), len(train_data[1]) | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
整数を単語に戻してみる
整数をテキストに戻す方法を知っていると便利です。整数を文字列にマッピングする辞書オブジェクトを検索するためのヘルパー関数を定義します。 | # 単語を整数にマッピングする辞書
word_index = imdb.get_word_index()
# インデックスの最初の方は予約済み
word_index = {k:(v+3) for k,v in word_index.items()}
word_index["<PAD>"] = 0
word_index["<START>"] = 1
word_index["<UNK>"] = 2 # unknown
word_index["<UNUSED>"] = 3
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
... | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
decode_reviewを使うと、最初のレビューのテキストを表示できます。 | decode_review(train_data[0]) | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
データの準備
レビュー(整数の配列)は、ニューラルネットワークに投入する前に、テンソルに変換する必要があります。これには2つの方法があります。
配列をワンホット(one-hot)エンコーディングと同じように、単語の出現を表す0と1のベクトルに変換します。例えば、[3, 5]という配列は、インデックス3と5を除いてすべてゼロの10,000次元のベクトルになります。そして、これをネットワークの最初の層、すなわち、浮動小数点のベクトルデータを扱うことができるDense(全結合)層とします。ただし、これは単語数×レビュー数の行列が必要なメモリ集約的な方法です。
もう一つの方法では、配列をパディングによって同じ長さに揃え、サンプル数 * 長... | train_data = keras.preprocessing.sequence.pad_sequences(train_data,
value=word_index["<PAD>"],
padding='post',
maxlen=256)
test_data = keras.preprocess... | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
サンプルの長さを見てみましょう。 | len(train_data[0]), len(train_data[1]) | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
次に、パディング済みの最初のサンプルを確認します。 | print(train_data[0]) | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
モデルの構築
ニューラルネットワークは、層を積み重ねることで構成されます。この際、2つの大きな決定が必要です。
モデルにいくつの層を設けるか?
層ごとに何個の隠れユニットを使用するか?
この例では、入力データは単語インデックスの配列で構成されています。推定の対象となるラベルは、0または1です。この問題のためのモデルを構築しましょう。 | # 入力の形式は映画レビューで使われている語彙数(10,000語)
vocab_size = 10000
model = keras.Sequential()
model.add(keras.layers.Embedding(vocab_size, 16))
model.add(keras.layers.GlobalAveragePooling1D())
model.add(keras.layers.Dense(16, activation=tf.nn.relu))
model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))
model.summary() | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
これらの層は、分類器を構成するため一列に積み重ねられます。
最初の層はEmbedding(埋め込み)層です。この層は、整数にエンコードされた語彙を受け取り、それぞれの単語インデックスに対応する埋め込みベクトルを検索します。埋め込みベクトルは、モデルの訓練の中で学習されます。ベクトル化のために、出力行列には次元が1つ追加されます。その結果、次元は、(batch, sequence, embedding)となります。
次は、GlobalAveragePooling1D(1次元のグローバル平均プーリング)層です。この層は、それぞれのサンプルについて、シーケンスの次元方向に平均値をもとめ、固定長のベクトルを返します。この結果、モデルは最も... | model.compile(optimizer=tf.keras.optimizers.Adam(),
loss='binary_crossentropy',
metrics=['accuracy']) | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
検証用データを作る
訓練を行う際、モデルが見ていないデータでの正解率を検証したいと思います。もとの訓練用データから、10,000個のサンプルを取り分けて検証用データ(validation set)を作ります。(なぜ、ここでテスト用データを使わないのでしょう? 今回の目的は、訓練用データだけを使って、モデルの開発とチューニングを行うことです。その後、テスト用データを1回だけ使い、正解率を検証するのです。) | x_val = train_data[:10000]
partial_x_train = train_data[10000:]
y_val = train_labels[:10000]
partial_y_train = train_labels[10000:] | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
モデルの訓練
512個のサンプルからなるミニバッチを使って、40エポックモデルを訓練します。この結果、x_trainとy_trainに含まれるすべてのサンプルを40回繰り返すことになります。訓練中、検証用データの10,000サンプルを用いて、モデルの損失と正解率をモニタリングします。 | history = model.fit(partial_x_train,
partial_y_train,
epochs=40,
batch_size=512,
validation_data=(x_val, y_val),
verbose=1) | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
モデルの評価
さて、モデルの性能を見てみましょう。2つの値が返されます。損失(エラーを示す数値であり、小さい方が良い)と正解率です。 | results = model.evaluate(test_data, test_labels, verbose=2)
print(results) | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
この、かなり素朴なアプローチでも87%前後の正解率を達成しました。もっと高度なアプローチを使えば、モデルの正解率は95%に近づけることもできるでしょう。
正解率と損失の時系列グラフを描く
model.fit() は、訓練中に発生したすべてのことを記録した辞書を含むHistory オブジェクトを返します。 | history_dict = history.history
history_dict.keys() | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
4つのエントリがあります。それぞれが、訓練と検証の際にモニターしていた指標を示します。これを使って、訓練時と検証時の損失を比較するグラフと、訓練時と検証時の正解率を比較するグラフを作成することができます。 | import matplotlib.pyplot as plt
%matplotlib inline
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)
# "bo" は青いドット
plt.plot(epochs, loss, 'bo', label='Training loss')
# ”b" は青い実線
plt.plot(epochs, val... | site/ja/r1/tutorials/keras/basic_text_classification.ipynb | tensorflow/docs-l10n | apache-2.0 |
then you can import the necessary modules ... | from clyngor.as_pyasp import TermSet,Atom
from urllib.request import urlopen
from meneco.meneco import query, utils, sbml | meneco.ipynb | bioasp/meneco | gpl-3.0 |
Next, you can load a draft network from an sbml file ... | draft_sbml= urlopen('https://raw.githubusercontent.com/bioasp/meneco/master/Ectodata/ectocyc.sbml')
draftnet = sbml.readSBMLnetwork(draft_sbml, 'draft') | meneco.ipynb | bioasp/meneco | gpl-3.0 |
load the seeds ... | seeds_sbml = urlopen('https://raw.githubusercontent.com/bioasp/meneco/master/Ectodata/seeds.sbml')
seeds = sbml.readSBMLseeds(seeds_sbml) | meneco.ipynb | bioasp/meneco | gpl-3.0 |
and load the targets ... | targets_sbml = urlopen('https://raw.githubusercontent.com/bioasp/meneco/master/Ectodata/targets.sbml')
targets = sbml.readSBMLtargets(targets_sbml) | meneco.ipynb | bioasp/meneco | gpl-3.0 |
Then you can check the draft network for unproducible targets ... | model = query.get_unproducible(draftnet, targets, seeds)
unproducible = set(a[0] for pred in model if pred == 'unproducible_target' for a in model[pred])
print('{0} unproducible targets:\n\t{1}\n'.format(len(unproducible), '\n\t'.join(unproducible))) | meneco.ipynb | bioasp/meneco | gpl-3.0 |
You can load another reaction network like metacyc repair data base ... | repair_sbml = urlopen('https://raw.githubusercontent.com/bioasp/meneco/master/Ectodata/metacyc_16-5.sbml')
repairnet = sbml.readSBMLnetwork(repair_sbml, 'repairnet') | meneco.ipynb | bioasp/meneco | gpl-3.0 |
and combine the draft network with the repair database ... | combinet = draftnet
combinet = TermSet(combinet.union(repairnet)) | meneco.ipynb | bioasp/meneco | gpl-3.0 |
and then check for which targets producibilty cannot be restored even with the combined networks ... | model = query.get_unproducible(combinet, targets, seeds)
never_producible = set(a[0] for pred in model if pred == 'unproducible_target' for a in model[pred])
print('{0} unreconstructable targets:\n\t{1}\n'.format(len(never_producible), '\n\t'.join(never_producible))) | meneco.ipynb | bioasp/meneco | gpl-3.0 |
and for which targets the production paths are repairable ... | reconstructable_targets = unproducible.difference(never_producible)
print('{0} reconstructable targets:\n\t{1}\n'.format(len(reconstructable_targets), '\n\t'.join(reconstructable_targets))) | meneco.ipynb | bioasp/meneco | gpl-3.0 |
You can compute the essential reactions for the repairable target ... | essential_reactions = set()
for t in reconstructable_targets:
single_target = TermSet()
single_target.add(Atom('target("' + t + '")'))
print('\nComputing essential reactions for', t,'... ', end=' ')
model = query.get_intersection_of_completions(draftnet, repairnet, seeds, single_target)
pr... | meneco.ipynb | bioasp/meneco | gpl-3.0 |
You can compute a completion of minimal size suitable to produce all reconstructable targets ... | targets = TermSet(Atom('target("' + t+'")') for t in reconstructable_targets)
model = query.get_minimal_completion_size(draftnet, repairnet, seeds, targets)
one_min_sol_lst = set(a[0] for pred in model if pred == 'xreaction' for a in model[pred])
optimum = len(one_min_sol_lst)
print('minimal size =',optimum)
print('... | meneco.ipynb | bioasp/meneco | gpl-3.0 |
We can compute the common reactions in all completion with a given size ... | model = query.get_intersection_of_optimal_completions(draftnet, repairnet, seeds, targets, optimum)
cautious_sol_lst = set(a[0] for pred in model if pred == 'xreaction' for a in model[pred])
print('Intersection of all solutions of size {0}:\n\t{1}\n'.format(
optimum, '\n\t'.join(cautious_sol_lst))) | meneco.ipynb | bioasp/meneco | gpl-3.0 |
We can compute the union of all completion with a given size ... | model = query.get_union_of_optimal_completions(draftnet, repairnet, seeds, targets, optimum)
brave_sol_lst = set(a[0] for pred in model if pred == 'xreaction' for a in model[pred])
print('Intersection of all solutions of size {0}:\n\t{1}\n'.format(
optimum, '\n\t'.join(brave_sol_lst))) | meneco.ipynb | bioasp/meneco | gpl-3.0 |
And finally compute all (for this notebook we print only the first three) completions with a given size ... | models = query.get_optimal_completions(draftnet, repairnet, seeds, targets, optimum)
count = 0
for model in models:
one_min_sol_lst = set(a[0] for pred in model if pred == 'xreaction' for a in model[pred])
count += 1
print('Completion {0}:\n\t{1}\n'.format(
str(count), '\n\t'.join(one_min_sol_lst))... | meneco.ipynb | bioasp/meneco | gpl-3.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.