Esempio n. 1
0
def train(x, labels, batch_size=400):
    output_size = 1
    n_epochs = 1000
    sequence = 1  # 20 frames per second
    input_size = 640 * 480
    lr = 0.01
    batch_size = 400
    x = x.reshape((batch_size, input_size))
    input_seq = torch.Tensor(get_input_seq(x, batch_size, sequence))
    target_seq = torch.Tensor(labels)
    model = Model(input_size=input_size,
                  output_size=output_size,
                  hidden_dim=50,
                  n_layers=1)
    criterion = torch.nn.MSELoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=lr)

    # train run
    for epoch in range(1, n_epochs + 1):
        optimizer.zero_grad()
        # 8000 outputs, 400 batch size * sequence
        output, hidden = model(input_seq)
        output = output.reshape((400))
        loss = criterion(output, target_seq.view(-1).float())
        loss.backward()
        optimizer.step()
        accuracy = (output == target_seq).sum()
        if epoch % 10 == 0:
            print('Epoch: {}/{}.............'.format(epoch, n_epochs), end=' ')
            print("Loss: {:.4f}".format(loss.item()))
    def regular_train(self, save=True, epochs=5):
        print("Regular training")
        batch_size = 1
        buffer_size = 10000
        self.dataset = self.dataset.shuffle(buffer_size).batch(
            batch_size, drop_remainder=True)

        vocab_size = len(self.vocab)
        embedding_dim = 256
        units = 1024

        model = Model(vocab_size, embedding_dim, units)

        optimizer = tf.train.AdamOptimizer()

        def loss_function(real, preds):
            return tf.losses.sparse_softmax_cross_entropy(labels=real,
                                                          logits=preds)

        model.build(tf.TensorShape([batch_size, self.seq_length]))

        checkpoint_dir = './training_checkpoints'
        losses = []
        iterations = []
        iteration = 0
        for epoch in range(epochs):
            start = time.time()

            # initializing the hidden state at the start of every epoch
            # initally hidden is None
            hidden = model.reset_states()

            for (batch, (inp, target)) in enumerate(self.dataset):
                with tf.GradientTape() as tape:
                    # feeding the hidden state back into the model
                    # This is the interesting step
                    predictions = model(inp)
                    loss = loss_function(target, predictions)

                grads = tape.gradient(loss, model.variables)
                optimizer.apply_gradients(zip(grads, model.variables))

                if batch % 100 == 0:
                    print('Epoch {} Batch {} Loss {:.4f}'.format(
                        epoch + 1, batch, loss))

                    losses.append(loss)
                    iterations.append(iteration)
                    iteration += 1
            print('Epoch {}'.format(epoch + 1))
            print('Time taken for 1 epoch {} sec\n'.format(time.time() -
                                                           start))

        if save:
            model.save_weights(os.path.join(checkpoint_dir, "ckpt"))

        return model, losses, iterations
Esempio n. 3
0
import numpy as np

from preprocessing import getSentenceData
from rnn import Model

word_dim = 8000
hidden_dim = 100
X_train, y_train = getSentenceData('data/reddit-comments-2015-08.csv', word_dim)

np.random.seed(10)
rnn = Model(word_dim, hidden_dim)

losses = rnn.train(X_train[:100], y_train[:100], learning_rate=0.005, nepoch=10, evaluate_loss_after=1)
Esempio n. 4
0
from flask_cors import CORS
from flask import Flask, Response, request
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
from bson.json_util import dumps, RELAXED_JSON_OPTIONS
from dotenv import load_dotenv
from rnn import Model


# Initializing API
load_dotenv() # .env
app = Flask(__name__) # Flask
CORS(app)
app.config["MONGO_URI"] = os.environ.get('mongo_uri') # MongoDB
mongo = PyMongo(app)
model = Model('../model/input.txt', '../model/latest') # RNN Model

# Main api endpoint, takes a POST request which requires three
# words for the model to start generating off of. It will return
# a JSON response with what it's generated.
# 
# Form Data:
#   keywords - String of three words separated by spaces
@app.route('/api/generate', methods=["POST"])
def generate():
    if request.method == "POST":
        # Request data
        keywords = request.form['keywords'].split(" ")

        # Verifying that three keywords have been submitted.
        if(len(keywords) != 3):
Esempio n. 5
0
""" Main file for running the model """

import sys

from rnn import Data, Model

if __name__ == '__main__':
    # model = Model.build_model(65, 256, 1024, 64)
    # for input_example_batch, target_example_batch in Data.get_training_dataset().take(1):
    #     example_batch_predictions = model(input_example_batch)
    #     print(example_batch_predictions.shape, "# (batch_size, sequence_length, vocab_size)")
    #
    #     si = tf.random.multinomial(example_batch_predictions[0], num_samples=1)
    #     sampled_indices = tf.squeeze(si, axis=-1).numpy()
    #     print(sampled_indices)
    #     print(Model.loss(target_example_batch, example_batch_predictions))
    #     print('Prediction shape: ', example_batch_predictions.shape, " # (batch size, seq len, vocab size)")
    #     print('scalar loss:      ', example_batch_predictions.numpy().mean())
    #     Model.train_model(model)

    model = Model.get_model_from_checkpoint()
    print(Model.generate_text(model, sys.argv[1]))
Esempio n. 6
0
from rnn import Model
from rnn.utils.ops import zoneout
from rnn.layers import Recurrent, GRU
from rnn.utils.ops import index
from rnn.layers import Loss
import tensorflow as tf
import numpy as np


def sine_data(ix, size=50):
    x = np.arange(ix, ix + size, step=0.2)
    y = np.cos(x)
    return y[:-1], y[1:]

with tf.Graph().as_default():
    # no. of time steps is 30, each time step has input of size 5
    # in case of dynamic recurrence, leave no. of time steps as None
    model = Model(input_shape=[30, 1], output_shape=[30, 1])
    model.add(Recurrent(10))
    model.add(GRU(15))
    state = model.get_last_state()
    loss = Loss(state)
    print(state2)
    # last_step = index(state, -1)
    # print(state)

    # feeding data to model
    # shape of the data must be of same size you provided in Model()
    model.feed(np.random.randn(30, 1))
    print(model.run(state).shape)
Esempio n. 7
0
                num_examples_seen += 1
        return losses


# fazendo a rede propriamente

word_dim = 8000
hidden_dim = 100
X_train, y_train, sentences, word_to_index = getSentenceData(
    'data/reddit-comments-2015-08.csv', word_dim)

X_train.shape
training_size = 1000

np.random.seed(27)
rnn = Model(word_dim, hidden_dim)
#
losses = rnn.train(X_train[:training_size],
                   y_train[:training_size],
                   learning_rate=0.5,
                   nepoch=20,
                   evaluate_loss_after=2)

word_to_index['UNKNOWN_TOKEN']

test = 128
X_test, y_test = X_train[test], y_train[test]
y_pred = rnn.predict(X_test)
y_test
sentences[test + 1]
word_to_index.items