calculate w
calculate log p train and valid