1.数据集

```#读入数据
import sys
sys.path.append("..")
from dataset import spiral
print('x',x.shape)    #x (300, 2)
print('t',t.shape)    #t (300, 3)```

2.神经网络的实现

```class TwoLayerNet:
def __init__(self,input_size,hidden_size,output_size):
I,H,O = input_size,hidden_size,output_size

#初始化权重和偏置
W1 = 0.01 * np.random.randn(I,H)    #形状：I*H
b1 = np.zeros(H)
W2 = 0.01 * np.random.randn(H,O)
b2 = np.zeros(O)

#生成层
self.layers = [
Affine(W1,b1),
Sigmoid(),
Affine(W2,b2)
]
#Softmax With Loss层和其他层的处理方式不同
#所以不将它放在layers列表中，而是单独存储在变量loss_layer中
self.loss_layer = SoftmaxWithLoss()
for layer in self.layers:
self.params += layer.params
def predict(self,x):
for layer in self.layers:
x = layer.forward(x)
return x

def forward(self,x,t):
score = self.predict(x)
loss = self.loss_layer.forward(score,t)
return loss

def backward(self,dout = 1):
dout = self.loss_layer.backward(dout)
for layer in reversed(self.layers):
dout = layer.backward(dout)
return dout```

3.学习过程+结果

```#1.设定超参数
max_epoch = 300
batch_size = 30
hidden_size = 10
learning_rate  = 1.0
#2.读入数据，生成模型和优化器
model = TwoLayerNet(input_size=2,hidden_size=hidden_size,output_size=3)
optimizer = SGD(lr=learning_rate)
#学习用的变量
data_size = len(x)
max_iters = data_size // batch_size
total_loss = 0
loss_count = 0
loss_list = []
for epoch in range(max_epoch):
#3.打乱数据
idx = np.random.permutation(data_size)
x = x[idx]
t = t[idx]

for iters in range(max_iters):
batch_x = x[iters*batch_size:(iters+1)*batch_size]
batch_t = t[iters*batch_size:(iters+1)*batch_size]
#4.计算梯度，更新参数
loss = model.forward(batch_x,batch_t)
model.backward()
total_loss += loss
loss_count += 1
#5.定期输出学习过程
if (iters+1)%10 == 0:
avg_loss = total_loss / loss_count
print('| epoch %d | iter %d / %d | loss %0.2f'% (epoch+1,iters + 1,max_iters,avg_loss))
loss_list.append(avg_loss)
total_loss,loss_count = 0,0```

4.Affine层

```class Affine:
def __init__(self,W,b):
self.params = [W,b]#保存参数
self.x = None
def forward(self,x):
W,b = self.params
out = np.dot(x,W) + b
self.x = x
return out
def backward(self,dout):
W,b = self.params
dx = np.dot(dout,W.T)
dW = np.dot(self.x.T,dout)
db = np.sum(dout,axis=0)

return dx```

5.Sigmoid层

```class Sigmoid:
def __init__(self):
self.params = []
self.out = None
def forward(self,x):
out = 1 / (1 + np.exp(-x))
self.out = out
return out
def backward(self,dout):
dx = dout * (1.0 - self.out) * self.out
return dx```

6.Softmax With Loss层

```def softmax(x):
if x.ndim == 1:
x = x - np.max(x)
x = np.exp(x)/np.sum(np.exp(x))
elif x.ndim == 2:
x = x - x.max(axis = 1,keepdims = True)
x = np.exp(x)
x /= x.sum(axis=1, keepdims=True)
return x
def cross_entropy_error(y,t):
if y.ndim == 1:
t = t.reshape(1,t.size)
y = y.reshape(1,y.size)

#因为监督标签是one-hot-vector形式，所以这里要取下标
if t.size == y.size:
t = t.argmax(dim=1)

batch_size = y.shape[0]
#没看懂为啥
return -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7)) / batch_size

class SoftmaxWithLoss:
def __init__(self):
self.params = []
self.y = None #softmx的输出
self.t = None #监督标签

def forward(self,x,t):
self.t = t
self.y = softmax(x)

if self.t.size == self.y.size:
self.t = self.t.argmax(axis=1)

loss = cross_entropy_error(self.y,self.t)
return loss
def backward(self,dout =1):
batch_size = self.t.shape[0]
dx = self.y.copy()
dx[np.arange(batch_size),self.t] -= 1
dx *= dout
dx = dx/batch_size

return dx```

(1)监督数据为one-hot形式

(2)监督数据为标签形式（直接是“2”“3”这样的标签）

7.SGD层

```class SGD:
'''