--linear regression
def data_iter(batch_size, features, labels):
num_examples = len(features)
indices = list(range(num_examples))
random.shuffle(indices) # random read 10 samples
for i in range(0, num_examples, batch_size):
j = torch.LongTensor(indices[i: min(i + batch_size, num_examples)]) # the last time may be not enough for a whole batch
yield features.index_select(0, j), labels.index_select(0, j) #return a generateur
通过这个函数返回一个generateur,之后放入循环可迭代。
线性回归主要分,设定参数,确定损失函数,设置优化方程,即求导,就损失函数的最小值。、
返回使得损失函数最小的参数和偏差。
--softmax分类
与回归问题不同,我们需要输出的是离散的值,属于分类问题
通过多个输出的概率,来判断种类
使用交叉熵损失函数
\ell(\boldsymbol{\Theta}) = \frac{1}{n} \sum_{i=1}^n H\left(\boldsymbol y^{(i)}, \boldsymbol {\hat y}^{(i)}\right ),
神经网络为两层 4
--多层感知机
最简单版本
神经网络一共三层 输入层 隐藏层 输出层
激活函数选择:
ReLU函数 提供一个很简单的非线性变化
Relu(x) = max(x,0)
ReLU函数只保留正数元素,将负数元素清零。
Sigmoid 函数 范围 [0,1] 使用较多
tanh函数 值域[-1, 1]
在神经网络层数较多的时候,最好使用ReLu函数,因为简单计算量少,而sigmoid和tanh函数计算量大很多。