class VisdomPlotter(object): """Plots to Visdom Server""" def __init__(self, env_name='gan'): """Initilized visdom, environment and plots dictionary""" self.viz = Visdom() self.env = env_name self.plots = {} def plot(self, var_name, split_name, x, y, xlabel='epoch'): """ Plots graphs in visdom server. Usually generator/discrimantor loss values """ if var_name not in self.plots: self.plots[var_name] = self.viz.line(X=np.array([x, x]), Y=np.array([y, y]), env=self.env, opts=dict(legend=[split_name], title=var_name, xlabel=xlabel, ylabel=var_name)) else: self.viz.updateTrace(X=np.array([x]), Y=np.array([y]), env=self.env, win=self.plots[var_name], name=split_name) def draw(self, var_name, images): """ Draws images in visdom server """ if var_name not in self.plots: self.plots[var_name] = self.viz.images(images, env=self.env) else: self.viz.images(images, env=self.env, win=self.plots[var_name])
class VisdomLinePlotter(object): """Plots to Visdom""" def __init__(self, env_name='main'): self.viz = Visdom() self.env = env_name self.plots = {} def plot(self, var_name, split_name, x, y, env=None): if env is not None: print_env = env else: print_env = self.env if var_name not in self.plots: self.plots[var_name] = self.viz.line(X=np.array([x, x]), Y=np.array([y, y]), env=print_env, opts=dict(legend=[split_name], title=var_name, xlabel='Epochs', ylabel=var_name)) else: self.viz.updateTrace(X=np.array([x]), Y=np.array([y]), env=print_env, win=self.plots[var_name], name=split_name) def plot_mask(self, masks, epoch): self.viz.bar(X=masks, env=self.env, opts=dict( stacked=True, title=epoch, ))
class VisdomPlotter(object): """Plots to Visdom""" def __init__(self, env_name='relational_net'): self.viz = Visdom() self.env = env_name self.plots = {} def plot(self, var_name, split_name, x, y, xlabel='iteration'): if var_name not in self.plots: self.plots[var_name] = self.viz.line(X=np.array([x, x]), Y=np.array([y, y]), env=self.env, opts=dict(legend=[split_name], title=var_name, xlabel=xlabel, ylabel=var_name)) else: self.viz.updateTrace(X=np.array([x]), Y=np.array([y]), env=self.env, win=self.plots[var_name], name=split_name) def draw(self, var_name, images): images = (images + 0.5) * 255 if var_name not in self.plots: self.plots[var_name] = self.viz.images(images, env=self.env) else: self.viz.images(images, env=self.env, win=self.plots[var_name]) def print(self, var_name, text): if var_name not in self.plots: self.plots[var_name] = self.viz.text(text, env=self.env) else: self.viz.text(text, env=self.env, win=self.plots[var_name])
class VisdomLinePlotter(object): """Plots to Visdom""" def __init__(self, env_name='main'): self.viz = Visdom(server='172.220.4.32', port='6006') self.env = env_name self.plots = {} def plot(self, var_name, split_name, x, y, env=None): if env is not None: print_env = env else: print_env = self.env if var_name not in self.plots: self.plots[var_name] = self.viz.line(X=np.array([x, x]), Y=np.array([y, y]), env=print_env, opts=dict(legend=[split_name], title=var_name, xlabel='Epochs', ylabel=var_name)) else: self.viz.updateTrace(X=np.array([x]), Y=np.array([y]), env=print_env, win=self.plots[var_name], name=split_name) def plot_heatmap(self, map, epoch): self.viz.heatmap(X=map, env=self.env, opts=dict(title='activations {}'.format(epoch), xlabel='modules', ylabel='classes'))
class VisdomWriter(object): def __init__(self, env=None): """Extended Visdom Writer""" self.vis = Visdom(env=env) def update_text(self, text): """Text Memo (usually used to note hyperparameter-configurations)""" self.vis.text(text) def update_loss(self, step_i, loss, title='Learning Curve', xlabel='Epoch', ylabel='Loss'): """Update loss (X: Step (Epoch) / Y: loss)""" if step_i == 1: # line plot self.win = self.vis.line(X=np.array([step_i]), Y=np.array([loss]), opts=dict( title=title, xlabel=xlabel, ylabel=ylabel, )) else: self.vis.updateTrace(X=np.array([step_i]), Y=np.array([loss]), win=self.win)
class Dashboard: def __init__(self, port, env_name="GrayChannel"): self.vis = Visdom(port=port) self.env = env_name self.plots = {} def plot(self, var_name, split_name, x, y): if var_name not in self.plots: self.plots[var_name] = self.vis.line(X=np.array([x, x]), Y=np.array([y, y]), env=self.env, opts=dict(legend=[split_name], title=var_name, xlabel="Iters", ylabel=var_name)) else: self.vis.updateTrace(X=np.array([x, x]), Y=np.array([y, y]), env=self.env, win=self.plots[var_name], name=split_name) def image(self, image, title): if image.is_cuda: image = image.cpu() if isinstance(image, Variable): imgae = image.data image = image.numpy() img_env = self.env + '_images' self.vis.image(image, env=img_env, opts=dict(title=title))
class VisdomWriter(object): def __init__(self, title, xlabel='Epoch', ylabel='Loss'): """Extended Visdom Writer""" self.vis = Visdom() assert self.vis.check_connection() self.title = title self.xlabel = xlabel self.ylabel = ylabel self.x = 0 self.win = None def update_text(self, text): """Text Memo (usually used to note hyperparameter-configurations)""" self.vis.text(text) def update(self, y): """Update loss (X: Step (Epoch) / Y: loss)""" self.x += 1 if self.win is None: self.win = self.vis.line(X=np.array([self.x]), Y=np.array([y]), opts=dict( title=self.title, xlabel=self.xlabel, ylabel=self.ylabel, )) else: self.vis.updateTrace(X=np.array([self.x]), Y=np.array([y]), win=self.win)
class VisdomPlotter(object): def __init__(self, env_name='gan'): self.viz = Visdom() self.env = env_name self.plots = {} def plot(self, var_name, split_name, x, y, xlabel='epoch'): if var_name not in self.plots: self.plots[var_name] = self.viz.line(X=np.array([x, x]), Y=np.array([y, y]), env=self.env, opts=dict(legend=[split_name], title=var_name, xlabel=xlabel, ylabel=var_name)) else: self.viz.updateTrace(X=np.array([x]), Y=np.array([y]), env=self.env, win=self.plots[var_name], name=split_name) def draw(self, var_name, images): if var_name not in self.plots: self.plots[var_name] = self.viz.images(images, env=self.env) else: self.viz.images(images, env=self.env, win=self.plots[var_name])
class Trainer: def __init__(self, args, model): self.name = args.name self.model = model self.l1win = None self.l2win = None self.l1meter = AverageValueMeter() self.l2meter = AverageValueMeter() self.visdom = Visdom( port=args.vis_port) if args.vis_steps > 0 else None @property def mode(self): return 'training' if self.model.training else 'testing' @property def losses(self): return self.l1meter.value()[0], self.l2meter.value()[1] def reset(self): self.l1meter.reset() self.l2meter.reset() def log_losses(self, epoch, step): l1, l2 = self.losses message = f'{self.name} is {self.mode} (epoch: {epoch}, step: {step}) ' message += f'l1 average: {l1}, l2 average: {l2}' print(message) def vis_losses(self, epoch): l1, l2 = self.losses x, y1, y2 = np.array([epoch]), np.array([l1]), np.array([l2]) if self.l1win is None or self.l2win is None: opt = dict(xlabel='epochs', xtickstep=1, ylabel='mean loss', width=900) self.l1win = self.visdom.line(X=x, Y=y1, opts=dict( title=f'l1 loss ({self.name})', **opt)) self.l2win = self.visdom.line(X=x, Y=y2, opts=dict( title=f'l2 loss ({self.name})', **opt)) else: n = '1' if self.model.training else '2' self.visdom.updateTrace(X=x, Y=y1, win=self.l1win, name=n) self.visdom.updateTrace(X=x, Y=y2, win=self.l2win, name=n) def vis_images(self, epoch, step, images): title = f'({self.name}, epoch: {epoch}, step: {step})' for key, image in images.items(): self.visdom.image(image.cpu().data, env=self.mode, opts=dict(title=f'{key} {title}'))
class Ploter(object): def __init__(self, env_name): self.viz = Visdom(env=env_name) self.win = None def append(self, x, y, name): if self.win is not None: self.viz.updateTrace(X=np.array([x]), Y=np.array([y]), win=self.win, name=name) else: self.win = self.viz.line(X=np.array([x]), Y=np.array([y]), opts={'legend': [name]})
class VisdomLinePlotter(object): """Plots to Visdom""" def __init__(self, env_name='main'): self.viz = Visdom() self.env = env_name self.plots = {} def plot(self, var_name, split_name, x, y, exp_name='test', env=None): if env is not None: print_env = env else: print_env = self.env if var_name not in self.plots: self.plots[var_name] = self.viz.line(X=np.array([x,x]), Y=np.array([y,y]), env=print_env, opts=dict( legend=[split_name], title=var_name, xlabel='Epochs', ylabel=var_name )) else: self.viz.updateTrace(X=np.array([x]), Y=np.array([y]), env=print_env, win=self.plots[var_name], name=split_name) if not os.path.exists('runs/%s/data/'%(exp_name)): os.makedirs('runs/%s/data/'%(exp_name)) file = open('runs/%s/data/%s_%s_data.csv'%(exp_name, split_name, var_name), 'a') file.write('%d, %f\n'%(x, y)) file.close() def plot_mask(self, masks, epoch): self.viz.bar( X=masks, env=self.env, opts=dict( stacked=True, title=epoch, ) ) def plot_image(self, image, epoch, exp_name='test'): self.viz.image(image, env=exp_name+'_img', opts=dict( caption=epoch, )) def plot_images(self, images, run_split, epoch, nrow, padding=2, exp_name='test'): self.viz.images(images, env=exp_name+'_img', nrow=nrow, padding=padding, opts=dict( caption='%s_%d'%(run_split, epoch), # title='Random images', jpgquality=100, ))
class VisdomLinePlotter(object): """Plots to Visdom""" def __init__(self, env_name='challenge'): self.viz = Visdom() self.env = env_name self.plots = {} def plot(self, var_name, split_name, x, y): if var_name not in self.plots: self.plots[var_name] = self.viz.line(X=np.array([x,x]), Y=np.array([y,y]), env=self.env, opts=dict( legend=[split_name], title=var_name, xlabel='Epochs', ylabel=var_name )) else: self.viz.updateTrace(X=np.array([x]), Y=np.array([y]), env=self.env, win=self.plots[var_name], name=split_name)
class VisdomExt(): r""" Extension doing visdom plotting. """ def __init__(self, channels, plot_options=[], visdom_kwargs={}, **kwargs): self.viz = Visdom(**visdom_kwargs) self.list_names = channels self.env = visdom_kwargs.get('env', 'main') # `line` method squeezes the input, in order to maintain the shape # we have to repeat it twice making its shape (2, M), where M is # the number of lines self.windows = [] self.p = {} for i, channel_set in enumerate(channels): try: channel_set_opts = plot_options[i] except IndexError: channel_set_opts = {} # we have to initialize the plot with some data, but NaNs are ignored dummy_data = [np.nan] * len(channel_set) dummy_ind = [0.] * len(channel_set) channel_set_opts.update(dict(legend=channel_set)) for channel in channel_set: self.p[channel] = i self.windows.append(self.viz.line(np.vstack([dummy_data, dummy_data]), np.vstack([dummy_ind, dummy_ind]), opts=channel_set_opts, env=self.env )) def update(self,iteration, value, channel): val = np.array([value], dtype='float32') iter = np.array([iteration], dtype='float32') self.viz.updateTrace(iter, val, append=True, name=channel, win=self.windows[self.p[channel]], env=self.env) @classmethod def load(cls, windows, p, visdom_kwargs={}): vis = cls([]) vis.windows = windows vis.p = p vis.env = visdom_kwargs.get('env', 'main') vis.viz = Visdom(**visdom_kwargs) return vis
class Plot(object): def __init__(self, title, port=8080): self.viz = Visdom(port=port) self.windows = {} self.title = title def register_scatterplot(self, name, xlabel, ylabel): win = self.viz.scatter(X=numpy.zeros((1, 2)), opts=dict(title=self.title, markersize=5, xlabel=xlabel, ylabel=ylabel)) self.windows[name] = win def update_scatterplot(self, name, x, y): self.viz.updateTrace(X=numpy.array([x]), Y=numpy.array([y]), win=self.windows[name])
class Dashboard: def __init__(self, port, envname): self.vis = Visdom(port=port) self.logPlot = None self.dataCount = 0 self.envname = envname def appendlog(self, value, logname, addcount=True): if addcount: self.dataCount += 1 if self.logPlot: self.vis.updateTrace(X=np.array([self.dataCount]), Y=np.array([value]), win=self.logPlot, name=logname, env=self.envname) else: self.logPlot = self.vis.line(np.array([value]), np.array([self.dataCount]), env=self.envname, opts=dict(title=self.envname, legend=[logname])) def image(self, image, title, mode='img', denorm=True, caption=''): # denorm: de-normalization if image.is_cuda: image = image.cpu() if isinstance(image, Variable): image = image.data if denorm: image[0] = image[0] * .2741 + .4710 image[1] = image[1] * .2661 + .4498 image[2] = image[2] * .2809 + .4034 image = image.sub_(image.min()) image = image.div_(image.max()) image = image.numpy() self.vis.image(image, env=self.envname + '-' + mode, opts=dict(title=title, caption=caption))
class Graph: def __init__(self, env): self.last1 = 0. self.last2 = 0. self.last3 = 0. self.last4 = 0. self.last5 = 0. self.last6 = 0. self.x = 0. self.legend = ['source_generator', 'target_generator', 'source_disc', 'target_disc', 'disc source', 'disc target'] self.viz = Visdom() self.env = env self.plots = {} def add_point(self, x, var_name='all'): if var_name not in self.plots: self.plots[var_name] = self.viz.line(X=np.column_stack((x, x, x, x, x, x)), Y=np.column_stack((self.last1, self.last2, self.last3, self.last4, self.last5, self.last6)), env=self.env, opts=dict(legend=self.legend)) else: self.viz.updateTrace(X=np.column_stack((x, x, x, x, x, x)), Y=np.column_stack((self.last1, self.last2, self.last3, self.last4, self.last5, self.last6)), env=self.env, win=self.plots[var_name]) def draw(self, var_name, images): if var_name not in self.plots: self.plots[var_name] = self.viz.images(images, env=self.env, opts=dict(caption=var_name)) else: self.viz.images(images, env=self.env, win=self.plots[var_name], opts=dict(caption=var_name)) def draw_figure(self, var_name, fig): fig.canvas.draw() data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='') data = data.reshape(fig.canvas.get_width_height()[::-1] + (3, 1)) data = np.moveaxis(data, -2, 0) data = np.moveaxis(data, -1, 0) self.draw(var_name, data)
class Arby: def __init__(self, polo_bot, krx_bot, mode='realtime'): r = 10 # 0.3~4 btc self.polo_bot = polo_bot self.krx_bot = krx_bot self.alt_name = krx_bot.alt_name self.mode = mode if self.alt_name == 'XRP': self.polo_bot.btc_deposit(0.1 * r) self.krx_bot.btc_deposit(0.1 * r) self.polo_bot.alt_deposit(300 * r) self.krx_bot.alt_deposit(300 * r) self.krx_bot.krw_deposit(200000 * r) # self.btc_polo = 0.01 *r # 3,360,000 # self.btc_krx = 0.19 *r# 3,360,000 # self.btc_depo_delayed = 0 # self.krw_krx = 200000*r # self.alt_polo = 100*r #360 # self.alt_krx = 100*r elif self.alt_name == 'ETH': self.polo_bot.btc_deposit(0.1 * r) self.krx_bot.btc_deposit(0.1 * r) self.polo_bot.alt_deposit(1 * r) self.krx_bot.alt_deposit(1 * r) self.krx_bot.krw_deposit(200000 * r) # self.btc_polo = 0.01 *r # 3,360,000 # self.btc_krx = 0.19 *r# 3,360,000 # self.btc_depo_delayed = 0 # self.krw_krx = 200000*r # self.alt_polo = 1*r # self.alt_krx = 1*r # 400,000 elif self.alt_name == 'ETC': self.polo_bot.btc_deposit(0.1 * r) self.krx_bot.btc_deposit(0.1 * r) self.polo_bot.alt_deposit(15 * r) self.krx_bot.alt_deposit(15 * r) self.krx_bot.krw_deposit(200000 * r) # self.btc_polo = 0.01 *r # 3,360,000 # self.btc_krx = 0.19 *r# 3,360,000 # self.btc_depo_delayed = 0 # self.krw_krx = 200000*r # self.alt_polo = 15*r # self.alt_krx = 15*r#26,000 elif self.alt_name == 'LTC': self.polo_bot.btc_deposit(0.1 * r) self.krx_bot.btc_deposit(0.1 * r) self.polo_bot.alt_deposit(5 * r) self.krx_bot.alt_deposit(5 * r) self.krx_bot.krw_deposit(200000 * r) # self.btc_polo = 0.1 # 3,360,000 #30 # self.btc_krx = 0.1 # 3,360,000 #30 # self.btc_depo_delayed = 0 # self.krw_krx = 200000*r # self.alt_polo = 5 # 30 # self.alt_krx = 5 # 55,800 # 30 elif self.alt_name == 'DASH': self.polo_bot.btc_deposit(0.1 * r) self.krx_bot.btc_deposit(0.1 * r) self.polo_bot.alt_deposit(1 * r) self.krx_bot.alt_deposit(1 * r) self.krx_bot.krw_deposit(200000 * r) # self.btc_polo = 0.1 *r # 3,360,000 # self.btc_krx = 0.1 *r# 3,360,000 # self.btc_depo_delayed = 0 # self.krw_krx = 200000*r # self.alt_polo = 1.5*r # self.alt_krx = 1.5*r #230,000 #self.btc_tx_delayed = 0 #self.alt_tx_delayed = 0 #### THIS will be changed to API. This is simulated delayed transaction. self.polo_btc_tx_info = None self.polo_alt_tx_info = None self.krx_btc_tx_info = None self.krx_alt_tx_info = None self.delay_tx = 8 * 60 # as sec ## Collect price if self.mode == 'simulation': self.load_data() self.read_data(0) self.time_init = self.time_stamp elif self.mode == 'realtime': self.collect_price_() prem = self.calculate_premium_(1000000) # impossible threshold self.time_init = time.time() ## Values to log INIT self.Y_prem_pos = np.array([prem[0]]) self.Y_prem_neg = np.array([prem[1]]) self.Y_krx_altkrw_sell_price = np.array( [self.krx_bot.altkrw_sell_price]) self.Y_krx_altkrw_buy_price = np.array([self.krx_bot.altkrw_buy_price]) self.Y_krx_btckrw_sell_price = np.array( [self.krx_bot.btckrw_sell_price]) self.Y_krx_btckrw_buy_price = np.array([self.krx_bot.btckrw_buy_price]) self.Y_krx_sell_price = np.array([self.krx_bot.sell_price]) self.Y_krx_buy_price = np.array([self.krx_bot.buy_price]) self.Y_polo_sell_price = np.array([self.polo_bot.sell_price]) self.Y_polo_buy_price = np.array([self.polo_bot.buy_price]) self.Y_polo_btcusd_price = np.array([self.polo_bot.btcusd]) time_stamp = time.time() - self.time_init curr_time = np.array([time_stamp / 60.]) # per minute self.X = curr_time ## Needs Visdom try: from visdom import Visdom self.viz = Visdom() self.viz.env = self.alt_name self.win_prem_ticker = None self.win_altbtc_ticker = None self.win_altkrw_ticker = None self.win_btckrw_ticker = None self.win_btcusd_ticker = None self.viz.close(win=self.win_prem_ticker) self.viz.close(win=self.win_altbtc_ticker) self.viz.close(win=self.win_altkrw_ticker) self.viz.close(win=self.win_btckrw_ticker) self.viz.close(win=self.win_btcusd_ticker) X = np.column_stack((curr_time, curr_time)) # PREM self.win_prem_ticker = self.viz.line( X=X, Y=np.column_stack( (self.Y_prem_pos * 100, self.Y_prem_neg * 100)), win=self.win_prem_ticker, opts=dict(title=self.alt_name + ' Premium in BTC', legend=['+ : krx > polo', '- : polo > krx'])) # ALTBTC Y = np.column_stack( ((self.Y_polo_buy_price + self.Y_polo_sell_price) / 2, (self.Y_krx_buy_price + self.Y_krx_sell_price) / 2)) self.win_altbtc_ticker = self.viz.line( X=X, Y=Y, win=self.win_altbtc_ticker, opts=dict(title=self.alt_name + ' Price in BTC', legend=['POLONIEX', 'BITHUMB'])) # ALTKRW self.win_altkrw_ticker = self.viz.line( X=curr_time, Y=np.array([(self.krx_bot.altkrw_buy_price + self.krx_bot.altkrw_buy_price) / 2]), win=self.win_altkrw_ticker, opts=dict(title=self.alt_name + ' Price in KRW', legend=['BITHUMB'])) # BTCKRW self.win_btckrw_ticker = self.viz.line( X=curr_time, Y=np.array([(self.krx_bot.btckrw_buy_price + self.krx_bot.btckrw_buy_price) / 2]), win=self.win_btckrw_ticker, opts=dict(title='BTC Price in KRW', legend=['BITHUMB'])) # BTCUSD self.win_btcusd_ticker = self.viz.line( X=curr_time, Y=np.array([self.polo_bot.btcusd]), win=self.win_btcusd_ticker, opts=dict(title='BTC Price in USD', legend=['POLONIEX'])) except ImportError: print('visdom not imported') self.btc_init = self.btc_sum() self.alt_init = self.alt_sum() self.asset_init = self.asset_in_btc() #self.btc_polo + self.btc_krx #self.alt_init = self.alt_polo + self.alt_krx self.eval_asset_init = self.asset_in_btc() self.btc_ratio = 1 self.alt_ratio = 1 self.total_ratio = 1 self.prem_pos = 0 self.prem_neg = 0 self.prem_pos_failed = 0 self.prem_neg_failed = 0 def load_data(self): self.data_name = [ 'X', 'Y_prem_pos', 'Y_prem_neg', 'Y_krx_altkrw_sell_price', 'Y_krx_altkrw_buy_price', 'Y_krx_btckrw_sell_price', 'Y_krx_btckrw_buy_price', 'Y_krx_sell_price', 'Y_krx_buy_price', 'Y_polo_sell_price', 'Y_polo_buy_price', 'Y_polo_btcusd_price' ] self.data_dict = dict() for e in self.data_name: self.data_dict[e] = np.array( pd.read_csv('./data/' + self.alt_name + '/' + e + '.csv'))[:, 1] def read_data(self, iter_arb): self.krx_bot.altkrw_buy_price = self.data_dict[ 'Y_krx_altkrw_buy_price'][iter_arb] self.krx_bot.altkrw_sell_price = self.data_dict[ 'Y_krx_altkrw_sell_price'][iter_arb] self.krx_bot.btckrw_buy_price = self.data_dict[ 'Y_krx_btckrw_sell_price'][iter_arb] self.krx_bot.btckrw_sell_price = self.data_dict[ 'Y_krx_btckrw_buy_price'][iter_arb] self.krx_bot.buy_price = self.data_dict['Y_krx_sell_price'][iter_arb] self.krx_bot.sell_price = self.data_dict['Y_krx_sell_price'][iter_arb] self.polo_bot.btcusd = self.data_dict['Y_polo_btcusd_price'][iter_arb] self.polo_bot.sell_price = self.data_dict['Y_polo_sell_price'][ iter_arb] self.polo_bot.buy_price = self.data_dict['Y_polo_buy_price'][iter_arb] self.time_stamp = self.data_dict['X'][iter_arb] * 60.0 def refresh(self): btcsum = self.asset_in_btc() self.total_ratio = btcsum / self.asset_init self.btc_ratio = self.btc_sum() / self.btc_init self.alt_ratio = self.alt_sum() / self.alt_init def btc_sum(self): return self.polo_bot.btc_balance + self.krx_bot.btc_balance + self.polo_bot.btc_in_tx + self.krx_bot.btc_in_tx def alt_sum(self): return self.polo_bot.alt_balance + self.krx_bot.alt_balance + self.polo_bot.alt_in_tx + self.krx_bot.alt_in_tx def asset_in_btc(self): total_btc = self.btc_sum() + self.polo_bot.eval_alt(self.alt_sum()) return total_btc def asset_in_usd(self): return asset_in_btc() * self.polo_bot.btcusd def asset_in_krw(self): return asset_in_btc() * self.krx_bot.btckrw_sell_price def show_asset(self): print('\t==== My Wallet ====') # print('\tPOLONIEX') # print('\t{} : {}'.format('BTC', self.polo_bot.btc_balance)) # print('\t{} : {}'.format(self.alt_name, self.polo_bot.alt_balance)) # #print('\t{} : {}'.format('BTC in transact', self.btc_tx_delayed)) # TODO :Need Direction # #print('\t{} : {}'.format('ALT in transact', self.alt_tx_delayed)) # TODO :Need Direction # print('\t', self.krx_bot.exchange_name) # print('\t{} : {}'.format('BTC',self.krx_bot.btc_balance)) # print('\t{} : {}'.format(self.alt_name, self.krx_bot.alt_balance)) # print('\t{} : {}'.format('KRW',self.krx_bot.krw_balance)) btcsum = self.asset_in_btc() self.total_ratio = btcsum / self.asset_init print('\tWorths BTC : {} \t ratio = {}'.format(btcsum, self.total_ratio)) self.btc_ratio = self.btc_sum() / self.btc_init self.alt_ratio = self.alt_sum() / self.alt_init print('\tCoin ratio : BTC : {}\t {} : {}'.format( self.btc_ratio, self.alt_name, self.alt_ratio)) print('\tArbitrage : +1 ({},{})\t -1 ({},{})\t'.format( self.prem_pos, self.prem_pos_failed, self.prem_neg, self.prem_neg_failed)) print('\tTrade amount : POLO : {} BTC\t {} : {} KRW'.format( self.polo_bot.btc_trd_amount, self.krx_bot.exchange_name, self.krx_bot.krw_trd_amount)) print( '\tTransaction amount : POLO {:.2f} USD\t {} : {:.6f} BTC {:.4f} {}' .format(self.polo_bot.usd_with_daily_amount, self.krx_bot.exchange_name, self.krx_bot.btc_with_daily_amount, self.krx_bot.alt_with_daily_amount, self.alt_name)) print('\t===================') print() ## TODO ## It's important to check spending BTC first. If no BTC. Need Emergent Transfer! ## If, i have enough BTC: ## First, ALT -> BTC. ## then, BTC->ALT ## Chekcout both ALT, BTC amount needed from my wallet for the operation. ## Also need to check there are enough ask orders on exchange. ## Sell the expensive first def krx_sell_polo_buy(self): success = True try: self.krx_bot.alt2btc() # krx : ALT -> BTC except ValueError as e: print("\t{} : {} -> BTC : FAILED!!!!".format( self.krx_bot.exchange_name, self.alt_name)) if (self.polo_bot.alt_in_tx == 0): # If transaction is on going, do not transact try: # ALT : polo -> krx self.polo_alt_tx_info = self.polo_bot.transact_alt2krx( self.polo_bot.alt_balance) if self.mode == 'simulation': self.polo_alt_tx_info['time'] = self.time_stamp except ValueError as e: print(e) success = False try: self.polo_bot.btc2alt() # polo : BTC -> ALT except ValueError as e: print("\tPOLO : BTC -> {} : FAILED!!!!".format(self.alt_name)) if (self.krx_bot.btc_in_tx == 0): try: # BTC : krx -> polo self.krx_btc_tx_info = self.krx_bot.transact_btc2polo( self.krx_bot.btc_balance) if self.mode == 'simulation': self.krx_btc_tx_info['time'] = self.time_stamp except ValueError as e: print(e) success = False return success def polo_sell_krx_buy(self): #1 success = True try: self.polo_bot.alt2btc() # polo : ALT->BTC except ValueError as e: print("\tPOLO : {} -> BTC : FAILED!!!!".format(self.alt_name)) if (self.krx_bot.alt_in_tx == 0): try: # ALT : krx -> polo self.krx_alt_tx_info = self.krx_bot.transact_alt2polo( self.krx_bot.alt_balance) # SEND all ALT if self.mode == 'simulation': self.krx_alt_tx_info['time'] = self.time_stamp except ValueError as e: print(e) success = False try: self.krx_bot.btc2alt() # krx : BTC-> ALT except ValueError as e: print("\t{} : BTC -> {} : FAILED!!!!".format( self.krx_bot.exchange_name, self.alt_name)) if (self.polo_bot.btc_in_tx == 0): try: # BTC : polo -> krx self.polo_btc_tx_info = self.polo_bot.transact_btc2krx( self.polo_bot.btc_balance) # SEND all BTC if self.mode == 'simulation': self.polo_btc_tx_info['time'] = self.time_stamp except ValueError as e: print(e) success = False return success def arbitrage(self, prem_alert): success = False if prem_alert == 1: if (self.krx_sell_polo_buy()): self.prem_pos += 1 success = True else: self.prem_pos_failed += 1 elif prem_alert == -1: if (self.polo_sell_krx_buy()): self.prem_neg += 1 success = True else: self.prem_neg_failed += 1 return success def check_transaction(self, mode='realtime'): refr = False if mode == 'realtime': time_now = time.time() elif mode == 'simulation': time_now = self.time_stamp if self.polo_alt_tx_info: # ALT : polo -> krx if time_now - self.polo_alt_tx_info['time'] > self.delay_tx: self.krx_bot.alt_deposit(self.polo_alt_tx_info['amount']) print("ALT : POLO -> {}".format(self.krx_bot.exchange_name)) self.polo_alt_tx_info = None self.polo_bot.alt_in_tx = 0 refr = True if self.polo_btc_tx_info: # BTC : polo -> krx if time_now - self.polo_btc_tx_info['time'] > self.delay_tx: self.krx_bot.btc_deposit(self.polo_btc_tx_info['amount']) print("BTC : POLO -> {}".format(self.krx_bot.exchange_name)) self.polo_btc_tx_info = None self.polo_bot.btc_in_tx = 0 refr = True if self.krx_alt_tx_info: # ALT : krx -> polo if time_now - self.krx_alt_tx_info['time'] > self.delay_tx: self.polo_bot.alt_deposit(self.krx_alt_tx_info['amount']) print("ALT : {} -> POLO".format(self.krx_bot.exchange_name)) self.krx_alt_tx_info = None self.krx_bot.alt_in_tx = 0 refr = True if self.krx_btc_tx_info: # BTC : polo -> krx if time_now - self.krx_btc_tx_info['time'] > self.delay_tx: self.polo_bot.btc_deposit(self.krx_btc_tx_info['amount']) print("BTC : {} -> POLO".format(self.krx_bot.exchange_name)) self.krx_btc_tx_info = None self.krx_bot.btc_in_tx = 0 refr = True return refr def calculate_premium_fiat(): usdkrw = Currency('USDKRW') curr = float(usdkrw.get_ask()) btcusd = self.polo_bot.btcusd print("\tBTC premeium KRW/USD : ", str((self.krx_bot.btckrw_buy_price / (curr * btcusd))), 'with btcusd =', btcusd) def collect_price_(self, mode='realtime', iter_arb=0): # Only collect data if mode == 'realtime': self.krx_bot.collect_price() self.polo_bot.collect_price() elif mode == 'simulation': self.read_data(iter_arb) def collect_price(self, mode='realtime', iter_arb=0): # Collect data and log and visualize self.collect_price_(mode=mode, iter_arb=iter_arb) # LOG #Y = np.column_stack((polo_price[i], krx_price[i])) krx_sell_price = np.array([self.krx_bot.sell_price]) krx_buy_price = np.array([self.krx_bot.buy_price]) polo_sell_price = np.array([self.polo_bot.sell_price]) polo_buy_price = np.array([self.polo_bot.buy_price]) # also Ticker self.Y_krx_buy_price = np.append(self.Y_krx_buy_price, krx_buy_price) self.Y_krx_sell_price = np.append(self.Y_krx_sell_price, krx_sell_price) self.Y_polo_buy_price = np.append(self.Y_polo_buy_price, polo_buy_price) self.Y_polo_sell_price = np.append(self.Y_polo_sell_price, polo_sell_price) # side info for simulation self.Y_krx_altkrw_buy_price = np.append( self.Y_krx_altkrw_buy_price, np.array([self.krx_bot.altkrw_buy_price])) self.Y_krx_altkrw_sell_price = np.append( self.Y_krx_altkrw_sell_price, np.array([self.krx_bot.altkrw_sell_price])) self.Y_krx_btckrw_buy_price = np.append( self.Y_krx_btckrw_buy_price, np.array([self.krx_bot.btckrw_buy_price])) self.Y_krx_btckrw_sell_price = np.append( self.Y_krx_btckrw_sell_price, np.array([self.krx_bot.btckrw_sell_price])) btcusd = self.polo_bot.btcusd self.Y_polo_btcusd_price = np.append(self.Y_polo_btcusd_price, np.array([btcusd])) self.time_stamp = time.time() - self.time_init curr_time = np.array([self.time_stamp / 60.]) # per minute self.X = np.append(self.X, curr_time) try: # ALTBTC X = np.column_stack((curr_time, curr_time)) Y = np.column_stack(((polo_buy_price + polo_sell_price) / 2, (krx_buy_price + krx_sell_price) / 2)), self.viz.updateTrace( X=X, Y=Y[0], # TODO : Why thue f**k [0]? win=self.win_altbtc_ticker, ) # ALTKRW self.viz.updateTrace( X=curr_time, Y=np.array([(self.krx_bot.altkrw_buy_price + self.krx_bot.altkrw_buy_price) / 2]), win=self.win_altkrw_ticker, ) # BTCKRW self.viz.updateTrace( X=curr_time, Y=np.array([(self.krx_bot.btckrw_buy_price + self.krx_bot.btckrw_buy_price) / 2]), win=self.win_btckrw_ticker, ) # BTCUSD self.viz.updateTrace( X=curr_time, Y=np.array([btcusd]), win=self.win_btcusd_ticker, ) except ImportError: print('Visdom not imported') def calculate_premium_( self, threshold): # calculate premium of positive and negative prem_pos_r = self.krx_bot.sell_price / ( self.polo_bot.buy_price * (1 + self.polo_bot.fee_trd)) - 1 prem_neg_r = self.polo_bot.sell_price * ( 1 - self.polo_bot.fee_trd) / self.krx_bot.buy_price - 1 return (prem_pos_r, prem_neg_r) def calculate_premium(self, threshold): # cal premium and compare for arbitrage (prem_pos_r, prem_neg_r) = self.calculate_premium_(threshold) ################## # print('BITHUMB : \tBUY: ', b_buy_price, '\tSELL: ', b_sell_price, '\t|') # print('POLO : \tBUY: ', p_buy_price, '\tSELL: ', p_sell_price, '\t|') # print(pform.format(krx_name, krx_bot.buy_price, krx_bot.sell_price)) # print(pform.format('POLONIEX', polo_bot.buy_price, polo_bot.sell_price)) # print("\tPemium monitoring: POLO : BTC->{} | BITH : {}->BTC : {:5.2f}" # .format(self.alt_name, # self.alt_name, # prem_pos_r * 100)) # print("\tPemium monitoring: POLO : {}->BTC | BITH : BTC->{} : {:5.2f}" # .format(self.alt_name, # self.alt_name, # prem_neg_r * 100)) #print() ################# prem = 0 ##### Premium compare ###### TODO Threshold : ratio? or delta? if (prem_pos_r > threshold): #### POLO : BTC -> Target / BITHUMB : Taret -> BTC #print('#################### PREMIUM ALERT ####################\a') #print() print('\tPREM RATIO: ', prem_pos_r * 100, ' %') print('\t\t\t{}\t\t|\t\t POLO'.format(self.krx_bot.exchange_name)) print('\t\t{}\t->\tBTC\t|\tBTC\t->\t{}'.format( self.alt_name, self.alt_name)) print('\t{:10.6f}\t {:10.6f}\t|{:10.6f}\t {:10.6f}'.format( self.krx_bot.alt_balance, self.krx_bot.btc_balance, self.polo_bot.btc_balance, self.polo_bot.alt_balance)) prem = 1 if (prem_neg_r > threshold): # Each market threshold need comission #### POLO : Target -> BTC / BITHUMB : BTC -> Target #print('#################### PREMIUM ALERT ####################\a') #print() #print('\tPOLO : {} -> BTC \t|\t {} : BTC -> {}'.format(self.alt_name, #self.krx_bot.exchange_name, self.alt_name)) print('\tPREM RATIO: ', prem_neg_r * 100, ' %') print('\t\t\t{}\t\t|\t\t POLO'.format(self.krx_bot.exchange_name)) print('\t\t{}\t<-\tBTC\t|\tBTC\t<-\t{}'.format( self.alt_name, self.alt_name)) print('\t{:10.6f}\t {:10.6f}\t|{:10.6f}\t {:10.6f}'.format( self.krx_bot.alt_balance, self.krx_bot.btc_balance, self.polo_bot.btc_balance, self.polo_bot.alt_balance)) prem = -1 ## log self.Y_prem_pos = np.append(self.Y_prem_pos, prem_pos_r) self.Y_prem_neg = np.append(self.Y_prem_neg, prem_neg_r) ## Needs visdom try: curr_time = np.array([self.time_stamp / 60.]) # per minute X = np.column_stack((curr_time, curr_time)) Y = np.column_stack( (np.array([prem_pos_r * 100]), np.array([prem_neg_r * 100]))) self.viz.updateTrace( X=X, Y=Y, win=self.win_prem_ticker, ) except ImportError: print('Visdom not imported') return prem def log_data(self): pd.DataFrame(self.X).to_csv("./log/" + self.alt_name + "/X.csv") pd.DataFrame(self.Y_prem_pos).to_csv("./log/" + self.alt_name + "/Y_prem_pos.csv") pd.DataFrame(self.Y_prem_neg).to_csv("./log/" + self.alt_name + "/Y_prem_neg.csv") pd.DataFrame(self.Y_krx_altkrw_sell_price).to_csv( "./log/" + self.alt_name + "/Y_krx_altkrw_sell_price.csv") pd.DataFrame( self.Y_krx_altkrw_buy_price).to_csv("./log/" + self.alt_name + "/Y_krx_altkrw_buy_price.csv") pd.DataFrame(self.Y_krx_btckrw_sell_price).to_csv( "./log/" + self.alt_name + "/Y_krx_btckrw_sell_price.csv") pd.DataFrame( self.Y_krx_btckrw_buy_price).to_csv("./log/" + self.alt_name + "/Y_krx_btckrw_buy_price.csv") pd.DataFrame(self.Y_krx_sell_price).to_csv("./log/" + self.alt_name + "/Y_krx_sell_price.csv") pd.DataFrame(self.Y_krx_buy_price).to_csv("./log/" + self.alt_name + "/Y_krx_buy_price.csv") pd.DataFrame(self.Y_polo_sell_price).to_csv("./log/" + self.alt_name + "/Y_polo_sell_price.csv") pd.DataFrame(self.Y_polo_buy_price).to_csv("./log/" + self.alt_name + "/Y_polo_buy_price.csv") pd.DataFrame( self.Y_polo_btcusd_price).to_csv("./log/" + self.alt_name + "/Y_polo_btcusd_price.csv")
class Trainer(object): def __init__(self, model, optimizer, train_criterion, config_file, experiment, train_dataset, val_dataset, device, checkpoint_file=None, resume_optim=False, val_criterion=None): """ General purpose training script :param model: Network model :param optimizer: object of the Optimizer class, wrapping torch.optim and lr :param train_criterion: Training loss function :param config_file: configuration .ini file for training parameters :param experiment: name of the experiment, used to create logging dir :param train_dataset: PyTorch dataset :param val_dataset: PyTorch dataset :param device: IDs of the GPUs to use - value of $CUDA_VISIBLE_DEVICES :param checkpoint_file: Name of file with saved weights and optim params :param resume_optim: whether to resume optimization :param val_criterion: loss function to be used for validation """ self.model = model self.train_criterion = train_criterion if val_criterion is None: self.val_criterion = self.train_criterion else: self.val_criterion = val_criterion self.experiment = experiment self.optimizer = optimizer if 'CUDA_VISIBLE_DEVICES' not in os.environ: os.environ['CUDA_VISIBLE_DEVICES'] = device # read the config settings = configparser.ConfigParser() with open(config_file, 'r') as f: settings.read_file(f) self.config = {} section = settings['training'] self.config['n_epochs'] = section.getint('n_epochs') self.config['batch_size'] = section.getint('batch_size') self.config['do_val'] = section.getboolean('do_val') self.config['shuffle'] = section.getboolean('shuffle') self.config['seed'] = section.getint('seed') self.config['num_workers'] = section.getint('num_workers') self.config['snapshot'] = section.getint('snapshot') self.config['val_freq'] = section.getint('val_freq') self.config['cuda'] = torch.cuda.is_available() self.config['max_grad_norm'] = section.getfloat('max_grad_norm', 0) section = settings['logging'] self.config['log_visdom'] = section.getboolean('visdom') self.config['print_freq'] = section.getint('print_freq') self.logdir = osp.join(os.getcwd(), 'logs', self.experiment) if not osp.isdir(self.logdir): os.makedirs(self.logdir) if self.config['log_visdom']: # start plots self.vis_env = experiment self.loss_win = 'loss_win' self.vis = Visdom() self.vis.line(X=np.zeros((1, 2)), Y=np.zeros((1, 2)), win=self.loss_win, opts={ 'legend': ['train_loss', 'val_loss'], 'xlabel': 'epochs', 'ylabel': 'loss' }, env=self.vis_env) self.lr_win = 'lr_win' self.vis.line(X=np.zeros(1), Y=np.zeros(1), win=self.lr_win, opts={ 'legend': ['learning_rate'], 'xlabel': 'epochs', 'ylabel': 'log(lr)' }, env=self.vis_env) criterion_params = { k: v.data.cpu().numpy()[0] for k, v in self.train_criterion.named_parameters() } self.n_criterion_params = len(criterion_params) if self.n_criterion_params: self.criterion_param_win = 'cparam_win' self.vis.line(X=np.zeros((1, self.n_criterion_params)), Y=np.asarray(list( criterion_params.values()))[np.newaxis, :], win=self.criterion_param_win, env=self.vis_env, opts={ 'legend': list(criterion_params.keys()), 'xlabel': 'epochs', 'ylabel': 'value' }) logfile = osp.join(self.logdir, 'log.txt') stdout = Logger.Logger(logfile) print('Logging to {:s}'.format(logfile)) sys.stdout = stdout # log all the command line options print('---------------------------------------') print('Experiment: {:s}'.format(self.experiment)) for k, v in list(self.config.items()): print('{:s}: {:s}'.format(k, str(v))) print('Using GPU {:s} / {:d}'.format(device, torch.cuda.device_count())) print('---------------------------------------') # set random seed torch.manual_seed(self.config['seed']) if self.config['cuda']: torch.cuda.manual_seed(self.config['seed']) self.start_epoch = int(0) if checkpoint_file: if osp.isfile(checkpoint_file): loc_func = None if self.config[ 'cuda'] else lambda storage, loc: storage checkpoint = torch.load(checkpoint_file, map_location=loc_func) load_state_dict(self.model, checkpoint['model_state_dict']) if resume_optim: self.optimizer.learner.load_state_dict( checkpoint['optim_state_dict']) self.start_epoch = checkpoint['epoch'] if 'criterion_state_dict' in checkpoint: c_state = checkpoint['criterion_state_dict'] append_dict = { k: torch.Tensor([0.0]) for k, _ in self.train_criterion.named_parameters() if not k in c_state } c_state.update(append_dict) self.train_criterion.load_state_dict(c_state) print('Loaded checkpoint {:s} epoch {:d}'.format( checkpoint_file, checkpoint['epoch'])) self.train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=self.config['batch_size'], shuffle=self.config['shuffle'], num_workers=self.config['num_workers'], pin_memory=True, collate_fn=safe_collate) if self.config['do_val']: self.val_loader = torch.utils.data.DataLoader( val_dataset, batch_size=self.config['batch_size'], shuffle=self.config['shuffle'], num_workers=self.config['num_workers'], pin_memory=True, collate_fn=safe_collate) else: self.val_loader = None # activate GPUs if self.config['cuda']: self.model.cuda() self.train_criterion.cuda() self.val_criterion.cuda() def save_checkpoint(self, epoch): filename = osp.join(self.logdir, 'epoch_{:03d}.pth.tar'.format(epoch)) checkpoint_dict =\ {'epoch': epoch, 'model_state_dict': self.model.state_dict(), 'optim_state_dict': self.optimizer.learner.state_dict(), 'criterion_state_dict': self.train_criterion.state_dict()} torch.save(checkpoint_dict, filename) def train_val(self, lstm): """ Function that does the training and validation :param lstm: whether the model is an LSTM :return: """ for epoch in range(self.start_epoch, self.config['n_epochs']): # VALIDATION if self.config['do_val'] and ( (epoch % self.config['val_freq'] == 0) or (epoch == self.config['n_epochs'] - 1)): val_batch_time = Logger.AverageMeter() val_loss = Logger.AverageMeter() self.model.eval() end = time.time() val_data_time = Logger.AverageMeter() for batch_idx, (data, target) in enumerate(self.val_loader): val_data_time.update(time.time() - end) kwargs = dict(target=target, criterion=self.val_criterion, optim=self.optimizer, train=False) if lstm: loss, _ = step_lstm(data, self.model, self.config['cuda'], **kwargs) else: loss, _ = step_feedfwd(data, self.model, self.config['cuda'], **kwargs) val_loss.update(loss) val_batch_time.update(time.time() - end) if batch_idx % self.config['print_freq'] == 0: print('Val {:s}: Epoch {:d}\t' \ 'Batch {:d}/{:d}\t' \ 'Data time {:.4f} ({:.4f})\t' \ 'Batch time {:.4f} ({:.4f})\t' \ 'Loss {:f}' \ .format(self.experiment, epoch, batch_idx, len(self.val_loader)-1, val_data_time.val, val_data_time.avg, val_batch_time.val, val_batch_time.avg, loss)) if self.config['log_visdom']: self.vis.save(envs=[self.vis_env]) end = time.time() print('Val {:s}: Epoch {:d}, val_loss {:f}'.format( self.experiment, epoch, val_loss.avg)) if self.config['log_visdom']: self.vis.updateTrace(X=np.asarray([epoch]), Y=np.asarray([val_loss.avg]), win=self.loss_win, name='val_loss', append=True, env=self.vis_env) self.vis.save(envs=[self.vis_env]) # SAVE CHECKPOINT if epoch % self.config['snapshot'] == 0: self.save_checkpoint(epoch) print('Epoch {:d} checkpoint saved for {:s}'.\ format(epoch, self.experiment)) # ADJUST LR lr = self.optimizer.adjust_lr(epoch) if self.config['log_visdom']: self.vis.updateTrace(X=np.asarray([epoch]), Y=np.asarray([np.log10(lr)]), win=self.lr_win, name='learning_rate', append=True, env=self.vis_env) # TRAIN self.model.train() train_data_time = Logger.AverageMeter() train_batch_time = Logger.AverageMeter() end = time.time() for batch_idx, (data, target) in enumerate(self.train_loader): train_data_time.update(time.time() - end) kwargs = dict(target=target, criterion=self.train_criterion, optim=self.optimizer, train=True, max_grad_norm=self.config['max_grad_norm']) if lstm: loss, _ = step_lstm(data, self.model, self.config['cuda'], **kwargs) else: loss, _ = step_feedfwd(data, self.model, self.config['cuda'], **kwargs) train_batch_time.update(time.time() - end) if batch_idx % self.config['print_freq'] == 0: n_iter = epoch * len(self.train_loader) + batch_idx epoch_count = float(n_iter) / len(self.train_loader) print('Train {:s}: Epoch {:d}\t' \ 'Batch {:d}/{:d}\t' \ 'Data Time {:.4f} ({:.4f})\t' \ 'Batch Time {:.4f} ({:.4f})\t' \ 'Loss {:f}\t' \ 'lr: {:f}'.\ format(self.experiment, epoch, batch_idx, len(self.train_loader)-1, train_data_time.val, train_data_time.avg, train_batch_time.val, train_batch_time.avg, loss, lr)) if self.config['log_visdom']: self.vis.updateTrace(X=np.asarray([epoch_count]), Y=np.asarray([loss]), win=self.loss_win, name='train_loss', append=True, env=self.vis_env) if self.n_criterion_params: for name, v in self.train_criterion.named_parameters( ): v = v.data.cpu().numpy()[0] self.vis.updateTrace( X=np.asarray([epoch_count]), Y=np.asarray([v]), win=self.criterion_param_win, name=name, append=True, env=self.vis_env) self.vis.save(envs=[self.vis_env]) end = time.time() # Save final checkpoint epoch = self.config['n_epochs'] self.save_checkpoint(epoch) print('Epoch {:d} checkpoint saved'.format(epoch)) if self.config['log_visdom']: self.vis.save(envs=[self.vis_env])
# Loss computation and weight update step loss = torch.mean((out[0, :, 0] - target[:, 0])**2) optimizer.zero_grad() loss.backward() optimizer.step() # Log loss with open(log_file, 'a') as logfile: logfile.write("{},".format(float(loss))) # Visualization update if settings.VISUALIZE: smooth_loss = smooth_loss * decay_rate + ( 1 - decay_rate) * loss.data.cpu().numpy() viz.updateTrace(win=loss_plot, X=np.array([counter]), Y=loss.data.cpu().numpy(), name='loss') viz.updateTrace(win=loss_plot, X=np.array([counter]), Y=smooth_loss, name='smooth loss') real_star = target[:, 0].data.cpu().numpy().astype(int) pred_star = out[0, :, 0].data.cpu().numpy().round().clip(1, 5).astype(int) for idx in range(len(real_star)): smooth_pred_dist[pred_star[idx] - 1] += 1 smooth_real_dist[real_star[idx] - 1] += 1 smooth_real_dist *= decay_rate smooth_pred_dist *= decay_rate
class Callback(object): """ Used to log/visualize the evaluation metrics during training. The values are stored at the end of each epoch. """ def __init__(self, metrics): """ Args: metrics : a list of callbacks. Possible values: "CoherenceMetric" "PerplexityMetric" "DiffMetric" "ConvergenceMetric" """ # list of metrics to be plot self.metrics = metrics def set_model(self, model): """ Save the model instance and initialize any required variables which would be updated throughout training """ self.model = model self.previous = None # check for any metric which need model state from previous epoch if any( isinstance(metric, (DiffMetric, ConvergenceMetric)) for metric in self.metrics): self.previous = copy.deepcopy(model) # store diff diagonals of previous epochs self.diff_mat = Queue() if any(metric.logger == "visdom" for metric in self.metrics): if not VISDOM_INSTALLED: raise ImportError("Please install Visdom for visualization") self.viz = Visdom() # store initial plot windows of every metric (same window will be updated with increasing epochs) self.windows = [] if any(metric.logger == "shell" for metric in self.metrics): # set logger for current topic model self.log_type = logging.getLogger('gensim.models.ldamodel') def on_epoch_end(self, epoch, topics=None): """ Log or visualize current epoch's metric value Args: epoch : current epoch no. topics : topic distribution from current epoch (required for coherence of unsupported topic models) """ # stores current epoch's metric values current_metrics = {} # plot all metrics in current epoch for i, metric in enumerate(self.metrics): label = str(metric) value = metric.get_value(topics=topics, model=self.model, other_model=self.previous) current_metrics[label] = value if metric.logger == "visdom": if epoch == 0: if value.ndim > 0: diff_mat = np.array([value]) viz_metric = self.viz.heatmap(X=diff_mat.T, env=metric.viz_env, opts=dict( xlabel='Epochs', ylabel=label, title=label)) # store current epoch's diff diagonal self.diff_mat.put(diff_mat) # saving initial plot window self.windows.append(copy.deepcopy(viz_metric)) else: viz_metric = self.viz.line(Y=np.array([value]), X=np.array([epoch]), env=metric.viz_env, opts=dict(xlabel='Epochs', ylabel=label, title=label)) # saving initial plot window self.windows.append(copy.deepcopy(viz_metric)) else: if value.ndim > 0: # concatenate with previous epoch's diff diagonals diff_mat = np.concatenate( (self.diff_mat.get(), np.array([value]))) self.viz.heatmap(X=diff_mat.T, env=metric.viz_env, win=self.windows[i], opts=dict(xlabel='Epochs', ylabel=label, title=label)) self.diff_mat.put(diff_mat) else: self.viz.updateTrace(Y=np.array([value]), X=np.array([epoch]), env=metric.viz_env, win=self.windows[i]) if metric.logger == "shell": statement = "".join( ("Epoch ", str(epoch), ": ", label, " estimate: ", str(value))) self.log_type.info(statement) # check for any metric which need model state from previous epoch if isinstance(metric, (DiffMetric, ConvergenceMetric)): self.previous = copy.deepcopy(self.model) return current_metrics
X=np.array(times), Y=np.array(ttt[0]), opts=dict( markersize=10, markers=1, legend=['0-shot accuracy'], ylabel='Percent Correct', xlabel='Episode', xtick=1, ytick=1, # xtype='log' ) ) viz.updateTrace( X=np.array(times), Y=np.array(ttt[1]), win=win, name='1-shot accuracy', ) viz.updateTrace( X=np.array(times), Y=np.array(ttt[2]), win=win, name='2-shot accuracy' ) viz.updateTrace( X=np.array(times), Y=np.array(ttt[3]), win=win, name='3-shot accuracy' ) viz.updateTrace(
opts=dict( xtickmin=-15, xtickmax=10, xtickstep=1, ytickmin=-300, ytickmax=200, ytickstep=1, markersymbol='dot', markercolor=np.random.randint(0, 255, num_data), markersize=5, ), ) viz.updateTrace( X=x, Y=y, win=win, ) # model & optimizer model = nn.Linear(1, 1) output = model(Variable(x)) loss_func = nn.L1Loss() optimizer = optim.SGD(model.parameters(), lr=0.01) # train loss_arr = [] label = Variable(y_noise) for i in range(num_epoch):
class Callback(object): """A class representing routines called reactively at specific phases during trained. These can be used to log or visualize the training progress using any of the metric scores developed before. The values are stored at the end of each training epoch. The following metric scores are currently available: * :class:`~gensim.models.callbacks.CoherenceMetric` * :class:`~gensim.models.callbacks.PerplexityMetric` * :class:`~gensim.models.callbacks.DiffMetric` * :class:`~gensim.models.callbacks.ConvergenceMetric` """ def __init__(self, metrics): """ Parameters ---------- metrics : list of :class:`~gensim.models.callbacks.Metric` The list of metrics to be reported by the callback. """ self.metrics = metrics def set_model(self, model): """Save the model instance and initialize any required variables which would be updated throughout training. Parameters ---------- model : :class:`~gensim.models.basemodel.BaseTopicModel` The model for which the training will be reported (logged or visualized) by the callback. """ self.model = model self.previous = None # check for any metric which need model state from previous epoch if any( isinstance(metric, (DiffMetric, ConvergenceMetric)) for metric in self.metrics): self.previous = copy.deepcopy(model) # store diff diagonals of previous epochs self.diff_mat = Queue() if any(metric.logger == "visdom" for metric in self.metrics): if not VISDOM_INSTALLED: raise ImportError("Please install Visdom for visualization") self.viz = Visdom() # store initial plot windows of every metric (same window will be updated with increasing epochs) self.windows = [] if any(metric.logger == "shell" for metric in self.metrics): # set logger for current topic model self.log_type = logging.getLogger('gensim.models.ldamodel') def on_epoch_end(self, epoch, topics=None): """Report the current epoch's metric value. Called at the end of each training iteration. Parameters ---------- epoch : int The epoch that just ended. topics : list of list of str, optional List of tokenized topics. This is required for the coherence metric. Returns ------- dict of (str, object) Mapping from metric names to their values. The type of each value depends on the metric type, for example :class:`~gensim.models.callbacks.DiffMetric` computes a matrix while :class:`~gensim.models.callbacks.ConvergenceMetric` computes a float. """ # stores current epoch's metric values current_metrics = {} # plot all metrics in current epoch for i, metric in enumerate(self.metrics): label = str(metric) value = metric.get_value(topics=topics, model=self.model, other_model=self.previous) current_metrics[label] = value if metric.logger == "visdom": if epoch == 0: if value.ndim > 0: diff_mat = np.array([value]) viz_metric = self.viz.heatmap(X=diff_mat.T, env=metric.viz_env, opts=dict( xlabel='Epochs', ylabel=label, title=label)) # store current epoch's diff diagonal self.diff_mat.put(diff_mat) # saving initial plot window self.windows.append(copy.deepcopy(viz_metric)) else: viz_metric = self.viz.line(Y=np.array([value]), X=np.array([epoch]), env=metric.viz_env, opts=dict(xlabel='Epochs', ylabel=label, title=label)) # saving initial plot window self.windows.append(copy.deepcopy(viz_metric)) else: if value.ndim > 0: # concatenate with previous epoch's diff diagonals diff_mat = np.concatenate( (self.diff_mat.get(), np.array([value]))) self.viz.heatmap(X=diff_mat.T, env=metric.viz_env, win=self.windows[i], opts=dict(xlabel='Epochs', ylabel=label, title=label)) self.diff_mat.put(diff_mat) else: self.viz.updateTrace(Y=np.array([value]), X=np.array([epoch]), env=metric.viz_env, win=self.windows[i]) if metric.logger == "shell": statement = "".join( ("Epoch ", str(epoch), ": ", label, " estimate: ", str(value))) self.log_type.info(statement) # check for any metric which need model state from previous epoch if isinstance(metric, (DiffMetric, ConvergenceMetric)): self.previous = copy.deepcopy(self.model) return current_metrics
markercolor=np.floor(np.random.random((2, 3)) * 255), ), ) win = viz.scatter( X=np.random.rand(255, 2), opts=dict( markersize=10, markercolor=np.random.randint(0, 255, (255, 3,)), ), ) # add new trace to scatter plot viz.updateTrace( X=np.random.rand(255), Y=np.random.rand(255), win=win, name='new_trace', ) # bar plots viz.bar(X=np.random.rand(20)) viz.bar( X=np.abs(np.random.rand(5, 3)), opts=dict( stacked=True, legend=['Facebook', 'Google', 'Twitter'], rownames=['2012', '2013', '2014', '2015', '2016'] ) ) viz.bar(
for epoch in range(1000): for i, data in enumerate(imgLoader, 0): # get the inputs inputs, labels = data # wrap them in Variable inputs, labels = Variable(inputs).cuda(), Variable(labels).cuda() # zero the parameter gradients optimizer.zero_grad() outputs = net(inputs) #print(outputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() # print statistics running_loss += loss.data[0] if i % 100 == 0: # print every 2000 mini-batches print('[ %d %5d] loss: %.3f' % (epoch, i + 1, running_loss / 100)) viz.updateTrace(X=np.array([epoch + i / 1900.0]), Y=np.array([running_loss]), win=win, name="2") running_loss = 0.0 if epoch % 30 == 0: checkpoint(epoch)
def main(): start_time = time.clock() lr = 0.02 iteration = 10000 bias = 0.5 viz = Visdom(env='sin') trainingData = np.empty([0, 1, 1]) for i in range(40): a = i * math.pi / 40 trainingData = np.append(trainingData, [[[a]]], 0) testData = np.random.uniform(0, math.pi, (30, 1, 1)) rightResult = np.sin(testData) rightDot = np.append(testData[:, :, 0], rightResult[:, :, 0], 1) neuralNetwork = [] inputLayer = InputLayer(len(trainingData[0, 0])) h1 = NeuroLayer(5, inputLayer, bias) neuralNetwork.append(h1) a1 = Sigmoid(h1) neuralNetwork.append(a1) outputLayer = NeuroLayer(1, a1, bias) outputActionLayer = PRelu(outputLayer) errorLayer = ErrorLayer(outputActionLayer) neuralNetwork.append(outputLayer) neuralNetwork.append(outputActionLayer) neuralNetwork.append(errorLayer) init_weight = h1.weight.copy() for itr in range(1, iteration): np.random.shuffle(trainingData) last_error = 0 for d in trainingData: inputLayer.data = d errorLayer.target = [math.sin(d[0, 0])] for layer in neuralNetwork: layer.forward() for layer in reversed(neuralNetwork): layer.backward() last_error += errorLayer.data for layer in neuralNetwork: layer.update(lr) if (100 > itr > 19 or itr % 20 == 0): if (itr == 20): win = viz.line(X=np.array([itr]), Y=np.array(last_error[0] / len(trainingData)), name="sin", win='loss') win2 = viz.scatter( X=np.random.rand(1, 2), name="sin dot", win='fitting', ) viz.updateTrace( X=np.array([itr]), Y=np.array(last_error[0] / len(trainingData)), win=win, ) testResult = np.empty([30, 1]) for i in range(len(testData)): inputLayer.data = testData[i] for layer in neuralNetwork[:-1]: layer.forward() testResult[i][0] = outputActionLayer.data[0][0] testDot = np.append(testData[:, :, 0], testResult, 1) dot = np.append(rightDot, testDot, 0) viz.scatter(X=dot, name="sin dot", win=win2, Y=[1] * 30 + [2] * 30, opts=dict( legend=['right', 'test'], markersize=5, )) print('=================================') print('last_error', last_error) elapsed_time = time.clock() - start_time print("all time", elapsed_time) print('=================================') print('init weight', init_weight) print('last weight', h1.weight) print('=================================') last_error = 0 for d in testData: inputLayer.data = d errorLayer.target = np.sin(d) for layer in neuralNetwork: layer.forward() last_error += errorLayer.data print('_______________') print(d, np.sin(d)) print("output", outputActionLayer.data) print("error", errorLayer.data) print('_______________') errorLayer.update(0) print('last_error', last_error / len(testData))
def train(*, dataset='en.txt', length=10, hidden_size=200, batch_size=128, emb_size=350, lr=1e-3, nb_epochs=1000000, latent_size=20, word_dropout=0., feed_latent=False, folder='out'): np.random.seed(42) viz = Visdom('http://romeo163') win = viz.line(X=np.array([0]), Y=np.array([0]), opts=dict(title='textgen, started at {}, folder={}'.format( datetime.now(), folder))) viz.line(X=np.array([0]), Y=np.array([0]), update='append', win=win) print('reading corpus...') corpus = open(dataset).read() print('tokenizing...') nlp = spacy.load('en') doc = nlp(corpus) corpus = [sent for sent in doc.sents if len(sent) <= length] corpus = [[tok.string.strip() for tok in sent[0:-1]] for sent in corpus] corpus = corpus[0:100000] print('Size of corpus : {}'.format(len(corpus))) print(corpus[0:5]) # the max length is +2 because we pad with the first and en character max_length = max(map(len, corpus)) + 2 # Fitting document vectorizer print('Fitting document vectorizer...') doc = DocumentVectorizer(pad=True, begin_character=True, end_character=True, length=max_length) doc._update(set([UNK_CHAR])) doc.partial_fit(corpus) vocab_size = len(doc.words_) print('vocab size : {}'.format(vocab_size)) # Model rnn = RNN( vocab_size=vocab_size, emb_size=emb_size, # embedding size hidden_size=hidden_size, # hidden size of encoder and decoder latent_size=latent_size, # latent Z size word_dropout=word_dropout, feed_latent=feed_latent ) # whether to feed latent in each decoder timestep or just use it in initialization rnn.cuda() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(rnn.parameters(), lr=lr) # Training loop stats = defaultdict(list) avg_loss = 0. avg_rec = 0. avg_kl = 0. avg_acc = 0. avg_elbo = 0. nb_updates = 0 print('Start training!') for epoch in range(nb_epochs): np.random.shuffle(corpus) for i in range(0, len(corpus), batch_size): rnn.zero_grad() X = doc.transform(corpus[i:i + batch_size]) # if a column values are all the padding character for # for all examples, remove it. # because if this is not done, it causes problems beucause of the way # 'lengths' is used, the timestep shape of the output of the RNN # is determined by the maximum length in the batch. # if the a col is zero, the timestep shape of the output the RNN # will not be the same than the timestep shape of the input. cols = (X.sum(axis=0) != 0) X = X[:, cols] # extract the length of each sentence and sort the sentences # by descending order of the length lengths = (X != 0).sum(axis=1) indices = np.argsort(lengths)[::-1] lengths = lengths[indices] lengths = lengths - 1 # because inp and out have one character less than X lengths = lengths.tolist() X = X[indices] X = torch.from_numpy(X) # The input is : w1 w2...w_{n-1}, the target is w2 w3...w_n # Note that w1 = 'begin' chracter, w_n = 'end' character inp = X[:, 0:-1] assert inp.size(1) == np.max(lengths) inp = Variable(inp) inp = inp.cuda() unmasked_target = X[:, 1:].contiguous() unmasked_target = Variable(unmasked_target) unmasked_target = unmasked_target.cuda() # Pass the input through the rnn autoencoder unmasked_out, (latent_mean, latent_log_var, latent) = rnn(inp, lengths) assert unmasked_out.size(0) == inp.size(0) * inp.size(1), ( unmasked_out.size(), inp.size()) mask = (inp != 0).view(-1) ind = torch.range(0, mask.size(0) - 1).long() ind = Variable(ind) ind = ind.cuda() assert ind.size() == mask.size() ind = torch.masked_select(ind, mask) assert ind.max() < (unmasked_out.size(0) - 1) out = torch.index_select(unmasked_out, 0, ind) target = unmasked_target.view(-1) target = torch.index_select(target, 0, ind) acc = get_acc(out, target) kl = (-0.5 * (1 + latent_log_var - latent_mean**2 - torch.exp(latent_log_var))).sum(1).mean() rec = criterion(out, target) kl_weight = min(nb_updates / 1000., 1.) #loss = rec + kl_weight * (kl if kl.data[0]>10 else 0) #loss = rec + kl loss = rec + kl_weight * kl elbo = rec + kl loss.backward() nn.utils.clip_grad_norm(rnn.parameters(), 2) optimizer.step() stats['acc'].append(acc.data[0]) stats['loss'].append(loss.data[0]) stats['rec'].append(rec.data[0]) stats['kl'].append(kl.data[0]) avg_kl = 0.9 * avg_kl + 0.1 * kl.data[0] avg_rec = 0.9 * avg_rec + 0.1 * rec.data[0] avg_acc = 0.9 * avg_acc + 0.1 * acc.data[0] avg_loss = 0.9 * avg_loss + 0.1 * loss.data[0] avg_elbo = 0.9 * avg_elbo + 0.1 * elbo.data[0] if nb_updates % 100 == 0: print( 'Epoch {:03d}, [{:05d}/{:05d}], Loss : {:.3f}, acc : {:.3f}, kl : {:.3f}, recons : {:.3f}, elbo : {:.3f}' .format(epoch, i + len(X), len(corpus), avg_loss, avg_acc, avg_kl, avg_rec, avg_elbo)) print('kl weight : {}'.format(kl_weight)) viz.updateTrace(X=np.array([nb_updates]), Y=np.array([avg_acc]), win=win, name='acc') viz.updateTrace(X=np.array([nb_updates]), Y=np.array([avg_kl]), win=win, name='kl') viz.updateTrace(X=np.array([nb_updates]), Y=np.array([avg_rec]), win=win, name='rec') viz.updateTrace(X=np.array([nb_updates]), Y=np.array([avg_elbo]), win=win, name='elbo') viz.updateTrace(X=np.array([nb_updates]), Y=np.array([avg_loss]), win=win, name='loss') pd.DataFrame(stats).to_csv('{}/stats.csv'.format(folder)) pred = unmasked_out.view(inp.size(0), inp.size(1), out.size(1)) pred = pred.max(2)[1][:, :, 0] pred = pred.data.cpu().numpy() pred[pred == UNK_CHAR] = 0 true = unmasked_target true = true.data.cpu().numpy() true = doc.inverse_transform(true) pred = doc.inverse_transform(pred) print('#### Predictions') for p, t in zip(pred[0:2], true[0:2]): t = to_str(t) p = to_str(p) print('True : "{}"'.format(t)) print('Pred : "{}"'.format(p)) print('_____') print('### Interpolation') alpha = torch.linspace(0, 1, 10).view(-1, 1).repeat( 1, latent_mean.size(1)) alpha = Variable(alpha).cuda() l = latent_mean[0:1].repeat( alpha.size(0), 1) * alpha + latent_mean[1:2].repeat( alpha.size(0), 1) * (1 - alpha) out = rnn.greedy_generate(l, length) out = out.numpy() out = doc.inverse_transform(out) for o in out: o = to_str(o) print('{}'.format(o)) print('### Sampling') l = latent_mean[0:1].repeat(10, 1) out = rnn.greedy_generate(l, length, deterministic=False) out = out.numpy() out = doc.inverse_transform(out) for o in out: o = to_str(o) print('Gen : {}'.format(o)) nb_updates += 1 torch.save(rnn, '{}/model.th'.format(folder))
class VisdomLogger(Logger): """ Logger that uses visdom to create learning curves Parameters: =========== - env: str, name of the visdom environment - log_checkpoints: bool, whether to use checkpoints or epoch averages for training loss - legend: tuple, names of the different losses that will be plotted. """ def __init__(self, env=None, log_checkpoints=True, losses=('loss', ), phases=('train', 'valid'), server='http://localhost', port=8097, max_y=None, **opts): self.viz = None if Visdom is not None: self.viz = Visdom(server=server, port=port, env=env) self.legend = ['%s.%s' % (p, l) for p in phases for l in losses] opts.update({'legend': self.legend}) self.opts = opts self.env = env self.max_y = max_y self.log_checkpoints = log_checkpoints self.losses = set(losses) self.last = {p: {l: None for l in losses} for p in phases} self.pane = self._init_pane() @skip_on_import_error(Visdom) def _init_pane(self): nan = np.array([np.NAN, np.NAN]) X = np.column_stack([nan] * len(self.legend)) Y = np.column_stack([nan] * len(self.legend)) return self.viz.line( X=X, Y=Y, env=self.env, opts=self.opts) def _update_last(self, epoch, loss, phase, loss_label): self.last[phase][loss_label] = {'X': epoch, 'Y': loss} def _plot_line(self, X, Y, phase, loss_label): name = "%s.%s" % (phase, loss_label) X = np.array([self.last[phase][loss_label]['X'], X]) Y = np.array([self.last[phase][loss_label]['Y'], Y]) if self.max_y: Y = np.clip(Y, Y.min(), self.max_y) self.viz.updateTrace( X=X, Y=Y, name=name, append=True, win=self.pane, env=self.env) def _plot_payload(self, epoch, losses, phase): for label, loss in losses.items(): if label not in self.losses: continue if self.last[phase][label] is not None: self._plot_line(epoch, loss, phase=phase, loss_label=label) self._update_last(epoch, loss, phase, label) @skip_on_import_error(Visdom) def epoch_end(self, payload): if self.log_checkpoints: # only use epoch end if checkpoint isn't being used return losses, epoch = payload['loss'], payload['epoch'] + 1 self._plot_payload(epoch, losses, 'train') @skip_on_import_error(Visdom) def validation_end(self, payload): losses, epoch = payload['loss'], payload['epoch'] + 1 self._plot_payload(epoch, losses, 'valid') @skip_on_import_error(Visdom) def checkpoint(self, payload): epoch = payload['epoch'] + payload["batch"] / payload["total_batches"] losses = payload['loss'] self._plot_payload(epoch, losses, 'train') @skip_on_import_error(Visdom) def attention(self, payload): title = "epoch {epoch}/ batch {batch_num}".format(**payload) if 'title' in self.opts: title = self.opts['title'] + ": " + title self.viz.heatmap( X=np.array(payload["att"]), env=self.env, opts={'rownames': payload["hyp"], 'columnnames': payload["target"], 'title': title})
opts=dict( markersize=10, markercolor=np.random.randint(0, 255, ( 255, 3, )), ), ) # assert that the window exists assert viz.win_exists(win) # add new trace to scatter plot viz.updateTrace( X=np.random.rand(255), Y=np.random.rand(255), win=win, name='new_trace', ) # bar plots viz.bar(X=np.random.rand(20)) viz.bar(X=np.abs(np.random.rand(5, 3)), opts=dict(stacked=True, legend=['Facebook', 'Google', 'Twitter'], rownames=['2012', '2013', '2014', '2015', '2016'])) viz.bar(X=np.random.rand(20, 3), opts=dict(stacked=False, legend=['The Netherlands', 'France', 'United States'])) # histogram viz.histogram(X=np.random.rand(10000), opts=dict(numbins=20))
def main(*, imageSize=32, dirs='.', batchSize=128, nThreads=1, niter=100000, lr=0.0002, beta1=0.5, ncols=3, outf='samples'): if not os.path.exists(outf): os.mkdir(outf) viz = Visdom('http://romeo163') win = viz.line(X=np.array([0]), Y=np.array([0]), opts=dict(title='any2any, started {}'.format(datetime.now()))) viz.line(X=np.array([0]), Y=np.array([0]), update='append', win=win) transform = transforms.Compose([ transforms.Scale(imageSize), transforms.CenterCrop(imageSize), transforms.ToTensor(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), ]) dataloaders = [] for dirname in dirs.split(','): dataset = datasets.ImageFolder(dirname, transform) dataloader = torch.utils.data.DataLoader( dataset, batch_size=batchSize, shuffle=True, num_workers=nThreads) dataloaders.append(dataloader) nb_minibatches = min(map(len, dataloaders)) code_size = 128 netG = GenDisco(nz=ncols+code_size, no=ncols) netG.apply(weights_init) netD = Discr(nc=ncols+code_size, imagesize=imageSize) netD.apply(weights_init) netE_G = Discr(nc=ncols, no=code_size, imagesize=imageSize) netE_G.apply(weights_init) netE_D = Discr(nc=ncols, no=code_size, imagesize=imageSize) netE_D.apply(weights_init) input = torch.FloatTensor(batchSize, ncols, imageSize, imageSize) input = Variable(input) target = torch.FloatTensor(batchSize, ncols, imageSize, imageSize) target = Variable(target) label = torch.FloatTensor(batchSize) label = Variable(label) real_label = 1 fake_label = 0 criterion = nn.BCELoss() #criterion = nn.MSELoss() criterion = criterion.cuda() def wsgan(output, label): label = label * 2 - 1 return torch.mean(output * label) #criterion = wsgan netE_G = netE_G.cuda() netE_D = netE_D.cuda() netG = netG.cuda() netD = netD.cuda() input = input.cuda() target = target.cuda() label = label.cuda() optimizerD = optim.Adam(netD.parameters(), lr=lr, betas = (beta1, 0.999)) optimizerG = optim.Adam(netG.parameters(), lr=lr, betas = (beta1, 0.999)) optimizerE_G = optim.Adam(netE_G.parameters(), lr=lr, betas = (beta1, 0.999)) optimizerE_D = optim.Adam(netE_D.parameters(), lr=lr, betas = (beta1, 0.999)) stats = defaultdict(list) j = 0 for epoch in range(niter): dataiters = [iter(loader) for loader in dataloaders] for i in range(nb_minibatches): if len(dataloaders) == 1: c1 = 0 c2 = 0 else: c1 = np.random.randint(0, len(dataloaders) - 1) c2 = np.random.randint(c1 + 1, len(dataloaders)) source_data = next(dataiters[c1]) target_data = next(dataiters[c2]) t = time.time() netD.zero_grad() netE_D.zero_grad() source_images, _ = source_data target_images, _ = target_data if source_images.size() != target_images.size(): continue #source_images.uniform_(-1, 1) if ncols == 1: source_images = source_images[:, 0:1] target_images = target_images[:, 0:1] errD = 0. input.data.resize_(source_images.size()).copy_(source_images) target.data.resize_(target_images.size()).copy_(target_images) batch_size = source_images.size(0) errD_vals = [] for X, Y in (input, target), (target, input): X_ = X.detach() Y_ = Y.detach() label.data.resize_(batch_size).fill_(real_label) code_g = netE_G(Y_) code_g = code_g.repeat(1, 1, imageSize, imageSize) code_g = code_g.mean(0).repeat(input.size(0), 1, 1, 1) code_d = netE_D(Y_) code_d = code_d.repeat(1, 1, imageSize, imageSize) code_d = code_d.mean(0).repeat(input.size(0), 1, 1, 1) target_and_code = torch.cat((Y_, code_d), 1) output = netD(target_and_code) output = output.view(-1, 1) errD_real = criterion(output, label) errD_real.backward() # train with fake input_and_code = torch.cat((X_, code_g), 1) fake = netG(input_and_code) label.data.fill_(fake_label) fake_and_code = torch.cat((fake, code_d), 1) output = netD(fake_and_code.detach()) output = output.view(-1, 1) errD_fake = criterion(output, label) errD_fake.backward() errD_vals += [errD_real.data[0], errD_fake.data[0]] errD += errD_real + errD_fake optimizerD.step() optimizerE_D.step() netG.zero_grad() netE_G.zero_grad() label.data.fill_(real_label) # fake labels are real for generator cost errG = 0. fakes = [] for X, Y in (input, target), (target, input): X_ = X.detach() Y_ = Y.detach() code_g = netE_G(Y_) code_g = code_g.repeat(1, 1, imageSize, imageSize) code_g = code_g.mean(0).repeat(input.size(0), 1, 1, 1) code_d = netE_D(Y_) code_d = code_d.repeat(1, 1, imageSize, imageSize) code_d = code_d.mean(0).repeat(input.size(0), 1, 1, 1) input_and_code = torch.cat((X_, code_g), 1) fake = netG(input_and_code) fakes.append(fake.data) src_code_g = netE_G(X_) src_code_g = src_code_g.repeat(1, 1, imageSize, imageSize) src_code_g = src_code_g.mean(0).repeat(input.size(0), 1, 1, 1) fake_and_src_code = torch.cat((fake, src_code_g), 1) rec = netG(fake_and_src_code) fake_and_code = torch.cat((fake, code_d), 1) output = netD(fake_and_code) output = output.view(-1, 1) gan_loss = criterion(output, label) rec_loss = 0.01 * ((rec - X_)**2).sum() / rec.size(0) loss = gan_loss + rec_loss loss.backward() errG += loss + rec_loss optimizerG.step() optimizerE_G.step() delta_t = time.time() - t stats['iter'].append(j) stats['errG'].append(errG.data[0]) stats['errD'].append(errD.data[0]) stats['rec'].append(rec_loss.data[0]) print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f, time : %.4f' % (epoch, niter, i, nb_minibatches, errD.data[0], errG.data[0], delta_t)) viz.updateTrace(X=np.array([stats['iter'][-1]]), Y=np.array([stats['errG'][-1]]), win=win, name='errG') viz.updateTrace(X=np.array([stats['iter'][-1]]), Y=np.array([stats['rec'][-1]]), win=win, name='rec') viz.updateTrace(X=np.array([stats['iter'][-1]]), Y=np.array([stats['errD'][-1]]), win=win, name='errD') if j % 10 == 0: pd.DataFrame(stats).to_csv('{}/stats.csv'.format(outf), index=False) # the first 64 samples from the mini-batch are saved. folder = '{}/cl_{}_{}'.format(outf, c1, c2) if not os.path.exists(folder): os.mkdir(folder) vutils.save_image((source_images[0:64,:,:,:]+1)/2., os.path.join(folder, 'source.png'), nrow=8) vutils.save_image((target_images[0:64,:,:,:]+1)/2., os.path.join(folder, 'target.png'), nrow=8) for name, f in (('source', fakes[0]), ('target', fakes[1])): img_folder = os.path.join(folder, name) if not os.path.exists(img_folder): os.mkdir(img_folder) vutils.save_image((f[0:64,:,:,:]+1)/2., '%s/epoch_%03d.png' % (img_folder, epoch), nrow=8) j += 1 # do checkpointing torch.save(netG.state_dict(), '%s/netG.pth' % (outf)) torch.save(netD.state_dict(), '%s/netD.pth' % (outf)) torch.save(netE_G.state_dict(), '%s/netE_G.pth' % (outf)) torch.save(netE_D.state_dict(), '%s/netE_D.pth' % (outf))
class Callback(object): """ Used to log/visualize the evaluation metrics during training. The values are stored at the end of each epoch. """ def __init__(self, metrics): """ Args: metrics : a list of callbacks. Possible values: "CoherenceMetric" "PerplexityMetric" "DiffMetric" "ConvergenceMetric" """ # list of metrics to be plot self.metrics = metrics def set_model(self, model): """ Save the model instance and initialize any required variables which would be updated throughout training """ self.model = model self.previous = None # check for any metric which need model state from previous epoch if any(isinstance(metric, (DiffMetric, ConvergenceMetric)) for metric in self.metrics): self.previous = copy.deepcopy(model) # store diff diagonals of previous epochs self.diff_mat = Queue() if any(metric.logger == "visdom" for metric in self.metrics): if not VISDOM_INSTALLED: raise ImportError("Please install Visdom for visualization") self.viz = Visdom() # store initial plot windows of every metric (same window will be updated with increasing epochs) self.windows = [] if any(metric.logger == "shell" for metric in self.metrics): # set logger for current topic model self.log_type = logging.getLogger('gensim.models.ldamodel') def on_epoch_end(self, epoch, topics=None): """ Log or visualize current epoch's metric value Args: epoch : current epoch no. topics : topic distribution from current epoch (required for coherence of unsupported topic models) """ # stores current epoch's metric values current_metrics = {} # plot all metrics in current epoch for i, metric in enumerate(self.metrics): label = str(metric) value = metric.get_value(topics=topics, model=self.model, other_model=self.previous) current_metrics[label] = value if metric.logger == "visdom": if epoch == 0: if value.ndim > 0: diff_mat = np.array([value]) viz_metric = self.viz.heatmap( X=diff_mat.T, env=metric.viz_env, opts=dict(xlabel='Epochs', ylabel=label, title=label) ) # store current epoch's diff diagonal self.diff_mat.put(diff_mat) # saving initial plot window self.windows.append(copy.deepcopy(viz_metric)) else: viz_metric = self.viz.line( Y=np.array([value]), X=np.array([epoch]), env=metric.viz_env, opts=dict(xlabel='Epochs', ylabel=label, title=label) ) # saving initial plot window self.windows.append(copy.deepcopy(viz_metric)) else: if value.ndim > 0: # concatenate with previous epoch's diff diagonals diff_mat = np.concatenate((self.diff_mat.get(), np.array([value]))) self.viz.heatmap( X=diff_mat.T, env=metric.viz_env, win=self.windows[i], opts=dict(xlabel='Epochs', ylabel=label, title=label) ) self.diff_mat.put(diff_mat) else: self.viz.updateTrace( Y=np.array([value]), X=np.array([epoch]), env=metric.viz_env, win=self.windows[i] ) if metric.logger == "shell": statement = "".join(("Epoch ", str(epoch), ": ", label, " estimate: ", str(value))) self.log_type.info(statement) # check for any metric which need model state from previous epoch if isinstance(metric, (DiffMetric, ConvergenceMetric)): self.previous = copy.deepcopy(self.model) return current_metrics
def main(): start_time = time.clock() lr = 0.0000006 # 学习率 iteration = 10000 # 迭代次数 bias = 0.5 # 初始化bias testNum = 30 # 测试集数量 viz = Visdom(env='x1+x2') trainingData = np.random.uniform(-100, 100, (30, 1, 2)) testData = np.random.uniform(-500, 500, (testNum, 1, 2)) rightResult = testData[:, :, 0] + testData[:, :, 1] rightDot = np.append(testData[:, 0, :], rightResult, 1) neuralNetwork = [] inputLayer = InputLayer(len(trainingData[0, 0])) h1 = NeuroLayer(5, inputLayer, bias) neuralNetwork.append(h1) a1 = PRelu(h1) neuralNetwork.append(a1) outputLayer = NeuroLayer(1, a1, bias) outputActionLayer = PRelu(outputLayer) errorLayer = ErrorLayer(outputActionLayer) neuralNetwork.append(outputLayer) neuralNetwork.append(outputActionLayer) neuralNetwork.append(errorLayer) init_weight = h1.weight.copy() for itr in range(1, iteration): np.random.shuffle(trainingData) last_error = 0 for d in trainingData: inputLayer.data = d errorLayer.target = [d[0, 0] + d[0, 1]] for layer in neuralNetwork: layer.forward() for layer in reversed(neuralNetwork): layer.backward() last_error += errorLayer.data for layer in neuralNetwork: layer.update(lr) if(100 > itr > 19 or itr % 20 == 0): if(itr == 20): win = viz.line( X=np.array([itr]), Y=np.array(last_error[0] / len(trainingData)), name="x1+x2", win='loss' ) win2 = viz.scatter( X=np.random.rand(1, 2), name="x1+x2 dot", win='fitting', ) viz.updateTrace( X=np.array([itr]), Y=np.array(last_error[0] / len(trainingData)), win=win, ) testResult = np.empty([testNum, 1]) for i in range(len(testData)): inputLayer.data = testData[i] for layer in neuralNetwork[:-1]: layer.forward() testResult[i][0] = outputActionLayer.data[0][0] testDot = np.append(testData[:, 0, :], testResult, 1) dot = np.append(rightDot, testDot, 0) viz.scatter( X=dot, name="x1+x2 dot", win=win2, Y=[1] * testNum + [2] * testNum, opts=dict( legend=['right', 'test'], markersize=5, ) ) print('=================================') print('last_error', last_error) elapsed_time = time.clock() - start_time print("all time", elapsed_time) print('=================================') print('init weight', init_weight) print('last weight', h1.weight) print('=================================') last_error = 0 for d in testData: inputLayer.data = d errorLayer.target = [d[0, 0] + d[0, 1]] for layer in neuralNetwork: layer.forward() last_error += errorLayer.data print('_______________') print(d, [d[0, 0] + d[0, 1]]) print("output", outputActionLayer.data) print("error", errorLayer.data) print('_______________') print('last_error', last_error / len(testData))
opts=dict( xtickmin=-10, xtickmax=10, xtickstep=1, ytickmin=0, ytickmax=500, ytickstep=1, markersymbol='dot', markercolor=np.random.randint(0, 255, num_data), markersize=5, ), ) viz.updateTrace( X=x, Y=y, win=win, ) # fully connected model with 5 hidden layer model = nn.Sequential( nn.Linear(1, 20), nn.ReLU(), nn.Linear(20, 10), nn.ReLU(), nn.Linear(10, 5), nn.ReLU(), nn.Linear(5, 1), ).cuda()
class Callback(object): """A class representing routines called reactively at specific phases during trained. These can be used to log or visualize the training progress using any of the metric scores developed before. The values are stored at the end of each training epoch. The following metric scores are currently available: * :class:`~gensim.models.callbacks.CoherenceMetric` * :class:`~gensim.models.callbacks.PerplexityMetric` * :class:`~gensim.models.callbacks.DiffMetric` * :class:`~gensim.models.callbacks.ConvergenceMetric` """ def __init__(self, metrics): """ Parameters ---------- metrics : list of :class:`~gensim.models.callbacks.Metric` The list of metrics to be reported by the callback. """ self.metrics = metrics def set_model(self, model): """Save the model instance and initialize any required variables which would be updated throughout training. Parameters ---------- model : :class:`~gensim.models.basemodel.BaseTopicModel` The model for which the training will be reported (logged or visualized) by the callback. """ self.model = model self.previous = None # check for any metric which need model state from previous epoch if any(isinstance(metric, (DiffMetric, ConvergenceMetric)) for metric in self.metrics): self.previous = copy.deepcopy(model) # store diff diagonals of previous epochs self.diff_mat = Queue() if any(metric.logger == "visdom" for metric in self.metrics): if not VISDOM_INSTALLED: raise ImportError("Please install Visdom for visualization") self.viz = Visdom() # store initial plot windows of every metric (same window will be updated with increasing epochs) self.windows = [] if any(metric.logger == "shell" for metric in self.metrics): # set logger for current topic model self.log_type = logging.getLogger('gensim.models.ldamodel') def on_epoch_end(self, epoch, topics=None): """Report the current epoch's metric value. Called at the end of each training iteration. Parameters ---------- epoch : int The epoch that just ended. topics : list of list of str, optional List of tokenized topics. This is required for the coherence metric. Returns ------- dict of (str, object) Mapping from metric names to their values. The type of each value depends on the metric type, for example :class:`~gensim.models.callbacks.DiffMetric` computes a matrix while :class:`~gensim.models.callbacks.ConvergenceMetric` computes a float. """ # stores current epoch's metric values current_metrics = {} # plot all metrics in current epoch for i, metric in enumerate(self.metrics): label = str(metric) value = metric.get_value(topics=topics, model=self.model, other_model=self.previous) current_metrics[label] = value if metric.logger == "visdom": if epoch == 0: if value.ndim > 0: diff_mat = np.array([value]) viz_metric = self.viz.heatmap( X=diff_mat.T, env=metric.viz_env, opts=dict(xlabel='Epochs', ylabel=label, title=label) ) # store current epoch's diff diagonal self.diff_mat.put(diff_mat) # saving initial plot window self.windows.append(copy.deepcopy(viz_metric)) else: viz_metric = self.viz.line( Y=np.array([value]), X=np.array([epoch]), env=metric.viz_env, opts=dict(xlabel='Epochs', ylabel=label, title=label) ) # saving initial plot window self.windows.append(copy.deepcopy(viz_metric)) else: if value.ndim > 0: # concatenate with previous epoch's diff diagonals diff_mat = np.concatenate((self.diff_mat.get(), np.array([value]))) self.viz.heatmap( X=diff_mat.T, env=metric.viz_env, win=self.windows[i], opts=dict(xlabel='Epochs', ylabel=label, title=label) ) self.diff_mat.put(diff_mat) else: self.viz.updateTrace( Y=np.array([value]), X=np.array([epoch]), env=metric.viz_env, win=self.windows[i] ) if metric.logger == "shell": statement = "".join(("Epoch ", str(epoch), ": ", label, " estimate: ", str(value))) self.log_type.info(statement) # check for any metric which need model state from previous epoch if isinstance(metric, (DiffMetric, ConvergenceMetric)): self.previous = copy.deepcopy(self.model) return current_metrics