def train(net: NeuralNet, inputs: Tensor, targets: Tensor, num_epochs: int = 5000, iterator: DataIterator = BatchIterator(), loss: Loss = MSE(), optimizer: Optimizer = SGD() ) -> None: for epoch in range(num_epochs): epoch_loss = 0.0 for batch in iterator(inputs, targets): predicted = net.forward(batch.inputs) epoch_loss += loss.loss(predicted, batch.targets) grad = loss.grad(predicted, batch.targets) net.backward(grad) optimizer.step(net) print(epoch, epoch_loss)
return [0, 0, 0, 1] elif x % 5 == 0: return [0, 0, 1, 0] elif x % 3 == 0: return [0, 1, 0, 0] else: return [1, 0, 0, 0] inputs = np.array([binary_encode(x) for x in range(101, 1024)]) targets = np.array([fizz_buzz_encode(x) for x in range(101, 1024)]) net = NeuralNet([ Linear(input_size=10, output_size=50), Tanh(), Linear(input_size=50, output_size=4) ]) train(net, inputs, targets, num_epochs=5000, optimizer=SGD(lr=0.001)) for x in range(1, 101): inputs = binary_encode(x) prediction = net.forward(inputs) actual = fizz_buzz_encode(x) labels = [str(x), "fizz", "buzz", "fizzbuzz"] prediction_idx = np.argmax(prediction) actual_idx = np.argmax(actual) print(x, labels[prediction_idx], labels[actual_idx])
""" Train a model to predict exclusive or of its inputs """ import numpy as np from nn import NeuralNet from layers import Linear, Tanh from train import train inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) targets = np.array([[1, 0], [0, 1], [0, 1], [1, 0]]) net = NeuralNet([ Linear(input_size=2, output_size=2), Tanh(), Linear(input_size=2, output_size=2) ]) train(net, inputs, targets, num_epochs=5000) for x, y in zip(inputs, targets): print(x, net.forward(x), y)
""" The canonical example of a function that can't be learned with a simple linear model is XOR """ import numpy as np from train import train from nn import NeuralNet from layers import Linear, Tanh inputs = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) targets = np.array([[1, 0], [0, 1], [0, 1], [1, 0]]) net = NeuralNet([ Linear(input_size=2, output_size=2), Tanh(), Linear(input_size=2, output_size=2) ]) train(net, inputs, targets) for x, y in zip(inputs, targets): predicted = net.forward(x) print(x, predicted, y)
for parameter in model.parameters(): parameter.requires_grad = False if (use_cuda): model.cuda() model.eval() summary(model, (1, 64, 64)) print("Loaded model...") preds = [] labels = [] for local_batch, local_labels in tqdm(test_generator): labels.extend(local_labels.numpy().tolist()) local_batch, local_labels = local_batch.to(device), local_labels.to(device) output = model.forward(local_batch) output = model.softmax(output) output = torch.max(output, 1)[1] preds.extend(output.cpu().numpy().tolist()) recall = recall_score(y_true=labels, y_pred=preds, average='weighted') prec = precision_score(y_true=labels, y_pred=preds, average='weighted') f1 = f1_score(y_true=labels, y_pred=preds, average='weighted') acc = accuracy_score(labels, preds) print("Accuracy: {}".format(acc)) print("Recall: {}\tPrecision: {}\tF1 Score: {}".format(recall, prec, f1)) print( classification_report(y_true=labels, y_pred=preds, target_names=["No Findings", "Pneumonia"]))
} idx_to_classes = {v: k for k, v in classes_to_idx.items()} criterion = nn.BCELoss() model = NeuralNet(0.01, criterion, 256, len(classes_to_idx)) model.load_state_dict(torch.load(sys.argv[1])) if use_cuda: model.cuda() model.eval() id = random.choice(os.listdir(os.path.join(root_dir, "images"))).split(".")[0] data = torch.load(os.path.join(root_dir, "images", id + ".pt")) true_labels = torch.load(os.path.join(root_dir, "labels", id + ".pt")) diseases = [] for x in range(len(true_labels)): if (true_labels[x] == 1.0): diseases.append(idx_to_classes[x]) print(id) print(true_labels) print(diseases) with torch.no_grad(): data_cuda, labels_cuda = data.to(device), true_labels.to(device) output_cuda = torch.round(model.forward(data_cuda.unsqueeze(0))) print(output_cuda.cpu().numpy()) # plt.imshow(data.permute(1,2,0)) # plt.show()
return [x >> i & 1 for i in range(10)] inputs = np.array([ binary_encode(x) for x in range(101, 1024) ]) targets = np.array([ fizz_buzz_encode(x) for x in range(101, 1024) ]) net = NeuralNet([ Linear(input_size = 10, output_size = 50), Tanh(), Linear(input_size = 50, output_size = 4) ]) train(net, inputs, targets, num_epochs = 5000, optimizer = SGD(lr = 0.001)) for x in range(1, 101): predicted = net.forward(binary_encode(x)) predicted_idx = np.argmax(predicted) actual_idx = np.argmax(fizz_buzz_encode(x)) labels = [str(x), "fizz", "buzz", "fizzbuzz"] print(x, labels[predicted_idx], labels[actual_idx])
l4 = Layer(100, 25, 'linear') layers = [l1, l2, l3, l4] learning_rate = 0.0002 loss_type = 'mean_square' opt_type = 'RMSprop' Q = NeuralNet(layers, learning_rate, loss_type, opt_type) Q.recover('model/', 'Q_net_all_11_0_1000') for i in range(test_num): video = Episode(i, test_num, test_name, feat_path, gt_path) frame_num = np.shape(video.feat)[0] summary = np.zeros(frame_num) Q_value = [] id_curr = 0 while id_curr < frame_num: action_value = Q.forward([video.feat[id_curr]]) a_index = np.argmax(action_value[0]) id_next = id_curr + a_index + 1 if id_next > frame_num - 1: break summary[id_next] = 1 Q_value.append(max(action_value[0])) id_curr = id_next name = 'output/sum_' + test_name[i % test_num] sio.savemat(name, {'summary': summary}) print('Test done.')
class Agent(object): def __init__(self, layers, batch, explore, explore_l, explore_d, learning, decay, path): # layers: architecture of Q network # batch: number of observations in mini-batch set # explore: exploration rate # decay: future reward decay rate # path: model path self.layers = layers self.batch_size = batch self.decay_rate = decay self.learning_rate = learning self.explore_low = explore_l self.explore_decay = explore_d self.explore_rate = explore self.directory = path self.num_action = self.layers[len(self.layers) - 1].num_output ##### build Q network self.Q = NeuralNet(self.layers, self.learning_rate, 'mean_square', 'RMSprop') self.Q.initialize() ##### data-related variables self.feat = [] self.gt = [] self.memory = Memo() self.selection = [] # initialize with data def data_init(self, current_eps): self.feat = current_eps.feat self.gt = current_eps.gt # select an action based on policy def policy(self, id_curr): exploration = np.random.choice( range(2), 1, p=[1 - self.explore_rate, self.explore_rate]) # exploration==1: explore # exploration==0: exploit if exploration == 1: # exploration action_index = np.random.choice(range(self.num_action), 1)[0] #print('\r') #print(' explore: '+str(action_index)) #print('\r') action_value = self.Q.forward([self.feat[id_curr]]) # record average Q value self.Q.ave_value.append(np.mean(action_value[0])) # print(action_value[0]) return action_index else: # exploitation action_value = self.Q.forward([self.feat[id_curr]]) # record average Q value self.Q.ave_value.append(np.mean(action_value[0])) # self.Q.ave_value = np.append(self.Q.ave_value,np.mean(action_value[0])) action_index = np.argmax(action_value[0]) #print('\r') ##print('exploit: '+str(action_index)) #print('\r') # print(action_value[0]) return action_index # perform action to get next state def action(self, id_curr, a_index): id_next = id_curr + a_index + 1 #action 0,1,2,... return id_next # compute the reward # REWARD 3:reward with distribution and terms on length of skipping def reward(self, id_curr, a_index, id_next): gaussian_value = [ 0.0001, 0.0044, 0.0540, 0.2420, 0.3989, 0.2420, 0.0540, 0.0044, 0.0001 ] # skipping interval,missing part seg_gt = self.gt[0][id_curr + 1:id_next] total = len(seg_gt) n1 = sum(seg_gt) n0 = total - n1 miss = (0.8 * n0 - n1) / 25 #largest action step. # accuracy acc = 0 if id_next - 4 > -1: if self.gt[0][id_next - 4] == 1: acc = acc + 0.0001 if id_next - 3 > -1: if self.gt[0][id_next - 3] == 1: acc = acc + 0.0044 if id_next - 2 > -1: if self.gt[0][id_next - 2] == 1: acc = acc + 0.0540 if id_next - 1 > -1: if self.gt[0][id_next - 1] == 1: acc = acc + 0.2420 if self.gt[0][id_next] == 1: acc = acc + 0.3989 if id_next + 1 < len(self.gt[0]): if self.gt[0][id_next + 1] == 1: acc = acc + 0.2420 if id_next + 2 < len(self.gt[0]): if self.gt[0][id_next + 2] == 1: acc = acc + 0.0540 if id_next + 3 < len(self.gt[0]): if self.gt[0][id_next + 3] == 1: acc = acc + 0.0044 if id_next + 4 < len(self.gt[0]): if self.gt[0][id_next + 4] == 1: acc = acc + 0.0001 r = miss + acc return r # update target Q value def update(self, r, id_curr, id_next, a_index): target = self.Q.forward([self.feat[id_curr]]) # target:[ [] ] target[0][a_index] = r + self.decay_rate * max( self.Q.forward([self.feat[id_next]])[0]) return target # run an episode to get case set for training def episode_run(self): frame_num = np.shape(self.feat)[0] self.selection = np.zeros(frame_num) id_curr = 0 self.selection[id_curr] = 1 while id_curr < frame_num: a_index = self.policy(id_curr) id_next = self.action(id_curr, a_index) if id_next > frame_num - 1: break self.selection[id_next] = 1 r = self.reward(id_curr, a_index, id_next) target_vector = self.update(r, id_curr, id_next, a_index)[0] input_vector = self.feat[id_curr] self.memorize(input_vector, target_vector) if self.memory.get_size() == self.batch_size: #print('training') self.train() id_curr = id_next # training Q net using one batch data def train(self): self.explore_rate = max(self.explore_rate - self.explore_decay, self.explore_low) x = self.memory.state y = self.memory.target self.Q.train(x, y) self.memory.reset() # store current observation to memory def memorize(self, state, target): # observation: new observation (s,a,r,s') self.memory.add(state, target) # reset data-related variables def data_reset(self): self.feat = [] self.gt = [] self.selection = [] # save trained Q net def save_model(self, filename): # module backup path = self.directory self.Q.saving(path, filename)