markdown
stringlengths 0
37k
| code
stringlengths 1
33.3k
| path
stringlengths 8
215
| repo_name
stringlengths 6
77
| license
stringclasses 15
values |
---|---|---|---|---|
Model evaluation
We can now rank our evaluation of all the models to choose the best one for our problem. While both Decision Tree and Random Forest score the same, we choose to use Random Forest as they correct for decision trees' habit of overfitting to their training set. | models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron',
'Stochastic Gradient Decent', 'Linear SVC',
'Decision Tree'],
'Score': [acc_svc, acc_knn, acc_log,
acc_random_forest, acc_gaussian, acc_perceptron,
acc_sgd, acc_linear_svc, acc_decision_tree]})
models.sort_values(by='Score', ascending=False)
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_pred
})
submission.to_csv('data/titanic-kaggle/submission.csv', index=False) | titanic-data-science-solutions.ipynb | Startupsci/data-science-notebooks | mit |
The 10th degree polynomial appears to give the best fit overall.
The lower order polynomials dont fit the curve exceedingly well below 100 K
Also, the polynomial tracks the heating curve (the slightly higher mV points from 80-150K) a little more closely than the cooling curve (295 to 80 K). Heating occurred much more slowly than cooling, so I expect it to me more accurate anyways. | # These mV values are also close ~0.5 degrees K of one another
print(fit_poly(273.15)) # fit
print(typeC.emf_mVK(273.15)) # NIST value | Type C calibrations/TypeC calcs corrected.ipynb | mannyfin/IRAS | bsd-3-clause |
It's also a good idea to check that the polynomial does not have any inflection points, at least in the area we are interested in using the polynomial (77 K - 273.15 K). We can use the second derivative test to see if this will be important for our case. | x = sp.symbols('x')
polynom = sp.Poly(fit_coeffs[0],x)
# print(fit_coeffs[0])
# find the second derivative of the polynomial
second_derivative = polynom.diff(x,x)
print(second_derivative)
sp.solve(second_derivative,x, domain= sp.S.Reals)
print(second_derivative.evalf(subs={x:77}))
print(second_derivative.evalf(subs={x:80}))
print('\n')
print(second_derivative.evalf(subs={x:120}))
print(second_derivative.evalf(subs={x:125}))
print('\n')
print(second_derivative.evalf(subs={x:135}))
print(second_derivative.evalf(subs={x:145}))
print('\n')
print(second_derivative.evalf(subs={x:283}))
print(second_derivative.evalf(subs={x:291}))
first_deriv = polynom.diff(x)
print(first_deriv)
sp.solve(first_deriv,x, domain= sp.S.Reals)
print(first_deriv.evalf(subs={x:80}))
print(first_deriv.evalf(subs={x:84})) | Type C calibrations/TypeC calcs corrected.ipynb | mannyfin/IRAS | bsd-3-clause |
Well this is not optimal-- there exists a local minimum at 83.86 K in our polynomial fit. We can attempt to fit an exponential curve to this very low temperature data and append this to the polynomial function. | lowT_df = df.query('T<103')
# Now lets fit the data to an exponential
# print(np.min(lowT_df['TypeC_calib_mV']))
def func(x, a, b, c, d):
return a * np.exp(b * x - c) + d
fit_coeffs = optimize.curve_fit(func, lowT_df['T'],lowT_df['TypeC_calib_mV'], p0=(1, 1, 90, -3))
print(fit_coeffs)
a = fit_coeffs[0][0]
b = fit_coeffs[0][1]
c = fit_coeffs[0][2]
d = fit_coeffs[0][3]
expfunc = func(lowT_df['T'],a,b,c,d)
fig3, ax3 = plt.subplots()
# ax3.plot(lowT_df['T'], a*np.exp(b*lowT_df['TypeC_calib_mV']), 'o',ms='0.5')
ax3.plot(lowT_df['T'], lowT_df['TypeC_calib_mV'], 'o',ms='0.5')
ax3.plot(lowT_df['T'], expfunc, 'o',ms='0.5',color='r') | Type C calibrations/TypeC calcs corrected.ipynb | mannyfin/IRAS | bsd-3-clause |
This appears to be a better fit than the polynomial in this regime. Now lets concatenate these two functions and interpolate near the points around 100 K to smooth things out if necessary. Recall that the two functions are fit_poly and expfunc | # select data from 103 to 120 K just so we can see the point of intersection a little better
checkT_df = df.query('77<=T<=120')
fig4, ax4 = plt.subplots()
ax4.plot(checkT_df['T'], fit_poly(checkT_df['T']), 'o', ms=0.5, label='polyfit', color='g')
ax4.plot(lowT_df['T'], expfunc, 'o', ms=0.5, label='expfunc', color='r')
ax4.plot(df['T'], df['TypeC_calib_mV'],'o',ms='0.5', label='Data', color='b')
ax4.set_xlim([80,110])
ax4.set_ylim([-1.88,-1.75])
ax4.legend() | Type C calibrations/TypeC calcs corrected.ipynb | mannyfin/IRAS | bsd-3-clause |
The two fitted plots almost match near 103 K, but there is a little 'cusp'-like shape near the point of intersection. Let's smooth it out. Also, notice that the expfunc fit is a little better than the polyfit. | def switch_fcn(x, switchpoint, smooth):
s = 0.5 + 0.5*np.tanh((x - switchpoint)/smooth)
return s
sw = switch_fcn(df['T'], 103, 0.2)
expfunc2 = func(df['T'],a,b,c,d)
len(expfunc2)
fig, ax = plt.subplots()
ax.plot(df['T'], sw,'o', ms=0.5)
def combined(switch, low_f1, high_f2):
comb = (1-switch)*low_f1 + switch*high_f2
return comb
comb_fcn = combined(sw, expfunc2,fit_poly(df['T']))
fig, ax = plt.subplots()
ax.plot(df['T'], comb_fcn, 'o', ms=0.5)
fig5, ax5 = plt.subplots()
ax5.plot(df['T'],comb_fcn, 'o', ms=2, label='combined')
ax5.plot(checkT_df['T'], fit_poly(checkT_df['T']), 'o', ms=0.5, label='polyfit', color='g')
ax5.plot(lowT_df['T'], expfunc, 'o', ms=0.5, label='expfunc2', color='r')
ax5.set_xlim([80,110])
ax5.set_ylim([-1.88,-1.75])
ax5.legend() | Type C calibrations/TypeC calcs corrected.ipynb | mannyfin/IRAS | bsd-3-clause |
Now I will take the polynomial and take the values from 77 K to 273 K for calibration and append them to the NIST values | # low temperature array
low_temp = np.arange(77.15,273.15, 0.1)
# low_temp_calib = fit_poly(low_temp)
low_temp_calib = combined(switch_fcn(low_temp, 103, 3), func(low_temp,a,b,c,d), fit_poly(low_temp))
# high temperature array
high_temp = np.arange(273.15,2588.15, 0.1)
high_temp_nist = typeC.emf_mVK(high_temp)
# concatentate and put into a dataframe and output to excel
Temperature = np.concatenate([low_temp, high_temp])
TypeC_mV = np.concatenate([low_temp_calib, high_temp_nist])
typeC_calibration = pd.DataFrame(data=TypeC_mV, index=Temperature, dtype='float32', columns = ['Type C (mV)'])
typeC_calibration.index.name = 'Temperature (Kelvin)'
print(typeC_calibration.head())
print(typeC_calibration.tail())
# Uncomment these lines and run the cell to output a calibration table
# write to excel
# xlwrite = pd.ExcelWriter('Type C calibration_low_res.xlsx')
# typeC_calibration.to_excel(xlwrite)
# xlwrite.save() | Type C calibrations/TypeC calcs corrected.ipynb | mannyfin/IRAS | bsd-3-clause |
But wait! Suppose we also want to fix that discontinuity at 273.15 K? We can apply the same procudure as before.
1. Apply a tanh(x) function: $switch = 0.5 + 0.5np.tanh((x - switchpoint)/smooth)$
2. Combine both functions: $comb = (1-switch)f1 + (switch)*f2 $ |
low_calib = combined(switch_fcn(Temperature, 103, 3), func(Temperature,a,b,c,d), fit_poly(Temperature))
high_calib = pd.DataFrame(index=high_temp, data=high_temp_nist,columns=['mV'])
dummy_df = pd.DataFrame(index=low_temp, data=np.zeros(len(low_temp)),columns=['mV'])
concat_high_calib = dummy_df.append(high_calib)
print(concat_high_calib.loc[272.9:273.5])
freezept_calib = combined(switch_fcn(Temperature, 273.15, 0.45), low_calib, concat_high_calib['mV'] )
freezept_calib.index.name = 'T'
freezept_calib.loc[272.9:273.5] | Type C calibrations/TypeC calcs corrected.ipynb | mannyfin/IRAS | bsd-3-clause |
The prior value at 273.15 K was -0.00867, when the actual value is 0. After the smoothing, the new value is -0.004336, about half of the prior value. Some of the values a little after 273.15 do not match exactly with the NIST table, but it is much better than the jump that we had before. | fig, ax = plt.subplots()
freezept_calib.plot(ax=ax, label ='combined')
ax.plot(Temperature,low_calib, label = 'low calib')
ax.plot(Temperature,concat_high_calib, label= 'high_calib')
ax.set_ylim([-.04,0.04])
ax.set_xlim([268,277])
ax.legend()
print(signal.argrelmin(freezept_calib.values))
# print(signal.argrelextrema(freezept_calib.values,np.less))
# print(signal.argrelextrema(freezept_calib.values,np.greater))
# No local maxima or minima!
# How about candidates for inflection points?
df = np.gradient(freezept_calib,0.1,)
fig, ax = plt.subplots()
ax.plot(Temperature, df)
d2f = np.gradient(df, 0.1)
ax.plot(Temperature, d2f)
d2f[:5]
# Uncomment these lines and run the cell to output a calibration table
# write to excel
xlwrite = pd.ExcelWriter('Type C calibration_corrected_temp.xlsx')
# freezept_calib is a Series, not a Dataframe, so use the line below
freezept_calib.to_frame().to_excel(xlwrite)
xlwrite.save()
# 4212018
fig, ax = plt.subplots()
ax.plot(Temperature,TypeC_mV)
# np.fft.fft(TypeC_mV)
def scaled_data(data, low_range=0, high_range=1, standardize=False, print_=False):
"""
scale data from input range to (low_range,high_range)
assumes data is a np 1d array
also allows capability for standardization (mean=0 and variance = 1)
"""
_min_ = np.min(data)
_max_ = np.max(data)
if standardize is True:
data = data - np.mean(data) #remove mean
scaled_data = data/np.std(data) #unit std
if print_ is True:
print('mean: '+str(np.mean(scaled_data)))
print('std: '+str(np.std(scaled_data)))
else:
scaled_data = (high_range - low_range)*(data - _min_)/(_max_ - _min_) + low_range
if print_ is True:
print('max: = '+str(np.max(scaled_data)))
print('min: = '+str(np.min(scaled_data)))
return scaled_data
def revert_to_unscaled(scaled, original):
"""
reverts normzlied data back to original scaling
"""
scaled_min = np.min(scaled)
scaled_max = np.max(scaled)
orig_min = np.min(original)
orig_max = np.max(original)
data = (scaled - scaled_min)*(orig_max - orig_min)/(scaled_max - scaled_min) + orig_min
return data
normmV = scaled_data(TypeC_mV, standardize=False, print_=True)
normT = scaled_data(Temperature, standardize=False, print_=True)
stdmV = scaled_data(TypeC_mV, standardize=True, print_=True)
stdT = scaled_data(Temperature, standardize=True, print_=True)
fig,ax = plt.subplots()
ax.plot(stdmV, stdT, label ='std')
plt.xlabel('stdmV')
plt.ylabel('stdT')
fig,ax = plt.subplots()
ax.plot(normmV, normT, label ='norm')
plt.xlabel('normmV')
plt.ylabel('normT')
def fit_fcn(mV, param):
return param[0]*np.tanh(param[1]*mV+param[2]) +param[3]
fig, ax = plt.subplots()
ax.plot(normmV,normT, label='norm')
ax.plot(normmV,fit_fcn(normmV, param=[1,0.1,-1, 1]), label='start')
try:
# popt, pcov = optimize.differential_evolution(fit_fcn, normmV, normT, p0=(2,1,-1,2), method='lm')
bounds = [slice(0,100.,1), slice(-10.,100.,1), slice(-10.,100.,1),slice(-10.,100.,1)]
# result = optimize.differential_evolution(lambda param:np.sum(fit_fcn(normmV, param) - normT)**2,bounds)
# result = optimize.basinhopping(lambda param:np.sum(fit_fcn(normmV, param) - normT)**2,bounds)
result = optimize.brute(lambda param:np.sum(fit_fcn(normmV, param) - normT)**2,bounds)
# result = optimize.least_squares(lambda param:np.sum(fit_fcn(normmV, param) - normT)**2,[1,1,-1, 1])
print(result)
ax.plot(normmV,fit_fcn(normmV, result), label='fitted')
except RuntimeError:
print("curve fit failed")
plt.legend() | Type C calibrations/TypeC calcs corrected.ipynb | mannyfin/IRAS | bsd-3-clause |
We will also need some specific modules and a litle "IPython magic" to show the plots: | from numpy import linalg as LA
from scipy import signal
import matplotlib.pyplot as plt
%matplotlib inline | FRF_plots.ipynb | pxcandeias/py-notebooks | mit |
Back to top
Dynamic system setup
In this example we will simulate a two degree of freedom system (2DOF) as a LTI system. For that purpose, we will define a mass and a stiffness matrix and use proportional damping: | MM = np.asmatrix(np.diag([1., 2.]))
print(MM)
KK = np.asmatrix([[20., -10.],[-10., 10.]])
print(KK)
C1 = 0.1*MM+0.02*KK
print(C1) | FRF_plots.ipynb | pxcandeias/py-notebooks | mit |
For the LTI system we will use a state space formulation. For that we will need the four matrices describing the system (A), the input (B), the output (C) and the feedthrough (D): | A = np.bmat([[np.zeros_like(MM), np.identity(MM.shape[0])], [LA.solve(-MM,KK), LA.solve(-MM,C1)]])
print(A)
Bf = KK*np.asmatrix(np.ones((2, 1)))
B = np.bmat([[np.zeros_like(Bf)],[LA.solve(MM,Bf)]])
print(B)
Cd = np.matrix((1,0))
Cv = np.asmatrix(np.zeros((1,MM.shape[1])))
Ca = np.asmatrix(np.zeros((1,MM.shape[1])))
C = np.bmat([Cd-Ca*LA.solve(MM,KK),Cv-Ca*LA.solve(MM,C1)])
print(C)
D = Ca*LA.solve(MM,Bf)
print(D) | FRF_plots.ipynb | pxcandeias/py-notebooks | mit |
The LTI system is simply defined as: | system = signal.lti(A, B, C, D) | FRF_plots.ipynb | pxcandeias/py-notebooks | mit |
To check the results presented ahead we will need the angular frequencies and damping coefficients of this system. The eigenanalysis of the system matrix yields them after some computations: | w1, v1 = LA.eig(A)
ix = np.argsort(np.absolute(w1)) # order of ascending eigenvalues
w1 = w1[ix] # sorted eigenvalues
v1 = v1[:,ix] # sorted eigenvectors
zw = -w1.real # damping coefficient time angular frequency
wD = w1.imag # damped angular frequency
zn = 1./np.sqrt(1.+(wD/-zw)**2) # the minus sign is formally correct!
wn = zw/zn # undamped angular frequency
print('Angular frequency: {}'.format(wn[[0,2]]))
print('Damping coefficient: {}'.format(zn[[0,2]])) | FRF_plots.ipynb | pxcandeias/py-notebooks | mit |
Back to top
Frequency response function
A frequency response function is a complex valued function of frequency. Let us see how it looks when we plot the real and imaginary parts in separate: | w, H = system.freqresp()
fig, ax = plt.subplots(2, 1)
fig.suptitle('Real and imaginary plots')
# Real part plot
ax[0].plot(w, H.real, label='FRF')
ax[0].axvline(wn[0], color='k', label='First mode', linestyle='--')
ax[0].axvline(wn[2], color='k', label='Second mode', linestyle='--')
ax[0].set_ylabel('Real [-]')
ax[0].grid(True)
ax[0].legend()
# Imaginary part plot
ax[1].plot(w, H.imag, label='FRF')
ax[1].axvline(wn[0], color='k', label='First mode', linestyle='--')
ax[1].axvline(wn[2], color='k', label='Second mode', linestyle='--')
ax[1].set_ylabel('Imaginary [-]')
ax[1].set_xlabel('Frequency [rad/s]')
ax[1].grid(True)
ax[1].legend()
plt.show() | FRF_plots.ipynb | pxcandeias/py-notebooks | mit |
Back to top
Nyquist plot
A Nyquist plot represents the real and imaginary parts of the complex FRF in a single plot: | plt.figure()
plt.title('Nyquist plot')
plt.plot(H.real, H.imag, 'b')
plt.plot(H.real, -H.imag, 'r')
plt.xlabel('Real [-]')
plt.ylabel('Imaginary[-]')
plt.grid(True)
plt.axis('equal')
plt.show() | FRF_plots.ipynb | pxcandeias/py-notebooks | mit |
Back to top
Bode plot
A Bode plot represents the complex FRF in magnitude-phase versus frequency: | w, mag, phase = system.bode()
fig, ax = plt.subplots(2, 1)
fig.suptitle('Bode plot')
# Magnitude plot
ax[0].plot(w, mag, label='FRF')
ax[0].axvline(wn[0], color='k', label='First mode', linestyle='--')
ax[0].axvline(wn[2], color='k', label='Second mode', linestyle='--')
ax[0].set_ylabel('Magnitude [dB]')
ax[0].grid(True)
ax[0].legend()
# Phase plot
ax[1].plot(w, phase*np.pi/180., label='FRF')
ax[1].axvline(wn[0], color='k', label='First mode', linestyle='--')
ax[1].axvline(wn[2], color='k', label='Second mode', linestyle='--')
ax[1].set_ylabel('Phase [rad]')
ax[1].set_xlabel('Frequency [rad/s]')
ax[1].grid(True)
ax[1].legend()
plt.show() | FRF_plots.ipynb | pxcandeias/py-notebooks | mit |
Back to top
Nichols plot
A Nichols plot combines the Bode plot in a single plot of magnitude versus phase: | plt.figure()
plt.title('Nichols plot')
plt.plot(phase*np.pi/180., mag)
plt.xlabel('Phase [rad/s]')
plt.ylabel('Magnitude [dB]')
plt.grid(True)
plt.show() | FRF_plots.ipynb | pxcandeias/py-notebooks | mit |
Загрузим данные | from sklearn.datasets import load_boston
bunch = load_boston()
print(bunch.DESCR)
X, y = pd.DataFrame(data=bunch.data, columns=bunch.feature_names.astype(str)), bunch.target
X.head() | 2. Бостон.ipynb | lithiumdenis/MLSchool | mit |
Зафиксируем генератор случайных чисел для воспроизводимости: | SEED = 22
np.random.seed = SEED | 2. Бостон.ipynb | lithiumdenis/MLSchool | mit |
Домашка!
Разделим данные на условно обучающую и отложенную выборки: | from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=SEED)
X_train.shape, y_train.shape, X_test.shape, y_test.shape | 2. Бостон.ipynb | lithiumdenis/MLSchool | mit |
Измерять качество будем с помощью метрики среднеквадратичной ошибки: | from sklearn.metrics import mean_squared_error | 2. Бостон.ipynb | lithiumdenis/MLSchool | mit |
<div class="panel panel-info" style="margin: 50px 0 0 0">
<div class="panel-heading">
<h3 class="panel-title">Задача 1.</h3>
</div>
<div class="panel">
Обучите <b>LinearRegression</b> из пакета <b>sklearn.linear_model</b> на обучающей выборке (<i>X_train, y_train</i>) и измерьте качество на <i>X_test</i>.
<br>
<br>
<i>P.s. Ошибка должна быть в районе 20. </i>
</div>
</div> | from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score
clf = LinearRegression()
clf.fit(X_train, y_train);
print('Вышла средняя ошибка, равная %5.4f' % \
(-np.mean(cross_val_score(clf, X_test, y_test, cv=5, scoring='neg_mean_squared_error')))) | 2. Бостон.ipynb | lithiumdenis/MLSchool | mit |
<div class="panel panel-info" style="margin: 50px 0 0 0">
<div class="panel-heading">
<h3 class="panel-title">Задача 2. (с подвохом)</h3>
</div>
<div class="panel">
Обучите <b>SGDRegressor</b> из пакета <b>sklearn.linear_model</b> на обучающей выборке (<i>X_train, y_train</i>) и измерьте качество на <i>X_test</i>.
</div>
</div> | from sklearn.linear_model import SGDRegressor
from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
X_scaled = ss.fit_transform(X_train)
y_scaled = ss.fit_transform(y_train)
sgd = SGDRegressor()
sgd.fit(X_scaled, y_scaled);
print('Вышла средняя ошибка, равная %5.4f' % \
(-np.mean(cross_val_score(sgd, X_scaled, y_scaled, cv=5, scoring='neg_mean_squared_error')))) | 2. Бостон.ipynb | lithiumdenis/MLSchool | mit |
<div class="panel panel-info" style="margin: 50px 0 0 0">
<div class="panel-heading">
<h3 class="panel-title">Задача 3.</h3>
</div>
<div class="panel">
Попробуйте все остальные классы:
<ul>
<li>Ridge
<li>Lasso
<li>ElasticNet
</ul>
<br>
В них, как вам уже известно, используются параметры регуляризации <b>alpha</b>. Настройте его как с помощью <b>GridSearchCV</b>, так и с помощью готовых <b>-CV</b> классов (<b>RidgeCV</b>, <b>LassoCV</b> и т.д.).
<br><br>
Найдите уже, в конце-концов, самую точную линейную модель!
</div>
</div> | from sklearn.preprocessing import StandardScaler, PolynomialFeatures
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import RidgeCV
############Ridge
params = {
'alpha': [10**x for x in range(-2,3)]
}
from sklearn.linear_model import Ridge
gsR = RidgeCV() #GridSearchCV(Ridge(), param_grid=params)
gsR.fit(X_train, y_train);
print('Вышла средняя ошибка, равная %5.4f' % \
(-np.mean(cross_val_score(gsR, X_test, y_test, cv=5, scoring='neg_mean_squared_error'))))
############Lasso
from sklearn.linear_model import Lasso
from sklearn.linear_model import LassoCV
gsL = GridSearchCV(Lasso(), param_grid=params) #LassoCV() - медленнее
gsL.fit(X_train, y_train);
print('Вышла средняя ошибка, равная %5.4f' % \
(-np.mean(cross_val_score(gsL, X_test, y_test, cv=5, scoring='neg_mean_squared_error'))))
from sklearn.linear_model import ElasticNet
from sklearn.linear_model import ElasticNetCV
gsE = GridSearchCV(ElasticNet(), param_grid=params) #ElasticNetCV() - просто заменить, не слишком точен
gsE.fit(X_train, y_train);
print('Вышла средняя ошибка, равная %5.4f' % \
(-np.mean(cross_val_score(gsE, X_test, y_test, cv=5, scoring='neg_mean_squared_error')))) | 2. Бостон.ipynb | lithiumdenis/MLSchool | mit |
Understanding Gradient Descent
In order to better understand gradient descent, let's implement it to solve a familiar problem - least-squares linear regression. While we are able to find the solution to ordinary least-squares linear regression analytically (recall its value as $\theta = (X^TX)^{−1}X^TY$), we can also find it using gradient descent.
Question 1:
First, let's consider the gradient function for ordinary least squares regression. Recall the OLS loss function as
$$Loss(\theta) = \frac{1}{n} \sum_{i=1}^n \left(y_i - f_\theta(x_i)\right)^2$$
And the function $f_\theta(x_i)$, for input data with $p$ dimensions, as
$$f_\theta(x_i) = \sum_{j=1}^p \theta_j x_{i,j} $$
Given these functions, what is the gradient function for OLS regression? First, state it in terms of a single component of $\theta$, $\theta_j$, using a sum over each data point $i$ in $X$. | q1_answer = r"""
Put your answer here, replacing this text.
$$\frac{\partial}{\partial \theta_j} Loss(\theta) = \frac{1}{n} \sum_{i=1}^n \dots$$
"""
display(Markdown(q1_answer))
q1_answer = r"""
**SOLUTION:**
$$\frac{\partial}{\partial \theta_j} Loss(\theta) = \frac{2}{n} \sum_{i=1}^n -x_{i,j} \left(y_i - f_\theta(x_i)\right)$$
"""
display(Markdown(q1_answer)) | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
Question 2:
Now, try to write that formula in terms of the matricies $X$, $y$, and $\theta$. | q2_answer = r"""
Put your answer here, replacing this text.
$$\frac{\partial}{\partial \theta} Loss(X) = \dots$$
"""
display(Markdown(q2_answer))
q2_answer = r"""
**SOLUTION:**
$$\frac{\partial}{\partial \theta} Loss(X) = -\frac{2}{n} X^T (y - X^T \theta)$$
"""
display(Markdown(q2_answer)) | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
Question 3:
Using this gradient function, complete the python function below which calculates the gradient for inputs $X$, $y$, and $\theta$. You should get a gradient of $[7, 48]$ on the simple data below. | def linear_regression_grad(X, y, theta):
grad = -2/X.shape[0] * X.T @ (y - X @ theta) #SOLUTION
return grad
theta = [1, 4]
simple_X = np.vstack([np.ones(10), np.arange(10)]).T
simple_y = np.arange(10) * 3 + 2
linear_regression_grad(simple_X, simple_y, theta) | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
Question 4:
Before we perform gradient descent, let's visualize the surface we're attempting to descend over. Run the next few cells to plot the loss surface as a function of $\theta_0$ and $\theta_1$, for some toy data. | def plot_surface_3d(X, Y, Z, angle):
highest_Z = max(Z.reshape(-1,1))
lowest_Z = min(Z.reshape(-1,1))
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z,
cmap=cm.coolwarm,
linewidth=0,
antialiased=False,
rstride=5, cstride=5)
ax.zaxis.set_major_locator(LinearLocator(5))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax.view_init(45, angle)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.title("Regression Loss Function")
plt.xlabel("Theta_0")
plt.ylabel("Theta_1")
plt.show() | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
We create some toy data in two dimensions to perform our regressions on: | np.random.seed(100)
X_1 = np.arange(50)/5 + 5
X = np.vstack([np.ones(50), X_1]).T
y = (X_1 * 2 + 3) + np.random.normal(0, 2.5, size=50)
plt.plot(X_1, y, ".") | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
And plot our loss: | angle_slider = widgets.FloatSlider(min=0, max=360, step=15, value=45)
def plot_regression_loss(angle):
t0_vals = np.linspace(-10,10,100)
t1_vals = np.linspace(-2,5,100)
theta_0,theta_1 = np.meshgrid(t0_vals, t1_vals)
thetas = np.vstack((theta_0.flatten(), theta_1.flatten()))
loss_vals = 2/X.shape[0] * sum(((y - (X @ thetas).T)**2).T)
loss_vals = loss_vals.reshape(100, -100)
plot_surface_3d(theta_0, theta_1, loss_vals, angle)
interact(plot_regression_loss, angle=angle_slider); | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
Consider:
- What do you notice about the loss surface for this simple regression example?
- Where are the optimal values $(\theta_0, \theta_1)$?
- Do you think that the shape of this surface will make gradient descent a viable solution to find these optimal values?
- What other loss surface shapes could you imagine?
Question 5:
Now, let's implement a general function to perform batch gradient descent. Given data X and y, initial weights $\theta_0$, a learning rate $\rho$, and a function gradient_function that has the same function signature as linear_regression_grad, implement a general gradient descent algorithm for finding optimal $\theta$. | def gradient_descent(X, y, theta0, gradient_function, learning_rate = 0.001, max_iter=1000000, epsilon=0.001):
theta_hat = theta0 # Initial guess
for t in range(1, max_iter):
grad = gradient_function(X, y, theta_hat)
# Now for the update step
theta_hat = theta_hat - learning_rate * grad #SOLUTION
# When our gradient is small enough, we have converged
if np.linalg.norm(grad) < epsilon:
print("converged after {} steps".format(t))
return theta_hat
# If we hit max_iter iterations
print("Warning - Failed to converge")
return theta_hat
theta_0 = [10, -1]
gradient_descent(X, y, theta_0, linear_regression_grad) | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
Now let's visualize how our regression estimates change as we perform gradient descent: | theta_0s = []
theta_1s = []
plot_idx = [1, 5, 20, 100, 500, 2000, 10000]
def plot_gradient_wrapper(X, y, theta):
grad = linear_regression_grad(X, y, theta)
theta_0s.append(theta[0])
theta_1s.append(theta[1])
t = len(theta_0s)
if t in plot_idx:
plt.subplot(121)
plt.xlim([4, 12])
plt.ylim([-2, 3])
plt.plot(theta_0s, theta_1s)
plt.plot(theta[0], theta[1], ".", color="b")
plt.title('theta(s) over time, t={}'.format(t))
plt.subplot(122)
plt.xlim([0, 20])
plt.ylim([-10, 40])
plt.plot(np.arange(50)/2.5, y, ".")
plt.plot(np.arange(50)/2.5, X @ theta)
plt.title('Regression line vs. data, t={}'.format(t))
plt.show()
return grad
gradient_descent(X, y, theta_0, plot_gradient_wrapper) | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
Question 6:
In Prof. Gonzalez's lecture, instead of using a constant learning rate, he used a learning rate that decreased over time, according to a function:
$$\rho(t) = \frac{r}{t}$$
Where $r$ represents some initial learning rate. This has the feature of decreasing the learning rate as we get closer to the optimal solution.
- Why might this be useful, compared to a constant learning rate?
- What problems might be caused by using too high of a learning rate?
- What about too low?
Extending to Logistic Regression
Question 7
As discussed in lecture, while ordinary least squares has a simple analytical solution, logistic regression must be fitted using gradient descent. Using the tools we've constructed, we can do just that. First, create a new function, logistic_regression_grad, which functions similarly to its counterpart linear_regression_grad. In the case of logistic regression, this should be the gradient of the logistic regression log-likelihood function - you may wish to refer to the lecture slides to find this gradient equation.
First, we define the sigmoid function: | def sigmoid(t):
return 1/(1 + np.e**-t) | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
And then complete the gradient function. You should get a gradient of about $[0.65, 0.61]$ for the given values $\theta$ on this example dataset. | def logistic_regression_grad(X, y, theta):
grad = (sigmoid(X @ theta) - y) @ X #SOLUTION
return grad
theta = [0, 1]
simple_X_1 = np.hstack([np.arange(10)/10, np.arange(10)/10 + 0.75])
simple_X = np.vstack([np.ones(20), simple_X_1]).T
simple_y = np.hstack([np.zeros(10), np.ones(10)])
linear_regression_grad(simple_X, simple_y, theta) | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
Now let's see how we can use our gradient descent tools to fit a regression on some real data! First, let's load the breast cancer dataset from lecture, and plot breast mass radius versus category - malignant or benign. As in lecture, we jitter the response variable to avoid overplotting. | import sklearn.datasets
data_dict = sklearn.datasets.load_breast_cancer()
data = pd.DataFrame(data_dict['data'], columns=data_dict['feature_names'])
data['malignant'] = (data_dict['target'] == 0)
data['malignant'] = data['malignant'] + 0.1*np.random.rand(len(data['malignant'])) - 0.05
X_log_1 = data['mean radius']
X_log = np.vstack([np.ones(len(X_log_1)), X_log_1.values]).T
y_log = data['malignant'].values
plt.plot(X_log_1, y_log, ".") | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
Question 8:
Now, using our earlier defined gradient_descent function, find optimal parameters $(\theta_0, \theta_1)$ to fit the breast cancer data. You will have to tune the learning rate beyond the default of the function, and think of what a good initial guess for $\theta$ would be, in both dimensions. | theta_log = gradient_descent(X_log, y_log, [0, 1], logistic_regression_grad, learning_rate=0.0001) #SOLUTION
theta_log | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
With optimal $\theta$ chosen, we can now plot our logistic curve and our decision boundary, and look at how our model categorizes our data: | y_lowX = X_log_1[sigmoid(X_log @ theta_log) < 0.5]
y_lowy = y_log[sigmoid(X_log @ theta_log) < 0.5]
y_highX = X_log_1[sigmoid(X_log @ theta_log) > 0.5]
y_highy = y_log[sigmoid(X_log @ theta_log) > 0.5]
sigrange = np.arange(5, 30, 0.05)
sigrange_X = np.vstack([np.ones(500), sigrange]).T
d_boundary = -theta_log[0]/theta_log[1]
plt.plot(sigrange, sigmoid(sigrange_X @ theta_log), ".", color="g")
plt.hlines(0.5, 5, 30, "g")
plt.vlines(d_boundary, -0.2, 1.2, "g")
plt.plot(y_lowX, y_lowy, ".", color="b")
plt.plot(y_highX, y_highy, ".", color="r")
plt.title("Classification (blue=benign, red=malignant), assuming a P=0.5 decision boundary") | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
And, we can calculate our classification accuracy. | n_errors = sum(y_lowy > 0.5) + sum(y_highy < 0.5)
accuracy = round((len(y_log)-n_errors)/len(y_log) * 1000)/10
print("Classification Accuracy - {}%".format(accuracy)) | sp17/disc/disc11/disc11_solution.ipynb | DS-100/sp17-materials | gpl-3.0 |
Target distribution
We use the peaks function from matlab, modified so it is positive:
$$
p(x,y) \propto |3 (1-x)^2 e^{-x^2 - (y+1)^2}
- 10 (\frac{x}{5} - x^3 - y^5) e^{-x^2 -y^2}
- \frac{1}{3} e^{-(x+1)^2 - y^2} |
$$ | # Generate a pdf
# the following steps generate a pdf; this is equivalent to the function "peaks(n)" in matlab
n = 100 # number of dimension
pdf = np.zeros([n, n])
sigma = np.zeros([n, n])
s = np.zeros([n, n])
x = -3.0
for i in range(0, n):
y = -3.0
for j in range(0, n):
pdf[j, i] = (
3.0 * (1 - x) ** 2 * np.exp(-(x**2) - (y + 1) ** 2)
- 10.0 * (x / 5 - x**3 - y**5) * np.exp(-(x**2) - y**2)
- 1.0 / 3 * np.exp(-((x + 1) ** 2) - y**2)
)
if pdf[j, i] < 0:
pdf[j, i] = pdf[j, i] * (
-1
) # in contrast to the peaks function: all negative values are multiplied by (-1)
y = y + 6.0 / (n - 1)
x = x + 6.0 / (n - 1)
pdf = pdf / pdf.max()
energy = -np.log(pdf)
# Plot the 3D plot of pdf
# --------------------------
X = np.arange(0, 100 + 100.0 / (n - 1), 100.0 / (n - 1))
Y = np.arange(0, 100 + 100.0 / (n - 1), 100.0 / (n - 1))
fig0 = plt.figure()
ax = fig0.gca(projection="3d")
X, Y = np.meshgrid(X, Y)
surf = ax.plot_surface(Y, X, pdf, rstride=2, cstride=2, cmap=plt.cm.coolwarm, linewidth=0.1)
# plt.gca().invert_xaxis()
plt.tight_layout()
pml.savefig("sim_anneal_2d_peaks.pdf")
plt.show()
# Plot the 3D plot of Energy function
# --------------------------
X = np.arange(0, 100 + 100.0 / (n - 1), 100.0 / (n - 1))
Y = np.arange(0, 100 + 100.0 / (n - 1), 100.0 / (n - 1))
fig0 = plt.figure()
ax = fig0.gca(projection="3d")
X, Y = np.meshgrid(X, Y)
surf = ax.plot_surface(Y, X, energy / energy.max(), rstride=2, cstride=2, cmap=plt.cm.coolwarm, linewidth=0.1)
# plt.gca().invert_xaxis()
plt.tight_layout()
pml.savefig("sim_anneal_2d_energy.pdf")
plt.show() | deprecated/simulated_annealing_2d_demo.ipynb | probml/pyprobml | mit |
Heat bath
The "heat bath" refers to a modified version of the distribution in which we vary the temperature. | Tplots = 10 # initial temperature for the plots
stepT = 4 # how many steps should the Temperature be *0.2 for
for i in range(0, stepT):
sigma = np.exp(-(energy) / Tplots)
sigma = sigma / sigma.max()
ttl = "T={:0.2f}".format(Tplots)
Tplots = Tplots * 0.2
X = np.arange(0, 100 + 100.0 / (n - 1), 100.0 / (n - 1))
Y = np.arange(0, 100 + 100.0 / (n - 1), 100.0 / (n - 1))
fig = plt.figure()
ax = fig.gca(projection="3d")
X, Y = np.meshgrid(X, Y)
ax.set_title(ttl)
ax.plot_surface(Y, X, sigma, rstride=2, cstride=2, cmap=plt.cm.coolwarm, linewidth=0, antialiased=False)
# plt.gca().invert_xaxis()
plt.tight_layout()
pml.savefig(f"sim_anneal_2d_cooled{i}.pdf")
plt.show() | deprecated/simulated_annealing_2d_demo.ipynb | probml/pyprobml | mit |
SA algorithm | def sim_anneal(proposal="gaussian", sigma=10):
np.random.seed(42)
xcur = np.array([np.floor(np.random.uniform(0, 100)), np.floor(np.random.uniform(0, 100))])
xcur = xcur.astype(int)
ns = 300 # number of samples to keep
T = 1 # start temperature
alpha = 0.99999 # cooling schedule
alpha = 0.99 # cooling schedule
# list of visited points, temperatures, probabilities
x_hist = xcur # will be (N,2) array
prob_hist = []
temp_hist = []
nreject = 0
iis = 0 # number of accepted points
npp = 0 # num proposed points
while npp < ns:
npp = npp + 1
if proposal == "uniform":
xnew = np.array([np.floor(np.random.uniform(0, 100)), np.floor(np.random.uniform(0, 100))])
elif proposal == "gaussian":
xnew = xcur + np.random.normal(size=2) * sigma
xnew = np.maximum(xnew, 0)
xnew = np.minimum(xnew, 99)
else:
raise ValueError("Unknown proposal")
xnew = xnew.astype(int)
# compare energies
Ecur = energy[xcur[0], xcur[1]]
Enew = energy[xnew[0], xnew[1]]
deltaE = Enew - Ecur
# print([npp, xcur, xnew, Ecur, Enew, deltaE])
temp_hist.append(T)
T = alpha * T
P = np.exp(-1.0 * deltaE / T)
P = min(1, P)
test = np.random.uniform(0, 1)
if test <= P:
xcur = xnew
iis = iis + 1
else:
nreject += 1
x_hist = np.vstack((x_hist, xcur))
prob_hist.append(pdf[xcur[0], xcur[1]])
npp = npp + 1
print(f"nproposed {npp}, naccepted {iis}, nreject {nreject}")
return x_hist, prob_hist, temp_hist | deprecated/simulated_annealing_2d_demo.ipynb | probml/pyprobml | mit |
Run experiments | proposals = {"gaussian", "uniform"}
x_hist = {}
prob_hist = {}
temp_hist = {}
for proposal in proposals:
print(proposal)
x_hist[proposal], prob_hist[proposal], temp_hist[proposal] = sim_anneal(proposal=proposal)
for proposal in proposals:
plt.figure()
plt.plot(temp_hist[proposal])
plt.title("temperature vs time")
plt.tight_layout()
pml.savefig(f"sim_anneal_2d_temp_vs_time_{proposal}.pdf")
plt.show()
for proposal in proposals:
plt.figure()
plt.plot(prob_hist[proposal])
plt.xlabel("iteration")
plt.ylabel("probability")
plt.tight_layout()
pml.savefig(f"sim_anneal_2d_prob_vs_time_{proposal}.pdf")
plt.show()
# Plot points visited
for proposal in proposals:
probs = prob_hist[proposal]
xa = x_hist[proposal]
f1, ax = plt.subplots()
ax.imshow(pdf.transpose(), aspect="auto", extent=[0, 100, 100, 0], interpolation="none")
# Maximum value achieved ploted with white cirlce
# maxi = np.argmax(probs) # index of best model
# ax.plot(xa[maxi,0],xa[maxi,1],'wo', markersize=10)
# Starting point with white cirlce
ax.plot(xa[0, 0], xa[0, 1], "wo", markersize=10)
# Global maximm with red cirlce
ind = np.unravel_index(np.argmax(pdf, axis=None), pdf.shape)
ax.plot(ind[0], ind[1], "ro", markersize=10)
ax.plot(xa[:, 0], xa[:, 1], "w+") # Plot the steps with white +
ax.set_ylabel("y")
ax.set_xlabel("x")
plt.tight_layout()
pml.savefig(f"sim_anneal_2d_samples_{proposal}.pdf")
plt.show() | deprecated/simulated_annealing_2d_demo.ipynb | probml/pyprobml | mit |
Compute seed-based time-frequency connectivity in sensor space
Computes the connectivity between a seed-gradiometer close to the visual cortex
and all other gradiometers. The connectivity is computed in the time-frequency
domain using Morlet wavelets and the debiased squared weighted phase lag index
[1]_ is used as connectivity metric.
.. [1] Vinck et al. "An improved index of phase-synchronization for electro-
physiological data in the presence of volume-conduction, noise and
sample-size bias" NeuroImage, vol. 55, no. 4, pp. 1548-1565, Apr. 2011. | # Author: Martin Luessi <[email protected]>
#
# License: BSD (3-clause)
import numpy as np
import mne
from mne import io
from mne.connectivity import spectral_connectivity, seed_target_indices
from mne.datasets import sample
from mne.time_frequency import AverageTFR
print(__doc__) | 0.20/_downloads/bf3ad991f7c7776e245520709f49cb04/plot_cwt_sensor_connectivity.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Set parameters | data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
event_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'
# Setup for reading the raw data
raw = io.read_raw_fif(raw_fname)
events = mne.read_events(event_fname)
# Add a bad channel
raw.info['bads'] += ['MEG 2443']
# Pick MEG gradiometers
picks = mne.pick_types(raw.info, meg='grad', eeg=False, stim=False, eog=True,
exclude='bads')
# Create epochs for left-visual condition
event_id, tmin, tmax = 3, -0.2, 0.5
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks,
baseline=(None, 0), reject=dict(grad=4000e-13, eog=150e-6),
preload=True)
# Use 'MEG 2343' as seed
seed_ch = 'MEG 2343'
picks_ch_names = [raw.ch_names[i] for i in picks]
# Create seed-target indices for connectivity computation
seed = picks_ch_names.index(seed_ch)
targets = np.arange(len(picks))
indices = seed_target_indices(seed, targets)
# Define wavelet frequencies and number of cycles
cwt_freqs = np.arange(7, 30, 2)
cwt_n_cycles = cwt_freqs / 7.
# Run the connectivity analysis using 2 parallel jobs
sfreq = raw.info['sfreq'] # the sampling frequency
con, freqs, times, _, _ = spectral_connectivity(
epochs, indices=indices,
method='wpli2_debiased', mode='cwt_morlet', sfreq=sfreq,
cwt_freqs=cwt_freqs, cwt_n_cycles=cwt_n_cycles, n_jobs=1)
# Mark the seed channel with a value of 1.0, so we can see it in the plot
con[np.where(indices[1] == seed)] = 1.0
# Show topography of connectivity from seed
title = 'WPLI2 - Visual - Seed %s' % seed_ch
layout = mne.find_layout(epochs.info, 'meg') # use full layout
tfr = AverageTFR(epochs.info, con, times, freqs, len(epochs))
tfr.plot_topo(fig_facecolor='w', font_color='k', border='k') | 0.20/_downloads/bf3ad991f7c7776e245520709f49cb04/plot_cwt_sensor_connectivity.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Checking out the data
This dataset has the number of riders for each hour of each day from January 1 2011 to December 31 2012. The number of riders is split between casual and registered, summed up in the cnt column. You can see the first few rows of the data above.
Below is a plot showing the number of bike riders over the first 10 days or so in the data set. (Some days don't have exactly 24 entries in the data set, so it's not exactly 10 days.) You can see the hourly rentals here. This data is pretty complicated! The weekends have lower over all ridership and there are spikes when people are biking to and from work during the week. Looking at the data above, we also have information about temperature, humidity, and windspeed, all of these likely affecting the number of riders. You'll be trying to capture all this with your model. | rides[:24*10].plot(x='dteday', y='cnt') | Your_first_neural_network.ipynb | jorgedominguezchavez/dlnd_first_neural_network | mit |
Splitting the data into training, testing, and validation sets
We'll save the data for the last approximately 21 days to use as a test set after we've trained the network. We'll use this set to make predictions and compare them with the actual number of riders. | # Save data for approximately the last 21 days
test_data = data[-21*24:]
# Now remove the test data from the data set
data = data[:-21*24]
# Separate the data into features and targets
target_fields = ['cnt', 'casual', 'registered']
features, targets = data.drop(target_fields, axis=1), data[target_fields]
test_features, test_targets = test_data.drop(target_fields, axis=1), test_data[target_fields] | Your_first_neural_network.ipynb | jorgedominguezchavez/dlnd_first_neural_network | mit |
We'll split the data into two sets, one for training and one for validating as the network is being trained. Since this is time series data, we'll train on historical data, then try to predict on future data (the validation set). | # Hold out the last 60 days or so of the remaining data as a validation set
train_features, train_targets = features[:-60*24], targets[:-60*24]
val_features, val_targets = features[-60*24:], targets[-60*24:] | Your_first_neural_network.ipynb | jorgedominguezchavez/dlnd_first_neural_network | mit |
Time to build the network
Below you'll build your network. We've built out the structure and the backwards pass. You'll implement the forward pass through the network. You'll also set the hyperparameters: the learning rate, the number of hidden units, and the number of training passes.
<img src="assets/neural_network.png" width=300px>
The network has two layers, a hidden layer and an output layer. The hidden layer will use the sigmoid function for activations. The output layer has only one node and is used for the regression, the output of the node is the same as the input of the node. That is, the activation function is $f(x)=x$. A function that takes the input signal and generates an output signal, but takes into account the threshold, is called an activation function. We work through each layer of our network calculating the outputs for each neuron. All of the outputs from one layer become inputs to the neurons on the next layer. This process is called forward propagation.
We use the weights to propagate signals forward from the input to the output layers in a neural network. We use the weights to also propagate error backwards from the output back into the network to update our weights. This is called backpropagation.
Hint: You'll need the derivative of the output activation function ($f(x) = x$) for the backpropagation implementation. If you aren't familiar with calculus, this function is equivalent to the equation $y = x$. What is the slope of that equation? That is the derivative of $f(x)$.
Below, you have these tasks:
1. Implement the sigmoid function to use as the activation function. Set self.activation_function in __init__ to your sigmoid function.
2. Implement the forward pass in the train method.
3. Implement the backpropagation algorithm in the train method, including calculating the output error.
4. Implement the forward pass in the run method. | class NeuralNetwork(object):
def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
# Set number of nodes in input, hidden and output layers.
self.input_nodes = input_nodes
self.hidden_nodes = hidden_nodes
self.output_nodes = output_nodes
# Initialize weights
self.weights_input_to_hidden = np.random.normal(0.0, self.input_nodes**-0.5,
(self.input_nodes, self.hidden_nodes))
self.weights_hidden_to_output = np.random.normal(0.0, self.hidden_nodes**-0.5,
(self.hidden_nodes, self.output_nodes))
self.lr = learning_rate
#### TODO: Set self.activation_function to your implemented sigmoid function ####
#
# Note: in Python, you can define a function with a lambda expression,
# as shown below.
self.activation_function = lambda x : 1/(1+np.exp(-x)) # Replace 0 with sigmoid calculation. DONE
### If the lambda code above is not something you're familiar with,
# You can uncomment out the following three lines and put your
# implementation there instead.
#
#def sigmoid(x):
# return 0 # Replace 0 with your sigmoid calculation here
#self.activation_function = sigmoid
def train(self, features, targets):
''' Train the network on batch of features and targets.
Arguments
---------
features: 2D array, each row is one data record, each column is a feature
targets: 1D array of target values
'''
n_records = features.shape[0]
delta_weights_i_h = np.zeros(self.weights_input_to_hidden.shape)
delta_weights_h_o = np.zeros(self.weights_hidden_to_output.shape)
for X, y in zip(features, targets):
#### Implement the forward pass here ####
### Forward pass ###
# TODO: Hidden layer - Replace these values with your calculations.
hidden_inputs = np.dot(X, self.weights_input_to_hidden ) # signals into hidden layer DONE
hidden_outputs = self.activation_function(hidden_inputs) # signals from hidden layer DONE
# TODO: Output layer - Replace these values with your calculations.
final_inputs = np.dot(hidden_outputs, self.weights_hidden_to_output) # signals into final output layer
final_outputs = final_inputs # signals from final output layer
#### Implement the backward pass here ####
### Backward pass ###
# TODO: Output error - Replace this value with your calculations.
error = y - final_outputs # Output layer error is the difference between desired target and actual output.
# TODO: Backpropagated error terms - Replace these values with your calculations.
output_error_term = error
# TODO: Calculate the hidden layer's contribution to the error
hidden_error = np.dot(self.weights_hidden_to_output, output_error_term)
# TODO: Backpropagated error terms - Replace these values with your calculations.
hidden_error_term = hidden_error * hidden_outputs * (1 - hidden_outputs)
# Weight step (input to hidden)
delta_weights_i_h += hidden_error_term * X[:, None]
# Weight step (hidden to output)
delta_weights_h_o += output_error_term * hidden_outputs[:, None]
# TODO: Update the weights - Replace these values with your calculations.
self.weights_hidden_to_output += self.lr * delta_weights_h_o / n_records # update hidden-to-output weights with gradient descent step
self.weights_input_to_hidden += self.lr * delta_weights_i_h / n_records # update input-to-hidden weights with gradient descent step
def run(self, features):
''' Run a forward pass through the network with input features
Arguments
---------
features: 1D array of feature values
'''
#### Implement the forward pass here ####
# TODO: Hidden layer - replace these values with the appropriate calculations.
hidden_inputs = np.dot(features, self.weights_input_to_hidden ) # signals into hidden layer
hidden_outputs = self.activation_function(hidden_inputs) # signals from hidden layer
# TODO: Output layer - Replace these values with the appropriate calculations.
final_inputs = np.dot(hidden_outputs, self.weights_hidden_to_output) # signals into final output layer
final_outputs = final_inputs # signals from final output layer
return final_outputs
def MSE(y, Y):
return np.mean((y-Y)**2) | Your_first_neural_network.ipynb | jorgedominguezchavez/dlnd_first_neural_network | mit |
Unit tests
Run these unit tests to check the correctness of your network implementation. This will help you be sure your network was implemented correctly befor you starting trying to train it. These tests must all be successful to pass the project. | import unittest
inputs = np.array([[0.5, -0.2, 0.1]])
targets = np.array([[0.4]])
test_w_i_h = np.array([[0.1, -0.2],
[0.4, 0.5],
[-0.3, 0.2]])
test_w_h_o = np.array([[0.3],
[-0.1]])
class TestMethods(unittest.TestCase):
##########
# Unit tests for data loading
##########
def test_data_path(self):
# Test that file path to dataset has been unaltered
self.assertTrue(data_path.lower() == 'bike-sharing-dataset/hour.csv')
def test_data_loaded(self):
# Test that data frame loaded
self.assertTrue(isinstance(rides, pd.DataFrame))
##########
# Unit tests for network functionality
##########
def test_activation(self):
network = NeuralNetwork(3, 2, 1, 0.5)
# Test that the activation function is a sigmoid
self.assertTrue(np.all(network.activation_function(0.5) == 1/(1+np.exp(-0.5))))
def test_train(self):
# Test that weights are updated correctly on training
network = NeuralNetwork(3, 2, 1, 0.5)
network.weights_input_to_hidden = test_w_i_h.copy()
network.weights_hidden_to_output = test_w_h_o.copy()
network.train(inputs, targets)
self.assertTrue(np.allclose(network.weights_hidden_to_output,
np.array([[ 0.37275328],
[-0.03172939]])))
self.assertTrue(np.allclose(network.weights_input_to_hidden,
np.array([[ 0.10562014, -0.20185996],
[0.39775194, 0.50074398],
[-0.29887597, 0.19962801]])))
def test_run(self):
# Test correctness of run method
network = NeuralNetwork(3, 2, 1, 0.5)
network.weights_input_to_hidden = test_w_i_h.copy()
network.weights_hidden_to_output = test_w_h_o.copy()
self.assertTrue(np.allclose(network.run(inputs), 0.09998924))
suite = unittest.TestLoader().loadTestsFromModule(TestMethods())
unittest.TextTestRunner().run(suite) | Your_first_neural_network.ipynb | jorgedominguezchavez/dlnd_first_neural_network | mit |
Training the network
Here you'll set the hyperparameters for the network. The strategy here is to find hyperparameters such that the error on the training set is low, but you're not overfitting to the data. If you train the network too long or have too many hidden nodes, it can become overly specific to the training set and will fail to generalize to the validation set. That is, the loss on the validation set will start increasing as the training set loss drops.
You'll also be using a method know as Stochastic Gradient Descent (SGD) to train the network. The idea is that for each training pass, you grab a random sample of the data instead of using the whole data set. You use many more training passes than with normal gradient descent, but each pass is much faster. This ends up training the network more efficiently. You'll learn more about SGD later.
Choose the number of iterations
This is the number of batches of samples from the training data we'll use to train the network. The more iterations you use, the better the model will fit the data. However, if you use too many iterations, then the model with not generalize well to other data, this is called overfitting. You want to find a number here where the network has a low training loss, and the validation loss is at a minimum. As you start overfitting, you'll see the training loss continue to decrease while the validation loss starts to increase.
Choose the learning rate
This scales the size of weight updates. If this is too big, the weights tend to explode and the network fails to fit the data. A good choice to start at is 0.1. If the network has problems fitting the data, try reducing the learning rate. Note that the lower the learning rate, the smaller the steps are in the weight updates and the longer it takes for the neural network to converge.
Choose the number of hidden nodes
The more hidden nodes you have, the more accurate predictions the model will make. Try a few different numbers and see how it affects the performance. You can look at the losses dictionary for a metric of the network performance. If the number of hidden units is too low, then the model won't have enough space to learn and if it is too high there are too many options for the direction that the learning can take. The trick here is to find the right balance in number of hidden units you choose. | import sys
### Set the hyperparameters here ###
iterations = 40000
learning_rate = 0.5
hidden_nodes = 35
output_nodes = 1
N_i = train_features.shape[1]
network = NeuralNetwork(N_i, hidden_nodes, output_nodes, learning_rate)
losses = {'train':[], 'validation':[]}
for ii in range(iterations):
# Go through a random batch of 128 records from the training data set
batch = np.random.choice(train_features.index, size=128)
X, y = train_features.ix[batch].values, train_targets.ix[batch]['cnt']
network.train(X, y)
# Printing out the training progress
train_loss = MSE(network.run(train_features).T, train_targets['cnt'].values)
val_loss = MSE(network.run(val_features).T, val_targets['cnt'].values)
sys.stdout.write("\rProgress: {:2.1f}".format(100 * ii/float(iterations)) \
+ "% ... Training loss: " + str(train_loss)[:5] \
+ " ... Validation loss: " + str(val_loss)[:5])
sys.stdout.flush()
losses['train'].append(train_loss)
losses['validation'].append(val_loss)
plt.plot(losses['train'], label='Training loss')
plt.plot(losses['validation'], label='Validation loss')
plt.legend()
_ = plt.ylim() | Your_first_neural_network.ipynb | jorgedominguezchavez/dlnd_first_neural_network | mit |
Check out your predictions
Here, use the test data to view how well your network is modeling the data. If something is completely wrong here, make sure each step in your network is implemented correctly. | fig, ax = plt.subplots(figsize=(8,4))
mean, std = scaled_features['cnt']
predictions = network.run(test_features).T*std + mean
ax.plot(predictions[0], label='Prediction')
ax.plot((test_targets['cnt']*std + mean).values, label='Data')
ax.set_xlim(right=len(predictions))
ax.legend()
dates = pd.to_datetime(rides.ix[test_data.index]['dteday'])
dates = dates.apply(lambda d: d.strftime('%b %d'))
ax.set_xticks(np.arange(len(dates))[12::24])
_ = ax.set_xticklabels(dates[12::24], rotation=45) | Your_first_neural_network.ipynb | jorgedominguezchavez/dlnd_first_neural_network | mit |
Network Architecture
The encoder part of the network will be a typical convolutional pyramid. Each convolutional layer will be followed by a max-pooling layer to reduce the dimensions of the layers. The decoder though might be something new to you. The decoder needs to convert from a narrow representation to a wide reconstructed image. For example, the representation could be a 4x4x8 max-pool layer. This is the output of the encoder, but also the input to the decoder. We want to get a 28x28x1 image out from the decoder so we need to work our way back up from the narrow decoder input layer. A schematic of the network is shown below.
Here our final encoder layer has size 4x4x8 = 128. The original images have size 28x28 = 784, so the encoded vector is roughly 16% the size of the original image. These are just suggested sizes for each of the layers. Feel free to change the depths and sizes, but remember our goal here is to find a small representation of the input data.
What's going on with the decoder
Okay, so the decoder has these "Upsample" layers that you might not have seen before. First off, I'll discuss a bit what these layers aren't. Usually, you'll see deconvolutional layers used to increase the width and height of the layers. They work almost exactly the same as convolutional layers, but it reverse. A stride in the input layer results in a larger stride in the deconvolutional layer. For example, if you have a 3x3 kernel, a 3x3 patch in the input layer will be reduced to one unit in a convolutional layer. Comparatively, one unit in the input layer will be expanded to a 3x3 path in a deconvolutional layer. Deconvolution is often called "transpose convolution" which is what you'll find with the TensorFlow API, with tf.nn.conv2d_transpose.
However, deconvolutional layers can lead to artifacts in the final images, such as checkerboard patterns. This is due to overlap in the kernels which can be avoided by setting the stride and kernel size equal. In this Distill article from Augustus Odena, et al, the authors show that these checkerboard artifacts can be avoided by resizing the layers using nearest neighbor or bilinear interpolation (upsampling) followed by a convolutional layer. In TensorFlow, this is easily done with tf.image.resize_images, followed by a convolution. Be sure to read the Distill article to get a better understanding of deconvolutional layers and why we're using upsampling.
Exercise: Build the network shown above. Remember that a convolutional layer with strides of 1 and 'same' padding won't reduce the height and width. That is, if the input is 28x28 and the convolution layer has stride = 1 and 'same' padding, the convolutional layer will also be 28x28. The max-pool layers are used the reduce the width and height. A stride of 2 will reduce the size by 2. Odena et al claim that nearest neighbor interpolation works best for the upsampling, so make sure to include that as a parameter in tf.image.resize_images or use tf.image.resize_nearest_neighbor. | learning_rate = 0.001
inputs_ =
targets_ =
### Encoder
conv1 =
# Now 28x28x16
maxpool1 =
# Now 14x14x16
conv2 =
# Now 14x14x8
maxpool2 =
# Now 7x7x8
conv3 =
# Now 7x7x8
encoded =
# Now 4x4x8
### Decoder
upsample1 =
# Now 7x7x8
conv4 =
# Now 7x7x8
upsample2 =
# Now 14x14x8
conv5 =
# Now 14x14x8
upsample3 =
# Now 28x28x8
conv6 =
# Now 28x28x16
logits =
#Now 28x28x1
# Pass logits through sigmoid to get reconstructed image
decoded =
# Pass logits through sigmoid and calculate the cross-entropy loss
loss =
# Get cost and define the optimizer
cost = tf.reduce_mean(loss)
opt = tf.train.AdamOptimizer(learning_rate).minimize(cost) | tutorials/autoencoder/Convolutional_Autoencoder.ipynb | WillenZh/deep-learning-project | mit |
Training
As before, here wi'll train the network. Instead of flattening the images though, we can pass them in as 28x28x1 arrays. | sess = tf.Session()
epochs = 20
batch_size = 200
sess.run(tf.global_variables_initializer())
for e in range(epochs):
for ii in range(mnist.train.num_examples//batch_size):
batch = mnist.train.next_batch(batch_size)
imgs = batch[0].reshape((-1, 28, 28, 1))
batch_cost, _ = sess.run([cost, opt], feed_dict={inputs_: imgs,
targets_: imgs})
print("Epoch: {}/{}...".format(e+1, epochs),
"Training loss: {:.4f}".format(batch_cost))
fig, axes = plt.subplots(nrows=2, ncols=10, sharex=True, sharey=True, figsize=(20,4))
in_imgs = mnist.test.images[:10]
reconstructed = sess.run(decoded, feed_dict={inputs_: in_imgs.reshape((10, 28, 28, 1))})
for images, row in zip([in_imgs, reconstructed], axes):
for img, ax in zip(images, row):
ax.imshow(img.reshape((28, 28)), cmap='Greys_r')
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
fig.tight_layout(pad=0.1)
sess.close() | tutorials/autoencoder/Convolutional_Autoencoder.ipynb | WillenZh/deep-learning-project | mit |
Denoising
As I've mentioned before, autoencoders like the ones you've built so far aren't too useful in practive. However, they can be used to denoise images quite successfully just by training the network on noisy images. We can create the noisy images ourselves by adding Gaussian noise to the training images, then clipping the values to be between 0 and 1. We'll use noisy images as input and the original, clean images as targets. Here's an example of the noisy images I generated and the denoised images.
Since this is a harder problem for the network, we'll want to use deeper convolutional layers here, more feature maps. I suggest something like 32-32-16 for the depths of the convolutional layers in the encoder, and the same depths going backward through the decoder. Otherwise the architecture is the same as before.
Exercise: Build the network for the denoising autoencoder. It's the same as before, but with deeper layers. I suggest 32-32-16 for the depths, but you can play with these numbers, or add more layers. | learning_rate = 0.001
inputs_ = tf.placeholder(tf.float32, (None, 28, 28, 1), name='inputs')
targets_ = tf.placeholder(tf.float32, (None, 28, 28, 1), name='targets')
### Encoder
conv1 =
# Now 28x28x32
maxpool1 =
# Now 14x14x32
conv2 =
# Now 14x14x32
maxpool2 =
# Now 7x7x32
conv3 =
# Now 7x7x16
encoded =
# Now 4x4x16
### Decoder
upsample1 =
# Now 7x7x16
conv4 =
# Now 7x7x16
upsample2 =
# Now 14x14x16
conv5 =
# Now 14x14x32
upsample3 =
# Now 28x28x32
conv6 =
# Now 28x28x32
logits =
#Now 28x28x1
# Pass logits through sigmoid to get reconstructed image
decoded =
# Pass logits through sigmoid and calculate the cross-entropy loss
loss =
# Get cost and define the optimizer
cost = tf.reduce_mean(loss)
opt = tf.train.AdamOptimizer(learning_rate).minimize(cost)
sess = tf.Session()
epochs = 100
batch_size = 200
# Set's how much noise we're adding to the MNIST images
noise_factor = 0.5
sess.run(tf.global_variables_initializer())
for e in range(epochs):
for ii in range(mnist.train.num_examples//batch_size):
batch = mnist.train.next_batch(batch_size)
# Get images from the batch
imgs = batch[0].reshape((-1, 28, 28, 1))
# Add random noise to the input images
noisy_imgs = imgs + noise_factor * np.random.randn(*imgs.shape)
# Clip the images to be between 0 and 1
noisy_imgs = np.clip(noisy_imgs, 0., 1.)
# Noisy images as inputs, original images as targets
batch_cost, _ = sess.run([cost, opt], feed_dict={inputs_: noisy_imgs,
targets_: imgs})
print("Epoch: {}/{}...".format(e+1, epochs),
"Training loss: {:.4f}".format(batch_cost)) | tutorials/autoencoder/Convolutional_Autoencoder.ipynb | WillenZh/deep-learning-project | mit |
Checking out the performance
Here I'm adding noise to the test images and passing them through the autoencoder. It does a suprisingly great job of removing the noise, even though it's sometimes difficult to tell what the original number is. | fig, axes = plt.subplots(nrows=2, ncols=10, sharex=True, sharey=True, figsize=(20,4))
in_imgs = mnist.test.images[:10]
noisy_imgs = in_imgs + noise_factor * np.random.randn(*in_imgs.shape)
noisy_imgs = np.clip(noisy_imgs, 0., 1.)
reconstructed = sess.run(decoded, feed_dict={inputs_: noisy_imgs.reshape((10, 28, 28, 1))})
for images, row in zip([noisy_imgs, reconstructed], axes):
for img, ax in zip(images, row):
ax.imshow(img.reshape((28, 28)), cmap='Greys_r')
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
fig.tight_layout(pad=0.1) | tutorials/autoencoder/Convolutional_Autoencoder.ipynb | WillenZh/deep-learning-project | mit |
Download prediction files
We release four groups of predictions:
base: the base MultiBERTs models (bert-base-uncased), with 5 coreference runs for each of 25 pretraining checkpoints.
cda_intervention-50k: as above, but with 50k steps of CDA applied to each checkpoint. 5 coreference runs for each of 25 pretraining checkpoints, paired with base.
from_scratch: trained from-scratch using CDA data. 5 coreference runs for each of 25 pretraining checkpoints, which are not paired with the above.
base_extra_seeds: 25 coreference runs for each of the first five pretraining seeds from base; used in Figure 6.
For each group, there are three files:
* run_info.tsv: run information, with columns pretrain_seed and finetune_seed
* label_info.tsv : labels and other metadata for each instance. 720 rows,
one for each Winogender example.
* preds.tsv: predictions on each instance, with rows aligned to those of
run_info.tsv and 720 columns which align to the rows of label_info.tsv.
The values in preds.tsv represent the index of the predicted referent, so for
Winogender this means:
- 0 is the occupation term
- 1 is the other_participant
You can also browse these files manually here: https://console.cloud.google.com/storage/browser/multiberts/public/example-predictions/coref | #@title Download predictions and metadata
scratch_dir = "/tmp/multiberts_coref"
if not os.path.isdir(scratch_dir):
os.mkdir(scratch_dir)
preds_root = "https://storage.googleapis.com/multiberts/public/example-predictions/coref"
GROUP_NAMES = [
'base',
'base_extra_seeds',
'cda_intervention-50k',
'from_scratch'
]
for name in GROUP_NAMES:
!mkdir -p $scratch_dir/$name
for fname in ['label_info.tsv', 'preds.tsv', 'run_info.tsv']:
!curl -s -O $preds_root/$name/$fname --output-dir $scratch_dir/$name
# Fetch Winogender occupations data from official repo https://github.com/rudinger/winogender-schemas
!curl -s -O https://raw.githubusercontent.com/rudinger/winogender-schemas/master/data/occupations-stats.tsv \
--output-dir $scratch_dir
!ls $scratch_dir/**
#@title Load run information
data_root = scratch_dir
all_run_info = []
for group_name in GROUP_NAMES:
run_info_path = os.path.join(data_root, group_name, "run_info.tsv")
run_info = pd.read_csv(run_info_path, sep='\t', index_col=0)
run_info['group_name'] = group_name
all_run_info.append(run_info)
run_info = pd.concat(all_run_info, axis=0, ignore_index=True)
run_info
# Count the number of runs in each group
run_info.groupby(by='group_name').apply(len)
#@title Load predictions
all_preds = []
for group_name in GROUP_NAMES:
preds_path = os.path.join(data_root, group_name, "preds.tsv")
all_preds.append(np.loadtxt(preds_path))
preds = np.concatenate(all_preds, axis=0)
preds.shape
#@title Load label info
label_info_path = os.path.join(data_root, GROUP_NAMES[0], "label_info.tsv")
label_info = pd.read_csv(label_info_path, sep='\t', index_col=0)
label_info | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Finally, load the occupations data from the U.S. Bureau of Labor Statistics, which we'll use to compute the bias correlation. | #@title Load occupations data
occupation_tsv_path = os.path.join(data_root, "occupations-stats.tsv")
# Link to BLS data
occupation_data = pd.read_csv(occupation_tsv_path, sep="\t").set_index("occupation")
occupation_pf = (occupation_data['bls_pct_female'] / 100.0).sort_index()
occupation_pf | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Define metrics
The values in preds.tsv represent binary predictions about whether each of our models predicts that the pronoun corresponds to the occupation term (0) or the other participant (1) in each Winogender example.
With this, we can compute two metrics:
- Accuracy against binary labels (whether the pronoun should refer to the occupation term, the answer column in label_info). For this, we'll run bootstrap over all 720 examples.
- Correlation of bias score against each occupation's P(female), according to the U.S. Bureau of Labor Statistics. This is done as in Webster et al. 2020 and Rudinger et al. 2018: for each profession, we compute the fraction of time when female pronouns resolve to it, the fraction of time that male pronouns resolve to it, and take the bias score to be the difference of these two quantities. For this, we'll aggregate to the 60 occupations, then run bootstrap over the set of occupations.
These will be used inside the bootstrap, so get_accuracy(), get_bias_corr(), and get_bias_slope() should all take two arguments, aligned lists of labels and predictions. | #@title Define metrics, test on one run
def get_accuracy(answers, binary_preds):
return np.mean(answers == binary_preds)
def get_bias_score(preds_row):
df = label_info.copy()
df['pred_occupation'] = (preds_row == 0)
m_pct = df[df["gender"] == "MASCULINE"].groupby(by="occupation")['pred_occupation'].agg('mean')
f_pct = df[df["gender"] == "FEMININE"].groupby(by="occupation")['pred_occupation'].agg('mean')
return (f_pct - m_pct).sort_index()
# Ensure this aligns with result of get_bias_score
sorted_occupations = sorted(list(label_info.occupation.unique()))
pf_bls = np.array([occupation_pf[occ] for occ in sorted_occupations])
def get_bias_corr_and_slope(pf_bls, bias_scores):
lr = scipy.stats.linregress(pf_bls, bias_scores)
return (lr.rvalue, lr.slope)
def get_bias_corr(pf_bls, bias_scores):
return get_bias_corr_and_slope(pf_bls, bias_scores)[0]
def get_bias_slope(pf_bls, bias_scores):
return get_bias_corr_and_slope(pf_bls, bias_scores)[1]
print("Accuracy:" , get_accuracy(label_info['answer'], preds[0]))
print("Bias r, slope:", get_bias_corr_and_slope(pf_bls, get_bias_score(preds[0]))) | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Computing the bias scores can be slow because of the grouping operations, so we preprocess all runs before running the bootstrap. This gives us a [num_runs, 60] matrix, and we can compute the final bias correlation inside the multibootstrap routine. | bias_scores = np.stack([get_bias_score(p) for p in preds], axis=0)
bias_scores.shape | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Finally, attach these to the run info dataframe - this will make it easier to filter by row later. | run_info['coref_preds'] = list(preds)
run_info['bias_scores'] = list(bias_scores) | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Plot overall scores for each group
Before we introduce the multibootstrap, let's get a high-level idea of what our metrics look like by just computing the mean scores for each group: | run_info['accuracy'] = [get_accuracy(label_info['answer'], p) for p in preds]
rs, slopes = zip(*[get_bias_corr_and_slope(pf_bls, bs) for bs in bias_scores])
run_info['bias_r'] = rs
run_info['bias_slope'] = slopes
run_info.groupby(by='group_name')[['accuracy', 'bias_r']].agg('mean') | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Note that accuracy is very similar across all groups, while - as we might expect - the bias correlation (bias_r) decreases significantly for the CDA runs. | # Accuracy across runs
data = run_info[run_info.group_name == 'base']
desc = data.groupby(by='pretrain_seed').agg(dict(accuracy='mean')).describe()
print(f"{desc.accuracy['mean']:.1%} +/- {desc.accuracy['std']:.1%}") | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
You can also check how much this varies by pretraining seed. As it turns out, not a lot. Here's a plot showing this for the base runs: | #@title Accuracy variation by pretrain run
fig = pyplot.figure(figsize=(15, 5))
ax = fig.gca()
sns.boxplot(ax=ax, x='pretrain_seed', y='accuracy', data=run_info[run_info.group_name == 'base'])
ax.set_title("Accuracy variation by pretrain seed, base")
ax.set_ylim(0, 1.0)
ax.axhline(0) | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
As a quick check, we can permute the seeds and see if much changes about our estimate: | # Accuracy across runs - randomized seed baseline
rng = np.random.RandomState(42)
data = run_info[run_info.group_name == 'base'].copy()
bs = data.accuracy.to_numpy()
data['accuracy_bs'] = rng.choice(bs, size=len(bs))
desc = data.groupby(by='pretrain_seed').agg(dict(accuracy_bs='mean')).describe()
print(f"With replacement: {desc.accuracy_bs['mean']:.1%} +/- {desc.accuracy_bs['std']:.1%}")
rng = np.random.RandomState(42)
data = run_info[run_info.group_name == 'base'].copy()
bs = data.accuracy.to_numpy()
rng.shuffle(bs)
data['accuracy_bs'] = bs
desc = data.groupby(by='pretrain_seed').agg(dict(accuracy_bs='mean')).describe()
print(f"Without replacement: {desc.accuracy_bs['mean']:.1%} +/- {desc.accuracy_bs['std']:.1%}") | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Figure 5 (Appendix): Bias correlation for each pre-training seed
Let's do the same as above, but for bias correlation. Again, this is on the whole run - no bootstrap yet - but should give us a sense of the variation you'd expect if you were to run this experiment ad-hoc on different pretraining seeds. As above, we'll just show the base runs: | fig = pyplot.figure(figsize=(15, 7))
ax = fig.gca()
base = sns.boxplot(ax=ax, x='pretrain_seed', y='bias_r', data=run_info[run_info.group_name == 'base'], palette=['darkslategray'])
ax.set_title("Winogender bias correlation (r) by pretrain seed")
ax.set_ylim(-0.2, 1.0)
ax.axhline(0)
legend_elements = [matplotlib.patches.Patch(facecolor='darkslategray', label='Base')]
ax.legend(handles=legend_elements, loc='upper right', fontsize=14)
ax.title.set_fontsize(16)
ax.set_xlabel("Pretraining Seed", fontsize=14)
ax.tick_params(axis='x', labelsize=14)
ax.set_ylabel("Bias correlation (r)", fontsize=14)
ax.tick_params(axis='y', labelsize=14)
# Expected range of accuracy if we randomly sampled data
import scipy.stats
[n/720.0 - 0.624 for n in scipy.stats.binom.interval(0.682, 720, 0.624)] | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Figure 3: Bias correlation by pretrain seed, base and CDA intervention
Now let's compare the base runs to running CDA for 50k steps. Again, no bootstrap yet - just plotting scores on full runs, to get a sense of how much difference we might expect to see if we did this ad-hoc and measured the effect size of CDA using just a single pretraining run. | expt_group = "cda_intervention-50k"
fig = pyplot.figure(figsize=(15, 7))
ax = fig.gca()
base = sns.boxplot(ax=ax, x='pretrain_seed', y='bias_r', data=run_info[run_info.group_name == 'base'], palette=['darkslategray'])
expt = sns.boxplot(ax=ax, x='pretrain_seed', y='bias_r', data=run_info[run_info.group_name == expt_group], palette=['lightgray'])
ax.set_title("Winogender bias correlation (r) by pretrain seed")
ax.set_ylim(-0.2, 1.0)
ax.axhline(0)
legend_elements = [matplotlib.patches.Patch(facecolor='darkslategray', label='Base'),
matplotlib.patches.Patch(facecolor='lightgray', label='CDA-incr')]
ax.legend(handles=legend_elements, loc='upper right', fontsize=14)
ax.title.set_fontsize(16)
ax.set_xlabel("Pretraining Seed", fontsize=14)
ax.tick_params(axis='x', labelsize=14)
ax.set_ylabel("Bias correlation (r)", fontsize=14)
ax.tick_params(axis='y', labelsize=14) | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Appendix D: Cross-Seed Variation
You might ask: how much of this variation is actually due to the coreference task training? We can see decently large error bars for each pretraining seed above, and we only had five coreference runs each.
One simple test is to ignore the pretraining seed. We'll create groups by randomly sampling (with replacement) five runs from the set of runs we have, then looking at the variance in the metrics. We can see that for bias_r, the variance is about 4x as high when using the real seeds (stdev = 0.097 vs 0.049), suggesting that most of the variation does in fact come from pretraining variation. | data = run_info[run_info.group_name == 'base'].copy()
bs = data.bias_r.to_numpy()
for i in range (5):
rng = np.random.RandomState(i)
data[f'bias_r_bs_{i}'] = rng.choice(bs, size=len(bs))
data.groupby(by='pretrain_seed').agg('mean').describe().loc[['mean', 'std']] | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Figure 6: Extra task runs
Another way to test this is to look at the base_extra_seeds runs, where we ran 5 different pretraining seeds with 25 task runs. This gives us a better estimate of the mean for each pretraining seed. | fig = pyplot.figure(figsize=(8, 7))
ax = fig.gca()
sns.boxplot(ax=ax, x='pretrain_seed', y='bias_r', data=run_info[run_info.group_name == 'base_extra_seeds'])
ax.set_title("Bias variation by pretrain seed, base w/extra seeds")
ax.set_ylim(-0.2, 1.0)
ax.axhline(0)
ax.title.set_fontsize(16)
ax.set_xlabel("Pretraining Seed", fontsize=14)
ax.tick_params(axis='x', labelsize=14)
ax.set_ylabel("Bias correlation (r)", fontsize=14)
#ax.tick_params(axis='y', labelsize=14) | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Now we can also use the multibootstrap as a statistical test to check for differences between these seeds. We'll compare seed 0 to seed 1, and do an unpaired analysis: | #@title Bootstrap to test if seed 1 is different from seed 0
num_bootstrap_samples = 1000 #@param {type: "integer"}
rseed=42
mask = (run_info.group_name == 'base_extra_seeds')
mask &= (run_info.pretrain_seed == 0) | (run_info.pretrain_seed == 1)
selected_runs = run_info[mask].copy()
# Set intervention and seed columns
selected_runs['intervention'] = (selected_runs.pretrain_seed == 1)
selected_runs['seed'] = selected_runs.pretrain_seed
print("Available runs:", len(selected_runs))
##
# Compute bias r
print("Computing bias r")
labels = pf_bls.copy()
print("Labels:", labels.dtype, labels.shape)
preds = np.stack(selected_runs.bias_scores)
print("Preds:", preds.dtype, preds.shape)
metric = get_bias_corr
samples = multibootstrap.multibootstrap(selected_runs, preds, labels,
metric, nboot=num_bootstrap_samples,
paired_seeds=False,
rng=rseed,
progress_indicator=tqdm)
multibootstrap.report_ci(samples, c=0.95, expect_negative_effect=False); | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Section 4.1 / Table 1: Paired analysis: base vs. CDA intervention
We've seen how much variation there can be across pretraining checkpoints, so let's use the multibootstrap to help us get a better estimate of the effectiveness of CDA. Here, we'll look at CDA for 50k steps as an intervention on the base checkpoints, and so we'll perform a paired analysis where we sample the same pretraining seeds from both sides.
base (L) is MultiBERTs following the original BERT recipe, and expt (L') has additional steps with counterfactual data applied to these same checkpoints. We have 25 pretraining seeds on base and the same 25 pretraining seeds on expt. | num_bootstrap_samples = 1000 #@param {type: "integer"}
rseed=42
expt_group = "cda_intervention-50k"
mask = (run_info.group_name == 'base')
mask |= (run_info.group_name == expt_group)
selected_runs = run_info[mask].copy()
# Set intervention and seed columns
selected_runs['intervention'] = selected_runs.group_name == expt_group
selected_runs['seed'] = selected_runs.pretrain_seed
print("Available runs:", len(selected_runs))
all_samples = {}
##
# Compute accuracy
print("Computing accuracy")
labels = np.array(label_info['answer'])
print("Labels:", labels.dtype, labels.shape)
preds = np.stack(selected_runs.coref_preds)
print("Preds:", preds.dtype, preds.shape)
metric = get_accuracy
samples = multibootstrap.multibootstrap(selected_runs, preds, labels,
metric, nboot=num_bootstrap_samples,
paired_seeds=True,
rng=rseed,
progress_indicator=tqdm)
all_samples['accuracy'] = samples
multibootstrap.report_ci(all_samples['accuracy'], c=0.95, expect_negative_effect=True);
print()
##
# Compute bias r
print("Computing bias r")
labels = pf_bls.copy()
print("Labels:", labels.dtype, labels.shape)
preds = np.stack(selected_runs.bias_scores)
print("Preds:", preds.dtype, preds.shape)
metric = get_bias_corr
samples = multibootstrap.multibootstrap(selected_runs, preds, labels,
metric, nboot=num_bootstrap_samples,
paired_seeds=True,
rng=rseed,
progress_indicator=tqdm)
all_samples['bias_r'] = samples
multibootstrap.report_ci(all_samples['bias_r'], c=0.95, expect_negative_effect=True); | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Plot result distribution
It can also be illustrative to look directly at the distribution of samples: | #@title Bias r
columns = ['Base', 'CDA intervention']
var_name = 'Group Name'
val_name = "Bias Correlation"
samples = all_samples['bias_r']
fig, axs = pyplot.subplots(1, 2, gridspec_kw=dict(width_ratios=[2, 1]), figsize=(15, 7))
bdf = pd.DataFrame(samples, columns=columns).melt(var_name=var_name, value_name=val_name)
bdf['x'] = 0
fig = pyplot.figure(figsize=(10, 7))
ax = axs[0]
sns.violinplot(ax=ax, x=var_name, y=val_name, data=bdf, inner='quartile')
ax.set_title("MultiBERTs CDA intervention - bias r")
ax.axhline(0)
var_name = 'Pretraining Steps'
val_name = "Accuracy delta"
bdf = pd.DataFrame(samples, columns=columns)
bdf['deltas'] = bdf['CDA intervention'] - bdf['Base']
bdf = bdf.drop(axis=1, labels=columns).melt(var_name=var_name, value_name=val_name)
bdf['x'] = 0
ax = axs[1]
sns.violinplot(ax=ax, x=var_name, y=val_name, data=bdf, inner='quartile',
palette='gray')
ax.set_title("MultiBERTs CDA intervention - bias r deltas")
ax.axhline(0)
multibootstrap.report_ci(samples, c=0.95, expect_negative_effect=True); | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Section 4.2 / Table 2: Unpaired analysis: CDA intervention vs. CDA from-scratch
Here, we'll compare our CDA 50k intervention to a set of models trained from-scratch with CDA data.
base (L) is the intevention CDA above, and expt (L') is a similar setup but pretraining from scratch with the counterfactually-augmented data. We have 25 pretraining seeds on base and 25 pretraining seeds on expt, but these are independent runs so we'll do an unpaired analysis. | num_bootstrap_samples = 1000 #@param {type: "integer"}
rseed=42
base_group = "cda_intervention-50k"
expt_group = "from_scratch"
mask = (run_info.group_name == base_group)
mask |= (run_info.group_name == expt_group)
selected_runs = run_info[mask].copy()
# Set intervention and seed columns
selected_runs['intervention'] = selected_runs.group_name == expt_group
selected_runs['seed'] = selected_runs.pretrain_seed
print("Available runs:", len(selected_runs))
all_samples = {}
##
# Compute accuracy
print("Computing accuracy")
labels = np.array(label_info['answer'])
print("Labels:", labels.dtype, labels.shape)
preds = np.stack(selected_runs.coref_preds)
print("Preds:", preds.dtype, preds.shape)
metric = get_accuracy
samples = multibootstrap.multibootstrap(selected_runs, preds, labels,
metric, nboot=num_bootstrap_samples,
paired_seeds=False,
rng=rseed,
progress_indicator=tqdm)
all_samples['accuracy'] = samples
multibootstrap.report_ci(all_samples['accuracy'], c=0.95, expect_negative_effect=True);
print()
##
# Compute bias r
print("Computing bias r")
labels = pf_bls.copy()
print("Labels:", labels.dtype, labels.shape)
preds = np.stack(selected_runs.bias_scores)
print("Preds:", preds.dtype, preds.shape)
metric = get_bias_corr
samples = multibootstrap.multibootstrap(selected_runs, preds, labels,
metric, nboot=num_bootstrap_samples,
paired_seeds=False,
rng=rseed,
progress_indicator=tqdm)
all_samples['bias_r'] = samples
multibootstrap.report_ci(all_samples['bias_r'], c=0.95, expect_negative_effect=True);
#@title Bias r
columns = ['CDA intervention', 'CDA from-scratch']
var_name = 'Group Name'
val_name = "Bias Correlation"
samples = all_samples['bias_r']
fig, axs = pyplot.subplots(1, 2, gridspec_kw=dict(width_ratios=[2, 1]), figsize=(15, 7))
bdf = pd.DataFrame(samples, columns=columns).melt(var_name=var_name, value_name=val_name)
bdf['x'] = 0
ax = axs[0]
sns.violinplot(ax=ax, x=var_name, y=val_name, data=bdf, inner='quartile')
ax.set_title("MultiBERTs CDA intervention vs. from-scratch - bias r")
ax.axhline(0)
var_name = 'Pretraining Steps'
val_name = "Accuracy delta"
bdf = pd.DataFrame(samples, columns=columns)
bdf['deltas'] = bdf['CDA from-scratch'] - bdf['CDA intervention']
bdf = bdf.drop(axis=1, labels=columns).melt(var_name=var_name, value_name=val_name)
bdf['x'] = 0
ax = axs[1]
sns.violinplot(ax=ax, x=var_name, y=val_name, data=bdf, inner='quartile',
palette='gray')
ax.set_title("MultiBERTs CDA intervention vs. from-scratch - bias r deltas")
ax.axhline(0)
multibootstrap.report_ci(samples, c=0.95, expect_negative_effect=True); | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
Do we actually need to do the full multiboostrap, where we sample over both seeds and examples simultaneously? We can check this with ablations where we sample over one axis only:
Seeds only (sample_examples=False)
Examples only (sample_seeds=False) | #@title As above, but sample seeds only
rseed=42
metric = get_bias_corr
samples = multibootstrap.multibootstrap(selected_runs, preds, labels,
metric, nboot=num_bootstrap_samples,
rng=rseed,
paired_seeds=False,
sample_examples=False,
progress_indicator=tqdm)
columns = ['CDA intervention', 'CDA from-scratch']
var_name = 'Group Name'
val_name = "Bias Correlation"
fig, axs = pyplot.subplots(1, 2, gridspec_kw=dict(width_ratios=[2, 1]), figsize=(15, 7))
bdf = pd.DataFrame(samples, columns=columns).melt(var_name=var_name, value_name=val_name)
bdf['x'] = 0
ax = axs[0]
sns.violinplot(ax=ax, x=var_name, y=val_name, data=bdf, inner='quartile')
ax.set_title("MultiBERTs CDA intervention vs. from-scratch - bias r")
ax.axhline(0)
var_name = 'Pretraining Steps'
val_name = "Accuracy delta"
bdf = pd.DataFrame(samples, columns=columns)
bdf['deltas'] = bdf['CDA from-scratch'] - bdf['CDA intervention']
bdf = bdf.drop(axis=1, labels=columns).melt(var_name=var_name, value_name=val_name)
bdf['x'] = 0
ax = axs[1]
sns.violinplot(ax=ax, x=var_name, y=val_name, data=bdf, inner='quartile',
palette='gray')
ax.set_title("MultiBERTs CDA intervention vs. from-scratch - bias r deltas")
ax.axhline(0)
multibootstrap.report_ci(samples, c=0.95, expect_negative_effect=True);
#@title As above, but sample examples only
rseed=42
metric = get_bias_corr
samples = multibootstrap.multibootstrap(selected_runs, preds, labels,
metric, nboot=num_bootstrap_samples,
rng=rseed,
paired_seeds=False,
sample_seeds=False,
progress_indicator=tqdm)
columns = ['CDA intervention', 'CDA from-scratch']
var_name = 'Group Name'
val_name = "Bias Correlation"
fig, axs = pyplot.subplots(1, 2, gridspec_kw=dict(width_ratios=[2, 1]), figsize=(15, 7))
bdf = pd.DataFrame(samples, columns=columns).melt(var_name=var_name, value_name=val_name)
bdf['x'] = 0
ax = axs[0]
sns.violinplot(ax=ax, x=var_name, y=val_name, data=bdf, inner='quartile')
ax.set_title("MultiBERTs CDA intervention vs. from-scratch - bias r")
ax.axhline(0)
var_name = 'Pretraining Steps'
val_name = "Accuracy delta"
bdf = pd.DataFrame(samples, columns=columns)
bdf['deltas'] = bdf['CDA from-scratch'] - bdf['CDA intervention']
bdf = bdf.drop(axis=1, labels=columns).melt(var_name=var_name, value_name=val_name)
bdf['x'] = 0
ax = axs[1]
sns.violinplot(ax=ax, x=var_name, y=val_name, data=bdf, inner='quartile',
palette='gray')
ax.set_title("MultiBERTs CDA intervention vs. from-scratch - bias r deltas")
ax.axhline(0)
multibootstrap.report_ci(samples, c=0.95, expect_negative_effect=True); | language/multiberts/coref.ipynb | google-research/language | apache-2.0 |
50% ham in sample | make_plot(0.5) | examples/squiggle_classifier_1/Read_Until_Efficiency.ipynb | akloster/porekit-python | isc |
10% ham in sample | make_plot(0.1) | examples/squiggle_classifier_1/Read_Until_Efficiency.ipynb | akloster/porekit-python | isc |
1% ham in sample | make_plot(0.01) | examples/squiggle_classifier_1/Read_Until_Efficiency.ipynb | akloster/porekit-python | isc |
Search Feature Sets
Feature sets are the logical containers for genomic features that might be defined in a GFF3, or other file that describes features in genomic coordinates. They are mapped to a single reference set, and belong to specific datasets. | for feature_set in c.search_feature_sets(dataset_id=dataset.id):
print feature_set
if feature_set.name == "gencode_v24lift37":
gencode = feature_set | python_notebooks/1kg_sequence_annotation_service.ipynb | david4096/bioapi-examples | apache-2.0 |
Get Feature Set by ID
With the identifier to a specific Feature Set, one can retrieve that feature set by ID. | feature_set = c.get_feature_set(feature_set_id=gencode.id)
print feature_set | python_notebooks/1kg_sequence_annotation_service.ipynb | david4096/bioapi-examples | apache-2.0 |
Search Features
With a Feature Set ID, it becomes possible to construct a Search Features Request. In this request, we can find genomic features by position, type, or name. In this request we simply return all features in the Feature Set. | counter = 0
for features in c.search_features(feature_set_id=feature_set.id):
if counter > 3:
break
counter += 1
print"Id: {},".format(features.id)
print" Name: {},".format(features.name)
print" Gene Symbol: {},".format(features.gene_symbol)
print" Parent Id: {},".format(features.parent_id)
if features.child_ids:
for i in features.child_ids:
print" Child Ids: {}".format(i)
print" Feature Set Id: {},".format(features.feature_set_id)
print" Reference Name: {},".format(features.reference_name)
print" Start: {},\tEnd: {},".format(features.start, features.end)
print" Strand: {},".format(features.strand)
print" Feature Type Id: {},".format(features.feature_type.id)
print" Feature Type Term: {},".format(features.feature_type.term)
print" Feature Type Sorce Name: {},".format(features.feature_type.source_name)
print" Feature Type Source Version: {}\n".format(features.feature_type.source_version) | python_notebooks/1kg_sequence_annotation_service.ipynb | david4096/bioapi-examples | apache-2.0 |
Note: Not all of the elements returned in the response are present in the example. All of the parameters will be shown in the get by id method.
We can perform a similar search, this time restricting to a specific genomic region. | for feature in c.search_features(feature_set_id=feature_set.id, reference_name="chr17", start=42000000, end=42001000):
print feature.name, feature.start, feature.end
feature = c.get_feature(feature_id=features.id)
print"Id: {},".format(feature.id)
print" Name: {},".format(feature.name)
print" Gene Symbol: {},".format(feature.gene_symbol)
print" Parent Id: {},".format(feature.parent_id)
if feature.child_ids:
for i in feature.child_ids:
print" Child Ids: {}".format(i)
print" Feature Set Id: {},".format(feature.feature_set_id)
print" Reference Name: {},".format(feature.reference_name)
print" Start: {},\tEnd: {},".format(feature.start, feature.end)
print" Strand: {},".format(feature.strand)
print" Feature Type Id: {},".format(feature.feature_type.id)
print" Feature Type Term: {},".format(feature.feature_type.term)
print" Feature Type Sorce Name: {},".format(feature.feature_type.source_name)
print" Feature Type Source Version: {}\n".format(feature.feature_type.source_version)
for vals in feature.attributes.vals:
print"{}: {}".format(vals, feature.attributes.vals[vals].values[0].string_value) | python_notebooks/1kg_sequence_annotation_service.ipynb | david4096/bioapi-examples | apache-2.0 |
Try changing the message in the previous code cell and re-running it. Does it behave as you expect?
You may remember from the Getting Started WIth Notebooks.ipynb notebook that if the last statement in a code cell returns a value, the value will be displayed as the output of the code cell when the cell contents have been executed.
If you place the name of a variable, or one or more comma separated variables, on the last line of a code cell, the value will be displayed.
What do you think the output of the following cell will be? Run the cell to find out. | message | robotVM/notebooks/Demo - Square 2 - Variables.ipynb | psychemedia/ou-robotics-vrep | apache-2.0 |
You can assign whatever object you like to a variable.
For example, we can assign numbers to them and do sums with them: | #Assign raw numbers to variables
apples=5
oranges=10
#Do a sum with the values represented by the variables and assign the result to a new variable
items_in_basket = apples + oranges
#Display the resulting value as the cell output
items_in_basket | robotVM/notebooks/Demo - Square 2 - Variables.ipynb | psychemedia/ou-robotics-vrep | apache-2.0 |
See if you can add the count of a new set of purchases to the number of items in your basket in the cell above. For example, what if you also bought 3 pears. And a bunch of bananas.
Making Use of Variables
Let's look back at our simple attempt at the square drawing program, in which we repeated blocks of instructions and set the numberical parameter values separately in each case.
Before we run the program, we need to load in the bits we need... | %run 'Set-up.ipynb'
%run 'Loading scenes.ipynb'
%run 'vrep_models/PioneerP3DX.ipynb' | robotVM/notebooks/Demo - Square 2 - Variables.ipynb | psychemedia/ou-robotics-vrep | apache-2.0 |
The original programme appears in the code cell below.
how many changes would you have to make to it in order to change the side length?
can you see how you might be able to simplify the act of changing the side length?
what would you need to change if you wanted to make the turns faster? Or slower?
HINT: think variables... | %%vrepsim '../scenes/OU_Pioneer.ttt' PioneerP3DX
import time
#side 1
robot.move_forward()
time.sleep(1)
#turn 1
robot.rotate_left(1.8)
time.sleep(0.45)
#side 2
robot.move_forward()
time.sleep(1)
#turn 2
robot.rotate_left(1.8)
time.sleep(0.45)
#side 3
robot.move_forward()
time.sleep(1)
#turn 3
robot.rotate_left(1.8)
time.sleep(0.45)
#side 4
robot.move_forward()
time.sleep(1) | robotVM/notebooks/Demo - Square 2 - Variables.ipynb | psychemedia/ou-robotics-vrep | apache-2.0 |
Using the above programme as a guide, see if you can write a programme in the code cell below that makes it easier to maintin and simplifies the act of changing the numerical parameter values. | %%vrepsim '../scenes/OU_Pioneer.ttt' PioneerP3DX
import time
#YOUR CODE HERE | robotVM/notebooks/Demo - Square 2 - Variables.ipynb | psychemedia/ou-robotics-vrep | apache-2.0 |
How did you get on?
How easy is is to change the side length now? Or find a new combination of the turn speed and turn angle to turn through ninety degrees (or thereabouts?). Try it and see...
Here's the programme I came up with: I used three variables, one for side length, one for turn time, and one for turn speed. Feel free to try running and modifying this programme too... | %%vrepsim '../scenes/OU_Pioneer.ttt' PioneerP3DX
import time
side_length_time=1
turn_speed=1.8
turn_time=0.45
#side 1
robot.move_forward()
time.sleep(side_length_time)
#turn 1
robot.rotate_left(turn_speed)
time.sleep(turn_time)
#side 2
robot.move_forward()
time.sleep(side_length_time)
#turn 2
robot.rotate_left(turn_speed)
time.sleep(turn_time)
#side 3
robot.move_forward()
time.sleep(side_length_time)
#turn 3
robot.rotate_left(turn_speed)
time.sleep(turn_time)
#side 4
robot.move_forward()
time.sleep(side_length_time) | robotVM/notebooks/Demo - Square 2 - Variables.ipynb | psychemedia/ou-robotics-vrep | apache-2.0 |
Document Table of Contents
1. Key Properties
2. Key Properties --> Software Properties
3. Key Properties --> Timestep Framework
4. Key Properties --> Meteorological Forcings
5. Key Properties --> Resolution
6. Key Properties --> Tuning Applied
7. Transport
8. Emissions
9. Concentrations
10. Optical Radiative Properties
11. Optical Radiative Properties --> Absorption
12. Optical Radiative Properties --> Mixtures
13. Optical Radiative Properties --> Impact Of H2o
14. Optical Radiative Properties --> Radiative Scheme
15. Optical Radiative Properties --> Cloud Interactions
16. Model
1. Key Properties
Key properties of the aerosol model
1.1. Model Overview
Is Required: TRUE Type: STRING Cardinality: 1.1
Overview of aerosol model. | # PROPERTY ID - DO NOT EDIT !
DOC.set_id('cmip6.aerosol.key_properties.model_overview')
# PROPERTY VALUE:
# Set as follows: DOC.set_value("value")
# TODO - please enter value(s)
| notebooks/messy-consortium/cmip6/models/emac-2-53-aerchem/aerosol.ipynb | ES-DOC/esdoc-jupyterhub | gpl-3.0 |
1.2. Model Name
Is Required: TRUE Type: STRING Cardinality: 1.1
Name of aerosol model code | # PROPERTY ID - DO NOT EDIT !
DOC.set_id('cmip6.aerosol.key_properties.model_name')
# PROPERTY VALUE:
# Set as follows: DOC.set_value("value")
# TODO - please enter value(s)
| notebooks/messy-consortium/cmip6/models/emac-2-53-aerchem/aerosol.ipynb | ES-DOC/esdoc-jupyterhub | gpl-3.0 |
1.3. Scheme Scope
Is Required: TRUE Type: ENUM Cardinality: 1.N
Atmospheric domains covered by the aerosol model | # PROPERTY ID - DO NOT EDIT !
DOC.set_id('cmip6.aerosol.key_properties.scheme_scope')
# PROPERTY VALUE(S):
# Set as follows: DOC.set_value("value")
# Valid Choices:
# "troposhere"
# "stratosphere"
# "mesosphere"
# "mesosphere"
# "whole atmosphere"
# "Other: [Please specify]"
# TODO - please enter value(s)
| notebooks/messy-consortium/cmip6/models/emac-2-53-aerchem/aerosol.ipynb | ES-DOC/esdoc-jupyterhub | gpl-3.0 |
1.4. Basic Approximations
Is Required: TRUE Type: STRING Cardinality: 1.1
Basic approximations made in the aerosol model | # PROPERTY ID - DO NOT EDIT !
DOC.set_id('cmip6.aerosol.key_properties.basic_approximations')
# PROPERTY VALUE:
# Set as follows: DOC.set_value("value")
# TODO - please enter value(s)
| notebooks/messy-consortium/cmip6/models/emac-2-53-aerchem/aerosol.ipynb | ES-DOC/esdoc-jupyterhub | gpl-3.0 |
1.5. Prognostic Variables Form
Is Required: TRUE Type: ENUM Cardinality: 1.N
Prognostic variables in the aerosol model | # PROPERTY ID - DO NOT EDIT !
DOC.set_id('cmip6.aerosol.key_properties.prognostic_variables_form')
# PROPERTY VALUE(S):
# Set as follows: DOC.set_value("value")
# Valid Choices:
# "3D mass/volume ratio for aerosols"
# "3D number concenttration for aerosols"
# "Other: [Please specify]"
# TODO - please enter value(s)
| notebooks/messy-consortium/cmip6/models/emac-2-53-aerchem/aerosol.ipynb | ES-DOC/esdoc-jupyterhub | gpl-3.0 |
1.6. Number Of Tracers
Is Required: TRUE Type: INTEGER Cardinality: 1.1
Number of tracers in the aerosol model | # PROPERTY ID - DO NOT EDIT !
DOC.set_id('cmip6.aerosol.key_properties.number_of_tracers')
# PROPERTY VALUE:
# Set as follows: DOC.set_value(value)
# TODO - please enter value(s)
| notebooks/messy-consortium/cmip6/models/emac-2-53-aerchem/aerosol.ipynb | ES-DOC/esdoc-jupyterhub | gpl-3.0 |
1.7. Family Approach
Is Required: TRUE Type: BOOLEAN Cardinality: 1.1
Are aerosol calculations generalized into families of species? | # PROPERTY ID - DO NOT EDIT !
DOC.set_id('cmip6.aerosol.key_properties.family_approach')
# PROPERTY VALUE:
# Set as follows: DOC.set_value(value)
# Valid Choices:
# True
# False
# TODO - please enter value(s)
| notebooks/messy-consortium/cmip6/models/emac-2-53-aerchem/aerosol.ipynb | ES-DOC/esdoc-jupyterhub | gpl-3.0 |
2. Key Properties --> Software Properties
Software properties of aerosol code
2.1. Repository
Is Required: FALSE Type: STRING Cardinality: 0.1
Location of code for this component. | # PROPERTY ID - DO NOT EDIT !
DOC.set_id('cmip6.aerosol.key_properties.software_properties.repository')
# PROPERTY VALUE:
# Set as follows: DOC.set_value("value")
# TODO - please enter value(s)
| notebooks/messy-consortium/cmip6/models/emac-2-53-aerchem/aerosol.ipynb | ES-DOC/esdoc-jupyterhub | gpl-3.0 |
2.2. Code Version
Is Required: FALSE Type: STRING Cardinality: 0.1
Code version identifier. | # PROPERTY ID - DO NOT EDIT !
DOC.set_id('cmip6.aerosol.key_properties.software_properties.code_version')
# PROPERTY VALUE:
# Set as follows: DOC.set_value("value")
# TODO - please enter value(s)
| notebooks/messy-consortium/cmip6/models/emac-2-53-aerchem/aerosol.ipynb | ES-DOC/esdoc-jupyterhub | gpl-3.0 |
2.3. Code Languages
Is Required: FALSE Type: STRING Cardinality: 0.N
Code language(s). | # PROPERTY ID - DO NOT EDIT !
DOC.set_id('cmip6.aerosol.key_properties.software_properties.code_languages')
# PROPERTY VALUE(S):
# Set as follows: DOC.set_value("value")
# TODO - please enter value(s)
| notebooks/messy-consortium/cmip6/models/emac-2-53-aerchem/aerosol.ipynb | ES-DOC/esdoc-jupyterhub | gpl-3.0 |
3. Key Properties --> Timestep Framework
Physical properties of seawater in ocean
3.1. Method
Is Required: TRUE Type: ENUM Cardinality: 1.1
Mathematical method deployed to solve the time evolution of the prognostic variables | # PROPERTY ID - DO NOT EDIT !
DOC.set_id('cmip6.aerosol.key_properties.timestep_framework.method')
# PROPERTY VALUE:
# Set as follows: DOC.set_value("value")
# Valid Choices:
# "Uses atmospheric chemistry time stepping"
# "Specific timestepping (operator splitting)"
# "Specific timestepping (integrated)"
# "Other: [Please specify]"
# TODO - please enter value(s)
| notebooks/messy-consortium/cmip6/models/emac-2-53-aerchem/aerosol.ipynb | ES-DOC/esdoc-jupyterhub | gpl-3.0 |
3.2. Split Operator Advection Timestep
Is Required: FALSE Type: INTEGER Cardinality: 0.1
Timestep for aerosol advection (in seconds) | # PROPERTY ID - DO NOT EDIT !
DOC.set_id('cmip6.aerosol.key_properties.timestep_framework.split_operator_advection_timestep')
# PROPERTY VALUE:
# Set as follows: DOC.set_value(value)
# TODO - please enter value(s)
| notebooks/messy-consortium/cmip6/models/emac-2-53-aerchem/aerosol.ipynb | ES-DOC/esdoc-jupyterhub | gpl-3.0 |
3.3. Split Operator Physical Timestep
Is Required: FALSE Type: INTEGER Cardinality: 0.1
Timestep for aerosol physics (in seconds). | # PROPERTY ID - DO NOT EDIT !
DOC.set_id('cmip6.aerosol.key_properties.timestep_framework.split_operator_physical_timestep')
# PROPERTY VALUE:
# Set as follows: DOC.set_value(value)
# TODO - please enter value(s)
| notebooks/messy-consortium/cmip6/models/emac-2-53-aerchem/aerosol.ipynb | ES-DOC/esdoc-jupyterhub | gpl-3.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.