from pg import*
from global_vars import*
from pg_model import*
from os import mkdir
from beepy import beep
import matplotlib.pyplot as plt
import os
from utils import directory_init, loss_reward_plot, make_plots
from utility import time_str
from shutil import copyfile, rmtree
from sys import argv
import sys
from traceback import print_exc
from glob import glob
from random import choice
from time import perf_counter
import tensorboard

'''Main script for running Policy Gradient Training Algorithm in Tensorflow Graph.'''

scaling = False #Scale loss/reward between files

logdir = 'tb_logs'

t_str = time_str(sec=True, year=False, isPath=True)
save_path, time_plot_path, write_path, cost_path, gr_path, log_dir = directory_init(t_str) #Create directories
copyfile('global_vars.py', log_dir + '/Parameters_PG') #Log parameter settings
train_dir = '../Training Sets/RL Tracks/*.wav' #Directory of training files
train_files = glob(train_dir) #Collect all file names
read_path = '../Training Sets/RL Tracks/rl2.wav' #Use specific training file

if compState: #Select scalar
        scalar_path = 'scalar_pickle_comp'
else:
    scalar_path = 'scalar_pickle'

model = PGModel(history_neurons, lookahead_neurons) #Build new model
#model.load('Logs/Wed Mar 24 13_40_04/Checkpoints') #Load saved model

opt = tf.keras.optimizers.Adam(learning_rate=lr)

if scaling:
    reward_scaling = {}
    loss_scaling = {}
    for f in train_files: #Create per-file scaling
        reward, loss = train_step(model, f, write_path, time_plot_path, cost_path, gr_path, lr, 0) #Run one training step
        reward_scaling[f] = 1/reward
        loss_scaling[f] = 1/loss
else:
    reward_scaling = dict.fromkeys(train_files, tf.constant(1.0, dtype='float32'))
    loss_scaling = dict.fromkeys(train_files, tf.constant(1.0, dtype='float32'))

reward_history = []
loss_history = []

try:
    for epoch in range(epochs):
        
        start = perf_counter()
        audio = AudioBuffer(read_path, frame_len, lookahead_frames) #Buffer to read/write audio
        comp = Compressor(thr, ratio) #Compressor
        feature = EnvelopeFinder(history_frames, lookahead_frames, 
        frame_len, history_neurons, lookahead_neurons, Fs, scalar_path) #Envelope finder to extract features

        #writer = tf.summary.create_file_writer(logdir)
        #tf.summary.trace_on(graph=True, profiler=True)
        episode_reward, episode_loss, plot_data = train_step_tf(model, audio, comp, feature, opt, gamma) #Run one training step
        #with writer.as_default():
            #tf.summary.trace_export(name="my_func_trace", step=0, profiler_outdir=logdir)

        make_plots(comp, time_plot_path, cost_path, gr_path, epoch, *plot_data)
        episode_reward = episode_reward * reward_scaling[read_path] #Normalize reward
        episode_loss = episode_loss * loss_scaling[read_path] #Normalize loss
        reward_history.append(episode_reward)
        loss_history.append(episode_loss)
        model.save(save_path)
        loss_reward_plot(loss_history, reward_history, t_str)
        end = perf_counter()
        print("Epoch {} completed: Time {:.2f} s ........... Reward: {:.4f} ........... Loss: {:.2f}".format(epoch+1, end-start, episode_reward, episode_loss))

    beep(1)

except Exception as E:
    beep(1)
    print_exc(file=sys.stdout)
    sys.exit()

finally:
    print('Terminating')
    if len(argv)>1 and argv[1] == 'NOLOG':
        rmtree(log_dir)


14eb6dd9c6edb052f2446bf88ac546b119e9ea8b
<mask token>

<mask token>
copyfile('global_vars.py', log_dir + '/Parameters_PG')
<mask token>
if compState:
    scalar_path = 'scalar_pickle_comp'
else:
    scalar_path = 'scalar_pickle'
<mask token>
if scaling:
    reward_scaling = {}
    loss_scaling = {}
    for f in train_files:
        reward, loss = train_step(model, f, write_path, time_plot_path,
            cost_path, gr_path, lr, 0)
        reward_scaling[f] = 1 / reward
        loss_scaling[f] = 1 / loss
else:
    reward_scaling = dict.fromkeys(train_files, tf.constant(1.0, dtype=
        'float32'))
    loss_scaling = dict.fromkeys(train_files, tf.constant(1.0, dtype='float32')
        )
<mask token>
try:
    for epoch in range(epochs):
        start = perf_counter()
        audio = AudioBuffer(read_path, frame_len, lookahead_frames)
        comp = Compressor(thr, ratio)
        feature = EnvelopeFinder(history_frames, lookahead_frames,
            frame_len, history_neurons, lookahead_neurons, Fs, scalar_path)
        episode_reward, episode_loss, plot_data = train_step_tf(model,
            audio, comp, feature, opt, gamma)
        make_plots(comp, time_plot_path, cost_path, gr_path, epoch, *plot_data)
        episode_reward = episode_reward * reward_scaling[read_path]
        episode_loss = episode_loss * loss_scaling[read_path]
        reward_history.append(episode_reward)
        loss_history.append(episode_loss)
        model.save(save_path)
        loss_reward_plot(loss_history, reward_history, t_str)
        end = perf_counter()
        print(
            'Epoch {} completed: Time {:.2f} s ........... Reward: {:.4f} ........... Loss: {:.2f}'
            .format(epoch + 1, end - start, episode_reward, episode_loss))
    beep(1)
except Exception as E:
    beep(1)
    print_exc(file=sys.stdout)
    sys.exit()
finally:
    print('Terminating')
    if len(argv) > 1 and argv[1] == 'NOLOG':
        rmtree(log_dir)

<mask token>
scaling = False
logdir = 'tb_logs'
t_str = time_str(sec=True, year=False, isPath=True)
save_path, time_plot_path, write_path, cost_path, gr_path, log_dir = (
    directory_init(t_str))
copyfile('global_vars.py', log_dir + '/Parameters_PG')
train_dir = '../Training Sets/RL Tracks/*.wav'
train_files = glob(train_dir)
read_path = '../Training Sets/RL Tracks/rl2.wav'
if compState:
    scalar_path = 'scalar_pickle_comp'
else:
    scalar_path = 'scalar_pickle'
model = PGModel(history_neurons, lookahead_neurons)
opt = tf.keras.optimizers.Adam(learning_rate=lr)
if scaling:
    reward_scaling = {}
    loss_scaling = {}
    for f in train_files:
        reward, loss = train_step(model, f, write_path, time_plot_path,
            cost_path, gr_path, lr, 0)
        reward_scaling[f] = 1 / reward
        loss_scaling[f] = 1 / loss
else:
    reward_scaling = dict.fromkeys(train_files, tf.constant(1.0, dtype=
        'float32'))
    loss_scaling = dict.fromkeys(train_files, tf.constant(1.0, dtype='float32')
        )
reward_history = []
loss_history = []
try:
    for epoch in range(epochs):
        start = perf_counter()
        audio = AudioBuffer(read_path, frame_len, lookahead_frames)
        comp = Compressor(thr, ratio)
        feature = EnvelopeFinder(history_frames, lookahead_frames,
            frame_len, history_neurons, lookahead_neurons, Fs, scalar_path)
        episode_reward, episode_loss, plot_data = train_step_tf(model,
            audio, comp, feature, opt, gamma)
        make_plots(comp, time_plot_path, cost_path, gr_path, epoch, *plot_data)
        episode_reward = episode_reward * reward_scaling[read_path]
        episode_loss = episode_loss * loss_scaling[read_path]
        reward_history.append(episode_reward)
        loss_history.append(episode_loss)
        model.save(save_path)
        loss_reward_plot(loss_history, reward_history, t_str)
        end = perf_counter()
        print(
            'Epoch {} completed: Time {:.2f} s ........... Reward: {:.4f} ........... Loss: {:.2f}'
            .format(epoch + 1, end - start, episode_reward, episode_loss))
    beep(1)
except Exception as E:
    beep(1)
    print_exc(file=sys.stdout)
    sys.exit()
finally:
    print('Terminating')
    if len(argv) > 1 and argv[1] == 'NOLOG':
        rmtree(log_dir)

from pg import *
from global_vars import *
from pg_model import *
from os import mkdir
from beepy import beep
import matplotlib.pyplot as plt
import os
from utils import directory_init, loss_reward_plot, make_plots
from utility import time_str
from shutil import copyfile, rmtree
from sys import argv
import sys
from traceback import print_exc
from glob import glob
from random import choice
from time import perf_counter
import tensorboard
<mask token>
scaling = False
logdir = 'tb_logs'
t_str = time_str(sec=True, year=False, isPath=True)
save_path, time_plot_path, write_path, cost_path, gr_path, log_dir = (
    directory_init(t_str))
copyfile('global_vars.py', log_dir + '/Parameters_PG')
train_dir = '../Training Sets/RL Tracks/*.wav'
train_files = glob(train_dir)
read_path = '../Training Sets/RL Tracks/rl2.wav'
if compState:
    scalar_path = 'scalar_pickle_comp'
else:
    scalar_path = 'scalar_pickle'
model = PGModel(history_neurons, lookahead_neurons)
opt = tf.keras.optimizers.Adam(learning_rate=lr)
if scaling:
    reward_scaling = {}
    loss_scaling = {}
    for f in train_files:
        reward, loss = train_step(model, f, write_path, time_plot_path,
            cost_path, gr_path, lr, 0)
        reward_scaling[f] = 1 / reward
        loss_scaling[f] = 1 / loss
else:
    reward_scaling = dict.fromkeys(train_files, tf.constant(1.0, dtype=
        'float32'))
    loss_scaling = dict.fromkeys(train_files, tf.constant(1.0, dtype='float32')
        )
reward_history = []
loss_history = []
try:
    for epoch in range(epochs):
        start = perf_counter()
        audio = AudioBuffer(read_path, frame_len, lookahead_frames)
        comp = Compressor(thr, ratio)
        feature = EnvelopeFinder(history_frames, lookahead_frames,
            frame_len, history_neurons, lookahead_neurons, Fs, scalar_path)
        episode_reward, episode_loss, plot_data = train_step_tf(model,
            audio, comp, feature, opt, gamma)
        make_plots(comp, time_plot_path, cost_path, gr_path, epoch, *plot_data)
        episode_reward = episode_reward * reward_scaling[read_path]
        episode_loss = episode_loss * loss_scaling[read_path]
        reward_history.append(episode_reward)
        loss_history.append(episode_loss)
        model.save(save_path)
        loss_reward_plot(loss_history, reward_history, t_str)
        end = perf_counter()
        print(
            'Epoch {} completed: Time {:.2f} s ........... Reward: {:.4f} ........... Loss: {:.2f}'
            .format(epoch + 1, end - start, episode_reward, episode_loss))
    beep(1)
except Exception as E:
    beep(1)
    print_exc(file=sys.stdout)
    sys.exit()
finally:
    print('Terminating')
    if len(argv) > 1 and argv[1] == 'NOLOG':
        rmtree(log_dir)

from pg import*
from global_vars import*
from pg_model import*
from os import mkdir
from beepy import beep
import matplotlib.pyplot as plt
import os
from utils import directory_init, loss_reward_plot, make_plots
from utility import time_str
from shutil import copyfile, rmtree
from sys import argv
import sys
from traceback import print_exc
from glob import glob
from random import choice
from time import perf_counter
import tensorboard

'''Main script for running Policy Gradient Training Algorithm in Tensorflow Graph.'''

scaling = False #Scale loss/reward between files

logdir = 'tb_logs'

t_str = time_str(sec=True, year=False, isPath=True)
save_path, time_plot_path, write_path, cost_path, gr_path, log_dir = directory_init(t_str) #Create directories
copyfile('global_vars.py', log_dir + '/Parameters_PG') #Log parameter settings
train_dir = '../Training Sets/RL Tracks/*.wav' #Directory of training files
train_files = glob(train_dir) #Collect all file names
read_path = '../Training Sets/RL Tracks/rl2.wav' #Use specific training file

if compState: #Select scalar
        scalar_path = 'scalar_pickle_comp'
else:
    scalar_path = 'scalar_pickle'

model = PGModel(history_neurons, lookahead_neurons) #Build new model
#model.load('Logs/Wed Mar 24 13_40_04/Checkpoints') #Load saved model

opt = tf.keras.optimizers.Adam(learning_rate=lr)

if scaling:
    reward_scaling = {}
    loss_scaling = {}
    for f in train_files: #Create per-file scaling
        reward, loss = train_step(model, f, write_path, time_plot_path, cost_path, gr_path, lr, 0) #Run one training step
        reward_scaling[f] = 1/reward
        loss_scaling[f] = 1/loss
else:
    reward_scaling = dict.fromkeys(train_files, tf.constant(1.0, dtype='float32'))
    loss_scaling = dict.fromkeys(train_files, tf.constant(1.0, dtype='float32'))

reward_history = []
loss_history = []

try:
    for epoch in range(epochs):
        
        start = perf_counter()
        audio = AudioBuffer(read_path, frame_len, lookahead_frames) #Buffer to read/write audio
        comp = Compressor(thr, ratio) #Compressor
        feature = EnvelopeFinder(history_frames, lookahead_frames, 
        frame_len, history_neurons, lookahead_neurons, Fs, scalar_path) #Envelope finder to extract features

        #writer = tf.summary.create_file_writer(logdir)
        #tf.summary.trace_on(graph=True, profiler=True)
        episode_reward, episode_loss, plot_data = train_step_tf(model, audio, comp, feature, opt, gamma) #Run one training step
        #with writer.as_default():
            #tf.summary.trace_export(name="my_func_trace", step=0, profiler_outdir=logdir)

        make_plots(comp, time_plot_path, cost_path, gr_path, epoch, *plot_data)
        episode_reward = episode_reward * reward_scaling[read_path] #Normalize reward
        episode_loss = episode_loss * loss_scaling[read_path] #Normalize loss
        reward_history.append(episode_reward)
        loss_history.append(episode_loss)
        model.save(save_path)
        loss_reward_plot(loss_history, reward_history, t_str)
        end = perf_counter()
        print("Epoch {} completed: Time {:.2f} s ........... Reward: {:.4f} ........... Loss: {:.2f}".format(epoch+1, end-start, episode_reward, episode_loss))

    beep(1)

except Exception as E:
    beep(1)
    print_exc(file=sys.stdout)
    sys.exit()

finally:
    print('Terminating')
    if len(argv)>1 and argv[1] == 'NOLOG':
        rmtree(log_dir)

