示例#1
0
def test_dataset():
    vocab = Vocabulary()
    vocab.build_from_file('small.txt')
    print vocab

    config = Config('parameters.json')
    dataset = Dataset(vocab, config)
    print dataset, '\n'

    print 'dataset[0] = '
    print type(dataset[0])
    print dataset[0], '\n'

    print 'next batch: '
    print dataset.next_batch()
示例#2
0
文件: data.py 项目: Halfish/jianzhu
                    9)  # at least 9 words, considering convolution operation
            padded_sentences = torch.zeros(actual_batch_size, max_seq_len,
                                           self.config.word_emb_dim)
            for i in range(actual_batch_size):
                padded_sentences[i][0:lengths[i], :] = torch.from_numpy(
                    sentences[i])
            padded_sentences = Variable(padded_sentences)
            if self.config.cuda:
                padded_sentences = padded_sentences.cuda()
            return padded_sentences, lengths


if __name__ == '__main__':
    import configure
    gvar = configure.GlobalVariable()
    dataset = ArchDataset(Config(source='parameters.json', train=True), gvar)
    for bag_sentences, bag_images, bag_lengths in dataset.next_batch():
        print(len(bag_sentences), len(bag_images), len(bag_lengths))
        print(bag_sentences[0].data)
        print(bag_images[0].data)
        print(bag_lengths[0])
        break
    '''
    print 'testing images'
    rawarch = RawArchDataset(Config('parameters.json', mode='image'), gvar, max_len = 10)
    for sentence_id, image_id, image in rawarch:
        print sentence_id, image_id, image.size(), image.mean()

    print 'testing keywords'
    rawarch = RawArchDataset(Config('parameters.json', mode='keywords'), gvar, max_len = 10)
    for sentence_id, sentence in rawarch:
示例#3
0
文件: main.py 项目: ftgreat/CopyNet-2
from zutil.config import Config
import tensorflow as tf
import data_util
import random
import seq2seq  # from local directory

from tensorflow.python.layers import core as layers_core

config = Config('parameters.json')

# prepare data
vocab = data_util.Vocabulary()
vocab.build_from_file('small.txt')
print 'got %d words in dictionary' % (vocab.size)
# should notice that vocab.size is much bigger than config.vocab_size

train_data = data_util.Dataset(vocab,
                               config.copy(dataset_filepath='train.txt'))
eval_data = data_util.Dataset(vocab, config.copy(dataset_filepath='eval.txt'))
infer_data = data_util.Dataset(vocab,
                               config.copy(dataset_filepath='infer.txt'))

# placeholder for inputs
encoder_inputs = tf.placeholder(tf.int32, shape=(config.batch_size, None))
decoder_inputs = tf.placeholder(tf.int32, shape=(config.batch_size, None))
decoder_outputs = tf.placeholder(tf.int32, shape=(config.batch_size, None))

# placeholder for sequence lengths
encoder_inputs_lengths = tf.placeholder(tf.int32, shape=(config.batch_size, ))
decoder_inputs_lengths = tf.placeholder(tf.int32, shape=(config.batch_size, ))
示例#4
0
文件: train.py 项目: Halfish/jianzhu
from data import ArchDataset
from model import MultimodalModule, AttentionModule
from configure import GlobalVariable
import numpy as np
import math
import torch
import time
from hyperboard import Agent
agent = Agent(address='127.0.0.1', port=5001)

from zutil.config import Config
config = Config(source='parameters.json')

print('building model')
assert config.model_type in {'rnn', 'cnn', 'simple', 'attention'}
if config.model_type == 'attention':
    model = AttentionModule(config)
    criterion = torch.nn.BCELoss()
else:
    model = MultimodalModule(config)
    criterion = torch.nn.CosineEmbeddingLoss()

print(model)
print(criterion)

if config.cuda:
    model = model.cuda()
    criterion = criterion.cuda()

params = model.parameters()
#optimizer = torch.optim.Adam(
示例#5
0
from data import RawArchDataset
from zutil.config import Config
from configure import GlobalVariable
import numpy as np
import time
import h5py

config = Config('parameters.json')
gvar = GlobalVariable()


def dump_feats():
    dataset = RawArchDataset(config.copy(mode='image'), gvar)
    print('got %d images' % len(dataset))
    static_feats = []

    for batch_id, images in enumerate(dataset.next_batch()):
        print('processing batch_id = ', batch_id)
        output = gvar.resnet(images).cpu().data
        output = output.view(-1, 512)  # output = output.view(-1, 512, 49)
        static_feats.append(output.numpy())

    print('concat static feats...')
    static_feats = np.concatenate(static_feats)

    print('saving as arch_feats.h5')
    h5file = h5py.File('arch_feats.h5', 'w')
    h5file.create_dataset('feats', data=static_feats)
    h5file.close()

示例#6
0
                yield self.split_image(batch_data)
                batch_data = []
        if len(batch_data) > 0:
            yield self.split_image(batch_data)

    def split_image(self, batch_data):
        images = torch.cat([image.unsqueeze(0) for image in batch_data], 0)
        images = Variable(images, requires_grad=False)
        if self.config.gpu:
            images = images.cuda(0)
        return images


config = Config(batch_size=1000,
                gpu=True,
                split_rate=0.8,
                learning_rate=1e-4,
                save_freq=10,
                max_epoch=20)
dataset = ArchDataset(config)
print config
print dataset

from zutil.convblock import ConvBlockModule


class CNN(nn.Module):
    def __init__(self, config):
        super(CNN, self).__init__()
        self.config = config
        self.conv = ConvBlockModule(dims=[3, 16, 32, 64, 64])
        self.fc = nn.Sequential(
示例#7
0
文件: model.py 项目: Halfish/jianzhu
        return out

    def forward(self, x):
        '''
        input: x, Variable, (batch_size, seq_len, word_emb_dim)
        output: out, Variable, (batch, sentence_emb_dim)
        '''
        out = x.unsqueeze(1)
        out = [self.conv_util(i, out) for i in range(len(self.config.kernel_sizes))]
        out = torch.cat(out, 2)
        out = out.view(out.size()[0], -1)
        out = self.fc(out)
        return out


if __name__ == '__main__':
    config = Config('parameters.json', model_type='rnn', which_text='title')
    print(config)
    multimodal = MultimodalModule(config)
    print(multimodal)
    words_emb = torch.autograd.Variable(torch.randn(10, 56, 300))
    img_features = torch.autograd.Variable(torch.randn(10, 512))
    sentence, image_features = multimodal(words_emb, img_features)
    print('output of multimodal', sentence.shape)

    cnn = CNNTextModule(config)
    input = torch.autograd.Variable(torch.randn(10, 56, 300))
    output = cnn(input)
    print('output of cnn')
    print(output.size())
示例#8
0
            index], targets[index]
        padded_questions = np.zeros((self.config.batch_size, self.config.max_qlen, \
            self.gvar.word_vectors.syn0.shape[1]))
        for i in range(self.config.batch_size):
            padded_questions[i, 0:lengths[i], :] = questions[i][0:lengths[i]]
        padded_questions = Variable(torch.from_numpy(padded_questions)).float()

        if self.config.gpu:
            padded_questions = padded_questions.cuda()

        return (padded_questions, lengths), targets


if __name__ == '__main__':
    gvar = GlobalVariable()
    config = Config('parameter.json', model_type='baseline')
    dataset = QuestionDataset(config, gvar)
    for questions, targets in dataset.next_batch():
        print questions
        print targets
        break

    print '---------------------'
    config = Config('parameter.json', model_type='lstm')
    dataset = QuestionDataset(config, gvar)
    for (questions, targets), lengths in dataset.next_batch():
        print questions
        print targets
        print lengths
        break