commit
ec8e2108a4
File diff suppressed because it is too large
Load Diff
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 501 KiB |
@ -0,0 +1,46 @@
|
||||
# Locales
|
||||
|
||||
export LC_ALL=en_US.UTF-8
|
||||
export LANG=en_US.UTF-8
|
||||
export LANGUAGE=en_US.UTF-8
|
||||
|
||||
# Aliases
|
||||
|
||||
alias rm='rm -i'
|
||||
alias cp='cp -i'
|
||||
alias mv='mv -i'
|
||||
|
||||
alias ls='ls -hFG'
|
||||
alias l='ls -lF'
|
||||
alias ll='ls -alF'
|
||||
alias lt='ls -ltrF'
|
||||
alias ll='ls -alF'
|
||||
alias lls='ls -alSrF'
|
||||
alias llt='ls -altrF'
|
||||
|
||||
# Colorize directory listing
|
||||
|
||||
alias ls="ls -ph --color=auto"
|
||||
|
||||
# Colorize grep
|
||||
|
||||
if echo hello|grep --color=auto l >/dev/null 2>&1; then
|
||||
export GREP_OPTIONS="--color=auto" GREP_COLOR="1;31"
|
||||
fi
|
||||
|
||||
# Shell
|
||||
|
||||
export CLICOLOR="1"
|
||||
|
||||
YELLOW="\[\033[1;33m\]"
|
||||
NO_COLOUR="\[\033[0m\]"
|
||||
GREEN="\[\033[1;32m\]"
|
||||
WHITE="\[\033[1;37m\]"
|
||||
|
||||
source ~/.scripts/git-prompt.sh
|
||||
|
||||
export PS1="\[\033[1;33m\]λ $WHITE\h $GREEN\w$YELLOW\$(__git_ps1 \" \[\033[35m\]{\[\033[36m\]%s\[\033[35m\]}\")$NO_COLOUR "
|
||||
|
||||
# Git
|
||||
|
||||
source ~/.scripts/git-completion.sh
|
@ -0,0 +1,43 @@
|
||||
[user]
|
||||
name =
|
||||
email =
|
||||
|
||||
[alias]
|
||||
st = status --branch --short
|
||||
ci = commit
|
||||
br = branch
|
||||
co = checkout
|
||||
df = diff
|
||||
l = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
|
||||
ll = log --stat
|
||||
|
||||
[merge]
|
||||
tool = vimdiff
|
||||
|
||||
[core]
|
||||
excludesfile = ~/.gitignore
|
||||
editor = vim
|
||||
|
||||
[color]
|
||||
branch = auto
|
||||
diff = auto
|
||||
status = auto
|
||||
|
||||
[color "branch"]
|
||||
current = yellow reverse
|
||||
local = yellow
|
||||
remote = green
|
||||
|
||||
[color "diff"]
|
||||
meta = yellow bold
|
||||
frag = magenta bold
|
||||
old = red bold
|
||||
new = green bold
|
||||
|
||||
[color "status"]
|
||||
added = yellow
|
||||
changed = green
|
||||
untracked = cyan
|
||||
|
||||
[push]
|
||||
default = matching
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,86 @@
|
||||
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import collections
|
||||
|
||||
from paddle.trainer_config_helpers.default_decorators import wrap_name_default
|
||||
import paddle.trainer_config_helpers as conf_helps
|
||||
|
||||
|
||||
class Layer(object):
|
||||
def __init__(self, name=None, parent_layers=None):
|
||||
assert isinstance(parent_layers, dict)
|
||||
self.name = name
|
||||
self.__parent_layers__ = parent_layers
|
||||
|
||||
def to_proto(self, context):
|
||||
"""
|
||||
function to set proto attribute
|
||||
"""
|
||||
kwargs = dict()
|
||||
for layer_name in self.__parent_layers__:
|
||||
if not isinstance(self.__parent_layers__[layer_name],
|
||||
collections.Sequence):
|
||||
v1_layer = self.__parent_layers__[layer_name].to_proto(
|
||||
context=context)
|
||||
else:
|
||||
v1_layer = map(lambda x: x.to_proto(context=context),
|
||||
self.__parent_layers__[layer_name])
|
||||
kwargs[layer_name] = v1_layer
|
||||
|
||||
if self.name is None:
|
||||
return self.to_proto_impl(**kwargs)
|
||||
elif self.name not in context:
|
||||
context[self.name] = self.to_proto_impl(**kwargs)
|
||||
|
||||
return context[self.name]
|
||||
|
||||
def to_proto_impl(self, **kwargs):
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
def __convert_to_v2__(method_name, parent_names, is_default_name=True):
|
||||
if is_default_name:
|
||||
wrapper = wrap_name_default(name_prefix=method_name)
|
||||
else:
|
||||
wrapper = None
|
||||
|
||||
class V2LayerImpl(Layer):
|
||||
def __init__(self, **kwargs):
|
||||
parent_layers = dict()
|
||||
other_kwargs = dict()
|
||||
for pname in parent_names:
|
||||
if kwargs.has_key(pname):
|
||||
parent_layers[pname] = kwargs[pname]
|
||||
|
||||
for key in kwargs.keys():
|
||||
if key not in parent_names:
|
||||
other_kwargs[key] = kwargs[key]
|
||||
|
||||
name = kwargs.get('name', None)
|
||||
super(V2LayerImpl, self).__init__(name, parent_layers)
|
||||
self.__other_kwargs__ = other_kwargs
|
||||
|
||||
if wrapper is not None:
|
||||
__init__ = wrapper(__init__)
|
||||
|
||||
def to_proto_impl(self, **kwargs):
|
||||
args = dict()
|
||||
for each in kwargs:
|
||||
args[each] = kwargs[each]
|
||||
for each in self.__other_kwargs__:
|
||||
args[each] = self.__other_kwargs__[each]
|
||||
return getattr(conf_helps, method_name)(**args)
|
||||
|
||||
return V2LayerImpl
|
@ -0,0 +1,120 @@
|
||||
# /usr/bin/env python
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""
|
||||
IMDB dataset: http://ai.stanford.edu/%7Eamaas/data/sentiment/aclImdb_v1.tar.gz
|
||||
"""
|
||||
import paddle.v2.dataset.common
|
||||
import tarfile
|
||||
import Queue
|
||||
import re
|
||||
import string
|
||||
import threading
|
||||
|
||||
__all__ = ['build_dict', 'train', 'test']
|
||||
|
||||
URL = 'http://ai.stanford.edu/%7Eamaas/data/sentiment/aclImdb_v1.tar.gz'
|
||||
MD5 = '7c2ac02c03563afcf9b574c7e56c153a'
|
||||
|
||||
|
||||
# Read files that match pattern. Tokenize and yield each file.
|
||||
def tokenize(pattern):
|
||||
with tarfile.open(paddle.v2.dataset.common.download(URL, 'imdb',
|
||||
MD5)) as tarf:
|
||||
# Note that we should use tarfile.next(), which does
|
||||
# sequential access of member files, other than
|
||||
# tarfile.extractfile, which does random access and might
|
||||
# destroy hard disks.
|
||||
tf = tarf.next()
|
||||
while tf != None:
|
||||
if bool(pattern.match(tf.name)):
|
||||
# newline and punctuations removal and ad-hoc tokenization.
|
||||
yield tarf.extractfile(tf).read().rstrip("\n\r").translate(
|
||||
None, string.punctuation).lower().split()
|
||||
tf = tarf.next()
|
||||
|
||||
|
||||
def build_dict(pattern, cutoff):
|
||||
word_freq = {}
|
||||
for doc in tokenize(pattern):
|
||||
for word in doc:
|
||||
paddle.v2.dataset.common.dict_add(word_freq, word)
|
||||
|
||||
# Not sure if we should prune less-frequent words here.
|
||||
word_freq = filter(lambda x: x[1] > cutoff, word_freq.items())
|
||||
|
||||
dictionary = sorted(word_freq, key=lambda x: (-x[1], x[0]))
|
||||
words, _ = list(zip(*dictionary))
|
||||
word_idx = dict(zip(words, xrange(len(words))))
|
||||
word_idx['<unk>'] = len(words)
|
||||
return word_idx
|
||||
|
||||
|
||||
def reader_creator(pos_pattern, neg_pattern, word_idx, buffer_size):
|
||||
UNK = word_idx['<unk>']
|
||||
|
||||
qs = [Queue.Queue(maxsize=buffer_size), Queue.Queue(maxsize=buffer_size)]
|
||||
|
||||
def load(pattern, queue):
|
||||
for doc in tokenize(pattern):
|
||||
queue.put(doc)
|
||||
queue.put(None)
|
||||
|
||||
def reader():
|
||||
# Creates two threads that loads positive and negative samples
|
||||
# into qs.
|
||||
t0 = threading.Thread(
|
||||
target=load, args=(
|
||||
pos_pattern,
|
||||
qs[0], ))
|
||||
t0.daemon = True
|
||||
t0.start()
|
||||
|
||||
t1 = threading.Thread(
|
||||
target=load, args=(
|
||||
neg_pattern,
|
||||
qs[1], ))
|
||||
t1.daemon = True
|
||||
t1.start()
|
||||
|
||||
# Read alternatively from qs[0] and qs[1].
|
||||
i = 0
|
||||
doc = qs[i].get()
|
||||
while doc != None:
|
||||
yield [word_idx.get(w, UNK) for w in doc], i % 2
|
||||
i += 1
|
||||
doc = qs[i % 2].get()
|
||||
|
||||
# If any queue is empty, reads from the other queue.
|
||||
i += 1
|
||||
doc = qs[i % 2].get()
|
||||
while doc != None:
|
||||
yield [word_idx.get(w, UNK) for w in doc], i % 2
|
||||
doc = qs[i % 2].get()
|
||||
|
||||
return reader()
|
||||
|
||||
|
||||
def train(word_idx):
|
||||
return reader_creator(
|
||||
re.compile("aclImdb/train/pos/.*\.txt$"),
|
||||
re.compile("aclImdb/train/neg/.*\.txt$"), word_idx, 1000)
|
||||
|
||||
|
||||
def test(word_idx):
|
||||
return reader_creator(
|
||||
re.compile("aclImdb/test/pos/.*\.txt$"),
|
||||
re.compile("aclImdb/test/neg/.*\.txt$"), word_idx, 1000)
|
@ -0,0 +1,79 @@
|
||||
"""
|
||||
imikolov's simple dataset: http://www.fit.vutbr.cz/~imikolov/rnnlm/
|
||||
"""
|
||||
import paddle.v2.dataset.common
|
||||
import tarfile
|
||||
|
||||
__all__ = ['train', 'test']
|
||||
|
||||
URL = 'http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz'
|
||||
MD5 = '30177ea32e27c525793142b6bf2c8e2d'
|
||||
|
||||
|
||||
def word_count(f, word_freq=None):
|
||||
add = paddle.v2.dataset.common.dict_add
|
||||
if word_freq == None:
|
||||
word_freq = {}
|
||||
|
||||
for l in f:
|
||||
for w in l.strip().split():
|
||||
add(word_freq, w)
|
||||
add(word_freq, '<s>')
|
||||
add(word_freq, '<e>')
|
||||
|
||||
return word_freq
|
||||
|
||||
|
||||
def build_dict(train_filename, test_filename):
|
||||
with tarfile.open(
|
||||
paddle.v2.dataset.common.download(
|
||||
paddle.v2.dataset.imikolov.URL, 'imikolov',
|
||||
paddle.v2.dataset.imikolov.MD5)) as tf:
|
||||
trainf = tf.extractfile(train_filename)
|
||||
testf = tf.extractfile(test_filename)
|
||||
word_freq = word_count(testf, word_count(trainf))
|
||||
|
||||
TYPO_FREQ = 50
|
||||
word_freq = filter(lambda x: x[1] > TYPO_FREQ, word_freq.items())
|
||||
|
||||
dictionary = sorted(word_freq, key=lambda x: (-x[1], x[0]))
|
||||
words, _ = list(zip(*dictionary))
|
||||
word_idx = dict(zip(words, xrange(len(words))))
|
||||
word_idx['<unk>'] = len(words)
|
||||
|
||||
return word_idx
|
||||
|
||||
|
||||
word_idx = {}
|
||||
|
||||
|
||||
def reader_creator(filename, n):
|
||||
global word_idx
|
||||
if len(word_idx) == 0:
|
||||
word_idx = build_dict('./simple-examples/data/ptb.train.txt',
|
||||
'./simple-examples/data/ptb.valid.txt')
|
||||
|
||||
def reader():
|
||||
with tarfile.open(
|
||||
paddle.v2.dataset.common.download(
|
||||
paddle.v2.dataset.imikolov.URL, 'imikolov',
|
||||
paddle.v2.dataset.imikolov.MD5)) as tf:
|
||||
f = tf.extractfile(filename)
|
||||
|
||||
UNK = word_idx['<unk>']
|
||||
for l in f:
|
||||
l = ['<s>'] + l.strip().split() + ['<e>']
|
||||
if len(l) >= n:
|
||||
l = [word_idx.get(w, UNK) for w in l]
|
||||
for i in range(n, len(l) + 1):
|
||||
yield tuple(l[i - n:i])
|
||||
|
||||
return reader
|
||||
|
||||
|
||||
def train(n):
|
||||
return reader_creator('./simple-examples/data/ptb.train.txt', n)
|
||||
|
||||
|
||||
def test(n):
|
||||
return reader_creator('./simple-examples/data/ptb.valid.txt', n)
|
@ -0,0 +1,43 @@
|
||||
import paddle.v2.dataset.imdb
|
||||
import unittest
|
||||
import re
|
||||
|
||||
TRAIN_POS_PATTERN = re.compile("aclImdb/train/pos/.*\.txt$")
|
||||
TRAIN_NEG_PATTERN = re.compile("aclImdb/train/neg/.*\.txt$")
|
||||
TRAIN_PATTERN = re.compile("aclImdb/train/.*\.txt$")
|
||||
|
||||
TEST_POS_PATTERN = re.compile("aclImdb/test/pos/.*\.txt$")
|
||||
TEST_NEG_PATTERN = re.compile("aclImdb/test/neg/.*\.txt$")
|
||||
TEST_PATTERN = re.compile("aclImdb/test/.*\.txt$")
|
||||
|
||||
|
||||
class TestIMDB(unittest.TestCase):
|
||||
word_idx = None
|
||||
|
||||
def test_build_dict(self):
|
||||
if self.word_idx == None:
|
||||
self.word_idx = paddle.v2.dataset.imdb.build_dict(TRAIN_PATTERN,
|
||||
150)
|
||||
|
||||
self.assertEqual(len(self.word_idx), 7036)
|
||||
|
||||
def check_dataset(self, dataset, expected_size):
|
||||
if self.word_idx == None:
|
||||
self.word_idx = paddle.v2.dataset.imdb.build_dict(TRAIN_PATTERN,
|
||||
150)
|
||||
|
||||
sum = 0
|
||||
for l in dataset(self.word_idx):
|
||||
self.assertEqual(l[1], sum % 2)
|
||||
sum += 1
|
||||
self.assertEqual(sum, expected_size)
|
||||
|
||||
def test_train(self):
|
||||
self.check_dataset(paddle.v2.dataset.imdb.train, 25000)
|
||||
|
||||
def test_test(self):
|
||||
self.check_dataset(paddle.v2.dataset.imdb.test, 25000)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -0,0 +1,20 @@
|
||||
import paddle.v2.dataset.imikolov
|
||||
import unittest
|
||||
|
||||
|
||||
class TestMikolov(unittest.TestCase):
|
||||
def check_reader(self, reader, n):
|
||||
for l in reader():
|
||||
self.assertEqual(len(l), n)
|
||||
|
||||
def test_train(self):
|
||||
n = 5
|
||||
self.check_reader(paddle.v2.dataset.imikolov.train(n), n)
|
||||
|
||||
def test_test(self):
|
||||
n = 5
|
||||
self.check_reader(paddle.v2.dataset.imikolov.test(n), n)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue