How can I turn twolayernet code into threelayernet code in backpropagation?

Viewed 40

I was learning backpropagation in python class and the task was to turn twolayernet code into threelayernet. Honestly I could not even understand twolayernet and it is almost impossible for me to turn that into threelayernet. So I really need help.

network = ThreeLayerNet(i_size=784, h1_size=100, h2_size=100, o_size=10)

train_size = x_train.shape[0]
batch_size = 100

learning_rate = 0.1

train_loss_list = []
train_acc_list = []
test_acc_list = []

iter_per_epoch = max(train_size // batch_size, 1)
epoch_num = 100
iters_num = epoch_num * iter_per_epoch

epoch = 0


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 network.params.keys():
        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)
        
        epoch += 1
        print( 'epoch {} accuracy: train {:.4f}, test {:.4f}'.format(epoch, train_acc,               test_acc) )

This is the code that I have been working on and I cannot figure out why I am having this error below

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-66-30b07a29d096> in <module>()
     24     t_batch = t_train[batch_mask]
     25 
---> 26     grad = network.gradient(x_batch, t_batch)
     27 
     28     for key in network.params.keys():

1 frames
<ipython-input-46-ec06e75be5ad> in backward(self, dout)
     16 
     17     def backward(self, dout):
---> 18         dx = np.dot(dout, self.W.T)
     19         self.dW = np.dot(self.x.T, dout)
     20         self.db = np.sum(dout, axis=0)

<__array_function__ internals> in dot(*args, **kwargs)

ValueError: shapes (100,10) and (100,784) not aligned: 10 (dim 1) != 100 (dim 0)

I think I might have done something wrong about hiddenlayer, but due to my short knowledge about python and deep learnig, I cannot figure out what it is.

I would be really thankful if anyone can help me. thank you very much.

0 Answers
Related