import numpy as np import math from sklearn.metrics import mean_squared_error import matplotlib.pyplot as plt import os import random import pandas as pd import seaborn as sns from tqdm import tqdm import datetime from scipy.stats import norm
# G就是(w1, ..., wm) # 高斯核的phi(x) def baseline_eval_gaussian(x, G, m): """Calculate the result of baseline random feature mapping
Parameters ---------- x: array, dimension = d The data point toinputto the baseline mapping G: matrix, dimension = m*d The matrix in the baseline random feature mapping m: integer The number of dimension that we want to reduce to """ left = np.cos(np.dot(G, x).astype(np.float32)) right = np.sin(np.dot(G, x).astype(np.float32)) return ((1/m)**0.5) * np.append(left, right)
def find_sigma(random_sample): all_distances = [] for i in range(len(random_sample)): # dimensionality: d #print(f'Calculating the distance of {i}th samples') distances = [] forj in range(len(random_sample)): if j!=i: distances.append(np.linalg.norm(random_sample[i] - random_sample[j])) distances.sort() all_distances.append(distances[50]) # 只取第50个 # all_distances.append(np.mean(distances)) return np.mean(all_distances)
1
find_sigma(np.random.normal(0, 1, (100,50)))
10.11949067744566
Main
Load dataset
1 2
from google.colab import drive drive.mount('/content/drive')
Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
1
%cd"/content/drive/My Drive/20FA/Recitation"
/content/drive/My Drive/20FA/Recitation
1 2
d = 10# Dimension of data d_ = 10# Number of multipliers
#d = 10 # dimension of the data point N = [d*i for i in range(1,d_+1)] # number of samplings episode = 100 epoch = 450 # number of experiments toperformforeach episode
for e in tqdm(range(episode), position=0, leave=True): #print(f'{N[n]} samplings, {e}th episode') true = np.repeat(true_values[e], epoch) # epoch=450 x = list_of_samples[e][0] y = list_of_samples[e][1] for i inrange(epoch):
[NbConvertApp] Converting notebook DM_recitation.ipynb to PDF
[NbConvertApp] Support files will be in DM_recitation_files/
[NbConvertApp] Making directory ./DM_recitation_files
[NbConvertApp] Writing 76544 bytes to ./notebook.tex
[NbConvertApp] Building PDF
[NbConvertApp] Running xelatex 3 times: [u'xelatex', u'./notebook.tex', '-quiet']
[NbConvertApp] Running bibtex 1 time: [u'bibtex', u'./notebook']
[NbConvertApp] WARNING | bibtex had problems, most likely because there were no citations
[NbConvertApp] PDF successfully created
[NbConvertApp] Writing 75123 bytes to DM_recitation.pdf