def detectAnomaly()

in aiops/AnomalyDetection/model/Model.py [0:0]


def detectAnomaly(trainData,valDatas,testDatas,testLabels,args,dataset):
    trainData=torch.tensor(trainData,dtype=torch.float)
    valDatas=torch.tensor(valDatas,dtype=torch.float)
    testDatas=torch.tensor(testDatas,dtype=torch.float)
    testLabels=torch.tensor(testLabels,dtype=torch.float)
    seed = 1282028438#getSeed()#
    setup_seed(seed)
    loadMod = args.loadMod != 0
    needTrain = args.needTrain != 0
    trainDataset=myDataset(trainData,args.winlen)
    valDataset=myDataset(valDatas,args.winlen,type="validate")
    testDataset=myDataset(testDatas,args.winlen,testLabels,"test")
    trainDataLoader=DataLoader(trainDataset,batch_size=args.batchSize,shuffle=True)
    valDataLoader=DataLoader(valDataset,batch_size=args.batchSize,shuffle=True)
    testDataLoader=DataLoader(testDataset,batch_size=args.batchSize,shuffle=False)
    dirName = "MCNet_"+str(dataset)
    if not os.path.exists(dirName):
        os.mkdir(dirName)
    modelPath = dirName + "/MCNet_"+str(seed)+".pth"
    if not loadMod:
        model=MCNet(args).to(args.device)
    else:
        model=loadModel(modelPath,args).to(args.device)
    loss_fn=lossFunc
    epochs = 3
    best_loss = 9999999999
    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr,weight_decay=0.1)
    trainStart=time.time()
    if needTrain:
        last_loss = 999999999
        count = 0
        torch.save(model.cpu().state_dict(), modelPath)
        model = model.to(args.device)
        print("Saved PyTorch Model State to " + modelPath)
        for t in range(epochs):
            print(f"Epoch {t + 1}\n-------------------------------")
            train(trainDataLoader,model,loss_fn,args,optimizer,t)
            test_loss=validate(valDataLoader,model,loss_fn,args)
            if math.isnan(test_loss):
                break
            if last_loss < test_loss:
                count += 1
            else:
                count = 0
            if count >= 2 or math.isnan(test_loss):
                break
            last_loss = test_loss
            if test_loss < best_loss:
                best_loss = test_loss
                torch.save(model.cpu().state_dict(), modelPath)
                model = model.to(args.device)
                print("Saved PyTorch Model State to " + modelPath)
    trainEnd=time.time()
    trainCost=trainEnd-trainStart
    print("trainCost:",trainCost)
    model = loadModel(modelPath, args).to(args.device)
    inferStart = time.time()
    test_loss, precision, recall, F1, accuracy = test(testDataLoader, model, loss_fn, args)
    inferEnd = time.time()
    with open(dirName + "/res" + str(seed) + ".csv", "w") as f:
        f.write("%f,%f,%f,%f\n" % (precision, recall, F1, accuracy))
    with open(dirName + "/config" + str(seed) + ".txt", "w") as f:
        f.write(str(args))
    return [precision, recall, F1, accuracy]