markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
Set up a recurrent Cloud Scheduler job for the Pub/Sub topic
Read more about possible ways to create cron jobs here.
Read about the cron job schedule format here. | scheduler_job_args = " ".join(
[
SIMULATOR_SCHEDULER_JOB,
f"--schedule='{SIMULATOR_SCHEDULE}'",
f"--topic={SIMULATOR_PUBSUB_TOPIC}",
f"--message-body={SIMULATOR_SCHEDULER_MESSAGE}",
]
)
! echo $scheduler_job_args
! gcloud scheduler jobs create pubsub $scheduler_job_args | community-content/tf_agents_bandits_movie_recommendation_with_kfp_and_vertex_sdk/mlops_pipeline_tf_agents_bandits_movie_recommendation/mlops_pipeline_tf_agents_bandits_movie_recommendation.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Define the Simulator logic in a Cloud Function to be triggered periodically, and deploy this Function
Specify dependencies of the Function in src/simulator/requirements.txt.
Read more about the available configurable arguments for deploying a Function here. For instance, based on the complexity of your Function, you m... | endpoints = ! gcloud ai endpoints list \
--region=$REGION \
--filter=display_name=$ENDPOINT_DISPLAY_NAME
print("\n".join(endpoints), "\n")
ENDPOINT_ID = endpoints[2].split(" ")[0]
print(f"ENDPOINT_ID={ENDPOINT_ID}")
ENV_VARS = ",".join(
[
f"PROJECT_ID={PROJECT_ID}",
f"REGION={REGION}",
... | community-content/tf_agents_bandits_movie_recommendation_with_kfp_and_vertex_sdk/mlops_pipeline_tf_agents_bandits_movie_recommendation/mlops_pipeline_tf_agents_bandits_movie_recommendation.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Create the Logger to asynchronously log prediction inputs and results
Create the Logger to get environment feedback as rewards from the MovieLens simulation environment based on prediction observations and predicted actions, formulate trajectory data, and store said data back to BigQuery. The Logger closes the RL feedb... | ! python3 -m unittest src.logger.test_main | community-content/tf_agents_bandits_movie_recommendation_with_kfp_and_vertex_sdk/mlops_pipeline_tf_agents_bandits_movie_recommendation/mlops_pipeline_tf_agents_bandits_movie_recommendation.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Create a Pub/Sub topic
Read more about creating Pub/Sub topics here | ! gcloud pubsub topics create $LOGGER_PUBSUB_TOPIC | community-content/tf_agents_bandits_movie_recommendation_with_kfp_and_vertex_sdk/mlops_pipeline_tf_agents_bandits_movie_recommendation/mlops_pipeline_tf_agents_bandits_movie_recommendation.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Define the Logger logic in a Cloud Function to be triggered by a Pub/Sub topic, which is triggered by the prediction code at each prediction request.
Specify dependencies of the Function in src/logger/requirements.txt.
Read more about the available configurable arguments for deploying a Function here. For instance, ba... | ENV_VARS = ",".join(
[
f"PROJECT_ID={PROJECT_ID}",
f"RAW_DATA_PATH={RAW_DATA_PATH}",
f"BATCH_SIZE={BATCH_SIZE}",
f"RANK_K={RANK_K}",
f"NUM_ACTIONS={NUM_ACTIONS}",
f"BIGQUERY_TMP_FILE={BIGQUERY_TMP_FILE}",
f"BIGQUERY_DATASET_ID={BIGQUERY_DATASET_ID}",
f... | community-content/tf_agents_bandits_movie_recommendation_with_kfp_and_vertex_sdk/mlops_pipeline_tf_agents_bandits_movie_recommendation/mlops_pipeline_tf_agents_bandits_movie_recommendation.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Create the Trigger to trigger re-training
Create the Trigger to recurrently re-run the pipeline to re-train the policy on new training data, using kfp.v2.google.client.AIPlatformClient.create_schedule_from_job_spec. You create a pipeline for orchestration on Vertex Pipelines, and a Cloud Scheduler job that recurrently ... | TRIGGER_SCHEDULE = "*/30 * * * *" # Schedule to trigger the pipeline. Eg. "*/30 * * * *" means every 30 mins.
ingest_op = load_component_from_url(
"https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/62a2a7611499490b4b04d731d48a7ba87c2d636f/community-content/tf_agents_bandits_movie_recommendat... | community-content/tf_agents_bandits_movie_recommendation_with_kfp_and_vertex_sdk/mlops_pipeline_tf_agents_bandits_movie_recommendation/mlops_pipeline_tf_agents_bandits_movie_recommendation.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Cleaning up
To clean up all Google Cloud resources used in this project, you can delete the Google Cloud
project you used for the tutorial.
Otherwise, you can delete the individual resources you created in this tutorial (you also need to clean up other resources that are difficult to delete here, such as the all/partia... | # Delete endpoint resource.
! gcloud ai endpoints delete $ENDPOINT_ID --quiet --region $REGION
# Delete Pub/Sub topics.
! gcloud pubsub topics delete $SIMULATOR_PUBSUB_TOPIC --quiet
! gcloud pubsub topics delete $LOGGER_PUBSUB_TOPIC --quiet
# Delete Cloud Functions.
! gcloud functions delete $SIMULATOR_CLOUD_FUNCTION... | community-content/tf_agents_bandits_movie_recommendation_with_kfp_and_vertex_sdk/mlops_pipeline_tf_agents_bandits_movie_recommendation/mlops_pipeline_tf_agents_bandits_movie_recommendation.ipynb | GoogleCloudPlatform/vertex-ai-samples | apache-2.0 |
Once we've grabbed the "Feature Collection" dataset, we can request a subset of the data: | # Can safely ignore the warnings
ncss = metar_dataset.subset() | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
What variables do we have available? | ncss.variables | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
<a href="#top">Top</a>
<hr style="height:2px;">
<a name="stationplot"></a>
2. Making a station plot
Make new NCSS query
Request data closest to a time | from datetime import datetime
query = ncss.query()
query.lonlat_box(north=34, south=24, east=-80, west=-90)
query.time(datetime(2017, 9, 10, 12))
query.variables('temperature', 'dewpoint', 'altimeter_setting',
'wind_speed', 'wind_direction', 'sky_coverage')
query.accept('csv')
# Get the data
data = nc... | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
Now we need to pull apart the data and perform some modifications, like converting winds to components and convert sky coverage percent to codes (octets) suitable for plotting. | import numpy as np
import metpy.calc as mpcalc
from metpy.units import units
# Since we used the CSV data, this is just a dictionary of arrays
lats = data['latitude']
lons = data['longitude']
tair = data['temperature']
dewp = data['dewpoint']
alt = data['altimeter_setting']
# Convert wind to components
u, v = mpcalc... | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
Create the map using cartopy and MetPy!
One way to create station plots with MetPy is to create an instance of StationPlot and call various plot methods, like plot_parameter, to plot arrays of data at locations relative to the center point.
In addition to plotting values, StationPlot has support for plotting text strin... | %matplotlib inline
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
from metpy.plots import StationPlot, sky_cover
# Set up a plot with map features
fig = plt.figure(figsize=(12, 12))
proj = ccrs.Stereographic(central_longitude=-95, central_latitude=35)
ax = fig.add_subpl... | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
Notice how there are so many overlapping stations? There's a utility in MetPy to help with that: reduce_point_density. This returns a mask we can apply to data to filter the points. | # Project points so that we're filtering based on the way the stations are laid out on the map
proj = ccrs.Stereographic(central_longitude=-95, central_latitude=35)
xy = proj.transform_points(ccrs.PlateCarree(), lons, lats)
# Reduce point density so that there's only one point within a 200km circle
mask = mpcalc.reduc... | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
Now we just plot with arr[mask] for every arr of data we use in plotting. | # Set up a plot with map features
fig = plt.figure(figsize=(12, 12))
ax = fig.add_subplot(1, 1, 1, projection=proj)
ax.add_feature(cfeature.STATES, edgecolor='black')
ax.coastlines(resolution='50m')
ax.gridlines()
# Create a station plot pointing to an Axes to draw on as well as the location of points
stationplot = St... | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
More examples for MetPy Station Plots:
- MetPy Examples
- MetPy Symbol list
<div class="alert alert-success">
<b>EXERCISE</b>:
<ul>
<li>Modify the station plot (reproduced below) to include dewpoint, altimeter setting, as well as the station id. The station id can be added using the `plot_text` method ... | # Use reduce_point_density
# Set up a plot with map features
fig = plt.figure(figsize=(12, 12))
ax = fig.add_subplot(1, 1, 1, projection=proj)
ax.add_feature(cfeature.STATES, edgecolor='black')
ax.coastlines(resolution='50m')
ax.gridlines()
# Create a station plot pointing to an Axes to draw on as well as the locatio... | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
<a href="#top">Top</a>
<hr style="height:2px;">
<a name="timeseries"></a>
3. Time Series request and plot
Let's say we want the past days worth of data...
...for Boulder (i.e. the lat/lon)
...for the variables mean sea level pressure, air temperature, wind direction, and wind_speed | from datetime import timedelta
# define the time range we are interested in
end_time = datetime(2017, 9, 12, 0)
start_time = end_time - timedelta(days=2)
# build the query
query = ncss.query()
query.lonlat_point(-80.25, 25.8)
query.time_range(start_time, end_time)
query.variables('altimeter_setting', 'temperature', '... | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
Let's get the data! | data = ncss.get_data(query)
print(list(data.keys())) | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
What station did we get? | station_id = data['station'][0].tostring()
print(station_id) | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
That indicates that we have a Python bytes object, containing the 0-255 values corresponding to 'K', 'M', 'I', 'A'. We can decode those bytes into a string: | station_id = station_id.decode('ascii')
print(station_id) | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
Let's get the time into datetime objects. We see we have an array with byte strings in it, like station id above. | data['time'] | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
So we can use a list comprehension to turn this into a list of date time objects: | time = [datetime.strptime(s.decode('ascii'), '%Y-%m-%dT%H:%M:%SZ') for s in data['time']] | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
Now for the obligatory time series plot... | from matplotlib.dates import DateFormatter, AutoDateLocator
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(time, data['wind_speed'], color='tab:blue')
ax.set_title(f'Site: {station_id} Date: {time[0]:%Y/%m/%d}')
ax.set_xlabel('Hour of day')
ax.set_ylabel('Wind Speed')
ax.grid(True)
# Improve on the default tic... | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
<div class="alert alert-success">
<b>EXERCISE</b>:
<ul>
<li>Pick a different location</li>
<li>Plot temperature and dewpoint together on the same plot</li>
</ul>
</div> | # Your code goes here
# %load solutions/time_series.py | notebooks/Surface_Data/Surface Data with Siphon and MetPy.ipynb | Unidata/unidata-python-workshop | mit |
Download a few Micro-C datasets, processed using distiller (https://github.com/mirnylab/distiller-nf), binned to 2048bp, and iteratively corrected. | if not os.path.exists('./data/coolers'):
os.mkdir('./data/coolers')
if not os.path.isfile('./data/coolers/HFF_hg38_4DNFIP5EUOFX.mapq_30.2048.cool'):
subprocess.call('curl -o ./data/coolers/HFF_hg38_4DNFIP5EUOFX.mapq_30.2048.cool'+
' https://storage.googleapis.com/basenji_hic/tutorials/coolers/HFF_hg... | manuscripts/akita/tutorial.ipynb | calico/basenji | apache-2.0 |
Write out these cooler files and labels to a samples table. | lines = [['index','identifier','file','clip','sum_stat','description']]
lines.append(['0', 'HFF', './data/coolers/HFF_hg38_4DNFIP5EUOFX.mapq_30.2048.cool', '2', 'sum', 'HFF'])
lines.append(['1', 'H1hESC', './data/coolers/H1hESC_hg38_4DNFI1O6IL1Q.mapq_30.2048.cool', '2', 'sum', 'H1hESC'])
samples_out = open('data/micro... | manuscripts/akita/tutorial.ipynb | calico/basenji | apache-2.0 |
Next, we want to choose genomic sequences to form batches for stochastic gradient descent, divide them into training/validation/test sets, and construct TFRecords to provide to downstream programs.
The script akita_data.py implements this procedure.
The most relevant options here are:
| Option/Argument | Value | Note |... | if os.path.isdir('data/1m'):
shutil.rmtree('data/1m')
! akita_data.py --sample 0.05 -g ./data/hg38_gaps_binsize2048_numconseq10.bed -l 1048576 --crop 65536 --local -o ./data/1m --as_obsexp -p 8 -t .1 -v .1 -w 2048 --snap 2048 --stride_train 262144 --stride_test 32768 ./data/hg38.ml.fa ./data/microc_cools.txt | manuscripts/akita/tutorial.ipynb | calico/basenji | apache-2.0 |
The data for training is now saved in data/1m as tfrecords (for training, validation, and testing), where contigs.bed contains the original large contiguous regions from which training sequences were taken, and sequences.bed contains the train/valid/test sequences. | ! cut -f4 data/1m/sequences.bed | sort | uniq -c
! head -n3 data/1m/sequences.bed | manuscripts/akita/tutorial.ipynb | calico/basenji | apache-2.0 |
Now train a model!
(Note: for training production-level models, please remove the --sample option when generating tfrecords) | # specify model parameters json to have only two targets
params_file = './params.json'
with open(params_file) as params_file:
params_tutorial = json.load(params_file)
params_tutorial['model']['head_hic'][-1]['units'] =2
with open('./data/1m/params_tutorial.json','w') as params_tutorial_file:
json.dump(para... | manuscripts/akita/tutorial.ipynb | calico/basenji | apache-2.0 |
Magics!
% and %% magics
interact
embed image
embed links, youtube
link notebooks
Check out http://matplotlib.org/gallery.html select your favorite. | %%bash
for num in {1..5}
do
for infile in *;
do
echo $num $infile
done
wc $infile
done
print "hi"
!pwd
!ping google.com
this_is_magic = "Can you believe you can pass variables and strings like this?"
hey = !echo $this_is_magic
hey | notebooks/04-More_basics.ipynb | balmandhunter/jupyter-tips-and-tricks | mit |
Numpy
If you have arrays of numbers, use numpy or pandas (built on numpy) to represent the data. Tons of very fast underlying code. | x = np.arange(10000)
print x # smart printing
print x[0] # first element
print x[-1] # last element
print x[0:5] # first 5 elements (also x[:5])
print x[:] # "Everything"
print x[-5:] # last five elements
print x[-5:-2]
print x[-5:-1] # not final value -- not inclusive on right
x = np.random.randint(5, 5000, (3... | notebooks/04-More_basics.ipynb | balmandhunter/jupyter-tips-and-tricks | mit |
Matplotlib and Numpy | from numpy.random import randn
num = 50
x = np.linspace(2.5, 300, num)
y = randn(num)
plt.scatter(x, y)
y > 1
y[y > 1]
y[(y < 1) & (y > -1)]
plt.scatter(x, y, c='b', s=50)
plt.scatter(x[(y < 1) & (y > -1)], y[(y < 1) & (y > -1)], c='r', s=50)
y[~((y < 1) & (y > -1))] = 1.0
plt.scatter(x, y, c='b')
plt.scatter(x, ... | notebooks/04-More_basics.ipynb | balmandhunter/jupyter-tips-and-tricks | mit |
Grab Python version of ggplot http://ggplot.yhathq.com/ | from ggplot import ggplot, aes, geom_line, stat_smooth, geom_dotplot, geom_point
ggplot(aes(x='x', y='y'), data=dframe) + geom_point() + stat_smooth(colour='blue', span=0.2) | notebooks/04-More_basics.ipynb | balmandhunter/jupyter-tips-and-tricks | mit |
Convolutional Neural Network (CNN)
<table class="tfo-notebook-buttons" align="left">
<td>
<a target="_blank" href="https://www.tensorflow.org/tutorials/images/cnn">
<img src="https://www.tensorflow.org/images/tf_logo_32px.png" />
View on TensorFlow.org</a>
</td>
<td>
<a target="_blank" href="https... | import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt | site/en/tutorials/images/cnn.ipynb | tensorflow/docs | apache-2.0 |
Download and prepare the CIFAR10 dataset
The CIFAR10 dataset contains 60,000 color images in 10 classes, with 6,000 images in each class. The dataset is divided into 50,000 training images and 10,000 testing images. The classes are mutually exclusive and there is no overlap between them. | (train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0 | site/en/tutorials/images/cnn.ipynb | tensorflow/docs | apache-2.0 |
Verify the data
To verify that the dataset looks correct, let's plot the first 25 images from the training set and display the class name below each image: | class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i])
# The CIFAR labels happen to be ... | site/en/tutorials/images/cnn.ipynb | tensorflow/docs | apache-2.0 |
Create the convolutional base
The 6 lines of code below define the convolutional base using a common pattern: a stack of Conv2D and MaxPooling2D layers.
As input, a CNN takes tensors of shape (image_height, image_width, color_channels), ignoring the batch size. If you are new to these dimensions, color_channels refers ... | model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu')) | site/en/tutorials/images/cnn.ipynb | tensorflow/docs | apache-2.0 |
Let's display the architecture of your model so far: | model.summary() | site/en/tutorials/images/cnn.ipynb | tensorflow/docs | apache-2.0 |
Above, you can see that the output of every Conv2D and MaxPooling2D layer is a 3D tensor of shape (height, width, channels). The width and height dimensions tend to shrink as you go deeper in the network. The number of output channels for each Conv2D layer is controlled by the first argument (e.g., 32 or 64). Typically... | model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10)) | site/en/tutorials/images/cnn.ipynb | tensorflow/docs | apache-2.0 |
Here's the complete architecture of your model: | model.summary() | site/en/tutorials/images/cnn.ipynb | tensorflow/docs | apache-2.0 |
The network summary shows that (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two Dense layers.
Compile and train the model | model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels)) | site/en/tutorials/images/cnn.ipynb | tensorflow/docs | apache-2.0 |
Evaluate the model | plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(test_acc) | site/en/tutorials/images/cnn.ipynb | tensorflow/docs | apache-2.0 |
Turing machine computation
Tape
We will represent the tape as a list of tape symbols and we will represent tape symbols as Python strings.
The string ' ' represents the blank symbol.
The string '|>' represents the start symbol, which indicates the beginning of the tape.
States
We will also encode states as Python st... | def run(transitions, input, steps):
"""simulate Turing machine for the given number of steps and the given input"""
# convert input from string to list of symbols
# we use '|>' as a symbol to indicate the beginning of the tape
input = ['|>'] + list(input) + [' ']
# sanitize transitions for 'accept... | turing/turing.ipynb | dsteurer/cs4814fa15 | mit |
The following function checks that the transition functions satisfies some simple syntactic requirements (don't move to the left of the start symbol, don't remove or add start symbols, don't change state after accepting, rejecting, or halting.) | def check_transitions(transitions, states, alphabet):
transitions = sanitize_transitions(transitions)
for current in states:
for read in alphabet:
next, write, move = transitions(current, read)
# we either stay in place or move one position
# to the left or right
... | turing/turing.ipynb | dsteurer/cs4814fa15 | mit |
Examples
Copy machine
The following Turing machine copies its input, i.e., it computes the function $f(x)=xx$.
The actual implementation uses different versions of the '0' and '1' symbol (called '0-read', '0-write' and '1-read', '1-write') in the two copies of the string $x$.
We could replace those by regular '0' and ... | def transitions_copy(current, read):
if read == '|>':
return 'start', read, 1
elif current == 'start':
if 'write' not in read:
return read + '-write', read + '-read', 1
else:
return 'accept', read, 1
elif 'write' in current:
if read != ' ':
... | turing/turing.ipynb | dsteurer/cs4814fa15 | mit |
Here is the full transitions function table of the machine: | transitions_table(transitions_copy,
['start', '0-write', '1-write', 'rewind'],
['0', '1', '0-read', '1-read', '0-write', '1-write']) | turing/turing.ipynb | dsteurer/cs4814fa15 | mit |
Here is an interactive simulation of the copy Turing machine (requires that ipython notebook is run locally).
You can either click on the simulate button to view the computation during a given range of steps or you can drag the current step slider to view the configuration of the machine at a particular step. (If you c... | simulate(transitions_copy, input='10011', unary=False) | turing/turing.ipynb | dsteurer/cs4814fa15 | mit |
Power-of-2 machine
The following Turing machine determines if the input is the unary encoding of a power of 2.
Furthermore, given any string $1^n$, it outputs a string of the form ${0,1}^n2^i$, where $i$ is the largest number such that $2^i$ divides $n$. | def transitions_power(current,read):
if read == '|>':
return 'start', read, 1;
elif current == 'rewind':
return current, read, -1
elif read == 'x':
return current, read, 1
elif current == 'start':
if read != '1':
return 'reject', read, 1
else:
... | turing/turing.ipynb | dsteurer/cs4814fa15 | mit |
Here is the full transition function table of the Turing machine: | transitions_table(transitions_power,
['start', 'start-even', 'even', 'odd', 'rewind'],
['0', '1', 'x', ' ', '|>']) | turing/turing.ipynb | dsteurer/cs4814fa15 | mit |
Here is an interactive simulation of the power Turing machine (requires that ipython notebook is run locally).
You can either click on the simulate button to view the computation during a given range of steps or you can drag the current step slider to view the configuration of the machine at a particular step.
(If you ... | simulate(transitions_power, input_unary=16, step_to=200, unary=True) | turing/turing.ipynb | dsteurer/cs4814fa15 | mit |
Whitening evoked data with a noise covariance
Evoked data are loaded and then whitened using a given noise covariance
matrix. It's an excellent quality check to see if baseline signals match
the assumption of Gaussian white noise during the baseline period.
Covariance estimation and diagnostic plots are based on
:footc... | # Authors: Alexandre Gramfort <alexandre.gramfort@inria.fr>
# Denis A. Engemann <denis.engemann@gmail.com>
#
# License: BSD-3-Clause
import mne
from mne import io
from mne.datasets import sample
from mne.cov import compute_covariance
print(__doc__) | stable/_downloads/64e3b6395952064c08d4ff33d6236ff3/evoked_whitening.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Set parameters | data_path = sample.data_path()
meg_path = data_path / 'MEG' / 'sample'
raw_fname = meg_path / 'sample_audvis_filt-0-40_raw.fif'
event_fname = meg_path / 'sample_audvis_filt-0-40_raw-eve.fif'
raw = io.read_raw_fif(raw_fname, preload=True)
raw.filter(1, 40, n_jobs=1, fir_design='firwin')
raw.info['bads'] += ['MEG 2443']... | stable/_downloads/64e3b6395952064c08d4ff33d6236ff3/evoked_whitening.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Compute covariance using automated regularization | method_params = dict(diagonal_fixed=dict(mag=0.01, grad=0.01, eeg=0.01))
noise_covs = compute_covariance(epochs, tmin=None, tmax=0, method='auto',
return_estimators=True, verbose=True, n_jobs=1,
projs=None, rank=None,
method... | stable/_downloads/64e3b6395952064c08d4ff33d6236ff3/evoked_whitening.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Show the evoked data: | evoked = epochs.average()
evoked.plot(time_unit='s') # plot evoked response | stable/_downloads/64e3b6395952064c08d4ff33d6236ff3/evoked_whitening.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
We can then show whitening for our various noise covariance estimates.
Here we should look to see if baseline signals match the
assumption of Gaussian white noise. we expect values centered at
0 within 2 standard deviations for 95% of the time points.
For the Global field power we expect a value of 1. | evoked.plot_white(noise_covs, time_unit='s') | stable/_downloads/64e3b6395952064c08d4ff33d6236ff3/evoked_whitening.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Example Model
Some useful utilities
. Remember that our image data is initially N x H x W x C, where:
* N is the number of datapoints
* H is the height of each image in pixels
* W is the height of each image in pixels
* C is the number of channels (usually 3: R, G, B)
This is the right way to represent the data when we... | # clear old variables
tf.reset_default_graph()
# setup input (e.g. the data that changes every batch)
# The first dim is None, and gets sets automatically based on batch size fed in
X = tf.placeholder(tf.float32, [None, 32, 32, 3])
y = tf.placeholder(tf.int64, [None])
is_training = tf.placeholder(tf.bool)
def simple_... | cs231n/assignment/assignment2/TensorFlow.ipynb | gutouyu/cs231n | mit |
TensorFlow supports many other layer types, loss functions, and optimizers - you will experiment with these next. Here's the official API documentation for these (if any of the parameters used above were unclear, this resource will also be helpful).
Layers, Activations, Loss functions : https://www.tensorflow.org/api... | def run_model(session, predict, loss_val, Xd, yd,
epochs=1, batch_size=64, print_every=100,
training=None, plot_losses=False):
# have tensorflow compute accuracy
correct_prediction = tf.equal(tf.argmax(predict,1), y)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float3... | cs231n/assignment/assignment2/TensorFlow.ipynb | gutouyu/cs231n | mit |
Training a specific model
In this section, we're going to specify a model for you to construct. The goal here isn't to get good performance (that'll be next), but instead to get comfortable with understanding the TensorFlow documentation and configuring your own model.
Using the code provided above as guidance, and us... | # clear old variables
tf.reset_default_graph()
# define our input (e.g. the data that changes every batch)
# The first dim is None, and gets sets automatically based on batch size fed in
X = tf.placeholder(tf.float32, [None, 32, 32, 3])
y = tf.placeholder(tf.int64, [None])
is_training = tf.placeholder(tf.bool)
# defi... | cs231n/assignment/assignment2/TensorFlow.ipynb | gutouyu/cs231n | mit |
To make sure you're doing the right thing, use the following tool to check the dimensionality of your output (it should be 64 x 10, since our batches have size 64 and the output of the final affine layer should be 10, corresponding to our 10 classes): | # Now we're going to feed a random batch into the model
# and make sure the output is the right size
x = np.random.randn(64, 32, 32,3)
with tf.Session() as sess:
with tf.device("/cpu:0"): #"/cpu:0" or "/gpu:0"
tf.global_variables_initializer().run()
ans = sess.run(y_out,feed_dict={X:x,is_training:... | cs231n/assignment/assignment2/TensorFlow.ipynb | gutouyu/cs231n | mit |
You should see the following from the run above
(64, 10)
True
GPU!
Now, we're going to try and start the model under the GPU device, the rest of the code stays unchanged and all our variables and operations will be computed using accelerated code paths. However, if there is no GPU, we get a Python exception and have t... | try:
with tf.Session() as sess:
with tf.device("/gpu:0") as dev: #"/cpu:0" or "/gpu:0"
tf.global_variables_initializer().run()
ans = sess.run(y_out,feed_dict={X:x,is_training:True})
%timeit sess.run(y_out,feed_dict={X:x,is_training:True})
except tf.errors.InvalidArgument... | cs231n/assignment/assignment2/TensorFlow.ipynb | gutouyu/cs231n | mit |
You should observe that even a simple forward pass like this is significantly faster on the GPU. So for the rest of the assignment (and when you go train your models in assignment 3 and your project!), you should use GPU devices. However, with TensorFlow, the default device is a GPU if one is available, and a CPU other... | # Inputs
# y_out: is what your model computes
# y: is your TensorFlow variable with label information
# Outputs
# mean_loss: a TensorFlow variable (scalar) with numerical loss
# optimizer: a TensorFlow optimizer
# This should be ~3 lines of code!
mean_loss = None
optimizer = None
pass
# batch normalizat... | cs231n/assignment/assignment2/TensorFlow.ipynb | gutouyu/cs231n | mit |
Train the model
Below we'll create a session and train the model over one epoch. You should see a loss of 1.4 to 2.0 and an accuracy of 0.4 to 0.5. There will be some variation due to random seeds and differences in initialization | sess = tf.Session()
sess.run(tf.global_variables_initializer())
print('Training')
run_model(sess,y_out,mean_loss,X_train,y_train,1,64,100,train_step) | cs231n/assignment/assignment2/TensorFlow.ipynb | gutouyu/cs231n | mit |
Check the accuracy of the model.
Let's see the train and test code in action -- feel free to use these methods when evaluating the models you develop below. You should see a loss of 1.3 to 2.0 with an accuracy of 0.45 to 0.55. | print('Validation')
run_model(sess,y_out,mean_loss,X_val,y_val,1,64) | cs231n/assignment/assignment2/TensorFlow.ipynb | gutouyu/cs231n | mit |
Train a great model on CIFAR-10!
Now it's your job to experiment with architectures, hyperparameters, loss functions, and optimizers to train a model that achieves >= 70% accuracy on the validation set of CIFAR-10. You can use the run_model function from above.
Things you should try:
Filter size: Above we used 7x7; t... | # Feel free to play with this cell
def my_model(X,y,is_training):
pass
tf.reset_default_graph()
X = tf.placeholder(tf.float32, [None, 32, 32, 3])
y = tf.placeholder(tf.int64, [None])
is_training = tf.placeholder(tf.bool)
y_out = my_model(X,y,is_training)
mean_loss = None
optimizer = None
pass
# batch normali... | cs231n/assignment/assignment2/TensorFlow.ipynb | gutouyu/cs231n | mit |
Describe what you did here
In this cell you should also write an explanation of what you did, any additional features that you implemented, and any visualizations or graphs that you make in the process of training and evaluating your network
Tell us here
Test Set - Do this only once
Now that we've gotten a result that ... | print('Test')
run_model(sess,y_out,mean_loss,X_test,y_test,1,64) | cs231n/assignment/assignment2/TensorFlow.ipynb | gutouyu/cs231n | mit |
Chi-Nu Array Detector Angles
Author: Patricia Schuster
Date: Fall 2016/Winter 2017
Institution: University of Michigan NERS
Email: pfschus@umich.edu
What are we doing today?
Goal: Import and analyze the angles between all of the detector pairs in the Chi-Nu array.
As a reminder, this is what the Chi-Nu array looks like... | %%html
<img src="fig/setup.png",width=80%,height=80%> | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
There are 45 detectors in this array, making for 990 detector pairs: | 45*44/2 | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
In order to characterize the angular distribution of the neutrons and gamma-rays emitted in a fission interaction, we are going to analyze the data from pairs of detectors at different angles from one another.
In this notebook I am going to import the detector angle data that Matthew provided me and explore the data. ... | # Import packages
import os.path
import time
import numpy as np
np.set_printoptions(threshold=np.nan) # print entire matrices
import sys
import inspect
import matplotlib.pyplot as plt
import scipy.io as sio
from tqdm import *
import pandas as pd
import seaborn as sns
sns.set_palette('spectral')
sns.set_style(style='wh... | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Step 1: Initialize pandas DataFrame with detector pairs
The detector pair angles are stored in a file lanl_detector_angles.mat. Write a function to load it as an array and then generate a pandas DataFrame
This was done before in bicorr.build_dict_det_pair(). Replace with a pandas dataFrame.
Columns will be:
Detector 1... | help(bicorr.build_ch_lists)
chList, fcList, detList, num_dets, num_det_pairs = bicorr.build_ch_lists(print_flag = True) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Initialize dataFrame with detector channel numbers | det_df = pd.DataFrame(columns=('d1', 'd2', 'd1d2', 'angle')) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
The pandas dataFrame should have 990 entries, one for each detector pair. Generate this. | # Fill pandas dataFrame with d1, d2, and d1d2
count = 0
det_pair_chs = np.zeros(num_det_pairs,dtype=np.int)
# Loop through all detector pairs
for i1 in np.arange(0,num_dets):
det1ch = detList[i1]
for i2 in np.arange(i1+1,num_dets):
det2ch = detList[i2]
det_df.loc[count,'d1' ] = det1ch
... | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Visualize the dataFrame so far
Try using the built-in pandas.DataFrame.plot method. | ax = det_df.plot('d1','d2',kind='scatter', marker = 's',edgecolor='none',s=13, c='d1d2')
plt.xlim([0,50])
plt.ylim([0,50])
ax.set_aspect('equal')
plt.xlabel('Detector 1 channel')
plt.ylabel('Detector 2 channel')
plt.show() | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
There are some problems with displaying the labels, so instead I will use matplotlib directly. I am writing a function to generate this plot since I will likely want to view it a lot. | bicorr.plot_det_df(det_df, which=['index']) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Step 2: Fill angles column
The lanl_detector_angles.mat file is located in my measurements folder: | os.listdir('../meas_info/') | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
What does this file look like? Import the .mat file and take a look. | det2detAngle = sio.loadmat('../meas_info/lanl_detector_angles.mat')['det2detAngle']
det2detAngle.shape
plt.pcolormesh(det2detAngle, cmap = "viridis")
cbar = plt.colorbar()
cbar.set_label('Angle (degrees)')
plt.xlabel('Detector 1')
plt.ylabel('Detector 2')
plt.show() | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
The array currently is ndets x ndets with an angle at every index. This is twice as many entries as we need because pairs are repeated at (d1,d2) and (d2,d1). Loop through the pairs and store the angle to the dataframe.
Fill the 'angle' column of the DataFrame: | for pair in det_df.index:
det_df.loc[pair,'angle'] = det2detAngle[int(det_df.loc[pair,'d1'])][int(det_df.loc[pair,'d2'])]
det_df.head() | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Visualize the angular data | bicorr.plot_det_df(det_df,which=['angle']) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Verify accuracy of pandas method
Make use of git to checkout old versions.
Previously, I generated a dictionary that mapped the detector pair d1d2 index to the angle. Verify that the new method using pandas is producing the same array of angles.
Old version using channel lists, dictionary | dict_pair_to_index, dict_index_to_pair = bicorr.build_dict_det_pair()
dict_pair_to_angle = bicorr.build_dict_pair_to_angle(dict_pair_to_index,foldername='../../measurements/')
det1ch_old, det2ch_old, angle_old = bicorr.unpack_dict_pair_to_angle(dict_pair_to_angle) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
New method using pandas det_df | det_df = bicorr.load_det_df()
det1ch_new = det_df['d1'].values
det2ch_new = det_df['d2'].values
angle_new = det_df['angle'].values | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Compare the two | plt.plot([0,180],[0,180],'r')
plt.plot(angle_old, angle_new, '.k')
plt.xlabel('Angle old (degrees)')
plt.ylabel('Angle new (degrees)')
plt.title('Compare angles from new and old method')
plt.show() | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Are the angle vectors within 0.001 degrees of each other? If so, then consider the two equal. | np.sum((angle_old - angle_new) < 0.001) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Yes, consider them the same.
Step 3: Extract information from det_df
I need to exact information from det_df using the pandas methods. What are a few things I want to do? | det_df.head() | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Return rows that meet a given condition
There are two primary methods for accessing rows in the dataFrame that meet certain conditions. In our case, the conditions may be which detector pairs or which angle ranges we want to access.
Return a True/False mask indicating which entries meet the conditions
Return a pandas ... | d = 8
ind_mask = (det_df['d2'] == d)
# Get a glimpse of the mask's first five elements
ind_mask.head()
# View the mask entries that are equal to true
ind_mask[ind_mask] | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
The other method is to use the .index method to return a pandas index structure. Pull the indices from det_df using the mask. | ind = det_df.index[ind_mask]
print(ind) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Count the number of rows
Using the mask | np.sum(ind_mask) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Using the index structure | len(ind) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Extract information for a single detector
Find indices for that detector | # A single detector, may be d1 or d2
d = 8
ind_mask = (det_df['d1']==d) | (det_df['d2']==d)
ind = det_df.index[ind_mask] | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
These lines can be accessed in det_df directly. | det_df[ind_mask].head() | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Return a list of the other detector pair
Since the detector may be d1 or d2, I may need to return a list of the other pair, regardless of the order. How can I generate an array of the other detector in the pair? | det_df_this_det = det_df.loc[ind,['d1','d2']]
det_df_this_det.head() | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
This is a really stupid method, but I can multiply the two detectors together and then divide by 8 to divide out that channel. | det_df_this_det['dN'] = det_df_this_det.d1 * det_df_this_det.d2 / d
det_df_this_det.head()
plt.plot(det_df_this_det['dN'],'.k')
plt.xlabel('Array in dataFrame')
plt.ylabel('dN (other channel)')
plt.title('Other channel for pairs including ch '+str(d))
plt.show() | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Return the angles | plt.plot(det_df.loc[ind,'angle'],'.k')
plt.xlabel('Index')
plt.ylabel('Angle between pairs')
plt.title('Angle for pairs including ch '+ str(d))
plt.show()
plt.plot(det_df_this_det['dN'],det_df.loc[ind,'angle'],'.k')
plt.axvline(d,color='r')
plt.xlabel('dN (other channel)')
plt.ylabel('Angle between pairs')
plt.title('... | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Extract information for a given pair
Find indices for that pair | d1 = 1
d2 = 4
if d2 < d1:
print('Warning: d2 < d1. Channels inverted')
ind_mask = (det_df['d1']==d1) & (det_df['d2']==d2)
ind = det_df.index[ind_mask]
det_df[ind_mask]
det_df[ind_mask]['angle'] | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
I will write a function that returns the index. | bicorr.d1d2_index(det_df,4,1) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Compare to speed of dictionary
For a large number of detector pairs, which is faster for retrieving the indices? | bicorr_data = bicorr.load_bicorr(bicorr_path = '../2017_01_09_pfs_build_bicorr_hist_master/1/bicorr1')
bicorr_data.shape
det_df = bicorr.load_det_df()
dict_pair_to_index, dict_index_to_pair = bicorr.build_dict_det_pair()
d1 = 4
d2 = 8
print(dict_pair_to_index[100*d1+d2])
print(bicorr.d1d2_index(det_df,d1,d2)) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Loop through bicorr_data and generate the index for all pairs.
Using the dictionary method | start_time = time.time()
for i in tqdm(np.arange(bicorr_data.size),ascii=True):
d1 = bicorr_data[i]['det1ch']
d2 = bicorr_data[i]['det2ch']
index = dict_pair_to_index[100*d1+d2]
print(time.time()-start_time) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Using the pandas dataFrame method | start_time = time.time()
for i in tqdm(np.arange(bicorr_data.size),ascii=True):
d1 = bicorr_data[i]['det1ch']
d2 = bicorr_data[i]['det2ch']
index = bicorr.d1d2_index(det_df,d1,d2)
print(time.time()-start_time) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
I'm not going to run this because tqdm says it will take approximately 24 minutes. So instead I should go with the dict method. But I would like to produce the dictionary from the pandas array directly.
Produce dictionaries from det_df
Instead of relying on dict_pair_to_index all the time, I will generate it on the ... | det_df.index
det_df.head()
det_df[['d1d2','d2']].head()
dict_index_to_pair = det_df['d1d2'].to_dict()
dict_pair_to_index = {v: k for k, v in dict_index_to_pair.items()}
dict_pair_to_angle = pd.Series(det_df['angle'].values,index=det_df['d1d2']).to_dict() | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Functionalize these dictionaries so I can produce them on the fly. | help(bicorr.build_dict_det_pair)
dict_pair_to_index, dict_index_to_pair, dict_pair_to_angle = bicorr.build_dict_det_pair(det_df) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Instructions: Save, load det_df file
I'm going to store the dataFrame using to_pickle. At this point, it only contains information on the pairs and angles. No bin column has been added. | det_df.to_pickle('../meas_info/det_df_pairs_angles.pkl')
det_df.to_csv('../meas_info/det_df_pairs_angles.csv',index = False) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Revive the dataFrame from the .pkl file. Write a function to do this automatically. Option to display plots. | help(bicorr.load_det_df)
det_df = bicorr.load_det_df()
det_df.head()
det_df = bicorr.load_det_df()
bicorr.plot_det_df(det_df, show_flag = True, which = ['index'])
bicorr.plot_det_df(det_df, show_flag = True, which = ['angle']) | methods/build_det_df_angles_pairs.ipynb | pfschus/fission_bicorrelation | mit |
Let us begin by developing a convenient method for displaying images in our notebooks. | img = sitk.GaussianSource(size=[64] * 2)
plt.imshow(sitk.GetArrayViewFromImage(img))
img = sitk.GaborSource(size=[64] * 2, frequency=0.03)
plt.imshow(sitk.GetArrayViewFromImage(img))
def myshow(img):
nda = sitk.GetArrayViewFromImage(img)
plt.imshow(nda)
myshow(img) | Python/02_Pythonic_Image.ipynb | InsightSoftwareConsortium/SimpleITK-Notebooks | apache-2.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.