Exemplo n.º 1
0
Arquivo: train.py Projeto: tbrx/CVAE
    model.assign_lr(learning_rate * (decay_rate ** epoch))
    train_loss = []
    test_loss = []
    st = time.time()
    
    for iteration in range(int(len(train_molecules)/batch_size)):
        n = np.random.randint(len(train_molecules), size = batch_size)
        x = np.array([train_molecules[i] for i in n])
        l = np.array([train_length[i] for i in n])
        c = np.array([train_labels[i] for i in n])
        cost = model.train(x, l, c)
        train_loss.append(cost)
    
    for iteration in range(int(len(test_molecules)/batch_size)):
        n = np.random.randint(len(test_molecules), size = batch_size)
        x = np.array([test_molecules[i] for i in n])
        l = np.array([test_length[i] for i in n])
        c = np.array([test_labels[i] for i in n])
        cost = model.test(x, l, c)
        test_loss.append(cost)
    
    train_loss = np.mean(np.array(train_loss))        
    test_loss = np.mean(np.array(test_loss))    
    end = time.time()    
    if epoch==0:
        print ('epoch\ttrain_loss\ttest_loss\ttime (s)')
    print ("%s\t%.3f\t%.3f\t%.3f" %(epoch, train_loss, test_loss, end-st))
    ckpt_path = 'save/model.ckpt'
    model.save(ckpt_path, epoch)

    # Learning rate scheduling
    #model.assign_lr(learning_rate * (decay_rate ** epoch))
    train_loss = []
    test_loss = []
    st = time.time()

    for iteration in range(len(train_molecules_input) // args.batch_size):
        n = np.random.randint(len(train_molecules_input), size=args.batch_size)
        x = np.array([train_molecules_input[i] for i in n])
        y = np.array([train_molecules_output[i] for i in n])
        l = np.array([train_length[i] for i in n])
        cost = model.train(x, y, l)
        train_loss.append(cost)

    for iteration in range(len(test_molecules_input) // args.batch_size):
        n = np.random.randint(len(test_molecules_input), size=args.batch_size)
        x = np.array([test_molecules_input[i] for i in n])
        y = np.array([test_molecules_output[i] for i in n])
        l = np.array([test_length[i] for i in n])
        cost = model.test(x, y, l)
        test_loss.append(cost)

    train_loss = np.mean(np.array(train_loss))
    test_loss = np.mean(np.array(test_loss))
    end = time.time()
    if epoch == 0:
        print('epoch\ttrain_loss\ttest_loss\ttime (s)')
    print("%s\t%.3f\t%.3f\t%.3f" % (epoch, train_loss, test_loss, end - st))
    ckpt_path = args.save_dir + '/model_' + str(epoch) + '.ckpt'
    model.save(ckpt_path, epoch)