def validate_and_save(global_step, valid_set, listener, speller, optimizer, tf_rate):
    # Validation
    val_loss = []
    val_ler = []
    
    for batch_data, batch_label in valid_set:
        batch_loss, batch_ler = batch_iterator(batch_data, batch_label, listener, speller, optimizer, 
                                               tf_rate, idx2char, is_training=False, data='libri', **conf['model_parameter'])
        val_loss.append(batch_loss)
        val_ler.extend(batch_ler)
    
    val_loss = np.array([sum(val_loss)/len(val_loss)])
    val_ler = np.array([sum(val_ler)/len(val_ler)])

    print("Step:", global_step)
    print("Loss:", sum(val_loss)/len(val_loss))
    print("CER:", sum(val_ler)/len(val_ler))

    torch.save(listener, listener_model_path+str(global_step))
    torch.save(speller, speller_model_path+str(global_step))
for category in categories:
  data[category] = {}

  for ratio in tir:
    global_step = 0
    total = 0

    conf['meta_variable']['data_path'] = 'TIMIT/timit_mfcc_39_{}_{}.pkl'.format(ratio, category)

    X_test, y_test = load_dataset(**conf['meta_variable'])
    test_set = create_dataloader(X_test, y_test, **conf['model_parameter'], **conf['training_parameter'], shuffle=False)

    listener = torch.load(conf['training_parameter']['pretrained_listener_path'], map_location=lambda storage, loc: storage)
    speller = torch.load(conf['training_parameter']['pretrained_speller_path'], map_location=lambda storage, loc: storage)
    optimizer = torch.optim.Adam([{'params':listener.parameters()}, {'params':speller.parameters()}], lr=conf['training_parameter']['learning_rate'])

    for batch_index,(batch_data,batch_label) in enumerate(test_set):
      batch_loss, batch_ler = batch_iterator(batch_data, batch_label, listener, speller, optimizer, 0, is_training=False, **conf['model_parameter'])
      total += np.array(batch_ler).mean()
      global_step += 1
      print('PER for {} {}: {}'.format(ratio, category, total / global_step), end='\r', flush=True)

    data[category][ratio] = total / global_step
    print()

    df = pd.DataFrame(data)
    df.to_csv('per_data.csv')
  print()
df = pd.DataFrame(data)
df.to_csv('per_data.csv')
print(df)
Ejemplo n.º 3
0
    batch_limit_counter = 0
    for batch_data, batch_label in train_set:
        print('batch step :', batch_step, end='\r', flush=True)
        if emp_normalize:
            batch_data = (batch_data - emp_mean) / emp_std

        #print("Batch_lable", torch.argmax(batch_label, dim=-1), flush = True)
        #print("Batch data min max: ", torch.max(batch_data), torch.min(batch_data), flush = True)
        #batch_loss, batch_ler = batch_iterator(batch_data, batch_label, listener, speller, optimizer, tf_rate,
        #                                       is_training=True, data='libri', **conf['model_parameter'])

        batch_loss, batch_ler = batch_iterator(batch_data,
                                               batch_label,
                                               listener,
                                               speller,
                                               optimizer,
                                               tf_rate,
                                               is_training=True,
                                               data='libri',
                                               **conf['model_parameter'])

        #las_module.flatten_parameters()

        train_loss.append(batch_loss)
        train_ler.extend(batch_ler)

        batch_step += 1
        batch_limit_counter += 1

        # if (global_step) % verbose_step == 0:
        #     log_writer.add_scalars('loss',{'train':batch_loss}, global_step)