Esempio n. 1
0
def get_params():
    W_xh = nd.random_normal(scale=std, shape=(input_dim, hidden_dim), ctx=ctx)
    W_hh = nd.random_normal(scale=std, shape=(hidden_dim, hidden_dim), ctx=ctx)
    b_h = nd.zeros(hidden_dim, ctx=ctx)

    W_hy = nd.random_normal(scale=std, shape=(hidden_dim, output_dim), ctx=ctx)
    b_y = nd.zeros(output_dim, ctx=ctx)

    params = [W_xh, W_hh, b_h, W_hy, b_y]
    for param in params:
        param.attach_grad()
    return params
Esempio n. 2
0
def get_parameters():
    W_xh = nd.random_normal(scale=config.std, shape=(config.input_dim, config.hidden_dim))
    W_hh = nd.random_normal(scale=config.std, shape=(config.hidden_dim, config.hidden_dim))
    b_h = nd.zeros(config.hidden_dim)

    W_hy = nd.random_normal(scale=config.std, shape=(config.hidden_dim, config.output_dim))
    b_y = nd.zeros(config.output_dim)

    parameters = [W_xh, W_hh, b_h, W_hy, b_y]
    for parameter in parameters:
        parameter.attach_grad()

    return parameters
def init_params():
    w = nd.random_normal(scale=1, shape=(num_inputs, 1))
    b = nd.zeros(shape=(1,))
    params = [w, b]
    for param in params:
        param.attach_grad()#自动求导 需要创建它们的梯度
    return params
Esempio n. 4
0
def test_optimizer(optimizer, optimizer_params):
    # Weights
    index = 0
    weight = nd.zeros(shape=(8,))
    # Optimizer from registry
    optimizer = opt.create(optimizer, **optimizer_params)
    state = optimizer.create_state(index, weight)
    # Run a few updates
    for i in range(1, 13):
        grad = nd.random_normal(shape=(8,))
        if isinstance(optimizer, SockeyeOptimizer):
            batch_state = BatchState(metric_val=random())
            optimizer.pre_update_batch(batch_state)
        optimizer.update(index, weight, grad, state)
        # Checkpoint
        if i % 3 == 0:
            if isinstance(optimizer, SockeyeOptimizer):
                checkpoint_state = CheckpointState(checkpoint=(i % 3 + 1), metric_val=random())
                optimizer.pre_update_checkpoint(checkpoint_state)
Esempio n. 5
0
    def __init__(self, vocab_size, tag2idx, embedding_dim, hidden_dim):
        super(BiLSTM_CRF, self).__init__()
        with self.name_scope():
            self.embedding_dim = embedding_dim
            self.hidden_dim = hidden_dim
            self.vocab_size = vocab_size
            self.tag2idx = tag2idx
            self.tagset_size = len(tag2idx)

            self.word_embeds = nn.Embedding(vocab_size, embedding_dim)
            self.lstm = rnn.LSTM(hidden_dim // 2, num_layers=1, bidirectional=True)

            # Maps the output of the LSTM into tag space.
            self.hidden2tag = nn.Dense(self.tagset_size)

            # Matrix of transition parameters.  Entry i,j is the score of
            # transitioning *to* i *from* j.
            self.transitions = nd.random_normal(shape=(self.tagset_size, self.tagset_size))

            self.hidden = self.init_hidden()
Esempio n. 6
0
def test_optimizer(optimizer, optimizer_params):
    # Weights
    index = 0
    weight = nd.zeros(shape=(8, ))
    # Optimizer from registry
    optimizer = opt.create(optimizer, **optimizer_params)
    state = optimizer.create_state(index, weight)
    # Run a few updates
    for i in range(1, 13):
        grad = nd.random_normal(shape=(8, ))
        if isinstance(optimizer, SockeyeOptimizer):
            batch_state = BatchState(metric_val=random())
            optimizer.pre_update_batch(batch_state)
        optimizer.update(index, weight, grad, state)
        # Checkpoint
        if i % 3 == 0:
            if isinstance(optimizer, SockeyeOptimizer):
                checkpoint_state = CheckpointState(checkpoint=(i % 3 + 1),
                                                   metric_val=random())
                optimizer.pre_update_checkpoint(checkpoint_state)
Esempio n. 7
0
def get_params():
    # 隐含层
    W_xz = nd.random_normal(scale=std, shape=(input_dim, hidden_dim), ctx=ctx)
    W_hz = nd.random_normal(scale=std, shape=(hidden_dim, hidden_dim), ctx=ctx)
    b_z = nd.zeros(hidden_dim, ctx=ctx)

    W_xr = nd.random_normal(scale=std, shape=(input_dim, hidden_dim), ctx=ctx)
    W_hr = nd.random_normal(scale=std, shape=(hidden_dim, hidden_dim), ctx=ctx)
    b_r = nd.zeros(hidden_dim, ctx=ctx)

    W_xh = nd.random_normal(scale=std, shape=(input_dim, hidden_dim), ctx=ctx)
    W_hh = nd.random_normal(scale=std, shape=(hidden_dim, hidden_dim), ctx=ctx)
    b_h = nd.zeros(hidden_dim, ctx=ctx)

    W_hy = nd.random_normal(scale=std, shape=(hidden_dim, output_dim), ctx=ctx)
    b_y = nd.zeros(output_dim, ctx=ctx)

    params = [W_xz, W_hz, b_z, W_xr, W_hr, b_r, W_xh, W_hh, b_h, W_hy, b_y]
    for param in params:
        param.attach_grad()
    return params
Esempio n. 8
0
    def __init__(self, vocab_size, tag2idx, embedding_dim, hidden_dim):
        super(BiLSTM_CRF, self).__init__()
        with self.name_scope():
            self.embedding_dim = embedding_dim
            self.hidden_dim = hidden_dim
            self.vocab_size = vocab_size
            self.tag2idx = tag2idx
            self.tagset_size = len(tag2idx)

            self.word_embeds = nn.Embedding(vocab_size, embedding_dim)
            self.lstm = rnn.LSTM(hidden_dim // 2,
                                 num_layers=1,
                                 bidirectional=True)

            # Maps the output of the LSTM into tag space.
            self.hidden2tag = nn.Dense(self.tagset_size)

            # Matrix of transition parameters.  Entry i,j is the score of
            # transitioning *to* i *from* j.
            self.transitions = nd.random_normal(shape=(self.tagset_size,
                                                       self.tagset_size))

            self.hidden = self.init_hidden()
Esempio n. 9
0
from mxnet import ndarray as nd
import numpy as np

x = nd.zeros((3, 4))
print(x)

x = nd.ones((3, 4))
print(x)

x = nd.array([[1, 2], [3, 4]])
print(x)

# 创建随机数组,每个元素的值都是随机采样而来,经常被用于初始化模型参数
y = nd.random_normal(0, 1, shape=(3, 4))
print(y)
print(y.shape)
print(y.size)

x = nd.random_normal(0, 1, shape=(3, 4))
print(x)
print(x + y)
print(x * y)
# 指数运算.
print(nd.exp(y))
# 转置
print(nd.dot(x, y.T))

# 广播
a = nd.arange(3).reshape((3, 1))
b = nd.arange(2).reshape((1, 2))
print('a:', a)
Esempio n. 10
0
# 显式记录要求求导的函数
with ag.record():
    y = x * 2
    z = y * x

# 对函数进行求导
z.backward()
print('x.grad:',x.grad)

'''
对控制流进行求导
'''
def f(a):
    b = a * 2
    while nd.norm(b).asscalar() < 1000:
        b = b * 2
    if nd.sum(b).asscalar() > 0:
        c = b
    else:
        c = 100 * b
    return c

a = nd.random_normal(shape=3)
a.attach_grad()
with ag.record():
    c = f(a)

c.backward()

print('c.grad = ',a.grad)
Esempio n. 11
0
        for batch_i, data, label in data_iter(batch_size):
            with autograd.record():
                output = net(data, w, b)
                loss = square_loss(output, label)
            loss.backward()
            adagrad([w, b], sqrs, lr, batch_size)
            if batch_i * batch_size % period == 0:
                total_loss.append(
                    np.mean(square_loss(net(X, w, b), y).asnumpy()))
        print("Batch size %d, Learning rate %f, Epoch %d, loss %.4e" %
              (batch_size, lr, epoch, total_loss[-1]))
    print('w:', np.reshape(w.asnumpy(), (1, -1)), 'b:', b.asnumpy()[0], '\n')
    x_axis = np.linspace(0, epochs, len(total_loss), endpoint=True)
    plt.semilogy(x_axis, total_loss)
    plt.xlabel('epoch')
    plt.ylabel('loss')
    plt.show()


if __name__ == '__main__':
    # 生成数据集。
    num_inputs = 2
    num_examples = 1000
    true_w = [2, -3.4]
    true_b = 4.2
    X = nd.random_normal(scale=1, shape=(num_examples, num_inputs))
    y = true_w[0] * X[:, 0] + true_w[1] * X[:, 1] + true_b
    y += .01 * nd.random_normal(scale=1, shape=y.shape)
    dataset = gluon.data.ArrayDataset(X, y)

    train(batch_size=10, lr=0.9, epochs=3, period=10)
Esempio n. 12
0
    # 配置训练参数
    trainer_g = gluon.Trainer(generator.collect_params(), 'Adam',
                              {'learning_rate': learning_rate})
    trainer_d = gluon.Trainer(discriminator.collect_params(), 'Adam',
                              {'learning_rate': learning_rate})

    print('Begin to train')
    for epoch in range(151):
        loss_Dis = 0.
        loss_Gen = 0.

        for real, _ in data:

            # 加载到指定设备内存(gpu/cpu)
            real = real.as_in_context(ctx)
            Z_dim = nd.random_normal(0, 1, shape=(len(real), 100), ctx=ctx)

            # 训练判别器
            fake = generator(Z_dim)
            with autograd.record():
                real_res = discriminator(real)
                fake_res = discriminator(fake)
                loss_d = net.loss_d(real_res, fake_res)
            loss_d.backward()
            trainer_d.step(batch_size)

            # 训练生成器
            with autograd.record():
                fake = generator(Z_dim)
                fake_res = discriminator(fake)
                loss_g = net.loss_g(fake_res)
Esempio n. 13
0
from mxnet import ndarray as nd
from mxnet import autograd
import numpy as np
import matplotlib.pyplot as plt
import random

num_input = 2  # 每个样本的特征数
num_example = 1000  # 样本数量

true_w = [2, -3.4]
true_b = 4.2

X = nd.random_normal(shape=(num_example, num_input))  # 随机生成数据集
y = true_w[0] * X[:, 0] + true_w[1] * X[:, 1] + true_b
y += 0.01 * nd.random_normal(shape=y.shape)

#plt.scatter(X[:,1].asnumpy(),y.asnumpy())
#plt.show()

# 数据读取,data_iter函数返回batch_size个随机的样本和对应的目标
# 由yield构造一个迭代器
batch_size = 10


def data_iter():
    # 产生一个随机索引
    idx = list(range(num_example))
    random.shuffle(idx)
    # 遍历所有样本
    for i in range(0, num_example, batch_size):
        j = nd.array(idx[i:min(i + batch_size, num_example)])
Esempio n. 14
0
 def init_hidden(self):
     return [nd.random_normal(shape=(2, 1, self.hidden_dim // 2)),
             nd.random_normal(shape=(2, 1, self.hidden_dim // 2))]
Esempio n. 15
0
    return [text_labels[int(i)] for i in label]

# data, label = minist_train[0:9]
# show_image(data)
# print(get_text_labels(label))

# load data
batch_size = 256
train_data = gluon.data.DataLoader(minist_train, batch_size, shuffle = True)
test_data = gluon.data.DataLoader(minist_test, batch_size, shuffle = False)

# initialize parameters
num_inputs = 784 # 28*28 num of features
num_outputs = 10 # num of class

W = nd.random_normal(shape=(num_inputs, num_outputs))
b = nd.random_normal(shape=num_outputs) # row vec

params = [W, b]

for param in params:
    param.attach_grad()

def softmax(X):
    exp = nd.exp(X)
    partition = exp.sum(axis = 1, keepdims=True) # return (nrows, 1) matrix
    return exp / partition

def net(X):
    return softmax(nd.dot(X.reshape((-1, num_inputs)), W) + b)
    # the second dim is num_inputs, the first dim will be automatically calculated
class Config:
    def __init__(self):
        self.num_inputs = 3
        self.training_size = 1000
        self.batch_size = 10
        self.learning_rate = 1e-2
        self.num_epochs = 5


config = Config()


true_w = [2.5, 4.7, -3.2]
true_b = 2.9

X = nd.random_normal(shape=(config.training_size, config.num_inputs))
y = nd.dot(X, nd.array(true_w)) + true_b
y += 0.01 * nd.random_normal(shape=y.shape)


def data_generator(batch_size):
    index = list(range(config.training_size))
    random.shuffle(index)

    for i in range(0, config.training_size, batch_size):
        j = nd.array(index[i:min(i + batch_size, config.training_size)])
        yield nd.take(X, j), nd.take(y, j)


w = nd.random_normal(shape=(config.num_inputs, 1))
b = nd.zeros((1,))
Esempio n. 17
0
sys.path.append('..')
from utils import utils

batch_size = 256
train_data, test_data = utils.load_data_fashion_mnist(batch_size)

# 定义一个只有一层的隐藏层模型
from mxnet import ndarray as nd

num_inputs = 28 * 28
num_outputs = 10

num_hidden = 255
weight_scale = 0.01

W1 = nd.random_normal(shape=(num_inputs, num_hidden), scale=weight_scale)
b1 = nd.zeros(num_hidden)

W2 = nd.random_normal(shape=(num_hidden, num_outputs), scale=weight_scale)
b2 = nd.zeros(num_outputs)

params = [W1, b1, W2, b2]

for param in params:
    param.attach_grad()


# 定义非线性激活函数
def relu(X):
    # 先广播,然后对应的取两者之间的最大值组成返回矩阵
    return nd.maximum(X, 0)
mnist_train = gluon.data.vision.FashionMNIST(train=True, transform=transform)
mnist_test = gluon.data.vision.FashionMNIST(train=False, transform=transform)

data, label = mnist_train[0]
print('shape:', data.shape, 'label:', label)

batch_size = 50
train_data = gluon.data.DataLoader(mnist_train, batch_size, shuffle=True)
test_data = gluon.data.DataLoader(mnist_test, batch_size, shuffle=False)

num_inputs = 28 * 28
num_hidden1 = 256
num_hidden2 = 128
num_outputs = 10
weight_scale = .01
w1 = nd.random_normal(shape=(num_inputs, num_hidden1), scale=weight_scale)
b1 = nd.random_normal(shape=(num_hidden1))
w2 = nd.random_normal(shape=(num_hidden1, num_hidden2), scale=weight_scale)
b2 = nd.random_normal(shape=(num_hidden2))
w3 = nd.random_normal(shape=(num_hidden2, num_outputs), scale=weight_scale)
b3 = nd.random_normal(shape=(num_outputs))
params = [w1, b1, w2, b2, w3, b3]
for param in params:
    param.attach_grad()

def softmax(X):
    exp = nd.exp(X)
    partition = exp.sum(axis=1, keepdims=True)
    return exp / partition

def relu(X):
Esempio n. 19
0
        H = nd.tanh(nd.dot(X, W_xh) + nd.dot(H, W_hh) + b_h)
        # compute output from hidden state
        Y = nd.dot(H, W_hy) + b_y
        _outputs.append(Y)

    return _outputs, H


# gradient clipping to avoid gradient explosion
def gradient_clipping(parameters, threshold, ctx):
    if threshold is not None:
        norm = nd.array([0.0], ctx)

        for parameter in parameters:
            norm += nd.sum(parameter.grad ** 2)
        norm = nd.sqrt(norm).asscalar()

        if norm > threshold:
            for parameter in parameters:
                parameter.grad[:] *= (threshold / norm)


if __name__ == '__main__':
    initial_state = nd.zeros(shape=(config.batch_size, config.hidden_dim))
    dump_data = [nd.random_normal(shape=(config.batch_size, config.input_dim)) for _ in range(config.num_steps)]

    parameters = get_parameters()
    _outputs, final_state = rnn(dump_data, initial_state, *parameters)

    print(_outputs, final_state)
Esempio n. 20
0
def databalanceGenerator(workQueue, maxdatabalance):
    conn = pymysql.Connect(host="localhost",
                           port=3306,
                           user="******",
                           password="******",
                           database="stock",
                           charset="utf8")

    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    featureNames = [
        'DateString', 'Date', 'Code', 'Day1OpenPrice', 'Day1ClosePrice',
        'Day1Diff', 'Day1Percent', 'Day1LowPrice', 'Day1HighPrice',
        'Day1Volume', 'Day1Amount', 'Day1Exchange', 'Day2OpenPrice',
        'Day2ClosePrice', 'Day2Diff', 'Day2Percent', 'Day2LowPrice',
        'Day2HighPrice', 'Day2Volume', 'Day2Amount', 'Day2Exchange',
        'Day3OpenPrice', 'Day3ClosePrice', 'Day3Diff', 'Day3Percent',
        'Day3LowPrice', 'Day3HighPrice', 'Day3Volume', 'Day3Amount',
        'Day3Exchange', 'Day4OpenPrice', 'Day4ClosePrice', 'Day4Diff',
        'Day4Percent', 'Day4LowPrice', 'Day4HighPrice', 'Day4Volume',
        'Day4Amount', 'Day4Exchange', 'Day5OpenPrice', 'Day5ClosePrice',
        'Day5Diff', 'Day5Percent', 'Day5LowPrice', 'Day5HighPrice',
        'Day5Volume', 'Day5Amount', 'Day5Exchange', 'Day6OpenPrice',
        'Day6ClosePrice', 'Day6Diff', 'Day6Percent', 'Day6LowPrice',
        'Day6HighPrice', 'Day6Volume', 'Day6Amount', 'Day6Exchange',
        'Day7OpenPrice', 'Day7ClosePrice', 'Day7Diff', 'Day7Percent',
        'Day7LowPrice', 'Day7HighPrice', 'Day7Volume', 'Day7Amount',
        'Day7Exchange', 'Day8OpenPrice', 'Day8ClosePrice', 'Day8Diff',
        'Day8Percent', 'Day8LowPrice', 'Day8HighPrice', 'Day8Volume',
        'Day8Amount', 'Day8Exchange', 'Day9OpenPrice', 'Day9ClosePrice',
        'Day9Diff', 'Day9Percent', 'Day9LowPrice', 'Day9HighPrice',
        'Day9Volume', 'Day9Amount', 'Day9Exchange', 'Day10OpenPrice',
        'Day10ClosePrice', 'Day10Diff', 'Day10Percent', 'Day10LowPrice',
        'Day10HighPrice', 'Day10Volume', 'Day10Amount', 'Day10Exchange',
        'Day11OpenPrice', 'Day11ClosePrice', 'Day11Diff', 'Day11Percent',
        'Day11LowPrice', 'Day11HighPrice', 'Day11Volume', 'Day11Amount',
        'Day11Exchange', 'Day12OpenPrice', 'Day12ClosePrice', 'Day12Diff',
        'Day12Percent', 'Day12LowPrice', 'Day12HighPrice', 'Day12Volume',
        'Day12Amount', 'Day12Exchange', 'Day13OpenPrice', 'Day13ClosePrice',
        'Day13Diff', 'Day13Percent', 'Day13LowPrice', 'Day13HighPrice',
        'Day13Volume', 'Day13Amount', 'Day13Exchange', 'Day14OpenPrice',
        'Day14ClosePrice', 'Day14Diff', 'Day14Percent', 'Day14LowPrice',
        'Day14HighPrice', 'Day14Volume', 'Day14Amount', 'Day14Exchange',
        'Day15OpenPrice', 'Day15ClosePrice', 'Day15Diff', 'Day15Percent',
        'Day15LowPrice', 'Day15HighPrice', 'Day15Volume', 'Day15Amount',
        'Day15Exchange', 'Day16OpenPrice', 'Day16ClosePrice', 'Day16Diff',
        'Day16Percent', 'Day16LowPrice', 'Day16HighPrice', 'Day16Volume',
        'Day16Amount', 'Day16Exchange', 'Day17OpenPrice', 'Day17ClosePrice',
        'Day17Diff', 'Day17Percent', 'Day17LowPrice', 'Day17HighPrice',
        'Day17Volume', 'Day17Amount', 'Day17Exchange', 'Day18OpenPrice',
        'Day18ClosePrice', 'Day18Diff', 'Day18Percent', 'Day18LowPrice',
        'Day18HighPrice', 'Day18Volume', 'Day18Amount', 'Day18Exchange',
        'Day19OpenPrice', 'Day19ClosePrice', 'Day19Diff', 'Day19Percent',
        'Day19LowPrice', 'Day19HighPrice', 'Day19Volume', 'Day19Amount',
        'Day19Exchange', 'Day20OpenPrice', 'Day20ClosePrice', 'Day20Diff',
        'Day20Percent', 'Day20LowPrice', 'Day20HighPrice', 'Day20Volume',
        'Day20Amount', 'Day20Exchange', 'Day21OpenPrice', 'Day21ClosePrice',
        'Day21Diff', 'Day21Percent', 'Day21LowPrice', 'Day21HighPrice',
        'Day21Volume', 'Day21Amount', 'Day21Exchange', 'Day22OpenPrice',
        'Day22ClosePrice', 'Day22Diff', 'Day22Percent', 'Day22LowPrice',
        'Day22HighPrice', 'Day22Volume', 'Day22Amount', 'Day22Exchange',
        'Day23OpenPrice', 'Day23ClosePrice', 'Day23Diff', 'Day23Percent',
        'Day23LowPrice', 'Day23HighPrice', 'Day23Volume', 'Day23Amount',
        'Day23Exchange', 'Day24OpenPrice', 'Day24ClosePrice', 'Day24Diff',
        'Day24Percent', 'Day24LowPrice', 'Day24HighPrice', 'Day24Volume',
        'Day24Amount', 'Day24Exchange', 'Day25OpenPrice', 'Day25ClosePrice',
        'Day25Diff', 'Day25Percent', 'Day25LowPrice', 'Day25HighPrice',
        'Day25Volume', 'Day25Amount', 'Day25Exchange', 'Day26OpenPrice',
        'Day26ClosePrice', 'Day26Diff', 'Day26Percent', 'Day26LowPrice',
        'Day26HighPrice', 'Day26Volume', 'Day26Amount', 'Day26Exchange',
        'Day27OpenPrice', 'Day27ClosePrice', 'Day27Diff', 'Day27Percent',
        'Day27LowPrice', 'Day27HighPrice', 'Day27Volume', 'Day27Amount',
        'Day27Exchange', 'Day28OpenPrice', 'Day28ClosePrice', 'Day28Diff',
        'Day28Percent', 'Day28LowPrice', 'Day28HighPrice', 'Day28Volume',
        'Day28Amount', 'Day28Exchange', 'Day29OpenPrice', 'Day29ClosePrice',
        'Day29Diff', 'Day29Percent', 'Day29LowPrice', 'Day29HighPrice',
        'Day29Volume', 'Day29Amount', 'Day29Exchange', 'Day30OpenPrice',
        'Day30ClosePrice', 'Day30Diff', 'Day30Percent', 'Day30LowPrice',
        'Day30HighPrice', 'Day30Volume', 'Day30Amount', 'Day30Exchange',
        'Day31OpenPrice', 'Day31ClosePrice', 'Day31Diff', 'Day31Percent',
        'Day31LowPrice', 'Day31HighPrice', 'Day31Volume', 'Day31Amount',
        'Day31Exchange', 'Day32OpenPrice', 'Day32ClosePrice', 'Day32Diff',
        'Day32Percent', 'Day32LowPrice', 'Day32HighPrice', 'Day32Volume',
        'Day32Amount', 'Day32Exchange', 'Day33OpenPrice', 'Day33ClosePrice',
        'Day33Diff', 'Day33Percent', 'Day33LowPrice', 'Day33HighPrice',
        'Day33Volume', 'Day33Amount', 'Day33Exchange', 'Day34OpenPrice',
        'Day34ClosePrice', 'Day34Diff', 'Day34Percent', 'Day34LowPrice',
        'Day34HighPrice', 'Day34Volume', 'Day34Amount', 'Day34Exchange',
        'Day35OpenPrice', 'Day35ClosePrice', 'Day35Diff', 'Day35Percent',
        'Day35LowPrice', 'Day35HighPrice', 'Day35Volume', 'Day35Amount',
        'Day35Exchange', 'Day36OpenPrice', 'Day36ClosePrice', 'Day36Diff',
        'Day36Percent', 'Day36LowPrice', 'Day36HighPrice', 'Day36Volume',
        'Day36Amount', 'Day36Exchange', 'Day37OpenPrice', 'Day37ClosePrice',
        'Day37Diff', 'Day37Percent', 'Day37LowPrice', 'Day37HighPrice',
        'Day37Volume', 'Day37Amount', 'Day37Exchange', 'Day38OpenPrice',
        'Day38ClosePrice', 'Day38Diff', 'Day38Percent', 'Day38LowPrice',
        'Day38HighPrice', 'Day38Volume', 'Day38Amount', 'Day38Exchange',
        'Day39OpenPrice', 'Day39ClosePrice', 'Day39Diff', 'Day39Percent',
        'Day39LowPrice', 'Day39HighPrice', 'Day39Volume', 'Day39Amount',
        'Day39Exchange', 'Day40OpenPrice', 'Day40ClosePrice', 'Day40Diff',
        'Day40Percent', 'Day40LowPrice', 'Day40HighPrice', 'Day40Volume',
        'Day40Amount', 'Day40Exchange', 'Week1OpenPrice', 'Week1ClosePrice',
        'Week1LowPrice', 'Week1HighPrice', 'Week1Volume', 'Week1Amount',
        'Week1Diff', 'Week1Percent', 'Week1Exchange', 'Week2OpenPrice',
        'Week2ClosePrice', 'Week2LowPrice', 'Week2HighPrice', 'Week2Volume',
        'Week2Amount', 'Week2Diff', 'Week2Percent', 'Week2Exchange',
        'Week3OpenPrice', 'Week3ClosePrice', 'Week3LowPrice', 'Week3HighPrice',
        'Week3Volume', 'Week3Amount', 'Week3Diff', 'Week3Percent',
        'Week3Exchange', 'Week4OpenPrice', 'Week4ClosePrice', 'Week4LowPrice',
        'Week4HighPrice', 'Week4Volume', 'Week4Amount', 'Week4Diff',
        'Week4Percent', 'Week4Exchange'
    ]
    predictNames = [
        'NextDayIncrease', 'NextDayIncrease1', 'NextDayIncrease2',
        'NextDayIncrease3', 'NextDayIncrease4', 'NextDayIncrease5',
        'NextDayIncrease6', 'NextDayIncrease7', 'NextDayIncrease8',
        'NextDayIncrease9', 'NextDayIncrease10', 'NextDayDecrease1',
        'NextDayDecrease2', 'NextDayDecrease3', 'NextDayDecrease4',
        'NextDayDecrease5', 'NextDayDecrease6', 'NextDayDecrease7',
        'NextDayDecrease8', 'NextDayDecrease9', 'NextDayDecrease10'
    ]

    while (not workQueue.empty()):
        code = workQueue.get()
        df = pd.read_sql(
            "SELECT {} from {} where code = {} and DATABALANCE = -1 and {} = true "
            .format(getSQLColumnString(featureNames + predictNames), tableName,
                    code, predictCategory),
            conn,
            columns=featureNames + predictNames)

        dfless = df[df[predictCategory] == 1]
        #unbalaced class data enhance, add more data through giving a random_normal to the real data
        enhanceIter = copyNumbers  #len(df) // len(dfless)
        for i in range(enhanceIter):
            dflessCopy = dfless.copy(True)
            dflessCopy['DATABALANCE'] = (i + maxdatabalance + 1)
            for columnName in dfless.iloc[0].index:
                if not columnName.startswith('Next'):
                    normalarr = None
                    if columnName.__contains__(
                            'Percent') or columnName.__contains__(
                                'Exchange') or columnName.__contains__('Diff'):
                        normalarr = nd.random_normal(shape=(len(dfless), 1),
                                                     scale=0.01).asnumpy()
                    elif columnName.__contains__('Price'):
                        normalarr = nd.random_normal(shape=(len(dfless), 1),
                                                     scale=1).asnumpy()
                        #
                    elif isinstance(
                            df[columnName].iloc[0],
                            str) or columnName.__contains__(
                                'Code') or columnName.__contains__('CODELONG'):
                        #columnName.__contains__('Date') or columnName.__contains__('Code') or columnName.__contains__('CODELONG')
                        continue
                    else:
                        normalarr = nd.random_normal(shape=(len(dfless), 1),
                                                     scale=100).asnumpy()
                    dflessCopy[
                        columnName] = dflessCopy[columnName].values.reshape(
                            len(dfless), 1) + normalarr
            dflessCopy['DATABALANCE'] = (i + maxdatabalance + 1)
            for index, row in dflessCopy.iterrows():
                insertAExample(row, cursor, conn)
            #ignore_index true, otherwise duplicate index will happen
            #df = df.append(dflessCopy,ignore_index=True)

    cursor.close()
    conn.close()
                                            transform=transform)

batch_size = 256

# Read Data
train_data = gluon.data.DataLoader(dataset=mnist_train,
                                   batch_size=batch_size,
                                   shuffle=True)
test_data = gluon.data.DataLoader(dataset=mnist_test,
                                  batch_size=batch_size,
                                  shuffle=False)

# Initialize Params
num_inputs = 28 * 28
num_outputs = 10
w = nd.random_normal(shape=[num_inputs, num_outputs])
b = nd.random_normal(shape=(num_outputs))
params = [w, b]

for param in params:
    param.attach_grad()


# Define Model
def softmax(X):
    exp = nd.exp(X)
    partition = exp.sum(axis=1, keepdims=True)
    return exp / partition


def net(X):
## 测试 
##所以我们的第一个教程是如何只利用ndarray和autograd来实现一个线性回归的训练。
from mxnet import ndarray as nd
from mxnet import autograd
import random
from mxnet import gluon
 
#### 准备输入数据 ###
num_inputs = 2##数据维度
num_examples = 1000##样例大小

## 真实的需要估计的参数
true_w = [2, -3.4]##权重
true_b = 4.2##偏置

X = nd.random_normal(shape=(num_examples, num_inputs))## 1000*2
y = true_w[0] * X[:, 0] + true_w[1] * X[:, 1] + true_b## 1000*1
y += .01 * nd.random_normal(shape=y.shape)##加入 噪声 服从均值0和标准差为0.01的正态分布

## 读取数据
'''
batch_size = 10
def data_iter():
    # 产生一个随机索引
    idx = list(range(num_examples))
    random.shuffle(idx)##打乱
    for i in range(0, num_examples, batch_size):##0 10 20 ...
        j = nd.array(idx[i:min(i+batch_size,num_examples)])##随机抽取10个样例
        yield nd.take(X, j), nd.take(y, j)##样例和标签 我们通过python的yield来构造一个迭代器。
'''
batch_size = 10
Esempio n. 23
0
## 测试 python 0_linear_regression_dis2_with_bis.py
##所以我们的第一个教程是如何只利用ndarray和autograd来实现一个线性回归的训练。
## 两维 线性回归 带 偏置
# y = a*x1 + b*x2 + c
from mxnet import ndarray as nd
from mxnet import autograd  #反向传播自动求梯度
import random

#### 准备输入数据 ###########################
##############################################
num_inputs = 2  ##数据维度
num_examples = 1000  ##样例大小
## 真实的需要估计的参数
true_w = [2, -3.4]  ##权重
true_b = 4.2  ##偏置
X = nd.random_normal(shape=(num_examples, num_inputs))  ## 1000*2
y = true_w[0] * X[:, 0] + true_w[1] * X[:, 1] + true_b  ## 1000*1
y += .01 * nd.random_normal(shape=y.shape)  ##加入 噪声 服从均值0和标准差为0.01的正态分布
## 读取数据
batch_size = 10  #每次训练输入 10个样本数据


def data_iter():
    # 产生一个随机索引
    idx = list(range(num_examples))
    random.shuffle(idx)  ##打乱
    for i in range(0, num_examples, batch_size):  ##0 10 20 ...
        j = nd.array(idx[i:min(i + batch_size, num_examples)])  ##随机抽取10个样例
        yield nd.take(X, j), nd.take(y, j)  ##样例和标签 我们通过python的yield来构造一个迭代器。

Esempio n. 24
0
import matplotlib as mpl

mpl.rcParams['figure.dpi'] = 120
import matplotlib.pyplot as plt

mx.random.seed(1)
random.seed(1)

num_inputs = 2
num_examples = 1000
true_w = [2, -3.4]
true_b = 4.2
X = nd.random.normal(scale=1, shape=(num_examples, num_inputs))
y = true_w[0] * X[:, 0] + true_w[1] * X[:, 1] + true_b
y += .01 * nd.random_normal(scale=1, shape=y.shape)

dataset = gluon.data.ArrayDataset(X, y)

# define model
net = gluon.nn.Sequential()
net.add(gluon.nn.Dense(1))
square_loss = gluon.loss.L2Loss()

# initialize model
# net.initialize()


# begin train
def train(batch_size, lr, mom, epochs, period):
    assert period >= batch_size and batch_size % period == 0
#一个稍微复杂点的数据集,它跟MNIST非常像,但是内容不再是分类数字,而是服饰
## 准备 训练和测试数据集
batch_size = 256#每次训练 输入的图片数量
train_data, test_data = utils.load_data_fashion_mnist(batch_size)


###########################################################
### 定义模型 ##################
#@@@@初始化模型参数 权重和偏置@@@@
num_inputs = 28*28##输入为图像尺寸 28*28
num_outputs = 10#输出10个标签

num_hidden = 256#定义一个只有一个隐含层的模型,这个隐含层输出256个节点
weight_scale = .01#初始化权重参数的 均匀分布均值
#输入到隐含层 权重 + 偏置
W1 = nd.random_normal(shape=(num_inputs, num_hidden), scale=weight_scale)
b1 = nd.zeros(num_hidden)
#隐含层到输出层 权重 + 偏置
W2 = nd.random_normal(shape=(num_hidden, num_outputs), scale=weight_scale)
b2 = nd.zeros(num_outputs)
params = [W1, b1, W2, b2]#所有参数
for param in params:#参数添加自动求导
    param.attach_grad()

##非线性激活函数
#为了让我们的模型可以拟合非线性函数,我们需要在层之间插入非线性的激活函数。
def relu(X):
    return nd.maximum(X, 0)
### 定义模型
def net(X):
    X = X.reshape((-1, num_inputs))
Esempio n. 26
0
def get_params():
    w = nd.random_normal(shape=(num_inputs, 1)) * 0.01
    b = nd.zeros((1, ))
    for param in (w, b):
        param.attach_grad()
    return (w, b)
from mxnet import ndarray as nd
from mxnet import autograd
from mxnet import gluon
import random

num_inputs = 2;
num_examples = 1000

true_w = [2, -3.4];
true_b = 4.2;

X = nd.random_normal(shape = (num_examples, num_inputs)) # design matrix with 2 features
y = true_w[0] * X[:, 0] + true_w[1]*X[:, 1] + true_b
y += 0.01 * nd.random_normal(shape=y.shape)  # noise

batch_size = 10
dataset = gluon.data.ArrayDataset(X, y)
data_iter = gluon.data.DataLoader(dataset, batch_size, shuffle=True)

# define model
net = gluon.nn.Sequential()
net.add(gluon.nn.Dense(1))

# initial parameters
net.initialize() # default randomly initialize

# loss
square_loss = gluon.loss.L2Loss()

# optimization
trainer = gluon.Trainer(
Esempio n. 28
0
    #
    # CKPT = "/home/jiancheng/code/segmentation/deeplabv3p_gluon/workspace/tmptf/xception/model.ckpt"
    # FLAG = "imagenet_pretrain_for_pascal"
    # CLASSES = 21

    set_gpu_usage()

    output_folder = os.path.join(BASE, FLAG, "TF2NPY")
    extract_tensors_from_checkpoint_file(CKPT, output_folder=output_folder)

    keras_model = npy_to_keras(classes=CLASSES,
                               load_from=output_folder,
                               save_to=os.path.join(BASE, FLAG,
                                                    "%s.h5" % FLAG))

    print("Keras model setup. Output shape: ", keras_model.output.get_shape())

    weight_converter = WeightConverter(keras_model=keras_model)

    gluon_model = DeepLabv3p_gluon(classes=CLASSES)
    gluon_model.initialize(ctx=mx.gpu())
    inputs = nd.random_normal(shape=(1, 3, 512, 512), ctx=mx.gpu())
    outputs = gluon_model(inputs)
    print("Gluon model setup. Output shape: ", outputs.shape)

    weight_converter.set_parameters(gluon_model)

    gluon_model.save_params(os.path.join(BASE, FLAG, "%s.params" % FLAG))

    print("Clear.")
Esempio n. 29
0
def train(pool_size, epochs, train_data, val_data,  ctx, netEn, netDe,  netD, netD2, trainerEn, trainerDe, trainerD, trainerD2, lambda1, batch_size, expname,  append=True, useAE = False):
    tp_file = open(expname + "_trainloss.txt", "w")  
    tp_file.close()  
    text_file = open(expname + "_validtest.txt", "w")
    text_file.close()
    #netGT, netDT, _, _ = set_test_network(opt.depth, ctx, opt.lr, opt.beta1,opt.ndf,  opt.ngf, opt.append)
    GAN_loss = gluon.loss.SigmoidBinaryCrossEntropyLoss()
    L1_loss = gluon.loss.L2Loss()
    image_pool = imagePool.ImagePool(pool_size)
    metric = mx.metric.CustomMetric(facc)
    metric2 = mx.metric.CustomMetric(facc)
    metricMSE = mx.metric.MSE()
    loss_rec_G = []
    loss_rec_D = []
    loss_rec_R = []
    acc_rec = []
    acc2_rec = []
    loss_rec_D2 = []
    loss_rec_G2 = []
    lr = 0.002
    #mu = nd.random_normal(loc=0, scale=1, shape=(batch_size/2,64,1,1), ctx=ctx) 
    mu = nd.random.uniform(low= -1, high=1, shape=(batch_size/2,64,1,1),ctx=ctx)
    #mu =  nd.zeros((batch_size/2,64,1,1),ctx=ctx)
    sigma = nd.ones((64,1,1),ctx=ctx)
    mu.attach_grad()
    sigma.attach_grad()    
    stamp = datetime.now().strftime('%Y_%m_%d-%H_%M')
    logging.basicConfig(level=logging.DEBUG)
    for epoch in range(epochs):

        tic = time.time()
        btic = time.time()
        train_data.reset()
        iter = 0
        #print('learning rate : '+str(trainerD.learning_rate ))
        for batch in train_data:
            ############################
            # (1) Update D network: maximize log(D(x, y)) + log(1 - D(x, G(x, z)))
            ###########################
            real_in = batch.data[0].as_in_context(ctx)
            real_out = batch.data[1].as_in_context(ctx)
            fake_latent= netEn(real_in)
            #real_latent = nd.random_normal(loc=0, scale=1, shape=fake_latent.shape, ctx=ctx)
            real_latent = nd.multiply(nd.power(sigma,2),nd.random_normal(loc=0, scale=1, shape=fake_latent.shape, ctx=ctx))
	    #nd.random.uniform( low=-1, high=1, shape=fake_latent.shape, ctx=ctx)
	    fake_out = netDe(fake_latent)
            fake_concat =  nd.concat(real_in, fake_out, dim=1) if append else  fake_out
            with autograd.record():
                # Train with fake image
                # Use image pooling to utilize history imagesi
                output = netD(fake_concat)
                output2 = netD2(fake_latent)
                fake_label = nd.zeros(output.shape, ctx=ctx)
                fake_latent_label = nd.zeros(output2.shape, ctx=ctx)
		noiseshape = (fake_latent.shape[0]/2,fake_latent.shape[1],fake_latent.shape[2],fake_latent.shape[3])
                eps2 = nd.multiply(nd.power(sigma,2),nd.random_normal(loc=0, scale=1, shape=fake_latent.shape, ctx=ctx))
		#eps2 = nd.random_normal(loc=0, scale=sigma.asscalar(), shape=fake_latent.shape, ctx=ctx) #
		#eps = nd.random.uniform( low=-1, high=1, shape=noiseshape, ctx=ctx)
		rec_output = netD(netDe(eps2))
                errD_fake = GAN_loss(rec_output, fake_label)
                errD_fake2 = GAN_loss(output, fake_label)
                errD2_fake = GAN_loss(output2, fake_latent_label)
                metric.update([fake_label, ], [output, ])
                metric2.update([fake_latent_label, ], [output2, ])
                real_concat =  nd.concat(real_in, real_out, dim=1) if append else  real_out
                output = netD(real_concat)
                output2 = netD2(real_latent)
                real_label = nd.ones(output.shape, ctx=ctx)
                real_latent_label =  nd.ones(output2.shape, ctx=ctx)
                errD_real = GAN_loss(output, real_label)
                errD2_real =  GAN_loss(output2, real_latent_label)
                #errD = (errD_real + 0.5*(errD_fake+errD_fake2)) * 0.5
                errD = (errD_real + errD_fake) * 0.5
                errD2 = (errD2_real + errD2_fake) * 0.5
		totalerrD = errD+errD2
                totalerrD.backward()
                #errD2.backward()
                metric.update([real_label, ], [output, ])
            	metric2.update([real_latent_label, ], [output2, ])
            trainerD.step(batch.data[0].shape[0])
            trainerD2.step(batch.data[0].shape[0])
            ############################
            # (2) Update G network: maximize log(D(x, G(x, z))) - lambda1 * L1(y, G(x, z))
            ###########################
            with autograd.record():
		sh = fake_latent.shape
		eps2 = nd.multiply(nd.power(sigma,2),nd.random_normal(loc=0, scale=1, shape=fake_latent.shape, ctx=ctx))
                #eps2 = nd.random_normal(loc=0, scale=sigma.asscalar(), shape=fake_latent.shape, ctx=ctx) #
		#eps = nd.random.uniform( low=-1, high=1, shape=noiseshape, ctx=ctx)
		rec_output = netD(netDe(eps2))
                fake_latent= (netEn(real_in))
                output2 = netD2(fake_latent)
                fake_out = netDe(fake_latent)
                fake_concat =  nd.concat(real_in, fake_out, dim=1) if append else  fake_out
                output = netD(fake_concat)
                real_label = nd.ones(output.shape, ctx=ctx)
                real_latent_label = nd.ones(output2.shape, ctx=ctx)
                errG2 = GAN_loss(rec_output, real_label)
                errR = L1_loss(real_out, fake_out) * lambda1
		errG = 10.0*GAN_loss(output2, real_latent_label)+errG2+errR+nd.mean(nd.power(sigma,2))
		errG.backward()
	    if epoch>50:
	    	sigma -= lr / sigma.shape[0] * sigma.grad
	    	print(sigma)
            trainerDe.step(batch.data[0].shape[0])
            trainerEn.step(batch.data[0].shape[0])
            loss_rec_G2.append(nd.mean(errG2).asscalar())
            loss_rec_G.append(nd.mean(nd.mean(errG)).asscalar()-nd.mean(errG2).asscalar()-nd.mean(errR).asscalar())
            loss_rec_D.append(nd.mean(errD).asscalar())
            loss_rec_R.append(nd.mean(errR).asscalar())
            loss_rec_D2.append(nd.mean(errD2).asscalar())
            _, acc2 = metric2.get()
            name, acc = metric.get()
            acc_rec.append(acc)
            acc2_rec.append(acc2)

            # Print log infomation every ten batches
            if iter % 10 == 0:
                _, acc2 = metric2.get()
                name, acc = metric.get()
                logging.info('speed: {} samples/s'.format(batch_size / (time.time() - btic)))
                #print(errD)
		logging.info('discriminator loss = %f, D2 loss = %f, generator loss = %f, G2 loss = %f,  binary training acc = %f , D2 acc = %f, reconstruction error= %f  at iter %d epoch %d'
                    	% (nd.mean(errD).asscalar(),nd.mean(errD2).asscalar(),
                      	nd.mean(errG-errG2-errR).asscalar(),nd.mean(errG2).asscalar(), acc,acc2,nd.mean(errR).asscalar() ,iter, epoch))
                iter = iter + 1
        btic = time.time()
        name, acc = metric.get()
        _, acc2 = metric2.get()
        tp_file = open(expname + "_trainloss.txt", "a")
        tp_file.write(str(nd.mean(errG2).asscalar()) + " " + str(
            nd.mean(nd.mean(errG)).asscalar() - nd.mean(errG2).asscalar() - nd.mean(errR).asscalar()) + " " + str(
            nd.mean(errD).asscalar()) + " " + str(nd.mean(errD2).asscalar()) + " " + str(nd.mean(errR).asscalar()) +" "+str(acc) + " " + str(acc2)+"\n")
        tp_file.close()
        metric.reset()
        metric2.reset()
        train_data.reset()

        logging.info('\nbinary training acc at epoch %d: %s=%f' % (epoch, name, acc))
        logging.info('time: %f' % (time.time() - tic))
        if epoch%10 ==0:# and epoch>0:
            text_file = open(expname + "_validtest.txt", "a")
            filename = "checkpoints/"+expname+"_"+str(epoch)+"_D.params"
            netD.save_params(filename)
            filename = "checkpoints/"+expname+"_"+str(epoch)+"_D2.params"
            netD2.save_params(filename)
            filename = "checkpoints/"+expname+"_"+str(epoch)+"_En.params"
            netEn.save_params(filename)
            filename = "checkpoints/"+expname+"_"+str(epoch)+"_De.params"
            netDe.save_params(filename)
            fake_img1 = nd.concat(real_in[0],real_out[0], fake_out[0], dim=1)
            fake_img2 = nd.concat(real_in[1],real_out[1], fake_out[1], dim=1)
            fake_img3 = nd.concat(real_in[2],real_out[2], fake_out[2], dim=1)
            fake_img4 = nd.concat(real_in[3],real_out[3], fake_out[3], dim=1)
            val_data.reset()
            text_file = open(expname + "_validtest.txt", "a")
            for vbatch in val_data:
                
                real_in = vbatch.data[0].as_in_context(ctx)
                real_out = vbatch.data[1].as_in_context(ctx)
                fake_latent= netEn(real_in)
                y = netDe(fake_latent)
                fake_out = y
                metricMSE.update([fake_out, ], [real_out, ])
            _, acc2 = metricMSE.get()
            text_file.write("%s %s %s\n" % (str(epoch), nd.mean(errR).asscalar(), str(acc2)))
            metricMSE.reset()
	    images = netDe(eps2)
            fake_img1T = nd.concat(images[0],images[1], images[2], dim=1)
	    fake_img2T = nd.concat(images[3],images[4], images[5], dim=1)
            fake_img3T = nd.concat(images[6],images[7], images[8], dim=1)
            fake_img = nd.concat(fake_img1T,fake_img2T, fake_img3T,dim=2)
            visual.visualize(fake_img)
            plt.savefig('outputs/'+expname+'_fakes_'+str(epoch)+'.png')
            text_file.close()

	    # Do 10 iterations of sampler update
	    fake_img1T = nd.concat(real_in[0],real_out[0], fake_out[0], dim=1)
            fake_img2T = nd.concat(real_in[1],real_out[1], fake_out[1], dim=1)
            fake_img3T = nd.concat(real_in[2],real_out[2], fake_out[2], dim=1)
            #fake_img4T = nd.concat(real_in[3],real_out[3], fake_out[3], dim=1)
            fake_img = nd.concat(fake_img1,fake_img2, fake_img3,fake_img1T,fake_img2T, fake_img3T,dim=2)
            visual.visualize(fake_img)
            plt.savefig('outputs/'+expname+'_'+str(epoch)+'.png')
	    '''if epoch > 100:
	      for ep2 in range(10):
	    	with autograd.record():
                	#eps = nd.random_normal(loc=0, scale=1, shape=noiseshape, ctx=ctx) #
			eps = nd.random.uniform( low=-1, high=1, shape=noiseshape, ctx=ctx)
			eps2 = nd.random_normal(loc=0, scale=0.02, shape=noiseshape, ctx=ctx)
                	eps2 = nd.tanh(eps2*sigma+mu)
                	eps2 = nd.concat(eps,eps2,dim=0)
			rec_output = netD(netDe(eps2))
			fake_label = nd.zeros(rec_output.shape, ctx=ctx)
                	errGS = GAN_loss(rec_output, fake_label)
    			errGS.backward()
		mu -= lr / mu.shape[0] * mu.grad
		sigma -= lr / sigma.shape[0] * sigma.grad
	    	print('mu ' + str(mu[0,0,0,0].asnumpy())+ '  sigma '+ str(sigma[0,0,0,0].asnumpy()))
	    '''
	    images = netDe(eps2)
            fake_img1T = nd.concat(images[0],images[1], images[2], dim=1)
            fake_img2T = nd.concat(images[3],images[4], images[5], dim=1)
            fake_img3T = nd.concat(images[6],images[7], images[8], dim=1)
            fake_img = nd.concat(fake_img1T,fake_img2T, fake_img3T,dim=2)
            visual.visualize(fake_img)
            plt.savefig('outputs/'+expname+'_fakespost_'+str(epoch)+'.png')
    return([loss_rec_D,loss_rec_G, loss_rec_R, acc_rec, loss_rec_D2, loss_rec_G2, acc2_rec])
Esempio n. 30
0
import random

import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 120
import matplotlib.pyplot as plt

# regression with regularization

num_train = 20
num_test = 100
num_inputs = 200

true_w = nd.ones((num_inputs, 1)) * 0.01
true_b = 0.05

X = nd.random_normal(shape=(num_train + num_test, num_inputs))
y = nd.dot(X, true_w)
y += .01 * nd.random_normal(shape=y.shape)

batch_size = 1
# split into test and train sets
X_train, X_test = X[:num_train, :], X[:num_test, :]
y_train, y_test = y[:num_train], y[num_train:]

dataset_train = gluon.data.ArrayDataset(X_train, y_train)
data_iter_train = gluon.data.DataLoader(dataset_train,
                                        batch_size,
                                        shuffle=True)

square_loss = gluon.loss.L2Loss()
Esempio n. 31
0
def main(opt):
    ctx = mx.gpu() if opt.use_gpu else mx.cpu()
    testclasspaths = []
    testclasslabels = []
    print('loading test files')
    filename = '_testlist.txt'
    with open(opt.dataset + "_" + opt.expname + filename, 'r') as f:
        for line in f:
            testclasspaths.append(line.split(' ')[0])
            if int(line.split(' ')[1]) == -1:
                testclasslabels.append(0)
            else:
                testclasslabels.append(1)
    neworder = range(len(testclasslabels))
    neworder = shuffle(neworder)

    c = list(zip(testclasslabels, testclasspaths))
    print('shuffling')
    random.shuffle(c)

    #testclasslabels, testclasspaths = zip(*c)
    #testclasslabels = testclasslabels[1:5000]
    #testclasspaths = testclasspaths[1:5000]
    ltnt = 512
    print('loading pictures')
    test_data = load_image.load_test_images(testclasspaths, testclasslabels,
                                            opt.batch_size, opt.img_wd,
                                            opt.img_ht, ctx, opt.noisevar)
    print('picture loading done')
    netEn, netDe, netD, netD2, netDS = set_network(opt.depth, ctx, 0, 0,
                                                   opt.ndf, opt.ngf,
                                                   opt.append)
    netEn.load_params('checkpoints/' + opt.expname + '_' + str(opt.epochs) +
                      '_En.params',
                      ctx=ctx)
    netDe.load_params('checkpoints/' + opt.expname + '_' + str(opt.epochs) +
                      '_De.params',
                      ctx=ctx)
    netD.load_params('checkpoints/' + opt.expname + '_' + str(opt.epochs) +
                     '_D.params',
                     ctx=ctx)
    netD2.load_params('checkpoints/' + opt.expname + '_' + str(opt.epochs) +
                      '_D2.params',
                      ctx=ctx)
    netDS.load_params('checkpoints/' + opt.expname + '_' + str(opt.epochs) +
                      '_SD.params',
                      ctx=ctx)
    print('Model loading done')
    lbllist = []
    scorelist1 = []
    scorelist2 = []
    scorelist3 = []
    scorelist4 = []
    test_data.reset()
    count = 0
    for batch in (test_data):
        count += 1
        print(str(count))  #, end="\r")
        real_in = batch.data[0].as_in_context(ctx)
        real_out = batch.data[1].as_in_context(ctx)
        lbls = batch.label[0].as_in_context(ctx)
        code = netEn((real_out))
        code = code + nd.random.normal(
            loc=0, scale=0.002, shape=code.shape, ctx=ctx)
        outnn = (netDe(code))
        out_concat = nd.concat(real_out, outnn, dim=1) if opt.append else outnn
        output4 = nd.mean((netD(out_concat)), (1, 3, 2)).asnumpy()
        code = netEn(real_in)
        #code=codet+nd.random.normal(loc=0, scale=0.0000001, shape=code.shape,ctx=ctx)
        #code2=codet+nd.random.normal(loc=0, scale=0.000001, shape=code.shape,ctx=ctx)
        #eq_code = heq(code.asnumpy(),2)
        #code = nd.array(eq_code, ctx=ctx)
        out = netDe(code)
        #out2 = netDe(code2)
        out_concat = nd.concat(real_in, out, dim=1) if opt.append else out
        output = netD(out_concat)  #Denoised image
        output3 = nd.mean((out - real_out)**2,
                          (1, 3, 2)).asnumpy()  #denoised-real
        output = nd.mean(output, (1, 3, 2)).asnumpy()
        out_concat = nd.concat(real_out, real_out,
                               dim=1) if opt.append else real_out

        output2 = netDS(netDe(code))  #Image with no noise
        output2 = nd.mean(output2, (1, 3, 2)).asnumpy()
        lbllist = lbllist + list(lbls.asnumpy())
        scorelist1 = scorelist1 + list(output)
        scorelist2 = scorelist2 + list(output2)
        scorelist3 = scorelist3 + list(output3)
        scorelist4 = scorelist4 + list(output4)

        fake_img1 = nd.concat(real_in[0], real_out[0], out[0], outnn[0], dim=1)
        fake_img2 = nd.concat(real_in[1], real_out[1], out[1], outnn[1], dim=1)
        fake_img3 = nd.concat(real_in[2], real_out[2], out[2], outnn[2], dim=1)
        fake_img4 = nd.concat(real_in[3], real_out[3], out[3], outnn[3], dim=1)
        fake_img = nd.concat(fake_img1, fake_img2, fake_img3, fake_img4, dim=2)
        #print(np.shape(fake_img))
        visual.visualize(fake_img)
        plt.savefig('outputs/T_' + opt.expname + '_' + str(count) + '.png')
    if not opt.isvalidation:

        fpr, tpr, _ = roc_curve(lbllist, scorelist1, 1)
        roc_auc1 = auc(fpr, tpr)
        fpr, tpr, _ = roc_curve(lbllist, scorelist2, 1)
        roc_auc2 = auc(fpr, tpr)
        fpr, tpr, _ = roc_curve(lbllist, scorelist3, 1)
        roc_auc3 = auc(fpr, tpr)
        fpr, tpr, _ = roc_curve(lbllist, scorelist4, 1)
        roc_auc4 = auc(fpr, tpr)
        plt.gcf().clear()
        plt.clf()
        sns.set(color_codes=True)
        posscores = [
            scorelist3[i] for i, v in enumerate(lbllist) if int(v) == 1
        ]
        negscores = [
            scorelist3[i] for i, v in enumerate(lbllist) if int(v) == 0
        ]
        #sns.distplot(posscores, hist=False, label="Known Classes" ,rug=True)
        sns.kdeplot(posscores, label="Known Classes")
        sns.kdeplot(negscores, label="Unnown Classes")
        #plt.hold()
        #sns.distplot(negscores, hist=False, label = "Unknown Classes", rug=True);
        plt.legend()
        plt.savefig('outputs/matdist_' + opt.expname + '_.png')

        plt.gcf().clear()
        inputT = nd.zeros((ltnt, ltnt, 1, 1), ctx=ctx)
        for i in range(0, ltnt):
            inputT[i, i, :, :] = -1
        out = netDe(inputT)
        count = 0
        for i in range(int(math.ceil(math.sqrt(ltnt)))):
            for j in range(int(math.ceil(math.sqrt(ltnt)))):
                if count < ltnt:
                    plt.subplot(math.ceil(math.sqrt(ltnt)),
                                math.ceil(math.sqrt(ltnt)), count + 1)
                    plt.imshow(
                        ((out[count].asnumpy().transpose(1, 2, 0) + 1.0) *
                         127.5).astype(np.uint8))
                    plt.axis('off')
                count += 1
        plt.savefig('outputs/atoms_' + opt.expname + '_.png')
        plt.gcf().clear()
        plt.clf()
        return ([roc_auc1, roc_auc2, roc_auc3, roc_auc4])
    else:
        return ([0, 0, 0, 0])
    fakecode = nd.random_normal(loc=0,
                                scale=1,
                                shape=(16, 4096, 1, 1),
                                ctx=ctx)
    out = netDe(fakecode)
    fake_img1 = nd.concat(out[0], out[1], out[2], out[3], dim=1)
    fake_img2 = nd.concat(out[7], out[6], out[5], out[4], dim=1)
    fake_img3 = nd.concat(out[8], out[9], out[10], out[11], dim=1)
    fake_img4 = nd.concat(out[15], out[14], out[13], out[12], dim=1)
    fake_img = nd.concat(fake_img1, fake_img2, fake_img3, fake_img4, dim=2)
    #print(np.shape(fake_img))
    visual.visualize(fake_img)
    plt.savefig('outputs/fakes_' + opt.expname + '_.png')
Esempio n. 32
0
    ]
    return [text_labels[int(i)] for i in label]


data, label = mnist_train[0:9]
show_images(data)
print(get_text_labels(label))

batch_size = 256
train_data = gluon.data.DataLoader(mnist_train, batch_size, shuffle=True)
test_data = gluon.data.DataLoader(mnist_test, batch_size, shuffle=False)

num_inputs = 784
num_outputs = 10

W = nd.random_normal(shape=(num_inputs, num_outputs))
b = nd.random_normal(shape=num_outputs)

params = [W, b]

for param in params:
    param.attach_grad()


def softmax(X):
    exp = nd.exp(X)
    partition = exp.sum(axis=1, keepdims=True)
    return exp / partition


X = nd.random_normal(shape=(2, 5))
Esempio n. 33
0
# 使用 record() 要求 MXNet 记录需要求导的程序
with ag.record():
    y = x * 2
    z = y * x

# 求导
z.backward()

print('x.grad:', x.grad)


# ############### 对控制流求导 ###############
def f(a):
    b = a * 2
    while nd.norm(b).asscalar() < 1000:
        b = b * 2
    if nd.sum(b).asscalar() > 0:
        c = b
    else:
        c = 100 * b
    return c


a = nd.random_normal(shape=(3, 4))
a.attach_grad()
with ag.record():
    c = f(a)
c.backward()
print(a.grad == c / a)
Esempio n. 34
0
        return nd.stack(dx1, dx2, axis=1)


if __name__ == "__main__":
    import seaborn as sns
    import matplotlib.pyplot as plt

    np.random.seed(123)
    mx.random.seed(123)
    ctx = mx.cpu()

    model = Model1(0.3, 0.3, ctx=ctx)
    start = nd.array([0.0, 0.0], ctx=ctx)
    sampler = Langevin(start.shape, ctx=ctx)
    res = sampler.sample(model, start, step_size=0.1, num=1000, burnin=200)

    plt.scatter(res[:, 0].asnumpy(), res[:, 1].asnumpy())
    sns.jointplot(res[:, 0].asnumpy(), res[:, 1].asnumpy(), stat_func=None)

    np.random.seed(123)
    mx.random.seed(123)

    model = Model2(0.3, 0.3, ctx=ctx)
    nchain = 1000
    start = nd.random_normal(shape=(nchain, 2), ctx=ctx)
    sampler = LangevinMultiChain(start.shape[1:], nchain, ctx=ctx)
    res = sampler.sample(model, start, step_size=0.1, burnin=200)

    plt.scatter(res[:, 0].asnumpy(), res[:, 1].asnumpy())
    sns.jointplot(res[:, 0].asnumpy(), res[:, 1].asnumpy(), stat_func=None)
Esempio n. 35
0

import mxnet as mx

#GPU
import sys
sys.path.append('..')
import utils
ctx = utils.try_gpu()
print('Will use ', ctx)

num_hidden = 256
weight_scale = .01

# ???
Wxh = nd.random_normal(shape=(vocab_size, num_hidden), ctx=ctx) * weight_scale
Whh = nd.random_normal(shape=(num_hidden, num_hidden), ctx=ctx) * weight_scale
bh = nd.zeros(num_hidden, ctx=ctx)
# ???
Why = nd.random_normal(shape=(num_hidden, vocab_size), ctx=ctx) * weight_scale
by = nd.zeros(vocab_size, ctx=ctx)

params = [Wxh, Whh, bh, Why, by]
for param in params:
    param.attach_grad()


def get_inputs(data):
    return [nd.one_hot(X, vocab_size) for X in data.T]

# 定义模型
import mxnet as mx

try:
    ctx = mx.gpu()
    nd.zeros((1,), ctx=ctx)
except:
    ctx = mx.cpu()
ctx

weight_scale = 0.01
num_output = 10

# output channels = 20,kernel = (5,5)
W1 = nd.random_normal(shape=(20, 1, 5, 5), scale=weight_scale, ctx=ctx)
b1 = nd.zeros(W1.shape[0], ctx=ctx)

# output channels = 50 ,kernel = (3,3)
W2 = nd.random_normal(shape=(50, 20, 3, 3), scale=weight_scale, ctx=ctx)
b2 = nd.zeros(W2.shape[0], ctx=ctx)

# output dim = 128
W3 = nd.random_normal(shape=(1250, 128), scale=weight_scale, ctx=ctx)
b3 = nd.zeros(W3.shape[1], ctx=ctx)

# output dim = 10
W4 = nd.random_normal(shape=(W3.shape[1], 10), scale=weight_scale, ctx=ctx)
b4 = nd.zeros(W4.shape[1], ctx=ctx)

params = [W1, b1, W2, b2, W3, b3, W4, b4]
Esempio n. 37
0
def make_noise(bs, nz, ctx):
    return random_normal(0, 1, shape=(bs, nz, 1, 1), ctx=ctx, dtype='float32')
Esempio n. 38
0
# 优化求解(梯度下降)
def SGD(params_p, lr_p):
    for param_p in params_p:
        # 必须是param_p[:] = ...  param = ...会重新创建新param,这个是没有attach_grad的
        param_p[:] = param_p - lr_p * param_p.grad


if __name__ == '__main__':
    # genData()
    num_inputs = 2
    num_examples = 1000
    X, y = common.genData(num_inputs, num_examples)  # 1.数据

    # 2.模型 (初始化)
    W = nd.random_normal(shape=(num_inputs, 1))
    b = nd.zeros((1, ))
    params = [W, b]
    for param in params:
        param.attach_grad()

    # 3.参数 + 训练
    batch_size = 10
    learning_rate = 0.001
    epoch = 10

    niter = 0
    smoothing_constant = 0.01
    losses = []
    moving_loss = 0
Esempio n. 39
0
            yield nd.take(test_img_nd,
                          j).as_in_context(ctx), nd.take(test_lab_nd,
                                                         j).as_in_context(ctx)
    else:
        idx = list(range(len(train_labels)))
        for i in range(0, len(train_labels), batch_size):
            j = nd.array(idx[i:min(i + batch_size, len(train_labels))])
            yield nd.take(train_img_nd,
                          j).as_in_context(ctx), nd.take(train_lab_nd,
                                                         j).as_in_context(ctx)


num_input = 28 * 28
num_output = 10

W = nd.random_normal(shape=(num_input, num_output), ctx=ctx)
b = nd.random_normal(shape=(num_output), ctx=ctx)

wb_params = [W, b]
for p in wb_params:
    p.attach_grad()


def net_wb(X):
    return nd.dot(X, W) + b


def relu(X):
    return nd.maximum(X, 0)

from mxnet import ndarray as nd
from mxnet import autograd
import matplotlib.pyplot as plt
import random

# data set
num_inputs = 2
num_examples = 1000

true_w = [2, -3.4]
true_b = 4.2

X = nd.random_normal(shape=(num_examples, num_inputs))
y = true_w[0] * X[:, 0] + true_w[1] * X[:, 1] + true_b
y += .01 * nd.random_normal(shape=y.shape)

plt.scatter(X[:, 0].asnumpy(), y.asnumpy())
plt.show()

# generate random training data set
batch_size = 10
def data_iter():
    idx = list(range(num_examples))
    random.shuffle(idx)
    for i in range(0, num_examples, batch_size):
        j = nd.array(idx[i: min(i+batch_size, num_examples)])
        yield nd.take(X, j), nd.take(y, j)

for data, label in data_iter():
    print(data, label)
data, label = mnist_train[0:10]
show_images(data)#图像
print(get_text_labels(label))##标号
## 准备 训练和测试数据集
# 这个DataLoader是一个iterator对象类(每次只载入一个banch的数据进入内存),非常适合处理规模较大的数据集
batch_size = 256#每次训练 输入的图片数量
train_data = gluon.data.DataLoader(mnist_train, batch_size, shuffle=True)#按照 batch_size 分割成 每次训练时的数据
test_data = gluon.data.DataLoader(mnist_test, batch_size, shuffle=False) #测试数据


###########################################################
### 定义模型 ##################
#@@@@初始化模型参数 权重和偏置@@@@
num_inputs = 784#输入数据维度  1*784
num_outputs = 10#输出标签维度 1*10
W = nd.random_normal(shape=(num_inputs, num_outputs))
 # 256*784  ×  784*10 ——————> 256*10 每张图像预测10个类别的概率 每次共256张图片
b = nd.random_normal(shape=num_outputs)#偏置
params = [W, b]#权重和偏置 参数
 # 模型参数附上梯度 自动微分时 会计算梯度信息
for param in params:
    param.attach_grad()

#@@@@@定义模型@@@@@@
## softmax 回归实现  exp(Xi)/(sum(exp(Xi))) 归一化概率 使得 10类概率之和为1
def softmax(X):
    exp = nd.exp(X)
    # 假设exp是矩阵,这里对行进行求和,并要求保留axis 1,
    # 就是返回 (nrows, 1) 形状的矩阵
    partition = exp.sum(axis=1, keepdims=True)
    return exp / partition
Esempio n. 42
0
                                            transform=transform)

batch_size = 256
train_data = gluon.data.DataLoader(mnist_train, batch_size, shuffle=True)
test_data = gluon.data.DataLoader(mnist_test, batch_size, shuffle=False)

num_inputs = 28 * 28
num_outputs = 10

num_hidden = 256
num_hidden10 = 512
weight_scale1 = 0.2
weight_scale10 = 0.4
weight_scale2 = 0.6

w1 = nd.random_normal(shape=(num_inputs, num_hidden), scale=weight_scale1)
b1 = nd.zeros(num_hidden)

w10 = nd.random_normal(shape=(num_hidden, num_hidden10), scale=weight_scale10)
b10 = nd.zeros(num_hidden10)

w2 = nd.random_normal(shape=(num_hidden10, num_outputs), scale=weight_scale2)
b2 = nd.zeros(num_outputs)

params = [w1, b1, w10, b10, w2, b2]

for p in params:
    p.attach_grad()


def relu(x):
Esempio n. 43
0
def gaussian(shape):
    return nd.random_normal(shape=shape).as_in_context(context)