Ejemplo n.º 1
0
def main():
    print 'Loading Data'
    x = cPickle.load(open('english_matrices.pkl', 'rb'))
    y = cPickle.load(open('chinese_matrices.pkl', 'rb'))
    print 'Done'

    # x = np.random.random((10, 10, 50, 1))
    # y = np.random.random((10, 10, 50, 1))
    encoder_lstm = LSTM(50, 100, 50)
    encoder_lstm.load_weights('encoder.pkl')
    outputs = []
    for i in range(10000):
        outputs.append(encoder_lstm.predict(x[i]))
    # for _ in range(10):
    #     for i in range(20):
    #         idx_start = i*500
    #         idx_end = min((i+1)*500, len(x))
    #         sys.stdout.write('\n\nTraining Data %d - %d' % (idx_start, idx_end))
    #         train(encoder_lstm, x[idx_start:idx_end], y[idx_start:idx_end][0], 50, 'encoder')
    #         encoder_lstm.save_weights('encoder.pkl')
    # outputs = encoder_lstm.predict(x[:10000])
    # encoder_lstm.save_weights('encoder.pkl')
    embed()
    decoder_lstm = LSTM(50, 100, 50)
    for _ in range(4):
        for i in range(20):
            idx_start = i * 500
            idx_end = min((i + 1) * 500, len(x))
            sys.stdout.write('\n\nTraining Data %d - %d' % (idx_start, idx_end))
            train(decoder_lstm, outputs[idx_start:idx_end], y[idx_start:idx_end], 50, 'decoder')
            decoder_lstm.save_weights('decoder.pkl')
Ejemplo n.º 2
0
# persist preprocessing steps
with open('processor.json', 'w') as f:
    f.write(jsonpickle.dumps(TEXT))

# persist model
rand_text, _ = next(iter(train_iter))
torch_onnx.export(net,
                  rand_text[[0]],  # Keep batch size to 1
                  'model.onnx',
                  verbose=False,
                  input_names=['input1'],
                  output_names=['output1'],
                  dynamic_axes={'input1': {0: 'batch'},
                                'output1': {0: 'batch'}})

# calculate set of quality metrics
y_pred, y_true = net.predict(val_iter)

f1_metric = f1_score(y_true, y_pred)
accuracy_metric = accuracy_score(y_true, y_pred)
print(classification_report(y_true, y_pred))

# write metrics
if not os.path.exists("metrics"):
    os.mkdir("metrics")

with open("metrics/f1.metric", "w+") as f:
    json.dump(f1_metric, f)
with open("metrics/accuracy.metric", "w+") as f:
    json.dump(accuracy_metric, f)
Ejemplo n.º 3
0
            torch.save(
                {
                    'epoch': epoch,
                    'model_state_dict': model.state_dict(),
                    'optimizer_state_dict': model.optimizer.state_dict(),
                    'loss': mean_loss,
                }, 'saved_models/lstm_adam_b10_lb168_model')
        elif epoch - best_loss[1] > patience:
            still_learning = False
        epoch += 1

# get just one sample for prediction
data_pred, target_pred = next(iter(test_loader))

# prepare data for comparison
pred = model.predict(Variable(data_pred), look_ahead)
target_scaled = scaler.inverse_transform(target_pred[0, :].detach().view(
    -1, 1).numpy())
pred_scaled = scaler.inverse_transform(pred[0, :].detach().view(-1, 1).numpy())

# compute MSE and MAE
train_loader, test_loader, scaler = build_loader(test_ratio,
                                                 look_back,
                                                 look_ahead,
                                                 batch_size=1)

mse_losses = []
mae_losses = []
residuals = []
for data, target in train_loader:
    mse_loss, mae_loss, resid = model.step(data,
Ejemplo n.º 4
0
X_train, Y_train, X_val, Y_val = train_test_splits(X, Y, N, 8)

model = LSTM(num_classes=NUM_CLASSES,
             max_seq_len=MAX_SEQ_LEN,
             num_features=NUM_FEATURES,
             learning_rate=0.0003,
             hidden_units=64,
             model_path=model_path)

print 'epoch,mean_loss,train_acc,val_acc'

for epoch in range(500):
    epoch_loss = 0
    for x_batch, y_batch in iter_batches(X_train, Y_train, BATCH_SIZE):
        epoch_loss += model.train_on_batch(x_batch, y_batch)
    epoch_loss = epoch_loss * 1.0 / (N / BATCH_SIZE)
    print '%s,%s,%s,%s' % (epoch, epoch_loss,
                           accuracy(model.predict(X_train),
                                    from_one_hot(Y_train)),
                           accuracy(model.predict(X_val), from_one_hot(Y_val)))

#  TODO  - CLEAN THIS SHIT UP
print '=== BASELINE ACC'
Y_val = from_one_hot(Y_val)
N1 = np.sum(Y_val)
N0 = len(Y_val) - N1
print '\t random (freqs): %s' % (max(N0, N1) * 1.0 / (N0 + N1))
Y_hat = model.predict(X_val)
random.shuffle(Y_hat)
print '\t random (permutations): %s' % accuracy(Y_hat, Y_val)
Ejemplo n.º 5
0
    parser = argparse.ArgumentParser(description='Test.')
    parser.add_argument(nargs='*',
                        action='store',
                        dest='input',
                        type=str,
                        help='The text to parse.')
    args = parser.parse_args()

    sentence = args.input

    english_dict = cPickle.load(open('english_dictionary.pkl'))
    chinese_dict = cPickle.load(open('chinese_dictionary.pkl'))

    encoder = LSTM(50, 100, 50)
    encoder.load_weights('encoder.pkl')
    decoder = LSTM(50, 100, 50)

    mat = []
    for word in sentence:
        mat.append(english_dict[word])
    mat = np.array(mat)
    mat = mat.reshape((mat.shape[0], mat.shape[1], 1))

    output = encoder.predict(mat)
    final = decoder.predict([output[-1].v], output[-1].h)

    translated_sentence = ''
    for word in final:
        translated_sentence += findWord(word.v, chinese_dict)

    print translated_sentence
Ejemplo n.º 6
0
             hidden_units=64,
             model_path=model_path)

print 'epoch,mean_loss,train_acc,val_acc'

xaxis = []
yaxis = []

for epoch in range(500):
    epoch_loss = 0
    for x_batch, y_batch in iter_batches(X_train, Y_train, BATCH_SIZE):
        epoch_loss += model.train_on_batch(x_batch, y_batch)
    epoch_loss = epoch_loss * 1.0 / (N / BATCH_SIZE)
    print '%s,%s,%s,%s' % (
        epoch, epoch_loss, 
        accuracy(model.predict(X_train), from_one_hot(Y_train)), 
        accuracy(model.predict(X_val), from_one_hot(Y_val))
        )
    xaxis.append(epoch)
    yaxis.append(accuracy(model.predict(X_train), from_one_hot(Y_train)))


plt.plot(xaxis, yaxis)
plt.show()

#  TODO  - CLEAN THIS SHIT UP
print '=== BASELINE ACC'
Y_val = from_one_hot(Y_val)
N1 = np.sum(Y_val)
N0 = len(Y_val) - N1
print '\t random (freqs): %s' % (max(N0, N1) * 1.0 / (N0 + N1))