Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Abuggoproject/main.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
82 lines (72 sloc)
2.98 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a sample Python script. | |
from mxnet.gluon import nn,rnn | |
from mxnet import nd | |
from Sequential import Sequential | |
import mxnet as mx | |
import random | |
import copy | |
from RnnCode import try_gpu | |
from mxnet import autograd, gluon, init, nd | |
from mxnet.gluon import loss as gloss, nn, rnn | |
# Press Shift+F10 to execute it or replace it with your code. | |
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. | |
def grad_clipping(params, theta, ctx): | |
norm = nd.array([0], ctx) | |
for param in params: | |
norm += (param.grad ** 2).sum() | |
norm = norm.sqrt().asscalar() | |
if norm > theta: | |
for param in params: | |
param.grad[:] *= theta / norm | |
def TraingingModel(chess_board, weights, possibilities): | |
num_epochs=8 | |
ctx=try_gpu() | |
model=Sequential(EMBEDDING_DIM=19,INPUT_DIM=19*19,LATENT_DIM=19) | |
decoder_inputs = nd.array([[int(i) for i in range(j * 19, j * 19 + 19)] for j in range(19 * 19)], dtype="float32") | |
model.initialize(force_reinit=True, ctx=ctx, init=init.Xavier()) | |
trainer = gluon.Trainer(model.collect_params(), 'sgd', | |
{'learning_rate': 1e2, 'momentum': 0, 'wd': 0}) | |
# print(model.collect_params()) | |
clipping_theta=1e-2 | |
loss = gloss.L2Loss() | |
for epoch in range(num_epochs): | |
for X,y,weight in zip(chess_board,possibilities,weights): | |
s, c = model.begin_state(batch_size=19, ctx=ctx) | |
s.detach() | |
c.detach() | |
with autograd.record(): | |
# for layer in model: | |
# X = layer(X,19*19,200,decoder_inputs,weight,s,c) | |
# | |
# print(layer.name, 'output shape:\t', X.shape) | |
output = model(X,19*19,200,decoder_inputs,weight,s,c) | |
l=loss(output,y).sum() | |
l.backward() | |
params = [p.data() for p in model.collect_params().values()] | |
grad_clipping(params, clipping_theta, ctx) | |
trainer.step(1) | |
print("{} epoch".format(epoch)+str(l)) | |
# Press the green button in the gutter to run the script. | |
if __name__ == '__main__': | |
weight = nd.array([[0, 3, 4, 3, 3, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13], | |
[1, 3, 5, 6, 8, 9, 10, 11, 12, 13, 14, 55, 66, 47, 28, 120, 121, 112, 213]]) | |
weights=[weight] | |
initlist = [0, 0, 0, 0, 135, 255, 0, 0, 0, 135, 0, 255, 135, 0, 0, 0, 0, 0, 0] | |
def shuf(seq): | |
random.seed(10) | |
s=copy.deepcopy(seq) | |
random.shuffle(s) | |
return s | |
chess_board=nd.array(list(map(lambda index:shuf(initlist),range(19)))) | |
chess_board=[chess_board] | |
poss=nd.zeros(shape=(361,),dtype="float32") | |
poss[0]=0.0065 | |
poss[1]=0.0025 | |
poss[25]=0.00013 | |
poss[34]=0.0012 | |
poss[44]=0.0011 | |
poss[90]=0.001023 | |
poss[100]=0.00023 | |
possibilities=[poss] | |
TraingingModel(chess_board, weights, possibilities) | |
# See PyCharm help at https://www.jetbrains.com/help/pycharm/ |