markdown
stringlengths
0
1.02M
code
stringlengths
0
832k
output
stringlengths
0
1.02M
license
stringlengths
3
36
path
stringlengths
6
265
repo_name
stringlengths
6
127
Provided is a buggy for loop that tries to accumulate some values out of some dictionaries. Insert a try/except so that the code passes.
di = [{"Puppies": 17, 'Kittens': 9, "Birds": 23, 'Fish': 90, "Hamsters": 49}, {"Puppies": 23, "Birds": 29, "Fish": 20, "Mice": 20, "Snakes": 7}, {"Fish": 203, "Hamsters": 93, "Snakes": 25, "Kittens": 89}, {"Birds": 20, "Puppies": 90, "Snakes": 21, "Fish": 10, "Kittens": 67}] total = 0 for diction in di: try: ...
_____no_output_____
MIT
Class ,Constractor,Inheritance,overriding,Exception.ipynb
Ks226/upgard_code
The code below takes the list of country, country, and searches to see if it is in the dictionary gold which shows some countries who won gold during the Olympics. However, this code currently does not work. Correctly add try/except clause in the code so that it will correctly populate the list, country_gold, with eith...
gold = {"US":46, "Fiji":1, "Great Britain":27, "Cuba":5, "Thailand":2, "China":26, "France":10} country = ["Fiji", "Chile", "Mexico", "France", "Norway", "US"] country_gold = [] print(gold.keys()) for x in country: try: x in gold.keys() country_gold.append(gold[x]) except KeyError: coun...
_____no_output_____
MIT
Class ,Constractor,Inheritance,overriding,Exception.ipynb
Ks226/upgard_code
The list, numb, contains integers. Write code that populates the list remainder with the remainder of 36 divided by each number in numb. For example, the first element should be 0, because 36/6 has no remainder. If there is an error, have the string “Error” appear in the remainder.
numb = [6, 0, 36, 8, 2, 36, 0, 12, 60, 0, 45, 0, 3, 23] remainder = [] for i in numb: if (i == 0): remainder.append("Error") elif (36 % i): remainder.append(36 % i) elif (36 % i == 0): remainder.append(0) print(remainder)
_____no_output_____
MIT
Class ,Constractor,Inheritance,overriding,Exception.ipynb
Ks226/upgard_code
Provided is buggy code, insert a try/except so that the code passes.
lst = [2,4,10,42,12,0,4,7,21,4,83,8,5,6,8,234,5,6,523,42,34,0,234,1,435,465,56,7,3,43,23] lst_three = [] for num in lst: try: if 3 % num == 0: lst_three.append(num) except ZeroDivisionError: pass print(lst_three)
_____no_output_____
MIT
Class ,Constractor,Inheritance,overriding,Exception.ipynb
Ks226/upgard_code
Write code so that the buggy code provided works using a try/except. When the codes does not work in the try, have it append to the list attempt the string “Error”.
full_lst = ["ab", 'cde', 'fgh', 'i', 'jkml', 'nop', 'qr', 's', 'tv', 'wxy', 'z'] attempt = [] for elem in full_lst: try: attempt.append(elem[1]) except: attempt.append("Error")
_____no_output_____
MIT
Class ,Constractor,Inheritance,overriding,Exception.ipynb
Ks226/upgard_code
The following code tries to append the third element of each list in conts to the new list third_countries. Currently, the code does not work. Add a try/except clause so the code runs without errors, and the string ‘Continent does not have 3 countries’ is appended to countries instead of producing an error.
conts = [['Spain', 'France', 'Greece', 'Portugal', 'Romania', 'Germany'], ['USA', 'Mexico', 'Canada'], ['Japan', 'China', 'Korea', 'Vietnam', 'Cambodia'], ['Argentina', 'Chile', 'Brazil', 'Ecuador', 'Uruguay', 'Venezuela'], ['Australia'], ['Zimbabwe', 'Morocco', 'Kenya', 'Ethiopa', 'South Africa'], ['Antarctica']] th...
_____no_output_____
MIT
Class ,Constractor,Inheritance,overriding,Exception.ipynb
Ks226/upgard_code
The buggy code below prints out the value of the sport in the list sport. Use try/except so that the code will run properly. If the sport is not in the dictionary, ppl_play, add it in with the value of 1.
sport = ["hockey", "basketball", "soccer", "tennis", "football", "baseball"] ppl_play = {"hockey":4, "soccer": 10, "football": 15, "tennis": 8} for x in sport: try: print(ppl_play[x]) except KeyError: ppl_play[x] = 1
_____no_output_____
MIT
Class ,Constractor,Inheritance,overriding,Exception.ipynb
Ks226/upgard_code
Provided is a buggy for loop that tries to accumulate some values out of some dictionaries. Insert a try/except so that the code passes. If the key is not there, initialize it in the dictionary and set the value to zero.
di = [{"Puppies": 17, 'Kittens': 9, "Birds": 23, 'Fish': 90, "Hamsters": 49}, {"Puppies": 23, "Birds": 29, "Fish": 20, "Mice": 20, "Snakes": 7}, {"Fish": 203, "Hamsters": 93, "Snakes": 25, "Kittens": 89}, {"Birds": 20, "Puppies": 90, "Snakes": 21, "Fish": 10, "Kittens": 67}] total = 0 for diction in di: try: ...
_____no_output_____
MIT
Class ,Constractor,Inheritance,overriding,Exception.ipynb
Ks226/upgard_code
Disaggregation
from __future__ import print_function, division import time from matplotlib import rcParams import matplotlib.pyplot as plt import pandas as pd import numpy as np from six import iteritems from nilmtk import DataSet, TimeFrame, MeterGroup, HDFDataStore from nilmtk.disaggregate import CombinatorialOptimisation, FHMM i...
_____no_output_____
Apache-2.0
docs/manual/user_guide/disaggregation_and_metrics.ipynb
Ming-er/nilmtk
Dividing data into train and test set
train = DataSet('/data/redd.h5') test = DataSet('/data/redd.h5')
_____no_output_____
Apache-2.0
docs/manual/user_guide/disaggregation_and_metrics.ipynb
Ming-er/nilmtk
Let us use building 1 for demo purposes
building = 1
_____no_output_____
Apache-2.0
docs/manual/user_guide/disaggregation_and_metrics.ipynb
Ming-er/nilmtk
Let's split data at April 30th
train.set_window(end="2011-04-30") test.set_window(start="2011-04-30") train_elec = train.buildings[1].elec test_elec = test.buildings[1].elec
_____no_output_____
Apache-2.0
docs/manual/user_guide/disaggregation_and_metrics.ipynb
Ming-er/nilmtk
Visualizing the data
train_elec.plot() test_elec.mains().plot()
_____no_output_____
Apache-2.0
docs/manual/user_guide/disaggregation_and_metrics.ipynb
Ming-er/nilmtk
REDD data set has got appliance level data sampled every 3 or 4 seconds and mains data sampled every 1 second. Let us verify the same.
fridge_meter = train_elec['fridge'] fridge_df = next(fridge_meter.load()) fridge_df.head() mains = train_elec.mains() mains_df = next(mains.load()) mains_df.head()
_____no_output_____
Apache-2.0
docs/manual/user_guide/disaggregation_and_metrics.ipynb
Ming-er/nilmtk
Since, both of these are sampled at different frequencies, we will downsample both to 1 minute resolution. We will also select the top-5 appliances in terms of energy consumption and use them for training our FHMM and CO models. Selecting top-5 appliances
top_5_train_elec = train_elec.submeters().select_top_k(k=5) top_5_train_elec
_____no_output_____
Apache-2.0
docs/manual/user_guide/disaggregation_and_metrics.ipynb
Ming-er/nilmtk
Training and disaggregation A function to disaggregate the mains data to constituent appliances and return the predictions
def predict(clf, test_elec, sample_period, timezone): pred = {} gt= {} # "ac_type" varies according to the dataset used. # Make sure to use the correct ac_type before using the default parameters in this code. for i, chunk in enumerate(test_elec.mains().load(physical_quantity = 'power', ac...
_____no_output_____
Apache-2.0
docs/manual/user_guide/disaggregation_and_metrics.ipynb
Ming-er/nilmtk
Train using 2 benchmarking algorithms - Combinatorial Optimisation (CO) and Factorial Hidden Markov Model (FHMM)
classifiers = {'CO':CombinatorialOptimisation(), 'FHMM':FHMM()} predictions = {} sample_period = 120 for clf_name, clf in classifiers.items(): print("*"*20) print(clf_name) print("*" *20) start = time.time() # Note that we have given the sample period to downsample the data to 1 minute. # If in...
******************** CO ******************** Training model for submeter 'ElecMeter(instance=11, building=1, dataset='REDD', appliances=[Appliance(type='microwave', instance=1)])' Training model for submeter 'ElecMeter(instance=8, building=1, dataset='REDD', appliances=[Appliance(type='sockets', instance=2)])' Training...
Apache-2.0
docs/manual/user_guide/disaggregation_and_metrics.ipynb
Ming-er/nilmtk
Using prettier labels!
appliance_labels = [m.label() for m in gt.columns.values] gt.columns = appliance_labels predictions['CO'].columns = appliance_labels predictions['FHMM'].columns = appliance_labels
_____no_output_____
Apache-2.0
docs/manual/user_guide/disaggregation_and_metrics.ipynb
Ming-er/nilmtk
Taking a look at the ground truth of top 5 appliance power consumption
gt.head() predictions['CO'].head() predictions['FHMM'].head()
_____no_output_____
Apache-2.0
docs/manual/user_guide/disaggregation_and_metrics.ipynb
Ming-er/nilmtk
Plotting the predictions against the actual usage
predictions['CO']['Fridge'].head(300).plot(label="Pred") gt['Fridge'].head(300).plot(label="GT") plt.legend() predictions['FHMM']['Fridge'].head(300).plot(label="Pred") gt['Fridge'].head(300).plot(label="GT") plt.legend()
_____no_output_____
Apache-2.0
docs/manual/user_guide/disaggregation_and_metrics.ipynb
Ming-er/nilmtk
Comparing NILM algorithms (CO vs FHMM) `nilmtk.utils.compute_rmse` is an extended of the following, handling both missing values and labels better:```pythondef compute_rmse(gt, pred): from sklearn.metrics import mean_squared_error rms_error = {} for appliance in gt.columns: rms_error[appliance] = np.sq...
? nilmtk.utils.compute_rmse rmse = {} for clf_name in classifiers.keys(): rmse[clf_name] = nilmtk.utils.compute_rmse(gt, predictions[clf_name]) rmse = pd.DataFrame(rmse) rmse
_____no_output_____
Apache-2.0
docs/manual/user_guide/disaggregation_and_metrics.ipynb
Ming-er/nilmtk
Write and Save Files in PythonEstimated time needed: **25** minutes ObjectivesAfter completing this lab you will be able to:* Write to files using Python libraries Table of Contents Writing Files Appending Files Additional File modes Copy a File Writing Files We can open a fil...
# Write line to file exmp2 = '/resources/data/Example2.txt' with open(exmp2, 'w') as writefile: writefile.write("This is line A")
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
We can read the file to see if it worked:
# Read file with open(exmp2, 'r') as testwritefile: print(testwritefile.read())
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
We can write multiple lines:
# Write lines to file with open(exmp2, 'w') as writefile: writefile.write("This is line A\n") writefile.write("This is line B\n")
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
The method .write() works similar to the method .readline(), except instead of reading a new line it writes a new line. The process is illustrated in the figure. The different colour coding of the grid represents a new line added to the file after each method call. You can check the file to see if your results are cor...
# Check whether write to file with open(exmp2, 'r') as testwritefile: print(testwritefile.read())
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
We write a list to a **.txt** file as follows:
# Sample list of text Lines = ["This is line A\n", "This is line B\n", "This is line C\n"] Lines # Write the strings in the list to text file with open('Example2.txt', 'w') as writefile: for line in Lines: print(line) writefile.write(line)
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
We can verify the file is written by reading it and printing out the values:
# Verify if writing to file is successfully executed with open('Example2.txt', 'r') as testwritefile: print(testwritefile.read())
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
However, note that setting the mode to **w** overwrites all the existing data in the file.
with open('Example2.txt', 'w') as writefile: writefile.write("Overwrite\n") with open('Example2.txt', 'r') as testwritefile: print(testwritefile.read())
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
Appending Files We can write to files without losing any of the existing data as follows by setting the mode argument to append: **a**. you can append a new line as follows:
# Write a new line to text file with open('Example2.txt', 'a') as testwritefile: testwritefile.write("This is line C\n") testwritefile.write("This is line D\n") testwritefile.write("This is line E\n")
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
You can verify the file has changed by running the following cell:
# Verify if the new line is in the text file with open('Example2.txt', 'r') as testwritefile: print(testwritefile.read())
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
Additional modes It's fairly ineffecient to open the file in **a** or **w** and then reopening it in **r** to read any lines. Luckily we can access the file in the following modes:* **r+** : Reading and writing. Cannot truncate the file.* **w+** : Writing and reading. Truncates the file.* **a+** : Appending and ...
with open('Example2.txt', 'a+') as testwritefile: testwritefile.write("This is line E\n") print(testwritefile.read())
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
There were no errors but read() also did not output anything. This is because of our location in the file. Most of the file methods we've looked at work in a certain location in the file. .write() writes at a certain location in the file. .read() reads at a certain location in the file and so on. You can think of this...
with open('Example2.txt', 'a+') as testwritefile: print("Initial Location: {}".format(testwritefile.tell())) data = testwritefile.read() if (not data): #empty strings return false in python print('Read nothing') else: print(testwritefile.read()) testwrite...
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
Finally, a note on the difference between **w+** and **r+**. Both of these modes allow access to read and write methods, however, opening a file in **w+** overwrites it and deletes all pre-existing data. To work with a file on existing data, use **r+** and **a+**. While using **r+**, it can be useful to add a .truncate...
with open('Example2.txt', 'r+') as testwritefile: data = testwritefile.readlines() testwritefile.seek(0,0) #write at beginning of file testwritefile.write("Line 1" + "\n") testwritefile.write("Line 2" + "\n") testwritefile.write("Line 3" + "\n") testwritefile.write("finished\n") #Uncomme...
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
Copy a File Let's copy the file **Example2.txt** to the file **Example3.txt**:
# Copy file to another with open('Example2.txt','r') as readfile: with open('Example3.txt','w') as writefile: for line in readfile: writefile.write(line)
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
We can read the file to see if everything works:
# Verify if the copy is successfully executed with open('Example3.txt','r') as testwritefile: print(testwritefile.read())
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
After reading files, we can also write data into files and save them in different file formats like **.txt, .csv, .xls (for excel files) etc**. You will come across these in further examples Now go to the directory to ensure the **.txt** file exists and contains the summary data that we wrote. Exercise Your local un...
#Run this prior to starting the exercise from random import randint as rnd memReg = 'members.txt' exReg = 'inactive.txt' fee =('yes','no') def genFiles(current,old): with open(current,'w+') as writefile: writefile.write('Membership No Date Joined Active \n') data = "{:^13} {:<11} {:<6}\n" ...
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
Start your solution below:
def cleanFiles(currentMem,exMem): ''' currentMem: File containing list of current members exMem: File containing list of old members Removes all rows from currentMem containing 'no' and appends them to exMem ''' pass # Code to help you see the files # Leave as is memReg = 'members....
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
Run the following to verify your code:
def testMsg(passed): if passed: return 'Test Passed' else : return 'Test Failed' testWrite = "testWrite.txt" testAppend = "testAppend.txt" passed = True genFiles(testWrite,testAppend) with open(testWrite,'r') as file: ogWrite = file.readlines() with open(testAppend,'r') as file: ogApp...
_____no_output_____
MIT
Python for Data Science, AI & Development/4. Working with Data in Python/Writing Files with Open.ipynb
aqafridi/Data-Analytics
Predict house price in America> Analyse and predict on house price dataset.- toc: true - badges: true- comments: true- categories: [self-taught]- image: images/chart-preview.png Introduction
import pandas as pd pd.options.display.max_columns = 999 import numpy as np import matplotlib.pyplot as plt from sklearn.model_selection import KFold from sklearn.metrics import mean_squared_error from sklearn import linear_model from sklearn.model_selection import KFold df = pd.read_csv("AmesHousing.tsv", delimiter="...
_____no_output_____
Apache-2.0
_notebooks/2017-09-15-predict-house-price.ipynb
phucnsp/blog
Feature Engineering Handle missing values: All columns: Drop any with 5% or more missing values for now. Text columns: Drop any with 1 or more missing values for now. Numerical columns: For columns with missing values, fill in with the most common value in that column 1: All columns: Drop any with 5% or more m...
## Series object: column name -> number of missing values num_missing = df.isnull().sum() # Filter Series to columns containing >5% missing values drop_missing_cols = num_missing[(num_missing > len(df)/20)].sort_values() # Drop those columns from the data frame. Note the use of the .index accessor df = df.drop(drop_mi...
_____no_output_____
Apache-2.0
_notebooks/2017-09-15-predict-house-price.ipynb
phucnsp/blog
Drop columns that: a. that aren't useful for ML b. leak data about the final sale
## Drop columns that aren't useful for ML df = df.drop(["PID", "Order"], axis=1) ## Drop columns that leak info about the final sale df = df.drop(["Mo Sold", "Sale Condition", "Sale Type", "Yr Sold"], axis=1)
_____no_output_____
Apache-2.0
_notebooks/2017-09-15-predict-house-price.ipynb
phucnsp/blog
Let's update transform_features()
def transform_features(df): num_missing = df.isnull().sum() drop_missing_cols = num_missing[(num_missing > len(df)/20)].sort_values() df = df.drop(drop_missing_cols.index, axis=1) text_mv_counts = df.select_dtypes(include=['object']).isnull().sum().sort_values(ascending=False) drop_missing_cols...
_____no_output_____
Apache-2.0
_notebooks/2017-09-15-predict-house-price.ipynb
phucnsp/blog
Feature Selection
numerical_df = transform_df.select_dtypes(include=['int', 'float']) numerical_df abs_corr_coeffs = numerical_df.corr()['SalePrice'].abs().sort_values() abs_corr_coeffs ## Let's only keep columns with a correlation coefficient of larger than 0.4 (arbitrary, worth experimenting later!) abs_corr_coeffs[abs_corr_coeffs > 0...
_____no_output_____
Apache-2.0
_notebooks/2017-09-15-predict-house-price.ipynb
phucnsp/blog
Which categorical columns should we keep?
## Create a list of column names from documentation that are *meant* to be categorical nominal_features = ["PID", "MS SubClass", "MS Zoning", "Street", "Alley", "Land Contour", "Lot Config", "Neighborhood", "Condition 1", "Condition 2", "Bldg Type", "House Style", "Roof Style", "Roof Matl", "Exteri...
_____no_output_____
Apache-2.0
_notebooks/2017-09-15-predict-house-price.ipynb
phucnsp/blog
Which columns are currently numerical but need to be encoded as categorical instead (because the numbers don't have any semantic meaning)? If a categorical column has hundreds of unique values (or categories), should we keep it? When we dummy code this column, hundreds of columns will need to be added back to the data...
## Which categorical columns have we still carried with us? We'll test tehse transform_cat_cols = [] for col in nominal_features: if col in transform_df.columns: transform_cat_cols.append(col) ## How many unique values in each categorical column? uniqueness_counts = transform_df[transform_cat_cols].apply(...
_____no_output_____
Apache-2.0
_notebooks/2017-09-15-predict-house-price.ipynb
phucnsp/blog
Update select_features()
def transform_features(df): num_missing = df.isnull().sum() drop_missing_cols = num_missing[(num_missing > len(df)/20)].sort_values() df = df.drop(drop_missing_cols.index, axis=1) text_mv_counts = df.select_dtypes(include=['object']).isnull().sum().sort_values(ascending=False) drop_missing_cols...
[25761.875549560471, 36527.812968130842, 24956.485193881424, 28486.738135675929]
Apache-2.0
_notebooks/2017-09-15-predict-house-price.ipynb
phucnsp/blog
FastPitch: Voice Modification with Pre-defined Pitch Transformations The [FastPitch](https://arxiv.org/abs/2006.06873) model is based on the [FastSpeech](https://arxiv.org/abs/1905.09263) model. Similarly to [FastSpeech2](https://arxiv.org/abs/2006.04558), which has been developed concurrently, it learns to predict th...
import os assert os.getcwd().split('/')[-1] == 'notebooks'
_____no_output_____
MIT
SpeechSynthesis/FastPitch/notebooks/FastPitch_voice_modification.ipynb
eba472/mongolian_tts
Generate audio samples Training a FastPitch model from scrath takes 3 to 27 hours depending on the type and number of GPUs, performance numbers can be found in Section "Training performance results" in `README.md`. Therefore, to save the time of running this notebook, we recommend to download the pretrained FastPitch ...
! mkdir -p output ! MODEL_DIR='../pretrained_models' ../scripts/download_fastpitch.sh ! MODEL_DIR='../pretrained_models' ../scripts/download_waveglow.sh
_____no_output_____
MIT
SpeechSynthesis/FastPitch/notebooks/FastPitch_voice_modification.ipynb
eba472/mongolian_tts
You can perform inference using the respective checkpoints that are passed as `--fastpitch` and `--waveglow` arguments. Next, you will use FastPitch model to generate audio samples for input text, including the basic version and the variations i npace, fade out, and pitch transforms, etc.
import IPython # store paths in aux variables fastp = '../pretrained_models/fastpitch/nvidia_fastpitch_200518.pt' waveg = '../pretrained_models/waveglow/waveglow_1076430_14000_amp.pt' flags = f'--cuda --fastpitch {fastp} --waveglow {waveg} --wn-channels 256'
_____no_output_____
MIT
SpeechSynthesis/FastPitch/notebooks/FastPitch_voice_modification.ipynb
eba472/mongolian_tts
1. Basic speech synthesis You need to create an input file with some text, or just input the text in the below cell:
%%writefile text.txt The forms of printed letters should be beautiful, and that their arrangement on the page should be reasonable and a help to the shapeliness of the letters themselves.
_____no_output_____
MIT
SpeechSynthesis/FastPitch/notebooks/FastPitch_voice_modification.ipynb
eba472/mongolian_tts
Run the script below to generate audio from the input text file:
# basic systhesis !python ../inference.py {flags} -i text.txt -o output/original > /dev/null IPython.display.Audio("output/original/audio_0.wav")
_____no_output_____
MIT
SpeechSynthesis/FastPitch/notebooks/FastPitch_voice_modification.ipynb
eba472/mongolian_tts
2. Add variations to the generated speech FastPitch allows us to exert additional control over the synthesized utterances, the key parameters are the pace, fade out, and pitch transforms in particular. 2.1 Pace FastPitch allows you to linearly adjust the pace of synthesized speech, similar to [FastSpeech](https://arx...
# Change the pace of speech to double with --pace 0.5 # (1.0 = unchanged) !python ../inference.py {flags} -i text.txt -o output/pace --pace 0.5 > /dev/null IPython.display.Audio("output/pace/audio_0.wav")
_____no_output_____
MIT
SpeechSynthesis/FastPitch/notebooks/FastPitch_voice_modification.ipynb
eba472/mongolian_tts
2.2 Raise or lower the pitch For every input character, the model predicts a pitch cue - an average pitch over a character in Hz. Pitch can be adjusted by transforming those pitch cues. A few simple examples are provided below.
# Raise/lower pitch by --pitch-transform-shift <Hz> # Synthesize with a -50 Hz shift !python ../inference.py {flags} -i text.txt -o output/riselowpitch --pitch-transform-shift -50 > /dev/null IPython.display.Audio("output/riselowpitch/audio_0.wav")
_____no_output_____
MIT
SpeechSynthesis/FastPitch/notebooks/FastPitch_voice_modification.ipynb
eba472/mongolian_tts
2.3 Flatten the pitch
# Flatten the pitch to a constant value with --pitch-transform-flatten !python ../inference.py {flags} -i text.txt -o output/flattenpitch --pitch-transform-flatten > /dev/null IPython.display.Audio("output/flattenpitch/audio_0.wav")
_____no_output_____
MIT
SpeechSynthesis/FastPitch/notebooks/FastPitch_voice_modification.ipynb
eba472/mongolian_tts
2.4 Invert the pitch
# Invert pitch wrt. to the mean pitch with --pitch-transform-invert !python ../inference.py {flags} -i text.txt -o output/invertpitch --pitch-transform-invert > /dev/null IPython.display.Audio("output/invertpitch/audio_0.wav")
_____no_output_____
MIT
SpeechSynthesis/FastPitch/notebooks/FastPitch_voice_modification.ipynb
eba472/mongolian_tts
2.5 Amplify the pitch
# Amplify pitch wrt. to the mean pitch with --pitch-transform-amplify 2.0 # values in the (1.0, 3.0) range work the best !python ../inference.py {flags} -i text.txt -o output/amplifypitch --pitch-transform-amplify 2.0 > /dev/null IPython.display.Audio("output/amplifypitch/audio_0.wav")
_____no_output_____
MIT
SpeechSynthesis/FastPitch/notebooks/FastPitch_voice_modification.ipynb
eba472/mongolian_tts
2.6 Combine the flags The flags can be combined. You can find all the available options by calling python inference.py --help.
!python ../inference.py --help
_____no_output_____
MIT
SpeechSynthesis/FastPitch/notebooks/FastPitch_voice_modification.ipynb
eba472/mongolian_tts
Below example shows how to generate an audio with a combination of the flags --pace --pitch-transform-flatten --pitch-transform-shift --pitch-transform-invert --pitch-transform-amplify
# Dobuble the speed and combine multiple transformations !python ../inference.py {flags} -i text.txt -o output/combine \ --pace 2.0 --pitch-transform-flatten --pitch-transform-shift 50 \ --pitch-transform-invert --pitch-transform-amplify 1.5 > /dev/null IPython.display.Audio("output/combine/audio_0.wav")
_____no_output_____
MIT
SpeechSynthesis/FastPitch/notebooks/FastPitch_voice_modification.ipynb
eba472/mongolian_tts
3. Inference performance benchmark
# Benchmark inference using AMP !python ../inference.py {flags} \ --include-warmup --batch-size 8 --repeats 100 --torchscript --amp \ -i ../phrases/benchmark_8_128.tsv -o output/benchmark
_____no_output_____
MIT
SpeechSynthesis/FastPitch/notebooks/FastPitch_voice_modification.ipynb
eba472/mongolian_tts
k-NN, Function Expectation, Density Estimation
from experiment_framework.helpers import build_convergence_curve_pipeline from empirical_privacy.one_bit_sum import GenSampleOneBitSum # from empirical_privacy import one_bit_sum_joblib as one_bit_sum # from empirical_privacy import lsdd # reload(one_bit_sum) def B_pmf(k, n, p): return binom(n, p).pmf(k) def B0_p...
/opt/conda/lib/python3.6/site-packages/seaborn/timeseries.py:183: UserWarning: The `tsplot` function is deprecated and will be removed in a future release. Please update your code to use the new `lineplot` function. warnings.warn(msg, UserWarning) /opt/conda/lib/python3.6/site-packages/seaborn/timeseries.py:183: User...
MIT
Notebooks/development/1-bit sum.ipynb
maksimt/empirical_privacy
Repeat the above using joblib to make sure the luigi implementation is correct
from math import ceil, log one_bit_sum.n_jobs=1 N = int(ceil(log(n_max) / log(2))) N_samples = np.logspace(4,N,num=N-3, base=2).astype(np.int) ax = plt.figure(figsize=(10,5)) ax = plt.gca() AlgArg = namedtuple('AlgArg', field_names=['f_handle', 'f_kwargs']) algs = [ AlgArg(one_bit_sum.get_knn_correctness_rate_cach...
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.5s remaining: 0.0s [Parallel(n_jobs=1)]: Done 2 out of 2 | elapsed: 1.3s remaining: 0.0s [Parallel(n_jobs=1)]: Done 3 out of 3 | elapsed: 2.0s remaining: 0.0s [Parallel(n_jobs=1)]: Done 3 out of 3 | elapsed: 2.0s finished /opt/conda...
MIT
Notebooks/development/1-bit sum.ipynb
maksimt/empirical_privacy
Timing GenSamples Without halving: 7.5secWith halving: 8.1sec (i.e. not much overhead)
from luigi_utils.sampling_framework import GenSamples import time class GS(GenSamples(GenSampleOneBitSum, generate_in_batch=True)): pass GSi = GS(dataset_settings = ccc_kwargs['dataset_settings'], random_seed='0', generate_positive_samples=True, num_samples=2**15) start = time.time() luigi.build([GSi], loca...
_____no_output_____
MIT
Notebooks/development/1-bit sum.ipynb
maksimt/empirical_privacy
More exp
def get_fit(res, N_samples): ntri, nsamp = res.shape sqrt2 = np.sqrt(2) Xlsq = np.hstack((np.ones((nsamp,1)), sqrt2/(N_samples.astype(np.float)**0.25)[:, np.newaxis])) y = 1.0 - res.reshape((nsamp*ntri, 1)) Xlsq = reduce(lambda x,y: np.vstack((x,y)), [Xlsq]*ntri) coef ...
_____no_output_____
MIT
Notebooks/development/1-bit sum.ipynb
maksimt/empirical_privacy
Uniforml distributed random variables $$g_0 = U[0,0.5]+\sum_{i=1}^{n-1} U[0,1]$$$$g_1 = U[0.5,1.0]+\sum_{i=1}^{n-1} U[0,1]$$ Let $\mu_n = \frac{n-1}{2}$ and $\sigma_n = \sqrt{\frac{n-0.75}{12}}$By the CLT $g_0\sim N(\mu_n+0.25, \sigma_n)$ and $g_1\sim N(\mu_n+0.75, \sigma_n)$.
from math import sqrt n=3 x = np.linspace(n/2.0-sqrt(n), n/2.0+sqrt(n)) sigma = sqrt((n-0.75)/12.0) sqrt2 = sqrt(2) mu = (n-1.0)/2 def g0_pdf(x): return norm.pdf(x, loc=mu+0.25, scale=sigma) def g1_pdf(x): return norm.pdf(x, loc=mu+0.75, scale=sigma) def d_pdf(x): return norm.pdf(x, loc=-0.5, scale=sigma*s...
_____no_output_____
MIT
Notebooks/development/1-bit sum.ipynb
maksimt/empirical_privacy
Comparing different numerical integration techniques
to_int = [f0,f1] print 'Quad' # for (i,f) in enumerate(to_int): # intr = integrate.quad(f, -np.inf, np.inf) # print 'func={0} err={1:.3e}'.format(i, abs(1-intr[0])) g_int(n)-integrate.quad(lambda x: np.abs(f0(x)-f1(x)), -np.inf, np.inf)[0] to_int = [f0,f1] print 'Quad' g_int(n)-integrate.quad(lambda x: np.abs...
_____no_output_____
MIT
Notebooks/development/1-bit sum.ipynb
maksimt/empirical_privacy
Sympy-based analysis
import sympy as sy n,k = sy.symbols('n k', integer=True) #k = sy.Integer(k) p = sy.symbols('p', real=True) q=1-p def binom_pmf(k, n, p): return sy.binomial(n,k)*(p**k)*(q**(n-k)) def binom_cdf(x, n, p): return sy.Sum([binom_pmf(j, n, p) for j in sy.Range(x+1)]) B0 = binom_pmf(k, n-1, p) B1 = binom_pmf(k-1, n-...
_____no_output_____
MIT
Notebooks/development/1-bit sum.ipynb
maksimt/empirical_privacy
Regression Week 2: Multiple Regression (gradient descent) In the first notebook we explored multiple regression using graphlab create. Now we will use graphlab along with numpy to solve for the regression weights with gradient descent.In this notebook we will cover estimating multiple regression weights via gradient d...
import graphlab
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
Load in house sales dataDataset is from house sales in King County, the region where the city of Seattle, WA is located.
sales = graphlab.SFrame('kc_house_data.gl/')
[INFO] 1449884188 : INFO: (initialize_globals_from_environment:282): Setting configuration variable GRAPHLAB_FILEIO_ALTERNATIVE_SSL_CERT_FILE to /home/nitin/anaconda/lib/python2.7/site-packages/certifi/cacert.pem 1449884188 : INFO: (initialize_globals_from_environment:282): Setting configurati...
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
If we want to do any "feature engineering" like creating new features or adjusting existing ones we should do this directly using the SFrames as seen in the other Week 2 notebook. For this notebook, however, we will work with the existing features. Convert to Numpy Array Although SFrames offer a number of benefits to ...
import numpy as np # note this allows us to refer to numpy as np instead
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
Now we will write a function that will accept an SFrame, a list of feature names (e.g. ['sqft_living', 'bedrooms']) and an target feature e.g. ('price') and will return two things:* A numpy matrix whose columns are the desired features plus a constant column (this is how we create an 'intercept')* A numpy array contain...
def get_numpy_data(data_sframe, features, output): data_sframe['constant'] = 1 # this is how you add a constant column to an SFrame # add the column 'constant' to the front of the features list so that we can extract it along with the others: features = ['constant'] + features # this is how you combine two ...
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
For testing let's use the 'sqft_living' feature and a constant as our features and price as our output:
(example_features, example_output) = get_numpy_data(sales, ['sqft_living'], 'price') # the [] around 'sqft_living' makes it a list print example_features[0,:] # this accesses the first row of the data the ':' indicates 'all columns' print example_output[0] # and the corresponding output
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
Predicting output given regression weights Suppose we had the weights [1.0, 1.0] and the features [1.0, 1180.0] and we wanted to compute the predicted output 1.0\*1.0 + 1.0\*1180.0 = 1181.0 this is the dot product between these two arrays. If they're numpy arrayws we can use np.dot() to compute this:
my_weights = np.array([1., 1.]) # the example weights my_features = example_features[0,] # we'll use the first data point predicted_value = np.dot(my_features, my_weights) print predicted_value
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
np.dot() also works when dealing with a matrix and a vector. Recall that the predictions from all the observations is just the RIGHT (as in weights on the right) dot product between the features *matrix* and the weights *vector*. With this in mind finish the following predict_output function to compute the predictions ...
def predict_output(feature_matrix, weights): # assume feature_matrix is a numpy matrix containing the features as columns and weights is a corresponding numpy array # create the predictions vector by using np.dot() return(predictions)
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
If you want to test your code run the following cell:
test_predictions = predict_output(example_features, my_weights) print test_predictions[0] # should be 1181.0 print test_predictions[1] # should be 2571.0
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
Computing the Derivative We are now going to move to computing the derivative of the regression cost function. Recall that the cost function is the sum over the data points of the squared difference between an observed output and a predicted output.Since the derivative of a sum is the sum of the derivatives we can com...
def feature_derivative(errors, feature): # Assume that errors and feature are both numpy arrays of the same length (number of data points) # compute twice the dot product of these vectors as 'derivative' and return the value return(derivative)
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
To test your feature derivartive run the following:
(example_features, example_output) = get_numpy_data(sales, ['sqft_living'], 'price') my_weights = np.array([0., 0.]) # this makes all the predictions 0 test_predictions = predict_output(example_features, my_weights) # just like SFrames 2 numpy arrays can be elementwise subtracted with '-': errors = test_predictions ...
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
Gradient Descent Now we will write a function that performs a gradient descent. The basic premise is simple. Given a starting point we update the current weights by moving in the negative gradient direction. Recall that the gradient is the direction of *increase* and therefore the negative gradient is the direction of...
from math import sqrt # recall that the magnitude/length of a vector [g[0], g[1], g[2]] is sqrt(g[0]^2 + g[1]^2 + g[2]^2) def regression_gradient_descent(feature_matrix, output, initial_weights, step_size, tolerance): converged = False weights = np.array(initial_weights) # make sure it's a numpy array whil...
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
A few things to note before we run the gradient descent. Since the gradient is a sum over all the data points and involves a product of an error and a feature the gradient itself will be very large since the features are large (squarefeet) and the output is large (prices). So while you might expect "tolerance" to be sm...
train_data,test_data = sales.random_split(.8,seed=0)
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
Although the gradient descent is designed for multiple regression since the constant is now a feature we can use the gradient descent function to estimat the parameters in the simple regression on squarefeet. The folowing cell sets up the feature_matrix, output, initial weights and step size for the first model:
# let's test out the gradient descent simple_features = ['sqft_living'] my_output = 'price' (simple_feature_matrix, output) = get_numpy_data(train_data, simple_features, my_output) initial_weights = np.array([-47000., 1.]) step_size = 7e-12 tolerance = 2.5e7
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
Next run your gradient descent with the above parameters. How do your weights compare to those achieved in week 1 (don't expect them to be exactly the same)? **Quiz Question: What is the value of the weight for sqft_living -- the second element of ‘simple_weights’ (rounded to 1 decimal place)?** Use your newly estimate...
(test_simple_feature_matrix, test_output) = get_numpy_data(test_data, simple_features, my_output)
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
Now compute your predictions using test_simple_feature_matrix and your weights from above. **Quiz Question: What is the predicted price for the 1st house in the TEST data set for model 1 (round to nearest dollar)?** Now that you have the predictions on test data, compute the RSS on the test data set. Save this value fo...
model_features = ['sqft_living', 'sqft_living15'] # sqft_living15 is the average squarefeet for the nearest 15 neighbors. my_output = 'price' (feature_matrix, output) = get_numpy_data(train_data, model_features, my_output) initial_weights = np.array([-100000., 1., 1.]) step_size = 4e-12 tolerance = 1e9
_____no_output_____
MIT
Machine_Learning_Regression/.ipynb_checkpoints/week-2-multiple-regression-assignment-2-blank-checkpoint.ipynb
nkmah2/ML_Uni_Washington_Coursera
Data can be found: https://data-seattlecitygis.opendata.arcgis.com/search?collection=Dataset&modified=2021-01-01%2C2021-10-13
import geopandas as gpd gpd.datasets.available world = gpd.read_file( gpd.datasets.get_path('naturalearth_lowres') ) seattle_zoning_2035 = gpd.read_file('Future_Land_Use__2035.shp') seattle_zoning_2035.head() education_centers = gpd.read_file('data/Environmental_Education_Centers.shp') education_centers
_____no_output_____
Apache-2.0
_notebooks/public_schools/EDA.ipynb
Hevia/blog
Advanced Logistic Regression in TensorFlow 2.0 Learning Objectives1. Load a CSV file using Pandas2. Create train, validation, and test sets3. Define and train a model using Keras (including setting class weights)4. Evaluate the model using various metrics (including precision and recall)5. Try common techniques for ...
import tensorflow as tf from tensorflow import keras import os import tempfile import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sns import sklearn from sklearn.metrics import confusion_matrix from sklearn.model_selection import train_test_split from sk...
TensorFlow version: 2.1.0
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
In the next cell, we're going to customize our Matplot lib visualization figure size and colors. Note that each time Matplotlib loads, it defines a runtime configuration (rc) containing the default styles for every plot element we create. This configuration can be adjusted at any time using the plt.rc convenience routi...
mpl.rcParams['figure.figsize'] = (12, 10) colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
_____no_output_____
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
Data processing and exploration Download the Kaggle Credit Card Fraud data setPandas is a Python library with many helpful utilities for loading and working with structured data and can be used to download CSVs into a dataframe.Note: This dataset has been collected and analysed during a research collaboration of Worl...
file = tf.keras.utils raw_df = pd.read_csv('https://storage.googleapis.com/download.tensorflow.org/data/creditcard.csv') raw_df.head()
_____no_output_____
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
Now, let's view the statistics of the raw dataframe.
raw_df[['Time', 'V1', 'V2', 'V3', 'V4', 'V5', 'V26', 'V27', 'V28', 'Amount', 'Class']].describe()
_____no_output_____
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
Examine the class label imbalanceLet's look at the dataset imbalance:
neg, pos = np.bincount(raw_df['Class']) total = neg + pos print('Examples:\n Total: {}\n Positive: {} ({:.2f}% of total)\n'.format( total, pos, 100 * pos / total))
Examples: Total: 284807 Positive: 492 (0.17% of total)
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
This shows the small fraction of positive samples. Clean, split and normalize the dataThe raw data has a few issues. First the `Time` and `Amount` columns are too variable to use directly. Drop the `Time` column (since it's not clear what it means) and take the log of the `Amount` column to reduce its range.
cleaned_df = raw_df.copy() # You don't want the `Time` column. cleaned_df.pop('Time') # The `Amount` column covers a huge range. Convert to log-space. eps=0.001 # 0 => 0.1¢ cleaned_df['Log Ammount'] = np.log(cleaned_df.pop('Amount')+eps)
_____no_output_____
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
Split the dataset into train, validation, and test sets. The validation set is used during the model fitting to evaluate the loss and any metrics, however the model is not fit with this data. The test set is completely unused during the training phase and is only used at the end to evaluate how well the model generaliz...
# Use a utility from sklearn to split and shuffle our dataset. train_df, test_df = train_test_split(cleaned_df, test_size=0.2) train_df, val_df = train_test_split(train_df, test_size=0.2) # Form np arrays of labels and features. train_labels = np.array(train_df.pop('Class')) bool_train_labels = train_labels != 0 val_l...
_____no_output_____
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
Normalize the input features using the sklearn StandardScaler.This will set the mean to 0 and standard deviation to 1.Note: The `StandardScaler` is only fit using the `train_features` to be sure the model is not peeking at the validation or test sets.
scaler = StandardScaler() train_features = scaler.fit_transform(train_features) val_features = scaler.transform(val_features) test_features = scaler.transform(test_features) train_features = np.clip(train_features, -5, 5) val_features = np.clip(val_features, -5, 5) test_features = np.clip(test_features, -5, 5) prin...
Training labels shape: (182276,) Validation labels shape: (45569,) Test labels shape: (56962,) Training features shape: (182276, 29) Validation features shape: (45569, 29) Test features shape: (56962, 29)
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
Caution: If you want to deploy a model, it's critical that you preserve the preprocessing calculations. The easiest way to implement them as layers, and attach them to your model before export. Look at the data distributionNext compare the distributions of the positive and negative examples over a few features. Good q...
pos_df = pd.DataFrame(train_features[ bool_train_labels], columns = train_df.columns) neg_df = pd.DataFrame(train_features[~bool_train_labels], columns = train_df.columns) sns.jointplot(pos_df['V5'], pos_df['V6'], kind='hex', xlim = (-5,5), ylim = (-5,5)) plt.suptitle("Positive distribution") sns.jointp...
_____no_output_____
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
Define the model and metricsDefine a function that creates a simple neural network with a densly connected hidden layer, a [dropout](https://developers.google.com/machine-learning/glossary/dropout_regularization) layer to reduce overfitting, and an output sigmoid layer that returns the probability of a transaction bei...
METRICS = [ keras.metrics.TruePositives(name='tp'), keras.metrics.FalsePositives(name='fp'), keras.metrics.TrueNegatives(name='tn'), keras.metrics.FalseNegatives(name='fn'), keras.metrics.BinaryAccuracy(name='accuracy'), keras.metrics.Precision(name='precision'), keras.metrics...
_____no_output_____
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
Understanding useful metricsNotice that there are a few metrics defined above that can be computed by the model that will be helpful when evaluating the performance.* **False** negatives and **false** positives are samples that were **incorrectly** classified* **True** negatives and **true** positives are samples ...
EPOCHS = 100 BATCH_SIZE = 2048 early_stopping = tf.keras.callbacks.EarlyStopping( monitor='val_auc', verbose=1, patience=10, mode='max', restore_best_weights=True) model = make_model() model.summary()
Model: "sequential_8" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_16 (Dense) (None, 16) 480 __________________________________...
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
Test run the model:
model.predict(train_features[:10])
_____no_output_____
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
Optional: Set the correct initial bias. These are initial guesses are not great. You know the dataset is imbalanced. Set the output layer's bias to reflect that (See: [A Recipe for Training Neural Networks: "init well"](http://karpathy.github.io/2019/04/25/recipe/2-set-up-the-end-to-end-trainingevaluation-skeleton--ge...
results = model.evaluate(train_features, train_labels, batch_size=BATCH_SIZE, verbose=0) print("Loss: {:0.4f}".format(results[0]))
Loss: 1.7441
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
The correct bias to set can be derived from:$$ p_0 = pos/(pos + neg) = 1/(1+e^{-b_0}) $$$$ b_0 = -log_e(1/p_0 - 1) $$$$ b_0 = log_e(pos/neg)$$
initial_bias = np.log([pos/neg]) initial_bias
_____no_output_____
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
Set that as the initial bias, and the model will give much more reasonable initial guesses. It should be near: `pos/total = 0.0018`
model = make_model(output_bias = initial_bias) model.predict(train_features[:10])
_____no_output_____
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
With this initialization the initial loss should be approximately:$$-p_0log(p_0)-(1-p_0)log(1-p_0) = 0.01317$$
results = model.evaluate(train_features, train_labels, batch_size=BATCH_SIZE, verbose=0) print("Loss: {:0.4f}".format(results[0]))
Loss: 0.0275
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
This initial loss is about 50 times less than if would have been with naive initilization.This way the model doesn't need to spend the first few epochs just learning that positive examples are unlikely. This also makes it easier to read plots of the loss during training. Checkpoint the initial weightsTo make the vario...
initial_weights = os.path.join(tempfile.mkdtemp(),'initial_weights') model.save_weights(initial_weights)
_____no_output_____
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst
Confirm that the bias fix helpsBefore moving on, confirm quick that the careful bias initialization actually helped.Train the model for 20 epochs, with and without this careful initialization, and compare the losses:
model = make_model() model.load_weights(initial_weights) model.layers[-1].bias.assign([0.0]) zero_bias_history = model.fit( train_features, train_labels, batch_size=BATCH_SIZE, epochs=20, validation_data=(val_features, val_labels), verbose=0) model = make_model() model.load_weights(initial_weig...
_____no_output_____
Apache-2.0
courses/machine_learning/deepdive2/introduction_to_tensorflow/solutions/adv_logistic_reg_TF2.0.ipynb
Best-Cloud-Practice-for-Data-Science/training-data-analyst