parse_args()
的使用:
parser = argparse.ArgumentParser()
;parser.add_argument('-epoches', type=int, default=15, help='batch size for dataloader')
;args = parser.parse_args()
把刚才的属性从parser
给args
,后面直接通过args
使用。import argparse
if __name__ == "__main__":
# 建立解析对象
parser = argparse.ArgumentParser()
# 给parser实例添加属性
parser.add_argument('-gpu', action='store_true', default=True, help='use gpu or not')
parser.add_argument('-bs', type=int, default=128, help='batch size for dataloader')
parser.add_argument('-epoches', type=int, default=15, help='batch size for dataloader')
# 把刚才的属性给args实例,后面就可以直接使用
args = parser.parse_args()
continous_feature_names = ['releaseYear', 'movieRatingCount', 'movieAvgRating', 'movieRatingStddev',
'userRatingCount', 'userAvgRating', 'userRatingStddev']
categorial_feature_names = ['userGenre1', 'userGenre2', 'userGenre3', 'userGenre4', 'userGenre5',
'movieGenre1', 'movieGenre2', 'movieGenre3', 'userId', 'movieId']
categorial_feature_vocabsize = [20] * 8 + [30001] + [1001]
# build dataset for train and test
batch_size = args.bs
train_data = build_dataset(args.train_path)
loader_train = DataLoader(train_data, batch_size=batch_size, num_workers=64, shuffle=True, pin_memory=True)
test_data = build_dataset(args.test_path)
loader_test = DataLoader(test_data, batch_size=batch_size, num_workers=64)
device = torch.device("cuda" if args.gpu else "cpu")
# train model
model = WideDeep(categorial_feature_vocabsize, continous_feature_names, categorial_feature_names, embed_dim=64)
if args.gpu:
model = model.to(device)
optimizer = optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-3)
best_acc = 0
# 这里就直接使用args.epoches
for ep in range(args.epoches):
train(ep)
best_acc = test(ep, best_acc)