AICS-Course icon indicating copy to clipboard operation
AICS-Course copied to clipboard

关于第二章实验的一些问题

Open lknt opened this issue 2 years ago • 1 comments

一个是您确实写了保存最优参数,但是似乎没有用起来,修改一下main_exp_2_1.py就可以(参数保存名字改为best.npy)

if __name__ == '__main__':
    mlp = build_mnist_mlp()
    evaluate(mlp)
    mlp.load_model('best.npy')
    evaluate(mlp)

另外一个是我对main_exp_2_1.py里面的evaluate的检测剩余批次的代码有些疑惑 其中的 last_batch = mlp.test_data.shape[0]/mlp.batch_size*mlp.batch_size 是否应该改为 last_batch = mlp.test_data.shape[0] % mlp.batch_size

def evaluate(mlp):
    pred_results = np.zeros([mlp.test_data.shape[0]])
    for idx in range(mlp.test_data.shape[0]/mlp.batch_size):
        batch_images = mlp.test_data[idx*mlp.batch_size:(idx+1)*mlp.batch_size, :-1]
        prob = mlp.forward(batch_images)
        pred_labels = np.argmax(prob, axis=1)
        pred_results[idx*mlp.batch_size:(idx+1)*mlp.batch_size] = pred_labels
    if mlp.test_data.shape[0] % mlp.batch_size >0: 
        **last_batch = mlp.test_data.shape[0]/mlp.batch_size*mlp.batch_size**
        batch_images = mlp.test_data[-last_batch:, :-1]
        prob = mlp.forward(batch_images)
        pred_labels = np.argmax(prob, axis=1)
        pred_results[-last_batch:] = pred_labels
    accuracy = np.mean(pred_results == mlp.test_data[:,-1])
    print('Accuracy in test set: %f' % accuracy)

lknt avatar Sep 03 '23 08:09 lknt

thanks for your question, 但是对于第二个问题,我不太记得清代码了。。

LeiWang1999 avatar Sep 03 '23 08:09 LeiWang1999