<jupyter_start><jupyter_text>Create Datapoints<jupyter_code>npoints = 10
def create_data(npoints):
    xs = (last-first)*np.random.random_sample((npoints,)) + first
    ys = torch.tensor(orig_p(xs)).float().view(npoints,1)
    xs = torch.tensor(xs).float().view(npoints,1)
    return xs, ys

def create_data_cheb(d):
    cheb_n = np.zeros(d)
    for i in reversed(range(0,d)):
        cheb_n[i] = np.cos(np.pi*(i)/(d-1))
    cheb_n = (last-first)*(np.copy(cheb_n) + 1)/(1.*2) + first
    ys = torch.tensor(orig_p(cheb_n)).float()
    xs = torch.tensor(cheb_n).float().view(d,1)
    return xs, ys

xs, ys = create_data_cheb(npoints)
p_xs = xs.data.numpy()
p_ys = ys.data.numpy()
plt.plot(p_xs, p_ys, 'x', label='data points')
plt.legend()<jupyter_output><empty_output><jupyter_text>DNN approximating $x^2$ on $[a,b]$ interval<jupyter_code>class Net_x2(nn.Module):
    def __init__(self, n, d, epsilon, a=0., b=1., w1=None, b1=None, p_k=None):
        super(Net_x2, self).__init__()
        self.n = n
        self.iter = int(np.log(1/epsilon))
        #self.lrelu = nn.LeakyReLU(0.1)
        print("number of h compositions %d" % self.iter)
        self.xrange = np.arange(first, last, 0.0005)
        self.a = a
        self.b = b
        self.c = (b-a)**2/4.
        
    def h(self, x):
        return 2*F.relu(x) - 4*F.relu(x-0.5) + 2*F.relu(x-1)

    def g1(self, x):
        return 2/(self.b-self.a)*F.relu(x-self.a) - 4/(self.b-self.a)*F.relu(x-(self.a+self.b)/2.) + 2/(self.b-self.a)*F.relu(x-self.b)
    
    def h_leaky(self, x):
        return 2*self.lrelu(x) - 4*self.lrelu(x-0.5) + 2*self.lrelu(x-1)
    
    def forward(self, x, plot=False):
        out = F.relu((self.a+self.b)*x - self.a*self.b)
        x = self.g1(x)
        
        if plot:
            test_x = torch.tensor(self.xrange).float().view(self.xrange.shape[0],1)
            plt.plot(self.xrange, test_x.data.numpy(), label='level 00')
            nn_ys = F.relu((self.a+self.b)*test_x - self.a*self.b).data.numpy()
            test_x = self.g1(test_x)
        
        for j in range(self.iter):
            
            out = out - self.c*x*(2.0**(-(j)*self.n))

            if plot:
                nn_ys = nn_ys - (self.c*test_x*(2.0**(-(j)*self.n))).data.numpy()
                plt.plot(self.xrange, (test_x*(2.0**(-(j)*self.n))).data.numpy(), ':', label='level %d'%j)
                plt.plot(self.xrange, nn_ys, label='out %d'%j)
                test_x = self.h(test_x)
            x = self.h(x)
                

        return out
eps = 0.00001
net = Net_x2(n, 1, eps, a=first, b=last)
#net = copy.deepcopy(net0)
criterion = nn.MSELoss()

net.eval()<jupyter_output>number of h compositions 11<jupyter_text>test accuracy<jupyter_code>test_xs, test_ys = create_data(10000)
output = net(test_xs)
loss = criterion(output, test_ys)
print("loss ", loss.data)
f, axes = plt.subplots(1,1, figsize=(8, 8), dpi=80, facecolor='w', edgecolor='k')


def plot_p_nn(net):
    xs, xys = test_xs[:100], test_ys[:100]
    nn_ys = net(xs).data.numpy()
    axes.plot(xys.numpy().squeeze(), ':', label='orig_x^2', color='r', linewidth=3)
    axes.plot(nn_ys, color='k', label='nn')
    axes.legend()
    

plot_p_nn(net)
axes.set_title('Approximation p^%d(x) using NN'%n)


plt.show()<jupyter_output><empty_output><jupyter_text>plot NN approximation and original $p(x)=x^2$<jupyter_code>xs = torch.tensor(np.array([0.2, 0.4, 0.8])).float().view(3,1)
nn_ys = net(xs).data.numpy()
print(nn_ys)
f, axes = plt.subplots(1,1, figsize=(8, 8), dpi=80, facecolor='w', edgecolor='k')


def plot_p_nn(orig_p, net, n1):
    x = np.arange(first, last, 0.0001)
    xs = torch.tensor(x).float().view(x.shape[0],1)
    nn_ys = net(xs, plot=True).data.numpy()
    axes.plot(x, orig_p(x), '--', label='orig_p')
    axes.plot(x, nn_ys, color='k', label='nn')
    axes.legend()
    

plot_p_nn(orig_p, net, 0)
axes.plot(p_xs, p_ys, 'o', color='k', label='data points')
axes.set_title('Approximation p^%d(x) using NN'%n)


plt.show()<jupyter_output><empty_output><jupyter_text>error dependency on $\epsilon$<jupyter_code>f, axes = plt.subplots(1,1, figsize=(8, 8), dpi=80, facecolor='w', edgecolor='k')
test_xs, test_ys = create_data(10000)
epsilons = [0.1**i for i in range(10)]
losses = []

for eps in epsilons:
    net = Net_x2(n, 1, eps, a=first, b=last)
    net.eval()
    output = net(test_xs)
    print(output.size(), test_ys.size())
    loss = criterion(output, test_ys).data
    print("eps", eps, "loss ", loss)
    losses += [loss]

axes.plot(epsilons, losses, '--', label='orig_p')
axes.set_yscale("log")
axes.set_xscale("log")
axes.set_xlabel("epsilon")
axes.set_ylabel("MSE")
axes.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1976073488570818, 'fraction_numerical': 0.027130954924161503, 'mean_word_length': 3.2256317689530687, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '494301', 'license': 'MIT', 'n_tokens_mistral': 2112, 'n_tokens_neox': 1991, 'n_words': 321, 'path': 'net_x2_ab.ipynb', 'repo_name': 'parshakova/cme216_chebnn', 'size': '4681'}