def reset(self, params, repetition):
    """
    Called once at the beginning of each experiment.
    """
    self.startTime = time.time()
    print(params)
    seed = params["seed"] + repetition
    torch.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)

    # Get our directories correct
    self.dataDir = os.path.join(params["datadir"], "speech_commands")
    self.resultsDir = os.path.join(params["path"], params["name"], "plots")

    if not os.path.exists(self.resultsDir):
      os.makedirs(self.resultsDir)

    self.use_cuda = not params["no_cuda"] and torch.cuda.is_available()
    if self.use_cuda:
      print("*********using cuda!")
    self.device = torch.device("cuda" if self.use_cuda else "cpu")

    self.use_preprocessed_dataset = False
    self.loadDatasets(params)

    # Parse 'n' and 'k' parameters
    n = params["n"]
    k = params["k"]
    if isinstance(n, basestring):
      n = map(int, n.split("_"))
    if isinstance(k, basestring):
      k = map(int, k.split("_"))

    if params["model_type"] == "cnn":
      c1_out_channels = params["c1_out_channels"]
      c1_k = params["c1_k"]
      if isinstance(c1_out_channels, basestring):
        c1_out_channels = map(int, c1_out_channels.split("_"))
      if isinstance(c1_k, basestring):
        c1_k = map(int, c1_k.split("_"))

      # Parse 'c1_input_shape; parameter
      if "c1_input_shape" in params:
        c1_input_shape = map(int, params["c1_input_shape"].split("_"))
      else:
        c1_input_shape = (1, 32, 32)

      sp_model = SparseNet(
        inputSize=c1_input_shape,
        outputSize=len(self.train_loader.dataset.classes),
        outChannels=c1_out_channels,
        c_k=c1_k,
        kernelSize=5,
        stride=1,
        dropout=params["dropout"],
        n=n,
        k=k,
        boostStrength=params["boost_strength"],
        weightSparsity=params["weight_sparsity"],
        weightSparsityCNN=params["weight_sparsity_cnn"],
        boostStrengthFactor=params["boost_strength_factor"],
        kInferenceFactor=params["k_inference_factor"],
        useBatchNorm=params["use_batch_norm"],
        normalizeWeights=params.get("normalize_weights", False)
      )
    elif params["model_type"] == "resnet9":
      sp_model = resnet9(num_classes=len(self.train_loader.dataset.classes),
                         in_channels=1)
    elif params["model_type"] == "linear":
      sp_model = SparseNet(
        n=n,
        k=k,
        inputSize=32*32,
        outputSize=len(self.train_loader.dataset.classes),
        boostStrength=params["boost_strength"],
        weightSparsity=params["weight_sparsity"],
        boostStrengthFactor=params["boost_strength_factor"],
        kInferenceFactor=params["k_inference_factor"],
        dropout=params["dropout"],
        useBatchNorm=params["use_batch_norm"],
        normalizeWeights=params.get("normalize_weights", False)
      )
    else:
      raise RuntimeError("Unknown model type")

    if torch.cuda.device_count() > 1:
      print("Using", torch.cuda.device_count(), "GPUs")
      sp_model = torch.nn.DataParallel(sp_model)

    self.model = sp_model.to(self.device)
    self.learningRate = params["learning_rate"]
    self.optimizer = self.createOptimizer(params, self.model)
    self.lr_scheduler = self.createLearningRateScheduler(params, self.optimizer)
def reset ( self , params , repetition ) : self . startTime = time . time ( ) print ( params ) seed = params [ "seed" ] + repetition torch . manual_seed ( seed ) np . random . seed ( seed ) random . seed ( seed ) self . dataDir = os . path . join ( params [ "datadir" ] , "speech_commands" ) self . resultsDir = os . path . join ( params [ "path" ] , params [ "name" ] , "plots" ) if not os . path . exists ( self . resultsDir ) : os . makedirs ( self . resultsDir ) self . use_cuda = not params [ "no_cuda" ] and torch . cuda . is_available ( ) if self . use_cuda : print ( "*********using cuda!" ) self . device = torch . device ( "cuda" if self . use_cuda else "cpu" ) self . use_preprocessed_dataset = False self . loadDatasets ( params ) n = params [ "n" ] k = params [ "k" ] if isinstance ( n , basestring ) : n = map ( int , n . split ( "_" ) ) if isinstance ( k , basestring ) : k = map ( int , k . split ( "_" ) ) if params [ "model_type" ] == "cnn" : c1_out_channels = params [ "c1_out_channels" ] c1_k = params [ "c1_k" ] if isinstance ( c1_out_channels , basestring ) : c1_out_channels = map ( int , c1_out_channels . split ( "_" ) ) if isinstance ( c1_k , basestring ) : c1_k = map ( int , c1_k . split ( "_" ) ) if "c1_input_shape" in params : c1_input_shape = map ( int , params [ "c1_input_shape" ] . split ( "_" ) ) else : c1_input_shape = ( 1 , 32 , 32 ) sp_model = SparseNet ( inputSize = c1_input_shape , outputSize = len ( self . train_loader . dataset . classes ) , outChannels = c1_out_channels , c_k = c1_k , kernelSize = 5 , stride = 1 , dropout = params [ "dropout" ] , n = n , k = k , boostStrength = params [ "boost_strength" ] , weightSparsity = params [ "weight_sparsity" ] , weightSparsityCNN = params [ "weight_sparsity_cnn" ] , boostStrengthFactor = params [ "boost_strength_factor" ] , kInferenceFactor = params [ "k_inference_factor" ] , useBatchNorm = params [ "use_batch_norm" ] , normalizeWeights = params . get ( "normalize_weights" , False ) ) elif params [ "model_type" ] == "resnet9" : sp_model = resnet9 ( num_classes = len ( self . train_loader . dataset . classes ) , in_channels = 1 ) elif params [ "model_type" ] == "linear" : sp_model = SparseNet ( n = n , k = k , inputSize = 32 * 32 , outputSize = len ( self . train_loader . dataset . classes ) , boostStrength = params [ "boost_strength" ] , weightSparsity = params [ "weight_sparsity" ] , boostStrengthFactor = params [ "boost_strength_factor" ] , kInferenceFactor = params [ "k_inference_factor" ] , dropout = params [ "dropout" ] , useBatchNorm = params [ "use_batch_norm" ] , normalizeWeights = params . get ( "normalize_weights" , False ) ) else : raise RuntimeError ( "Unknown model type" ) if torch . cuda . device_count ( ) > 1 : print ( "Using" , torch . cuda . device_count ( ) , "GPUs" ) sp_model = torch . nn . DataParallel ( sp_model ) self . model = sp_model . to ( self . device ) self . learningRate = params [ "learning_rate" ] self . optimizer = self . createOptimizer ( params , self . model ) self . lr_scheduler = self . createLearningRateScheduler ( params , self . optimizer )
Called once at the beginning of each experiment.