一:自动微分编程实现
例如对于函数y = (x1+x2)*(x2+1),其中叶子节点为x1,x2,a,b为中间变量。
###加法结点编程实现
class add1:
def __init__(self):
pass
#正向传播
def forward(self,x,y):
out = x+y
#反向传播
def backward(self,out):
dx= dout*1
dy = dout*1
return dx,dy
#加法结点编程实现b
class add2:
def __init__(self):
pass
def forward(self,x):
out = x+1
return out
#反向传播
def bacward(self,dout):
dx = dout*1
return dx
#乘法结点编程实现
class mul():
def __init__(self,x,y):
self.x = x
self.y = y
#正向传播
def forward(self,x,y):
self.x = x
self.y = y
out = x*y
return out
#反向传播
def forward(self,dout)
dx = dout*self.x
dy = dout*self.y
return dx,dy
#自动微分编程实现
node_a = add1()
node_b = add2()
node_y = mul()
#前向计算
node_a_out = node_a.forward(x1,x2)
node_b_out = node_b.forward(x2)
node_y_out = node_y.forward(node_a_out,node_b_out)
#后向计算
dout = 1
dout_ya,dout_yb = node_y.backward(dout)
dout_x1,dout_x2_a = node_a.backward(dout_ya)
dout_x2_b = node_b.backward(dout_yb)
dout_x2 = dout_x2_b+dout_x2_a
class Affine:
def __init__(self,W,b):
self.W = W
self.b = b
self.x = None
self.dW = None
self.db = None
def forward(self,x):
self.x = x
out = np.dot(x,self.W) + self.b
return out
def backward(self,dout):
dx = np.dot(dout,self.W.T)
dW = np.dot(self.x.T,dout)
db = np.sum(dout,axis = 0)
return dx,dW,db
class Sigmoid():
def __init__():
self.out = None
def forward(self,x):
self.out = 1/(1+np.exp(-x))
return self.out
def backward(self,dout):
return dout*self.out * (1-self.out)
2.batchsize如何使用?
#使用这种方法随机生成一部分batchsize在训练集中,默认不重复
batch_mask = np.random.choice(train_size, batch_size)
x_batch = x_train[batch_mask]
t_batch = t_train[batch_mask]
3.数据可视化方面
#1.生成列表,以便于绘图
train_loss_list = []
train_acc_list = []
test_acc_list = []
test_loss_list = []
#2.每多少次迭代之后,记录损失和分类准确率
for i in range(iters_num):
batch_mask = np.random.choice(train_size, batch_size)
x_batch = x_train[batch_mask]
t_batch = t_train[batch_mask]
# 梯度
grad = network.gradient(x_batch, t_batch)
# 更新
for key in ('W1', 'b1', 'W2', 'b2'):
network.params[key] -= learning_rate * grad[key]
loss = network.loss(x_batch, t_batch)
train_loss_list.append(loss)
if i % iter_per_epoch == 0:
train_acc = network.accuracy(x_train, t_train)
test_acc = network.accuracy(x_test, t_test)
train_acc_list.append(train_acc)
test_acc_list.append(test_acc)
print(train_acc, test_acc)
#3.绘制图形
plt.plot(x, train_acc_list, label='train acc')
plt.plot(x, test_acc_list, label='test acc', linestyle='--')
plt.xlabel("epochs")
plt.ylabel("accuracy")
#plt.ylim(0, 1.0)
plt.legend(loc='lower right')
plt.show()
相关图片: