コード例 #1
0
ファイル: deep_q_learner.py プロジェクト: ckcollab/plithos
 def __init__(self, state_num, action_num, experience_replay=True):
     self.state_num = state_num
     self.action_num = action_num
     self.experience_replay = experience_replay
     self.experience_pool = []
     self.model = get_model(state_num, action_num)
     train_conf = TrainerConfig()
     train_conf.learning_rate = LEARNING_RATE
     train_conf.weight_l2 = 0
     self.trainer = SGDTrainer(self.model, train_conf)
     self.trainer.training_names = []
     self.trainer.training_variables = []
     self.thread_lock = threading.Lock()
     self.epsilon = EPSILON
     self.tick = 0
コード例 #2
0
ファイル: agent.py プロジェクト: JunjieHu/deepy
 def __init__(self, state_num, action_num, experience_replay=True):
     self.state_num = state_num
     self.action_num = action_num
     self.experience_replay = experience_replay
     self.experience_pool = []
     self.model = get_model(state_num, action_num)
     train_conf = TrainerConfig()
     train_conf.learning_rate = LEARNING_RATE
     train_conf.weight_l2 = 0
     self.trainer = SGDTrainer(self.model, train_conf)
     self.trainer.training_names = []
     self.trainer.training_variables = []
     self.thread_lock = threading.Lock()
     self.epsilon = EPSILON
     self.tick = 0
コード例 #3
0
ファイル: rnn_auto_encoder.py プロジェクト: 52nlp/deepy
#!/usr/bin/env python
# -*- coding: utf-8 -*-

c

from deepy.networks import AutoEncoder
from deepy.layers import RNN, Dense
from deepy.trainers import SGDTrainer, LearningRateAnnealer

from util import get_data, VECTOR_SIZE, SEQUENCE_LENGTH

HIDDEN_SIZE = 50

model_path = os.path.join(os.path.dirname(__file__), "models", "rnn1.gz")

if __name__ == '__main__':
    model = AutoEncoder(input_dim=VECTOR_SIZE, input_tensor=3)
    model.stack_encoders(RNN(hidden_size=HIDDEN_SIZE, input_type="sequence", output_type="one"))
    model.stack_decoders(RNN(hidden_size=HIDDEN_SIZE, input_type="one", output_type="sequence", steps=SEQUENCE_LENGTH),
                         Dense(VECTOR_SIZE, 'softmax'))

    trainer = SGDTrainer(model)

    annealer = LearningRateAnnealer(trainer)

    trainer.run(get_data(), controllers=[annealer])

    model.save_params(model_path)

コード例 #4
0
ファイル: lstm_rnnlm.py プロジェクト: tangyaohua/dl4mt
default_model = os.path.join(os.path.dirname(__file__), "models", "lstm_rnnlmnew.gz")
default_dict  = '/home/tangyaohua/dl4mt/data/larger.corpus/vocab.chinese.pkl'
# default_dict = '/home/tangyh/Dropbox/PycharmProjects/dl4mt/session2/lm/resources/vocab.chinese.pkl'

if __name__ == '__main__':
    ap = ArgumentParser()
    ap.add_argument("--model", default='')
    ap.add_argument("--dictpath", default=default_dict)
    ap.add_argument("--small", action="store_true")
    args = ap.parse_args()

    vocab, lmdata = load_datagivendict(dictpath=args.dictpath, small=args.small, history_len=5, batch_size=16)
    inputx=T.imatrix('x')
    print len(vocab), 'len(vocab)'
    model = NeuralLM(len(vocab), test_data=None, input_tensor=inputx)
    model.stack(LSTM(hidden_size=100, output_type="sequence",
                    persistent_state=True, batch_size=lmdata.size,
                    reset_state_for_input=0),
                FullOutputLayer(len(vocab)))

    if os.path.exists(args.model):
        model.load_params(args.model)

    trainer = SGDTrainer(model, {"learning_rate": LearningRateAnnealer.learning_rate(1.2),
                                 "weight_l2": 1e-7})
    annealer = LearningRateAnnealer(trainer)

    trainer.run(lmdata, controllers=[annealer])

    model.save_params(default_model)
コード例 #5
0
"""
An auto-encoder for compress MNIST images.
"""

import logging, os
logging.basicConfig(level=logging.INFO)

from deepy.dataset import MnistDataset, MiniBatches
from deepy.networks import AutoEncoder
from deepy.layers import Dense
from deepy.trainers import SGDTrainer, LearningRateAnnealer
from deepy.utils import shared_scalar

model_path = os.path.join(os.path.dirname(__file__), "models",
                          "mnist_autoencoder.gz")

if __name__ == '__main__':
    model = AutoEncoder(input_dim=28 * 28, rep_dim=30)
    model.stack_encoders(Dense(50, 'tanh'), Dense(30))
    model.stack_decoders(Dense(50, 'tanh'), Dense(28 * 28))

    trainer = SGDTrainer(model, {
        'learning_rate': shared_scalar(0.05),
        'gradient_clipping': 3
    })

    mnist = MiniBatches(MnistDataset(for_autoencoder=True), batch_size=20)

    trainer.run(mnist, controllers=[LearningRateAnnealer(trainer)])

    model.save_params(model_path)
コード例 #6
0
ファイル: mnist_auto_encoder.py プロジェクト: JunjieHu/deepy
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
An auto-encoder for compress MNIST images.
"""


import logging, os
logging.basicConfig(level=logging.INFO)

from deepy.dataset import MnistDataset, MiniBatches
from deepy.networks import AutoEncoder
from deepy.layers import Dense
from deepy.trainers import SGDTrainer, LearningRateAnnealer
from deepy.utils import shared_scalar

model_path = os.path.join(os.path.dirname(__file__), "models", "mnist_autoencoder.gz")

if __name__ == '__main__':
    model = AutoEncoder(input_dim=28 * 28, rep_dim=30)
    model.stack_encoders(Dense(50, 'tanh'), Dense(30))
    model.stack_decoders(Dense(50, 'tanh'), Dense(28 * 28))

    trainer = SGDTrainer(model, {'learning_rate': shared_scalar(0.05), 'gradient_clipping': 3})

    mnist = MiniBatches(MnistDataset(for_autoencoder=True), batch_size=20)

    trainer.run(mnist, controllers=[LearningRateAnnealer(trainer)])

    model.save_params(model_path)
コード例 #7
0
ファイル: char_deep_rnn.py プロジェクト: zhp562176325/deepy
train_path = os.path.join(resource_dir, "ptb.train.txt")
valid_path = os.path.join(resource_dir, "ptb.valid.txt")
vocab = Vocab(char_based=True)
vocab.load(vocab_path, max_size=1000)

model = NeuralLM(input_dim=vocab.size, input_tensor=3)
model.stack(
    RNN(hidden_size=100, output_type="sequence"),
    RNN(hidden_size=100, output_type="sequence"),
    Dense(vocab.size, "softmax"))


if __name__ == '__main__':
    ap = ArgumentParser()
    ap.add_argument("--model", default=os.path.join(os.path.dirname(__file__), "models", "char_rnn_model1.gz"))
    ap.add_argument("--sample", default="")
    args = ap.parse_args()

    if os.path.exists(args.model):
        model.load_params(args.model)

    lmdata = LMDataset(vocab, train_path, valid_path, history_len=30, char_based=True, max_tokens=300)
    batch = SequentialMiniBatches(lmdata, batch_size=20)

    trainer = SGDTrainer(model)
    annealer = LearningRateAnnealer()

    trainer.run(batch, epoch_controllers=[annealer])

    model.save_params(args.model)
コード例 #8
0
batch_set = MiniBatches(dataset)

if __name__ == '__main__':
    model = NeuralClassifier(input_dim=26, input_tensor=3)
    model.stack(
        RNN(hidden_size=30,
            input_type="sequence",
            output_type="sequence",
            vector_core=0.1),
        RNN(hidden_size=30,
            input_type="sequence",
            output_type="sequence",
            vector_core=0.3),
        RNN(hidden_size=30,
            input_type="sequence",
            output_type="sequence",
            vector_core=0.6),
        RNN(hidden_size=30,
            input_type="sequence",
            output_type="one",
            vector_core=0.9), Dense(4), Softmax())

    trainer = SGDTrainer(model)

    annealer = LearningRateAnnealer()

    trainer.run(batch_set.train_set(),
                batch_set.valid_set(),
                controllers=[annealer])
コード例 #9
0
ファイル: agent.py プロジェクト: JunjieHu/deepy
class DQNAgent(object):
    """
    Agent of deep Q learning.
    """

    def __init__(self, state_num, action_num, experience_replay=True):
        self.state_num = state_num
        self.action_num = action_num
        self.experience_replay = experience_replay
        self.experience_pool = []
        self.model = get_model(state_num, action_num)
        train_conf = TrainerConfig()
        train_conf.learning_rate = LEARNING_RATE
        train_conf.weight_l2 = 0
        self.trainer = SGDTrainer(self.model, train_conf)
        self.trainer.training_names = []
        self.trainer.training_variables = []
        self.thread_lock = threading.Lock()
        self.epsilon = EPSILON
        self.tick = 0

    def action(self, state):
        if random.uniform(0, 1) < self.epsilon:
            return random.randint(0, self.action_num -1)
        else:
            with self.thread_lock:
                action = self.model.compute([state])
            return int(action[0].argmax())

    def learn(self, state, action, reward, next_state, enable_replay=True):
        # Learn
        next_q = self.model.compute([next_state])[0]
        best_a = next_q.argmax()
        max_q = next_q[best_a]
        target = reward + GAMMA * max_q
        # Forward
        with self.thread_lock:
            y = list(self.model.compute([state])[0])
        y_action = y[action]
        if target > y_action + TDERROR_CLAMP:
            target = y_action + TDERROR_CLAMP
        elif target < y_action - TDERROR_CLAMP:
            target = y_action - TDERROR_CLAMP
        y[action] = target
        # Back-propagate
        with self.thread_lock:
            self.trainer._learning_func([state], [y])
        # Replay
        if self.experience_replay and enable_replay:
            if self.tick % EXPERIENCE_RECORD_INTERVAL == 0:
                self.record_experience(state, action, reward, next_state)
            self.tick += 1
            self.replay()

    def replay(self):
        if not self.experience_pool:
            return
        for _ in range(REPLAY_TIMES):
            state, action, reward, next_state = random.choice(self.experience_pool)
            self.learn(state, action, reward, next_state, False)

    def record_experience(self, state, action, reward, next_state):
        if len(self.experience_pool) >= EXPERIENCE_SIZE:
            self.experience_pool.pop(0)
        self.experience_pool.append((state, action, reward, next_state))

    def save(self, path):
        self.model.save_params(path)

    def load(self, path):
        self.model.load_params(path)

    def set_epsilon(self, value):
        self.epsilon = value
コード例 #10
0
ファイル: char_deep_rnn.py プロジェクト: OlafLee/deepy
valid_path = os.path.join(resource_dir, "ptb.valid.txt")
vocab = Vocab(char_based=True)
vocab.load(vocab_path, max_size=1000)

model = NeuralLM(input_dim=vocab.size, input_tensor=3)
model.stack(
    RNN(hidden_size=100, output_type="sequence"),
    RNN(hidden_size=100, output_type="sequence"),
    Dense(vocab.size, "softmax"),
)


if __name__ == "__main__":
    ap = ArgumentParser()
    ap.add_argument("--model", default=os.path.join(os.path.dirname(__file__), "models", "char_rnn_model1.gz"))
    ap.add_argument("--sample", default="")
    args = ap.parse_args()

    if os.path.exists(args.model):
        model.load_params(args.model)

    lmdata = LMDataset(vocab, train_path, valid_path, history_len=30, char_based=True, max_tokens=300)
    batch = SequentialMiniBatches(lmdata, batch_size=20)

    trainer = SGDTrainer(model)
    annealer = LearningRateAnnealer(trainer)

    trainer.run(batch, controllers=[annealer])

    model.save_params(args.model)
コード例 #11
0
ファイル: deep_q_learner.py プロジェクト: ckcollab/plithos
class DQNAgent(object):
    """
    Agent of deep Q learning.
    """
    def __init__(self, state_num, action_num, experience_replay=True):
        self.state_num = state_num
        self.action_num = action_num
        self.experience_replay = experience_replay
        self.experience_pool = []
        self.model = get_model(state_num, action_num)
        train_conf = TrainerConfig()
        train_conf.learning_rate = LEARNING_RATE
        train_conf.weight_l2 = 0
        self.trainer = SGDTrainer(self.model, train_conf)
        self.trainer.training_names = []
        self.trainer.training_variables = []
        self.thread_lock = threading.Lock()
        self.epsilon = EPSILON
        self.tick = 0

    def get_action(self, state):
        if random.uniform(0, 1) < self.epsilon:
            return random.randint(0, self.action_num - 1)
        else:
            with self.thread_lock:
                action = self.model.compute([state])
            return int(action[0].argmax())

    def learn(self, state, action, reward, next_state, enable_replay=True):
        # Learn
        next_q = self.model.compute([next_state])[0]
        best_a = next_q.argmax()
        max_q = next_q[best_a]
        target = reward + GAMMA * max_q
        # Forward
        with self.thread_lock:
            y = list(self.model.compute([state])[0])
        y_action = y[action]
        if target > y_action + TDERROR_CLAMP:
            target = y_action + TDERROR_CLAMP
        elif target < y_action - TDERROR_CLAMP:
            target = y_action - TDERROR_CLAMP
        y[action] = target
        # Back-propagate
        with self.thread_lock:
            self.trainer.learning_func([state], [y])
        # Replay
        if self.experience_replay and enable_replay:
            if self.tick % EXPERIENCE_RECORD_INTERVAL == 0:
                self.record_experience(state, action, reward, next_state)
            self.tick += 1
            self.replay()

    def replay(self):
        if not self.experience_pool:
            return
        for _ in range(REPLAY_TIMES):
            state, action, reward, next_state = random.choice(
                self.experience_pool)
            self.learn(state, action, reward, next_state, False)

    def record_experience(self, state, action, reward, next_state):
        if len(self.experience_pool) >= EXPERIENCE_SIZE:
            self.experience_pool.pop(0)
        self.experience_pool.append((state, action, reward, next_state))

    def save(self, path):
        self.model.save_params(path)

    def load(self, path):
        self.model.load_params(path)
コード例 #12
0
if __name__ == '__main__':

    ap = ArgumentParser()
    ap.add_argument("--model", default=os.path.join(os.path.dirname(__file__), "models", "sequence_adding_100_2.gz"))
    args = ap.parse_args()

    model = NeuralRegressor(input_dim=2, input_tensor=3)
    model.stack(IRNN(hidden_size=100, input_type="sequence",
                     output_type="one"),
                      Dense(1))

    if os.path.exists(args.model):
        model.load_params(args.model)

    conf = TrainerConfig()
    conf.learning_rate = LearningRateAnnealer.learning_rate(0.01)
    conf.gradient_clipping = 3
    conf.patience = 50
    conf.gradient_tolerance = 5
    conf.avoid_nan = False
    trainer = SGDTrainer(model, conf)

    annealer = LearningRateAnnealer(patience=20)

    trainer.run(batch_set, controllers=[annealer])

    model.save_params(args.model)
    print "Identity matrix weight:"
    print model.first_layer().W_h.get_value().diagonal()
コード例 #13
0
"""
This experiment setting is described in http://arxiv.org/pdf/1502.03167v3.pdf.
MNIST MLP baseline model.
Gaussian initialization described in the paper did not convergence, I have no idea.
"""

import logging, os
logging.basicConfig(level=logging.INFO)

from deepy.dataset import MnistDataset, MiniBatches
from deepy.networks import NeuralClassifier
from deepy.layers import Dense, Softmax
from deepy.trainers import SGDTrainer

default_model = os.path.join(os.path.dirname(__file__), "models", "baseline1.gz")

if __name__ == '__main__':
    model = NeuralClassifier(input_dim=28 * 28)
    model.stack(Dense(100, 'sigmoid'),
                Dense(100, 'sigmoid'),
                Dense(100, 'sigmoid'),
                Dense(10, 'linear'),
                Softmax())

    trainer = SGDTrainer(model)

    batches = MiniBatches(MnistDataset(), batch_size=60)

    trainer.run(batches, epoch_controllers=[])

    model.save_params(default_model)
コード例 #14
0
"""
An auto-encoder for compress MNIST images.
"""

import logging, os
logging.basicConfig(level=logging.INFO)

from deepy.dataset import MnistDataset, MiniBatches
from deepy.networks import AutoEncoder
from deepy.layers import Dense
from deepy.trainers import SGDTrainer, LearningRateAnnealer
from deepy.utils import shared_scalar

model_path = os.path.join(os.path.dirname(__file__), "models",
                          "mnist_autoencoder.gz")

if __name__ == '__main__':
    model = AutoEncoder(input_dim=28 * 28, rep_dim=30)
    model.stack_encoders(Dense(50, 'tanh'), Dense(30))
    model.stack_decoders(Dense(50, 'tanh'), Dense(28 * 28))

    trainer = SGDTrainer(model, {
        'learning_rate': graph.shared(0.05),
        'gradient_clipping': 3
    })

    mnist = MiniBatches(MnistDataset(for_autoencoder=True), batch_size=20)

    trainer.run(mnist, epoch_controllers=[LearningRateAnnealer()])

    model.save_params(model_path)
コード例 #15
0
ファイル: rnn_auto_encoder.py プロジェクト: slorr80/deepy
# -*- coding: utf-8 -*-

import os
from deepy.networks import AutoEncoder
from deepy.layers import RNN, Dense
from deepy.trainers import SGDTrainer, LearningRateAnnealer

from util import get_data, VECTOR_SIZE, SEQUENCE_LENGTH

HIDDEN_SIZE = 50

model_path = os.path.join(os.path.dirname(__file__), "models", "rnn1.gz")

if __name__ == '__main__':
    model = AutoEncoder(rep_dim=10, input_dim=VECTOR_SIZE, input_tensor=3)
    model.stack_encoders(
        RNN(hidden_size=HIDDEN_SIZE, input_type="sequence", output_type="one"))
    model.stack_decoders(
        RNN(hidden_size=HIDDEN_SIZE,
            input_type="one",
            output_type="sequence",
            steps=SEQUENCE_LENGTH), Dense(VECTOR_SIZE, 'softmax'))

    trainer = SGDTrainer(model)

    annealer = LearningRateAnnealer(trainer)

    trainer.run(get_data(), controllers=[annealer])

    model.save_params(model_path)
コード例 #16
0
ファイル: mnist_batch_norm.py プロジェクト: 52nlp/deepy
In my experiment, it turns out the improvement of valid data stopped after 37 epochs. (See models/batch_norm1.log)
"""

import logging, os
logging.basicConfig(level=logging.INFO)

from deepy.dataset import MnistDataset, MiniBatches
from deepy.networks import NeuralClassifier
from deepy.layers import Dense, Softmax, BatchNormalization
from deepy.trainers import SGDTrainer

default_model = os.path.join(os.path.dirname(__file__), "models", "batch_norm1.gz")

if __name__ == '__main__':
    model = NeuralClassifier(input_dim=28*28)
    model.stack(Dense(100, 'sigmoid'),
                BatchNormalization(),
                Dense(100, 'sigmoid'),
                BatchNormalization(),
                Dense(100, 'sigmoid'),
                BatchNormalization(),
                Dense(10, 'linear'),
                Softmax())

    trainer = SGDTrainer(model)

    batches = MiniBatches(MnistDataset(), batch_size=60)

    trainer.run(batches, controllers=[])

    model.save_params(default_model)
コード例 #17
0
    ap.add_argument("--model", default="")
    ap.add_argument("--small", action="store_true")
    args = ap.parse_args()

    vocab, lmdata = load_data(small=args.small, history_len=5, batch_size=64)
    import pdb
    pdb.set_trace()
    model = NeuralLM(vocab.size)
    model.stack(
        RNN(hidden_size=100,
            output_type="sequence",
            hidden_activation='sigmoid',
            persistent_state=True,
            batch_size=lmdata.size,
            reset_state_for_input=0),
        ClassOutputLayer(output_size=100, class_size=100))

    if os.path.exists(args.model):
        model.load_params(args.model)

    trainer = SGDTrainer(
        model, {
            "learning_rate": LearningRateAnnealer.learning_rate(1.2),
            "weight_l2": 1e-7
        })
    annealer = LearningRateAnnealer()

    trainer.run(lmdata, epoch_controllers=[annealer])

    model.save_params(default_model)
コード例 #18
0
ファイル: word_pos_rnn.py プロジェクト: 52nlp/deepy
# Shuffle the data
random.Random(3).shuffle(data)

# Separate data
valid_size = int(len(data) * 0.15)
train_set = data[valid_size:]
valid_set = data[:valid_size]

dataset = SequentialDataset(train_set, valid=valid_set)
dataset.pad_left(20)
dataset.report()

batch_set = MiniBatches(dataset)

if __name__ == '__main__':
    model = NeuralClassifier(input_dim=26, input_tensor=3)
    model.stack(RNN(hidden_size=30, input_type="sequence", output_type="sequence", vector_core=0.1),
                       RNN(hidden_size=30, input_type="sequence", output_type="sequence", vector_core=0.3),
                       RNN(hidden_size=30, input_type="sequence", output_type="sequence", vector_core=0.6),
                       RNN(hidden_size=30, input_type="sequence", output_type="one", vector_core=0.9),
                       Dense(4),
                       Softmax())

    trainer = SGDTrainer(model)

    annealer = LearningRateAnnealer(trainer)

    trainer.run(batch_set.train_set(), batch_set.valid_set(), controllers=[annealer])