text
stringlengths 1
93.6k
|
|---|
# Forward pass
|
# y_preds, loss = net.forward(x_edges, x_edges_values, x_nodes, x_nodes_coord, y_edges, edge_cw)
|
y_preds, loss, x_edges_values = net.forward(x_nodes_coord, x_nodes_timew, y_tour, edge_cw)
|
loss = loss.mean() # Take mean of loss across multiple GPUs
|
loss = loss / accumulation_steps # Scale loss by accumulation steps
|
loss.backward()
|
# Backward pass
|
if (batch_num+1) % accumulation_steps == 0:
|
optimizer.step()
|
optimizer.zero_grad()
|
# Compute error metrics and mean tour lengths
|
# err_edges, err_tour, err_tsp, tour_err_idx, tsp_err_idx = edge_error(y_preds, y_edges, x_edges)
|
pred_tour_len = mean_tour_len_edges(x_edges_values, y_preds)
|
gt_tour_len = np.mean(batch.tour_len)
|
# Update running data
|
running_nb_data += batch_size
|
running_loss += batch_size* loss.data.item()* accumulation_steps # Re-scale loss
|
# running_err_edges += batch_size* err_edges
|
# running_err_tour += batch_size* err_tour
|
# running_err_tsp += batch_size* err_tsp
|
running_pred_tour_len += batch_size* pred_tour_len
|
running_gt_tour_len += batch_size* gt_tour_len
|
running_nb_batch += 1
|
# Log intermediate statistics
|
result = ('loss:{loss:.4f} pred_tour_len:{pred_tour_len:.3f} gt_tour_len:{gt_tour_len:.3f}'.format(
|
loss=running_loss/running_nb_data,
|
pred_tour_len=running_pred_tour_len/running_nb_data,
|
gt_tour_len=running_gt_tour_len/running_nb_data))
|
master_bar.child.comment = result
|
# Compute statistics for full epoch
|
loss = running_loss/ running_nb_data
|
err_edges = 0 # running_err_edges/ running_nb_data
|
err_tour = 0 # running_err_tour/ running_nb_data
|
err_tsp = 0 # running_err_tsp/ running_nb_data
|
pred_tour_len = running_pred_tour_len/ running_nb_data
|
gt_tour_len = running_gt_tour_len/ running_nb_data
|
return time.time()-start_epoch, loss, err_edges, err_tour, err_tsp, pred_tour_len, gt_tour_len
|
def metrics_to_str(epoch, time, learning_rate, loss, err_edges, err_tour, err_tsp, pred_tour_len, gt_tour_len):
|
result = ( 'epoch:{epoch:0>2d}\t'
|
'time:{time:.1f}h\t'
|
'lr:{learning_rate:.2e}\t'
|
'loss:{loss:.4f}\t'
|
# 'err_edges:{err_edges:.2f}\t'
|
# 'err_tour:{err_tour:.2f}\t'
|
# 'err_tsp:{err_tsp:.2f}\t'
|
'pred_tour_len:{pred_tour_len:.3f}\t'
|
'gt_tour_len:{gt_tour_len:.3f}'.format(
|
epoch=epoch,
|
time=time/3600,
|
learning_rate=learning_rate,
|
loss=loss,
|
# err_edges=err_edges,
|
# err_tour=err_tour,
|
# err_tsp=err_tsp,
|
pred_tour_len=pred_tour_len,
|
gt_tour_len=gt_tour_len))
|
return result
|
def test(net, config, master_bar, mode='test'):
|
# Set evaluation mode
|
net.eval()
|
# Assign parameters
|
num_nodes = config.num_nodes
|
num_neighbors = config.num_neighbors
|
batch_size = config.batch_size
|
batches_per_epoch = config.batches_per_epoch
|
beam_size = config.beam_size
|
val_filepath = config.val_filepath
|
val_target_filepath = config.val_filepath_solution
|
test_filepath = config.test_filepath
|
test_target_filepath = config.test_filepath_solution
|
# Load TSP data
|
if mode == 'val':
|
dataset = DataReader(num_nodes, num_neighbors, batch_size=batch_size, filepath=val_filepath, target_filepath=val_target_filepath, do_prep=False)
|
elif mode == 'test':
|
dataset = DataReader(num_nodes, num_neighbors, batch_size=batch_size, filepath=test_filepath, target_filepath=test_target_filepath, do_prep=False)
|
batches_per_epoch = dataset.max_iter
|
# Convert dataset to iterable
|
dataset = iter(dataset)
|
# Initially set loss class weights as None
|
edge_cw = None
|
# Initialize running data
|
running_loss = 0.0
|
# running_err_edges = 0.0
|
# running_err_tour = 0.0
|
# running_err_tsp = 0.0
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.