3.线性神经网络

3.线性神经网络

3.1 线性回归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
%matplotlib inline
import math
import time
import numpy as np
import torch
from d2l import torch as d2l

n=10000
a=torch.ones([n])
b=torch.ones([n])

#定义计时器
class Timer: #@save
#记录多次运行时间
def __init__(self):
self.times=[]
self.start()

def start(self):
#启动计时器
self.tik=time.time()

def stop(self):
#停止计时器并将时间记录在列表中
self.times.append(time.time()-self.tik)
return self.times[-1]

def avg(self):
#返回平均时间
return sum(self.times)/len(self.times)

def sum(self):
#返回时间总和
return sum(self.times)

def cumsum(self):
#返回累计时间
return np.array(self.times).cumsum().tolist()

阅读更多
2.预备知识