def __init__(self, reliability_for_action, discount, learning_rate, momentum, bias, hidden_layers, number_of_neurons): #set instance variables self.reliability_for_action = reliability_for_action self.discount = discount self.learning_rate = learning_rate self.bias = bias self.momentum = momentum self.hidden_layers = hidden_layers self.number_of_neurons = number_of_neurons #create mlp self.mlp = MLP(self.learning_rate, self.momentum) self.mlp.add_layer(Layer(6)) #create defined number of hidden layers for layer in range(self.hidden_layers): self.mlp.add_layer(Layer(int(self.number_of_neurons[layer]))) self.mlp.add_layer(Layer(3)) self.mlp.init_network(self.bias)
def get_weight(source_feature_path, target_feature_path, validation_feature_path): # 这三个feature根据类别不同,是不一样的. source与target这里需注意一下数据量threshold 2倍的事儿 """ :param source_feature: shape [N_tr, d], features from training set :param target_feature: shape [N_te, d], features from test set :param validation_feature: shape [N_v, d], features from validation set :return: """ print("Start calculating weight") print("Loading feature files") source_feature = np.load(source_feature_path) target_feature = np.load(target_feature_path) validation_feature_np = np.load(validation_feature_path) N_s, d = source_feature.shape N_t, _d = target_feature.shape if float(N_s) / N_t > 2: source_feature = random_select_src(source_feature, target_feature) else: source_feature = source_feature.copy() print('num_source is {}, num_target is {}, ratio is {}\n'.format(N_s, N_t, float(N_s) / N_t)) # check the ratio N_s, d = source_feature.shape target_feature = target_feature.copy() all_feature = np.concatenate((source_feature, target_feature)) all_label = np.asarray([1] * N_s + [0] * N_t, dtype=np.int32) # 1->source 0->target feature_for_train_np, feature_for_test_np, label_for_train_np, label_for_test_np = train_test_split(all_feature, all_label, train_size=0.8) # here is train, test split, concatenating the data from source and target decays = [1e-1, 3e-2, 1e-2, 3e-3, 1e-3, 3e-4, 1e-4, 3e-5, 1e-5, 0.0005] val_acc = [] domain_classifiers = [] # created the MLP network MLP = mlp_network.MLP(d, 2).cuda() loss_function = nn.CrossEntropyLoss() # convert all numpy to variables feature_for_train = Variable(torch.from_numpy(feature_for_train_np)).cuda() feature_for_test = Variable(torch.from_numpy(feature_for_test_np)).cuda() label_for_train = Variable(torch.from_numpy(label_for_train_np).long()).cuda() label_for_test = Variable(torch.from_numpy(label_for_test_np).long()).cuda() validation_feature = Variable(torch.from_numpy(validation_feature_np)).cuda() print("start training") for decay in decays: print("Decay: {}".format(decay)) for ep in range(1, 1001): optimizer = torch.optim.Adam(MLP.parameters(), lr=0.001, weight_decay=decay) pred = MLP(feature_for_train) loss = loss_function(pred, label_for_train) optimizer.zero_grad() loss.backward() optimizer.step() # check training accuracy if ep % 100 == 0: print("Check for testing set accuracy") pred_test = MLP(feature_for_test) predicted_test = torch.max(F.softmax(pred_test), dim=1)[1] pred_y = predicted_test.detach().cpu().numpy().squeeze() label_y = label_for_test.detach().cpu().numpy() accuracy = sum(pred_y == label_y) / label_y.size print("Accuracy is {}".format(accuracy)) pre_path = "MLP" + str(accuracy) path = pre_path.replace(".", "_") + ".pth" torch.save(MLP, path) domain_classifiers.append(path) val_acc.append(accuracy) index = val_acc.index(max(val_acc)) path_2_load = domain_classifiers[index] Best_MLP = torch.load(path_2_load) out = Best_MLP(validation_feature) domain_out = out.detach().cpu().numpy() print("Domain out: {}".format(domain_out)) return domain_out[:, :1] / domain_out[:, 1:] * N_s * 1.0 / N_t # (Ntr/Nts)*(1-M(fv))/M(fv)
class brainModel: def __init__(self, reliability_for_action, discount, learning_rate, momentum, bias, hidden_layers, number_of_neurons): #set instance variables self.reliability_for_action = reliability_for_action self.discount = discount self.learning_rate = learning_rate self.bias = bias self.momentum = momentum self.hidden_layers = hidden_layers self.number_of_neurons = number_of_neurons #create mlp self.mlp = MLP(self.learning_rate, self.momentum) self.mlp.add_layer(Layer(6)) #create defined number of hidden layers for layer in range(self.hidden_layers): self.mlp.add_layer(Layer(int(self.number_of_neurons[layer]))) self.mlp.add_layer(Layer(3)) self.mlp.init_network(self.bias) def get_params(self): """ returns the instance variables """ return self.reliability_for_action, self.discount, self.learning_rate, self.momentum, self.bias, self.hidden_layers, self.number_of_neurons def set_params(self, reliability_for_action, discount, learning_rate, momentum): """ sets changable instace variables, especially the mlp config """ self.reliability_for_action = reliability_for_action self.discount = discount self.learning_rate = learning_rate self.mlp.learning_rate = self.learning_rate self.mlp.discount = self.discount self.mlp.momentum = momentum def get_reward(self, input_vals): """ checks for reward """ right_color_no = 0 # 0 for red, 1 for green and 2 for yellow right_color_position_no = right_color_no + 1 right_color_position_difference = abs(input_vals[right_color_position_no]) right_color = 0.0 + input_vals[right_color_no] reward = 0.0 #right_color = 0.0 + right_color / 10 reward = right_color * (1 - right_color_position_difference) return reward def update_weights(self, old_q, new_q, old_action, new_action, old_input_vals, new_input_vals, reward): """ calculates target values for MLP and back propagates them """ old_q_vector = self.mlp.get_result(old_input_vals) if (reward == 1): prediction_error = reward else: prediction_error = reward + self.discount * new_q[new_action] - old_q_vector[old_action] new_q = [old_q_vector[0],old_q_vector[1],old_q_vector[2]] new_q[old_action] += prediction_error error = self.mlp.back_propagate(new_q) self.mlp.get_result(new_input_vals) def select_action(self,q_vector): """ selects action based on output of the MLP init_network """ h = numpy.array(q_vector) h_exp = numpy.exp(h * self.reliability_for_action) h_exp = h_exp / numpy.sum(h_exp) random = numpy.random.rand(1) action = 0 if random > h_exp[0] and random < h_exp[0] + h_exp[1]: action = 1 elif random > h_exp[0] + h_exp[1] and random < h_exp[0] + h_exp[1] + h_exp[2]: action = 2 #comment this in for 4 actions #elif random > h_exp[0] + h_exp[1] + h_exp[2]: # action = 3''' return action