Example #1
0
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Classify Module
# Module to classify the sentiment of movie reviews using a FastText model.

import nltk
import torch
import torchtext

import utility

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

input_arguments = utility.parse_input_arguments(module="classify")

# Initialize the field and dataset, build the vocabulary, and initialize the iterator.
nltk.download("punkt")
text_field = torchtext.data.Field(
    tokenize=nltk.word_tokenize,
    preprocessing=lambda x: list(nltk.bigrams(x)),
    fix_length=500)
dataset = torchtext.data.TabularDataset(input_arguments.movie_review_path,
                                        format="csv",
                                        skip_header=True,
                                        fields=[("review", text_field)])
text_field.build_vocab(dataset, max_size=25000)
iterator = torchtext.data.Iterator(dataset,
                                   batch_size=1000,
                                   shuffle=False,
Example #2
0
# Copyright (C) 2020 Andreas Pentaliotis
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Generate Module
# Module to generate images using a trained generator.

from keras.models import load_model

import utility

input_arguments = utility.parse_input_arguments(module="generate")
generator = load_model(input_arguments.generator_path)
images = utility.generate_images(generator, input_arguments.image_number)
utility.save(images, input_arguments.output_path)
Example #3
0
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Simulate Module
# Module to run reinforcement learning simulations.

import simulations
import utility

input_arguments = utility.parse_input_arguments()

state_encoding = "array" if "network" in input_arguments.agent_type else "integer"
environment = utility.create_environment(input_arguments.environment_type,
                                         input_arguments.grid_dimension_size,
                                         input_arguments.reward_function,
                                         state_encoding)

if input_arguments.simulation_function == "training_episodes":
    # Control the randomness to get reproducible results.
    utility.control_randomness(input_arguments.seed)

    # Create the agent and run the training episodes.
    agent = utility.create_agent(input_arguments.agent_type,
                                 environment.compute_state_space_size(),
                                 environment.compute_action_space_size())
Example #4
0
# Train Module
# Module to train a FastText model on movie review data.

import nltk
import torch
import torch.nn as nn
import torchtext

import fasttext
import utility

torch.manual_seed(1)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

input_arguments = utility.parse_input_arguments(module="train")

# Initialize the fields and dataset, and build the vocabularies.
nltk.download("punkt")
text_field = torchtext.data.Field(
    tokenize=nltk.word_tokenize,
    preprocessing=lambda x: list(nltk.bigrams(x)),
    fix_length=500)
label_field = torchtext.data.LabelField(dtype=torch.float)
dataset = torchtext.data.TabularDataset(input_arguments.movie_review_path,
                                        format="csv",
                                        skip_header=True,
                                        fields=[("review", text_field),
                                                ("sentiment", label_field)])
text_field.build_vocab(dataset, max_size=25000)
label_field.build_vocab(dataset)