Пример #1
0
import json
import numpy as np
import os.path as path
import tensorflow as tf

from python.dataset import AutoComplete, AutoCompleteFixed
from python.model import PureNLSTM

dirname = path.dirname(path.realpath(__file__))
article_dir = path.join(dirname, '..', '..', 'public')

tf.logging.set_verbosity(tf.logging.INFO)

train_dataset = AutoComplete(repeat=True)
test_dataset = AutoCompleteFixed(
    "parts of north africa",
    batch_size=1,
)
model = PureNLSTM(train_dataset,
                  name='autocomplete_nlstm_600',
                  embedding_size=600,
                  verbose=True)

for output_i, output in enumerate(model.predict(dataset=test_dataset)):
    probabilities = output['probabilities']
    predict_sorted = np.argsort(probabilities, axis=1)[:, ::-1]

    source = test_dataset.decode_source(output['source'])
    target = test_dataset.decode_target(output['target'])
    predict = test_dataset.decode_target(predict_sorted)
import numpy as np
import tensorflow as tf
from tqdm import tqdm

from python.dataset import AutoComplete
from python.model import PureGRU
from python.summary import ContextAccuracy

tf.logging.set_verbosity(tf.logging.INFO)

train_dataset = AutoComplete(repeat=True, batch_size=64)
test_dataset = AutoComplete(dataset='test', repeat=False, batch_size=16)
model = PureGRU(train_dataset,
                name='autocomplete_gru_600',
                embedding_size=600,
                verbose=True)

context_accuracy = ContextAccuracy()

for output_i, output in enumerate(
        tqdm(model.predict(dataset=test_dataset),
             total=test_dataset.observations)):
    probabilities = output['probabilities']
    predict_sorted = np.argsort(probabilities, axis=1)[:, ::-1]

    source = test_dataset.decode_source(output['source'])
    target = test_dataset.decode_target(output['target'])
    predict = test_dataset.decode_target(predict_sorted)

    context_accuracy.add(source, predict, target)
import tensorflow as tf

from python.dataset import AutoComplete
from python.model import PureLSTM

tf.random.set_random_seed(450849059)  # From random.org
tf.logging.set_verbosity(tf.logging.INFO)

train_dataset = AutoComplete(repeat=True, batch_size=64)
valid_dataset = AutoComplete(repeat=True, dataset='valid', batch_size=64)
test_dataset = AutoComplete(repeat=False, dataset='test', batch_size=64)

model = PureLSTM(train_dataset,
                 name='autocomplete_lstm_600',
                 embedding_size=600,
                 verbose=True)
model.train(max_steps=train_dataset.batches * 2, valid_dataset=valid_dataset)
import numpy as np
import tensorflow as tf

from python.dataset import AutoComplete
from python.model import PureLSTM

tf.logging.set_verbosity(tf.logging.INFO)

train_dataset = AutoComplete(repeat=True, batch_size=64)
test_dataset = AutoComplete(dataset='test', repeat=False, batch_size=1)
model = PureLSTM(train_dataset, name='autocomplete_lstm_600',
                 embedding_size=600,
                 verbose=True)
print(model.evaluate(dataset=test_dataset))
import numpy as np
import tensorflow as tf

from python.dataset import AutoComplete
from python.model import PassThrough

tf.logging.set_verbosity(tf.logging.INFO)

train_dataset = AutoComplete(repeat=True)
valid_dataset = AutoComplete(dataset='test', repeat=False, batch_size=1)
model = PassThrough(train_dataset,
                    name='autocomplete_passthough',
                    verbose=True)
model.train(max_steps=1)

for output_i, output in enumerate(model.predict(dataset=valid_dataset)):
    length = output['length']
    source = valid_dataset.decode_source(output['source'])
    target = valid_dataset.decode_target(output['target'])

    print(f'sequence {output_i} of length {length}')
    for char, word in zip(source, target):
        print(f' {char} -> {word}')

    break