Ejemplo n.º 1
0
    def __init__(self, name=None, autoconnect=None, **kwargs):
        Model.__init__(self)
        Base.__init__(self)
        self.metadata = Base.metadata

    	self.name = name
        self.autoconnect = autoconnect
 def __init__(self, train, column, choices=[], beta0=None, name='Enum model', bounds=None):
   Model.__init__(self, train)
   self.choices = choices
   self.column = column
   self.name = name
   self._beta0 = list(beta0)
   self._bounds = list(bounds)
Ejemplo n.º 3
0
def train(logdir1='logdir/default/train1', logdir2='logdir/default/train2', queue=True):
    model = Model(mode="train2", batch_size=hp.Train2.batch_size, queue=queue)

    # Loss
    loss_op = model.loss_net2()

    # Training Scheme
    global_step = tf.Variable(0, name='global_step', trainable=False)

    optimizer = tf.train.AdamOptimizer(learning_rate=hp.Train2.lr)
    with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
        var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'net/net2')
        train_op = optimizer.minimize(loss_op, global_step=global_step, var_list=var_list)

    # Summary
    summ_op = summaries(loss_op)

    session_conf = tf.ConfigProto(
        gpu_options=tf.GPUOptions(
            allow_growth=True,
            per_process_gpu_memory_fraction=0.6,
        ),
    )
    # Training
    with tf.Session(config=session_conf) as sess:
        # Load trained model
        sess.run(tf.global_variables_initializer())
        model.load(sess, mode='train2', logdir=logdir1, logdir2=logdir2)

        writer = tf.summary.FileWriter(logdir2, sess.graph)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        for epoch in range(1, hp.Train2.num_epochs + 1):
            for step in tqdm(range(model.num_batch), total=model.num_batch, ncols=70, leave=False, unit='b'):
                if queue:
                    sess.run(train_op)
                else:
                    mfcc, spec, mel = get_batch(model.mode, model.batch_size)
                    sess.run(train_op, feed_dict={model.x_mfcc: mfcc, model.y_spec: spec, model.y_mel: mel})

            # Write checkpoint files at every epoch
            summ, gs = sess.run([summ_op, global_step])

            if epoch % hp.Train2.save_per_epoch == 0:
                tf.train.Saver().save(sess, '{}/epoch_{}_step_{}'.format(logdir2, epoch, gs))

                # Eval at every n epochs
                with tf.Graph().as_default():
                    eval2.eval(logdir2, queue=False)

                # Convert at every n epochs
                with tf.Graph().as_default():
                    convert.convert(logdir2, queue=False)

            writer.add_summary(summ, global_step=gs)

        writer.close()
        coord.request_stop()
        coord.join(threads)
Ejemplo n.º 4
0
    def __init__(self, **kwargs):
        Model.__init__(self)
        Base.__init__(self)
        self.metadata = Base.metadata

        for name, val in kwargs.iteritems():
            self.__setattr__(name, val)
Ejemplo n.º 5
0
def get_friends_from_ml100k():
    dst_file = "./src/dataset/movie100k/ml100kfriend.dat"
    if path.isfile(dst_file):
        friend_data = pickle.load(open(dst_file, "rb"))
        return friend_data

    raw_data = get_moive100k(True);
    raw_model = Model(raw_data);
    cosine_sim = CosineSimilarity(raw_model)
    friend_data = {}

    for user_id in raw_model.get_user_ids():
        neighbors = cosine_sim.get_similarities(user_id)[:250]
        user_ids, x = zip(*neighbors)
        user_ids = list(user_ids)
        shuffle(user_ids)
        # note: 
        # Randomly choose 150 out of 250 neighbors as friends.
        # In such case, systems is able to (possiblly) choose strangers which 
        # are in top-250 similar users, but with a probability slightly
        # smaller than friends selection.
        friend_data[user_id] = user_ids[:150] 

    pickle.dump(friend_data,open(dst_file, "w"),protocol=2)
    return friend_data
Ejemplo n.º 6
0
    def tearDown(self):
        for m in (Model, ModelFK, ModelM2M):
            try:
                moderator.unregister(m)
            except:
                pass

        Model.add_to_class('objects', self.default_manager)
Ejemplo n.º 7
0
    def initialize_table(self):
        self.session.add_all([
            Server(network_id=1, address="irc.gamesurge.net", port=6667,
                   nickname="ppbot", alt_nickname="ppbot",
                   realname="Powered by Dong Energy"),
        ])

        Model.initialize_table(self)
Ejemplo n.º 8
0
    def __init__(self, token):
        self.token = token
        self._initialize()

        self.schedule(self.__cleanup, after=timedelta(minutes=1),
                      recurring=True)

        Model.bind_bot(self)
        Hook.bind_bot(self)
Ejemplo n.º 9
0
    def test_can_filter_model_after_register(self):
        moderator.register(Model)

        model = Model()
        model.save()
        self.assertTrue(model)

        models = Model.objects.unmoderated()
        self.assertIn(model, models)
Ejemplo n.º 10
0
    def test_can_create_model_after_register(self):
        moderator.register(Model)

        model = Model()
        model.save()
        self.assertTrue(model)

        model = Model.objects.create()
        self.assertTrue(model)
Ejemplo n.º 11
0
 def __init__(self, lower=[0.01, 50, 100], upper = [0.2, 150, 1000], algo_model=dtlz.dtlz1, algo_num_obj=1, algo_num_decs=1):
     Model.__init__(self)
     #define upper and lower bounds for dec variables: Mutation rate, no of candidates and no of gens
     self.name = 'Tuner Model'
     self.lower_bounds = lower
     self.upper_bounds = upper
     self.no_of_decisions = len(lower)
     self.algo_model = algo_model
     self.algo_num_obj = algo_num_obj
     self.algo_num_decs = algo_num_decs
Ejemplo n.º 12
0
def modelsPage():
    page = Model(dsn = app.config['dsn'])
    if request.method == 'GET':
        return page.list()

    elif 'addModel' in request.form:
        name = request.form['name']
        rider = request.form['rider']
        constructor = request.form['constructor']
        return page.addModel(name, rider, constructor)
    elif 'dbynameModel' in request.form:
        name = request.form['name']
        return page.deletebyName(name)
    elif 'dbyidModel' in request.form:
        ID = request.form['ID']
        return page.deletebyId(ID)
    elif 'updateModel' in request.form:
        ID = request.form['ID']
        name = request.form['name']
        rider = request.form['rider']
        constructor = request.form['constructor']
        return page.update(ID,name, rider, constructor)
    elif 'deleteAllModels' in request.form:
        return page.deleteAll()
    elif 'AutoFillModels' in request.form:
        return page.autoFill()
    elif 'searchbyName' in request.form:
        name = request.form['name']
        return page.find(name)
    else:
        return redirect(url_for('home_page'))
Ejemplo n.º 13
0
Archivo: user.py Proyecto: daeyun/ppbot
    def __init__(self, name=None, password=None, access=None, **kwargs):
        Model.__init__(self)
        Base.__init__(self)
        self.metadata = Base.metadata

    	self.name = name
    	self.password = password
    	self.access = access

        for name, val in kwargs.iteritems():
            self.__setattr__(name, val)
  def __init__(self, train):
    Model.__init__(self, train)
    self.bymonth = bymonth = train.groupby('month')

    # First of each month and last of last month
    dayages = bymonth.first()['dayage'].values
    self.dayages = numpy.concatenate([dayages, train.iloc[-1:]['dayage'].values])

    self.means = bymonth['count'].mean()
    self.nsegments = len(bymonth)
    self.nparams = self.nsegments + 1
Ejemplo n.º 15
0
    def test_diff_creates_changes_record(self):
        objects = Model.objects
        Model.add_to_class('objects', ModeratorManager())

        m = Model.objects.create(name='model first version')
        me = ModeratorEntry.objects.create(
            content_type=ContentType.objects.get_for_model(Model),
            object_id=m.pk
        )

        m.name = 'model second version'
        m.save()

        me.diff()

        self.assertTrue(me.changes.all())

        Model.add_to_class('objects', objects)
Ejemplo n.º 16
0
def eval(logdir='logdir/default/train1', queue=False):
    # Load graph
    model = Model(mode="test1", batch_size=hp.Test1.batch_size, queue=queue)

    # Accuracy
    acc_op = model.acc_net1()

    # Loss
    loss_op = model.loss_net1()

    # Summary
    summ_op = summaries(acc_op, loss_op)

    session_conf = tf.ConfigProto(
        allow_soft_placement=True,
        device_count={'CPU': 1, 'GPU': 0},
    )
    with tf.Session(config=session_conf) as sess:
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        writer = tf.summary.FileWriter(logdir, sess.graph)

        # Load trained model
        sess.run(tf.global_variables_initializer())
        model.load(sess, 'train1', logdir=logdir)

        if queue:
            summ, acc, loss = sess.run([summ_op, acc_op, loss_op])
        else:
            mfcc, ppg = get_batch(model.mode, model.batch_size)
            summ, acc, loss = sess.run([summ_op, acc_op, loss_op], feed_dict={model.x_mfcc: mfcc, model.y_ppgs: ppg})

        writer.add_summary(summ)

        print("acc:", acc)
        print("loss:", loss)
        print('\n')

        writer.close()

        coord.request_stop()
        coord.join(threads)
Ejemplo n.º 17
0
def calculate_time_by_hours_global(rw_data):
    print len(rw_data.keys())
    model = Model(rw_data)
    iplot = {}
    print len(model.get_user_ids())
    
    for u_id in model.get_user_ids():
        rates = model.get_rates(u_id)
        for i_id in rates.keys():
            ra, ts = rates[i_id]
            timestamp = float(ts)
            value = datetime.datetime.fromtimestamp(timestamp)
            hr = value.hour
            if hr in iplot:
                iplot[hr] = iplot[hr] + 1
            else:
                iplot[hr] = 1

    return iplot
Ejemplo n.º 18
0
 def __init__( self, feature_engineer=None, model=None ):
     # configure logging
     self.logger = logging.getLogger("Pipeline")
     handler = logging.FileHandler(settings.LOG_PATH)
     handler.setFormatter(logging.Formatter(
         '%(asctime)s %(levelname)s %(name)s: %(message)s'))
     self.logger.addHandler(handler)
     self.logger.setLevel(logging.DEBUG)
     
     self.settings = Settings()        
     self.feature_engineer = FeatureEngineer( self.settings )
     self.model = Model().get_model( Settings() )
Ejemplo n.º 19
0
Archivo: run.py Proyecto: zingp/cbot
    print("testing time:", time.time() - start_time)
    # for ref, pre in zip(references, predictions):
    #     print(ref)
    #     print("-"*20)
    #     print(pre)
    #     print("*"*100)


if __name__ == '__main__':
    args = set_parser()
    config = Config(args)
    if args.mode == 'train':
        train(config)
    else:
        test_set = get_dataset(config.test_path,
                               config.w2i_vocabs,
                               config,
                               is_train=False)
        model = Model(n_emb=args.n_emb,
                      n_hidden=args.n_hidden,
                      vocab_size=args.vocab_size,
                      dropout=args.dropout,
                      d_ff=args.d_ff,
                      n_head=args.n_head,
                      n_block=args.n_block)
        model_dict = torch.load(args.restore)
        model.load_state_dict(model_dict)
        model.to(device)
        test(test_set, model)
Ejemplo n.º 20
0
for i in range(1, args.nlayer):
    v[..., i * int(shape[-1] / args.nlayer):] = vp_i[i]  # Bottom velocity

v0 = ndimage.gaussian_filter(v, sigma=10)
v0[v < 1.51] = v[v < 1.51]
v0 = ndimage.gaussian_filter(v0, sigma=3)
rho0 = (v0 + .5) / 2
dm = v0**(-2) - v**(-2)
dm[:, -1] = 0.
# Set up model structures
if is_tti:
    model = Model(shape=shape,
                  origin=origin,
                  spacing=spacing,
                  vp=v0,
                  epsilon=.045 * (v0 - 1.5),
                  delta=.03 * (v0 - 1.5),
                  rho=rho0,
                  theta=.1 * (v0 - 1.5),
                  dm=dm,
                  space_order=so)
else:
    model = Model(shape=shape,
                  origin=origin,
                  spacing=spacing,
                  vp=v0,
                  rho=rho0,
                  dm=dm,
                  space_order=so)

# Time axis
t0 = 0.
Ejemplo n.º 21
0
import sys
sys.path.append(".")
import numpy as np
from sklearn.cross_validation import KFold, LabelKFold
from utils import weighted_precision_recall, read_training_data, f1_score
from models import Model

if __name__ == "__main__":
    csv_path = sys.argv[1]
    image_prefix = sys.argv[2]
    (X, Y, categories, sample_weights) = read_training_data(csv_path, image_prefix = image_prefix, category = True, sample_weight = True)
    print "#T = %d, #F = %d, #N = %d"%(sum(1 for y in Y if y == 0), sum(1 for y in Y if y == 1), sum(1 for y in Y if y == 2))
    print "X =", X.shape, ", Y =",Y.shape

    # Machine Learning models
    model = Model()
    
    kf = LabelKFold(categories, n_folds = 5) # ***shuffle internally***
    #kf = KFold(len(X), n_folds = 5, shuffle = True, random_state = 123)
    #cross validation
    S = []
    for train_index, test_index in kf:
        trainX, testX = X[train_index], X[test_index]
        trainY, testY = Y[train_index], Y[test_index]
        K = None 
        #fit data
        model.fit(trainX[:K], trainY[:K], sample_weight = sample_weights[train_index][:K])
        #make prediction
        predY = model.predict(testX)
        
        #evaluation
Ejemplo n.º 22
0
def train(logdir='logdir/default/train1', queue=True):
    model = Model(mode="train1", batch_size=hp.Train1.batch_size, queue=queue)

    # Loss
    loss_op = model.loss_net1()

    # Accuracy
    acc_op = model.acc_net1()

    # Training Scheme
    global_step = tf.Variable(0, name='global_step', trainable=False)

    optimizer = tf.train.AdamOptimizer(learning_rate=hp.Train1.lr)
    with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
        var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'net/net1')
        train_op = optimizer.minimize(loss_op, global_step=global_step, var_list=var_list)

    # Summary
    for v in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'net/net1'):
        tf.summary.histogram(v.name, v)
    tf.summary.scalar('net1/train/loss', loss_op)
    tf.summary.scalar('net1/train/acc', acc_op)
    summ_op = tf.summary.merge_all()

    session_conf = tf.ConfigProto(
        gpu_options=tf.GPUOptions(
            allow_growth=True,
        ),
    )
    # Training
    with tf.Session(config=session_conf) as sess:
        # Load trained model
        sess.run(tf.global_variables_initializer())
        model.load(sess, 'train1', logdir=logdir)

        writer = tf.summary.FileWriter(logdir, sess.graph)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        for epoch in range(1, hp.Train1.num_epochs + 1):
            for step in tqdm(range(model.num_batch), total=model.num_batch, ncols=70, leave=False, unit='b'):
                if queue:
                    sess.run(train_op)
                else:
                    mfcc, ppg = get_batch(model.mode, model.batch_size)
		    #print("MFCC shape: {}".format(mfcc.shape))
		    #print("types: {} and {}".format(mfcc.dtype, ppg.dtype))
		    #print("PPG shape: {}".format(ppg.shape))
                    sess.run(train_op, feed_dict={model.x_mfcc: mfcc, model.y_ppgs: ppg})

            # Write checkpoint files at every epoch
            summ, gs = sess.run([summ_op, global_step], feed_dict={model.x_mfcc:  mfcc, model.y_ppgs: ppg})
	    # There was a problem where in certain environments placeholder must be fed for these ops
            if epoch % hp.Train1.save_per_epoch == 0:
                tf.train.Saver().save(sess, '{}/epoch_{}_step_{}'.format(logdir, epoch, gs))

            # Write eval accuracy at every epoch
            with tf.Graph().as_default():
                eval1.eval(logdir=logdir, queue=False)

            writer.add_summary(summ, global_step=gs)

        writer.close()
        coord.request_stop()
        coord.join(threads)
Ejemplo n.º 23
0
parser.add_argument('--gpu', default=0, type=int, help='gpu id')
parser.add_argument('--log_path',
                    default='./logs',
                    type=str,
                    help='path to log')

args = parser.parse_args()

train_dataset = Dataset(args.data_path, args.dataset, True)
train_loader = data.DataLoader(train_dataset, batch_size=args.bs, shuffle=True)
test_dataset = Dataset(args.data_path, args.dataset, False)
test_loader = data.DataLoader(test_dataset, batch_size=1, shuffle=False)

device = torch.device('cuda:' + str(args.gpu))

model = Model().to(device)

writer = SummaryWriter(args.log_path)

mseloss = nn.MSELoss(reduction='sum').to(device)
bceloss = nn.BCELoss(reduction='sum').to(device)
optimizer = optim.Adam(model.parameters(), lr=1e-4)

if args.load:
    checkpoint = torch.load(
        os.path.join(args.save_path, 'checkpoint_latest.pth'))
    model.load_state_dict(checkpoint['model'])
    optimizer.load_state_dict(checkpoint['optimizer'])

    best_mae = torch.load(os.path.join(args.save_path,
                                       'checkpoint_best.pth'))['mae']
 def create_model(self):
     return Model(3, 6)
Ejemplo n.º 25
0
from flask import Flask, Response, request, render_template
from models import Model

app = Flask(__name__)

greeter = Model("bolt://103.89.6.76:7778", "proglan", "kelas2020")


@app.route('/')
def getdata_kurasi():

    getdata = greeter.get_kurasi()
    return render_template("index.html", data=getdata)


if __name__ == "__main__":
    app.run(port=80, debug=True)
 def __init__(self, train, name='Unnamed'):
   self.name = name
   Model.__init__(self, train)
Ejemplo n.º 27
0
    def initialize_table(self):
        self.session.add_all([
            Network('gamesurge')
        ])

        Model.initialize_table(self)
def train(X_train,
          y_train,
          batch_size,
          num_features,
          num_targets,
          learning_rate,
          weight_decay,
          hidden_size,
          epochs,
          fold_id,
          X_valid=None,
          y_valid=None,
          filter_p=False):

    device = checkDevice()
    train_dataset = TrainDataset(X_train, y_train)
    if ((X_valid is not None) and (y_valid is not None)):
        valid_dataset = TrainDataset(X_valid, y_valid)

    trainloader = torch.utils.data.DataLoader(train_dataset,
                                              batch_size=batch_size,
                                              shuffle=True)

    if ((X_valid is not None) and (y_valid is not None)):
        validloader = torch.utils.data.DataLoader(valid_dataset,
                                                  batch_size=batch_size,
                                                  shuffle=False)

    model = Model(num_features=num_features,
                  num_targets=num_targets,
                  hidden_size=hidden_size,
                  weights=getClassWeights(y_train))

    model.to(device)
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=learning_rate,
                                 weight_decay=weight_decay)

    scheduler = optim.lr_scheduler.OneCycleLR(optimizer=optimizer,
                                              pct_start=0.1,
                                              div_factor=1e3,
                                              max_lr=1e-2,
                                              epochs=epochs,
                                              steps_per_epoch=len(trainloader))

    loss_fn = model.loss_fn

    for epoch in range(epochs):
        start = time.time()
        train_loss = train_epoch(model, optimizer, scheduler, loss_fn,
                                 trainloader)
        end = time.time()
        print("Epoch: {}  |  Train Loss: {:.5f}  |  Time Elapsed: {:.2f}  |".
              format(epoch, train_loss, end - start))
        if ((X_valid is not None) and (y_valid is not None)):
            if filter_p:
                valid_loss = filtered_valid_BCE(model, validloader)
            else:
                valid_loss, _ = valid_fn(model, model.valid_fn, validloader)
            print("Epoch: {}  |  Test Score: {:.5f}  |".format(
                epoch, valid_loss))

    if ((X_valid is None) or (y_valid is None)):
        torch.save(model.state_dict(), f"./kaggle/working/AllDataset.pth")
    return model
            lambda size: (BORDER, BORDER))
        dirty.append(label_rect)

        # Render the widgets.
        dirty += render_widgets(surface, widgets, surf_w, surf_h, index, \
            label_rect)
            
    return render_frame, len(frame_map)


if __name__ == "__main__":
    import os.path
    localpath = os.path.dirname(__file__)

    # Create a Models.
    small = Model(os.path.join(localpath, "gis/SmallPatches"), \
        os.path.join(localpath, "csv/small"))
    # Create the values. We also include the transformation, for later use.
    values = [(Values(small, "SWTotal"), "None"),
              (Values(small, "NO3Total"), "None")]
    # Create the graphs.
    graphs = [Graph([Graphable(Values(small, "SWTotal"), \
            "SWTotal (min, mean, max)", \
            statistics = ['min', 'mean', 'max'])]), \
        Graph([Graphable(Values(small, "NO3Total", \
                transforms = [lambda v: patch_filter(v, patch_set)]), \
            "Field #{}".format(i), statistics = ['mean']) \
            for i, patch_set in small.get_patch_fields().items()])
    ]
    # Create the description strings...
    descriptions = []
    for value, transform in values:
Ejemplo n.º 30
0
    def addParamsClick(self):

        self.model.listOfParams.addToList(self.paramsEdt.text())
        # print(self.model.listoffeatures.list)
        l = len(self.model.listOfParams.list) - 1
        self.paramsListWidget.insertItem(l, self.model.listOfParams.list[-1])
        print(self.model.listOfParams.list)

    def findParamsClick(self):
        self.model.set.findParams(self.model.listOfParams.list)
        for i, a in enumerate(self.model.set.listofResults):
            self.findParamsListWidget.insertItem(i, str(a))

    def resetClick(self):
        self.model.resetmodel()
        self.clearLayout(self.tab_layout)

    def clearLayout(self, layout):
        self.featuresListWidget.clear()
        self.setWidget.clear()
        self.paramsListWidget.clear()
        self.findParamsListWidget.clear()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    model = Model()
    window = StartWindow(model)
    window.show()
    app.exit(app.exec_())
def MNISTNet1():
    print("BESTNET")
    conv1_params={
        'kernel_h': 3,
        'kernel_w': 3,
        'pad': 0,
        'stride': 1,
        'in_channel': 1,
        'out_channel': 32
    }
    conv2_params={
        'kernel_h': 3,
        'kernel_w': 3,
        'pad': 0,
        'stride': 1,
        'in_channel': 32,
        'out_channel': 32
    }
    pool1_params={
        'pool_type': 'max',
        'pool_height': 2,
        'pool_width': 2,
        'stride': 2,
        'pad': 0
    }
    pool2_params={
        'pool_type': 'max',
        'pool_height': 3,
        'pool_width': 3,
        'stride': 2,
        'pad': 0
    }
    model = Model()
    # fan_in = in_channel * kernel_h * kernel_w
    # fan_out = num_filters * kernel_h * kernel_w / Pooling_layer_area
    model.add(Convolution(conv1_params, name='conv1', initializer=Guassian(std=0.001)))
    model.add(ReLU(name='relu1'))
    model.add(Pooling(pool1_params, name='pooling1'))
    model.add(Convolution(conv2_params, name='conv2', initializer=Guassian(std=0.001)))
    model.add(ReLU(name='relu2'))
    model.add(Pooling(pool2_params, name='pooling2'))
    model.add(Dropout(ratio=0.25, name='dropout1'))
    model.add(Flatten(name='flatten'))
    model.add(FCLayer(800, 256, name='fclayer1', initializer=Guassian(std=0.01))) 
    model.add(ReLU(name='relu3'))
    model.add(FCLayer(256, 10, name='fclayer2', initializer=Guassian(std=0.01)))
    return model
def MNISTNet():
    conv1_params={
        'kernel_h': 3,
        'kernel_w': 3,
        'pad': 0,
        'stride': 1,
        'in_channel': 1,
        'out_channel': 6
    }
    conv2_params={
        'kernel_h': 3,
        'kernel_w': 3,
        'pad': 0,
        'stride': 1,
        'in_channel': 6,
        'out_channel': 16
    }
    pool1_params={
        'pool_type': 'max',
        'pool_height': 2,
        'pool_width': 2,
        'stride': 2,
        'pad': 0
    }
    pool2_params={
        'pool_type': 'max',
        'pool_height': 3,
        'pool_width': 3,
        'stride': 2,
        'pad': 0
    }
    model = Model()
    model.add(Convolution(conv1_params, name='conv1', initializer=Guassian(std=0.001)))
    model.add(ReLU(name='relu1'))
    model.add(Pooling(pool1_params, name='pooling1'))
    model.add(Convolution(conv2_params, name='conv2', initializer=Guassian(std=0.001)))
    model.add(ReLU(name='relu2'))
    model.add(Pooling(pool2_params, name='pooling2'))
    # model.add(Dropout(ratio=0.25, name='dropout1'))
    model.add(Flatten(name='flatten'))
    model.add(FCLayer(400, 256, name='fclayer1', initializer=Guassian(std=0.01))) 
    model.add(ReLU(name='relu3'))
    # model.add(Dropout(ratio=0.5))
    model.add(FCLayer(256, 10, name='fclayer2', initializer=Guassian(std=0.01)))
    return model
Ejemplo n.º 33
0
import os 
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
import os
import shutil
from keras.backend.tensorflow_backend import set_session
from config import Config
from models import Model
import matplotlib
matplotlib.use('Agg')


if os.path.isdir("output/"):
    shutil.rmtree("output/")
os.makedirs("output/")

config = Config(epochs = 30, gpu = "1", sampling_size_train = 40, sampling_size_val = 40, batch_size = 1 ,lr = 1e-4, val_size = 0.25)

session_config = tf.ConfigProto()
session_config.gpu_options.visible_device_list = config.gpu
session_config.gpu_options.allow_growth = True
set_session(tf.Session(config=session_config))

model = Model(config)
model.train_predict()
#y_scores, y_preds = model.train_predict()
#model.get_metrics(y_scores, y_preds)
#model.plot_ROCs(y_scores)
#model.plot_PRs(y_scores)
Ejemplo n.º 34
0
def dqnmain(project_name,
            do_boltzman_exploration=False,
            test=False,
            chkpt=None,
            hypeparams=hyperparameter_defaults,
            steps=1000,
            device='cuda'):
    image_arr = []

    if (not test):
        wdbrun = wandb.init(project=project_name,
                            config=hypeparams,
                            name=hypeparams['run_name'],
                            reinit=True,
                            monitor_gym=False)
        # run.save("*.pth")
        config = wdbrun.config
        max_reward = config.max_reward
        max_steps = config.max_steps
        memory_size = config.memory_size
        min_rb_size = config.min_rb_size
        sample_size = config.sample_size
        env_steps_before_train = config.env_steps_before_train
        tgt_model_update = config.tgt_model_update
        reward_scaler = config.reward_scaler
        eps_min = config.eps_min
        eps_decay = config.eps_decay
        gamma = config.gamma
        learning_rate = config.learning_rate
    else:
        max_reward = hypeparams['max_reward']
        max_steps = steps
        memory_size = hypeparams['memory_size']
        min_rb_size = hypeparams['min_rb_size']
        sample_size = hypeparams['sample_size']
        env_steps_before_train = hypeparams['env_steps_before_train']
        tgt_model_update = hypeparams['tgt_model_update']
        reward_scaler = hypeparams['reward_scaler']
        eps_min = hypeparams['eps_min']
        eps_decay = hypeparams['eps_decay']
        gamma = hypeparams['gamma']
        learning_rate = hypeparams['learning_rate']

    env = gym.make(hypeparams['env_name'])
    if hypeparams['env_name'] == 'Breakout-v0':
        #TODO
        env = FrameStackingAndResizingEnv(env, 84, 84,
                                          4)  # change stack size here
    env._max_episode_steps = 4000

    test_env = gym.make(hypeparams['env_name'])
    if hypeparams['env_name'] == 'Breakout-v0':
        #TODO
        test_env = FrameStackingAndResizingEnv(test_env, 84, 84,
                                               4)  # change stack size here
    test_env._max_episode_steps = 4000
    last_observation = env.reset()

    if hypeparams['env_name'] == 'Breakout-v0':
        m = ConvModel(env.observation_space.shape, env.action_space.n,
                      learning_rate).to(device)
    else:
        m = Model(env.observation_space.shape, env.action_space.n,
                  learning_rate).to(device)
    if chkpt is not None:
        m.load_state_dict(torch.load(chkpt))

    if hypeparams['env_name'] == 'Breakout-v0':
        tgt = ConvModel(env.observation_space.shape,
                        env.action_space.n).to(device)
    else:
        tgt = Model(env.observation_space.shape, env.action_space.n).to(
            device)  # target model, gets update fewer times
    update_tgt_model(m, tgt)

    rb = ReplayBuffer(memory_size)
    steps_since_train = 0
    epochs_since_tgt = 0

    step_num = -1 * min_rb_size
    i = 0

    episode_rewards = []
    rolling_reward = 0
    solved = False

    try:
        while (not solved) and step_num < max_steps:
            if test:
                screen = env.render('rgb_array')
                image_arr.append(screen)
                eps = 0
            else:
                eps = eps_decay**(step_num)

            if do_boltzman_exploration:
                if hypeparams['env_name'] == 'Breakout-v0':
                    logits = m(
                        torch.Tensor(last_observation).unsqueeze(0).to(
                            device))[0]
                    action = torch.distributions.Categorical(
                        logits=logits).sample().item()
                else:
                    logits = m(torch.Tensor(last_observation).to(device))[0]
                    action = torch.distributions.Categorical(
                        logits=logits).sample().item()
            else:
                if random.random() < eps:
                    action = env.action_space.sample()
                else:
                    if hypeparams['env_name'] == 'Breakout-v0':
                        action = m(
                            torch.Tensor(last_observation).unsqueeze(0).to(
                                device)).max(-1)[-1].item()
                    else:
                        action = m(torch.Tensor(last_observation).to(
                            device)).max(-1)[-1].item()

            observation, reward, done, info = env.step(action)
            rolling_reward += reward

            reward = reward / reward_scaler

            rb.insert(SARS(last_observation, action, reward, done,
                           observation))

            last_observation = observation

            if done:
                episode_rewards.append(rolling_reward)
                if test:
                    print(rolling_reward)
                rolling_reward = 0
                observation = env.reset()

            steps_since_train += 1
            i += 1
            step_num += 1
            if (
                    not test
            ) and rb.idx > min_rb_size and steps_since_train > env_steps_before_train:
                loss = train_step(m, rb.sample(sample_size), tgt,
                                  env.action_space.n, gamma, device)
                ave_reward = np.mean(episode_rewards)
                wdbrun.log(
                    {
                        'loss': loss.detach().cpu().item(),
                        'epsilon': eps,
                        'avg_reward': ave_reward
                    },
                    step=step_num)
                if ave_reward >= max_reward:
                    solved = True
                episode_rewards = []
                epochs_since_tgt += 1
                # print(step_num, loss.detach().item())
                if epochs_since_tgt > tgt_model_update:
                    # print('updating target model')
                    update_tgt_model(m, tgt)
                    rew, frames = run_test_episode(m, test_env, device)
                    # frames.shape == (T, H, W, C)
                    # wandb.log({'test_reward': rew, 'test_video': wandb.Video(frames.transpose(0, 3, 1, 2), str(rew), fps=25, format='mp4')})
                    wandb.log({'test_reward': rew})
                    epochs_since_tgt = 0
                    torch.save(
                        tgt.state_dict(),
                        f"{wandb.run.dir}/{hypeparams['run_name']}_{step_num}.pth"
                    )
                steps_since_train = 0
                if ave_reward >= max_reward:
                    solved = True
        wandb.join()
        env.close()
    except KeyboardInterrupt:
        sys.exit()
Ejemplo n.º 35
0
 def __init__(self, path):
     self.config = Configuration.construct(path)
     self.env = Environment(self.config)
     self.memory = ReplayMemory(self.config)
     self.model = Model(self.config)
     self.ep = None
Ejemplo n.º 36
0
Archivo: run.py Proyecto: zingp/cbot
def train(config):
    # train_path:train-context.json
    args = config.args
    train_set = get_dataset(config.train_path,
                            config.w2i_vocabs,
                            config,
                            is_train=True)
    dev_set = get_dataset(config.dev_path,
                          config.w2i_vocabs,
                          config,
                          is_train=False)
    # X:img,torch.stack;
    train_batch = get_dataloader(train_set, args.batch_size, is_train=True)
    model = Model(n_emb=args.n_emb,
                  n_hidden=args.n_hidden,
                  vocab_size=args.vocab_size,
                  dropout=args.dropout,
                  d_ff=args.d_ff,
                  n_head=args.n_head,
                  n_block=args.n_block)
    if args.restore != '':
        model_dict = torch.load(args.restore)
        model.load_state_dict(model_dict)
    model.to(device)
    optimizer = optim.Adam(filter(lambda p: p.requires_grad,
                                  model.parameters()),
                           lr=args.lr)
    best_score = -1000000

    for i in range(args.epoch):
        model.train()
        report_loss, start_time, n_samples = 0, time.time(), 0
        count, total = 0, len(train_set) // args.batch_size + 1
        for batch in train_batch:
            Y, T = batch
            Y = Y.to(device)
            T = T.to(device)
            optimizer.zero_grad()
            loss = model(Y, T)
            loss.backward()
            optimizer.step()
            report_loss += loss.item()
            #break
            n_samples += len(Y.data)
            count += 1
            if count % args.report == 0 or count == total:
                print('%d/%d, epoch: %d, report_loss: %.3f, time: %.2f' %
                      (count, total, i + 1, report_loss / n_samples,
                       time.time() - start_time))
                score = eval(model, dev_set, args.batch_size)
                model.train()
                if score > best_score:
                    best_score = score
                    save_model(os.path.join(args.dir, 'best_checkpoint.pt'),
                               model)
                else:
                    save_model(os.path.join(args.dir, 'checkpoint.pt'), model)
                report_loss, start_time, n_samples = 0, time.time(), 0

    return model
Ejemplo n.º 37
0
import sys
sys.path.append(".")
import cPickle
import numpy as np
from models import Model
from utils import weighted_precision_recall, read_training_data

if __name__ == "__main__":
    csv_path = sys.argv[1]
    image_prefix = sys.argv[2]
    model_path = sys.argv[3]
    (X, Y, categories, sample_weights) = read_training_data(csv_path, image_prefix = image_prefix, category = True, sample_weight = True)
    print "#T = %d, #F = %d, #N = %d"%(sum(1 for y in Y if y == 0), sum(1 for y in Y if y == 1), sum(1 for y in Y if y == 2))
    print "X =", X.shape, ", Y =",Y.shape
    # Machine Learning models
    model = Model()

    #fit data
    model.fit(X, Y, sample_weight = sample_weights)
    #make prediction
    predY = model.predict(X)
    #evaluation
    scores = weighted_precision_recall(Y, predY, sample_weight = sample_weights)
    print "IN SAMPLE -- (Precision/Recall) T: (%.3f,%.3f) / F: (%.3f,%.3f) / N:(%.3f,%.3f)" %(scores[0], scores[1], scores[2], scores[3], scores[4], scores[5])

    #save model
    with open(model_path, "wb") as f:
        cPickle.dump(model, f, cPickle.HIGHEST_PROTOCOL)
Ejemplo n.º 38
0
    vocab = build_vocab(train_dataset + valid_dataset)
    train_dataset.index_with(vocab)
    valid_dataset.index_with(vocab)
    data_loader_params = params.pop('data_loader')
    batch_size = data_loader_params['batch_size']
    train_loader = DataLoader.from_params(dataset=train_dataset,
                                          params=data_loader_params)
    valid_loader = DataLoader(valid_dataset,
                              batch_size=batch_size,
                              shuffle=False)

    # 2. setting up model and training details

    # model = build_model(vocab)
    model = Model.from_params(vocab=vocab, params=params["model"])
    model.cuda()
    trainer = Trainer.from_params(
        model=model,
        serialization_dir=serialization_dir,
        data_loader=train_loader,
        validation_data_loader=valid_loader,
        params=params['trainer'],
    )

    trainer.optimizer.weight_decay = 0.00001

    # 3. perform training with early stopping
    print("Starting training")
    trainer.train()
    print("Finished training")
Ejemplo n.º 39
0
    seed = 0
    if opt.seed == 0:
        seed = random.randint(1, 10000)
        random.seed(seed)
        torch.manual_seed(seed)
        torch.cuda.manual_seed(args.seed)
    else:
        seed = opt.seed
        torch.manual_seed(opt.seed)
        torch.cuda.manual_seed(opt.seed)
    torch.backends.cudnn.benchmark = True

    dictionary = Dictionary.load_from_file('data/dictionary.pkl')
    opt.ntokens = dictionary.ntoken

    model = Model(opt)
    model.apply(weights_init_kn)
    model = nn.DataParallel(model).cuda()

    train_dset = SelfCriticalDataset(opt.split, dictionary, opt)
    train_loader = DataLoader(train_dset,
                              opt.batch_size,
                              shuffle=True,
                              num_workers=0)
    opt.use_all = 1
    eval_dset = SelfCriticalDataset(opt.split_test, dictionary, opt)
    eval_loader = DataLoader(eval_dset,
                             opt.batch_size,
                             shuffle=False,
                             num_workers=0)
Ejemplo n.º 40
0
args = gen_args()
set_seed(args.random_seed)

os.system('mkdir -p ' + args.evalf)
os.system('mkdir -p ' + os.path.join(args.evalf, 'render'))

tee = Tee(os.path.join(args.evalf, 'eval.log'), 'w')

### evaluating

data_names = args.data_names

use_gpu = torch.cuda.is_available()

# create model and load weights
model = Model(args, use_gpu)
print("model_kp #params: %d" % count_parameters(model))

if args.eval_epoch < 0:
    model_name = 'net_best.pth'
else:
    model_name = 'net_epoch_%d_iter_%d.pth' % (args.eval_epoch, args.eval_iter)

model_path = os.path.join(args.outf, model_name)
print("Loading network from %s" % model_path)

if args.stage == 'dy':
    pretrained_dict = torch.load(model_path)
    model_dict = model.state_dict()
    # only load parameters in dynamics_predictor
    pretrained_dict = {
class Base:

	def __init__(self, config, loaders):

		self.config = config
		self.loaders = loaders

		# Model Configuration
		self.part_num = config.part_num
		self.pid_num = config.pid_num

		# Logger Configuration
		self.max_save_model_num = config.max_save_model_num
		self.output_path = config.output_path
		self.model_path = os.path.join(self.output_path, 'models/')
		self.log_path = os.path.join(self.output_path, 'logs/')

		# Train Configuration
		self.base_learning_rate = config.base_learning_rate
		self.milestones = config.milestones

		# init model
		self._init_device()
		self._init_model()
		self._init_creiteron()
		self._init_optimizer()


	def _init_device(self):
		self.device = torch.device('cuda')


	def _init_model(self):
		self.model = Model(part_num=self.part_num, class_num=self.config.pid_num)
		self.model = nn.DataParallel(self.model).to(self.device)


	def _init_creiteron(self):
		self.ide_creiteron = nn.CrossEntropyLoss()


	## compute average ide loss of all outputs
	def compute_ide_loss(self, logits_list, pids):
		avg_ide_loss = 0
		avg_logits = 0
		for i in xrange(self.part_num):
			logits_i = logits_list[i]
			avg_logits += 1.0 / float(self.part_num) * logits_i
			ide_loss_i = self.ide_creiteron(logits_i, pids)
			avg_ide_loss += 1.0 / float(self.part_num) * ide_loss_i
		return avg_ide_loss, avg_logits


	## init optimizer and lr_lr_scheduler
	def _init_optimizer(self):
		params = [{'params': self.model.module.resnet_conv.parameters(), 'lr': 0.1*self.base_learning_rate}]
		for i in xrange(self.part_num):
			params.append({'params': getattr(self.model.module, 'classifier' + str(i)).parameters(), 'lr': self.base_learning_rate})
		for i in xrange(self.part_num):
			params.append({'params': getattr(self.model.module, 'embedder' + str(i)).parameters(), 'lr': self.base_learning_rate})
		self.optimizer = optim.SGD(params=params, weight_decay=5e-4, momentum=0.9, nesterov=True)
		self.lr_scheduler = optim.lr_scheduler.MultiStepLR(self.optimizer, self.milestones, gamma=0.1)


	## save model as save_epoch
	def save_model(self, save_epoch):

		# save model
		file_path = os.path.join(self.model_path, 'model_{}.pkl'.format(save_epoch))
		torch.save(self.model.state_dict(), file_path)

		# if saved model is more than max num, delete the model with smallest iter
		if self.max_save_model_num > 0:
			root, _, files = os_walk(self.model_path)
			if len(files) > self.max_save_model_num:
				file_iters = sorted([int(file.replace('.pkl', '').split('_')[1]) for file in files], reverse=False)
				file_path = os.path.join(root, 'model_{}.pkl'.format(file_iters[0]))
				os.remove(file_path)


	## resume model from resume_epoch
	def resume_model(self, resume_epoch):
		model_path = os.path.join(self.model_path, 'model_{}.pkl'.format(resume_epoch))
		self.model.load_state_dict(torch.load(model_path))
		print('successfully resume model from {}'.format(model_path))


	## set model as train mode
	def set_train(self):
		self.model = self.model.train()

	## set model as eval mode
	def set_eval(self):
		self.model = self.model.eval()
Ejemplo n.º 42
0
def train(dataset='A'):
    # 训练数据集路径
    img_root_dir = './data/ShanghaiTech/part_' + dataset + '_final/train_data/images/'
    gt_root_dir = './data/ShanghaiTech/part_' + dataset + '_final/train_data/ground_truth/'
    # 测试数据集路径
    test_img_dir = './data/ShanghaiTech/part_' + dataset + '_final/test_data/images/'
    test_gt_dir = './data/ShanghaiTech/part_' + dataset + '_final/test_data/ground_truth/'

    img_file_list = os.listdir(img_root_dir)
    # gt_file_list = os.listdir(gt_root_dir)
    test_img_list = os.listdir(test_img_dir)
    # test_gt_list = os.listdir(test_gt_dir)

    model_name = 'mcdcnn_SHTech_' + dataset
    cfg = Config(model_name)
    cfg.lr = 1e-4
    tool = Tool()
    model = Model()
    network = model.mcdcnn()
    INPUT, GT_DMP, GT_CNT, EST_DMP, EST_CNT = network['INPUT'], network['GT_DMP'], network['GT_CNT'], \
                                              network['EST_DMP'], network['EST_CNT']

    loss = model.losses(GT_DMP, GT_CNT, EST_DMP, EST_CNT)

    # 学习率衰变设置
    global_step = tf.Variable(0, trainable=False)
    lr = tf.train.exponential_decay(cfg.lr,
                                    global_step=global_step,
                                    decay_steps=cfg.lr_decay_step,
                                    decay_rate=cfg.lr_decay_rate,
                                    staircase=True)
    # 优化器
    optimizer = tf.train.AdamOptimizer(lr).minimize(loss, global_step)

    # 模型保存设置
    saver = tf.train.Saver(max_to_keep=cfg.max_ckpt_keep)
    ckpt = tf.train.get_checkpoint_state(cfg.ckpt_router)

    # 创建会话
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        # 加载模型
        if ckpt and ckpt.model_checkpoint_path:
            print('load model')
            saver.restore(sess, ckpt.model_checkpoint_path)
        # 训练日志文件路径设置
        if not os.path.exists(cfg.log_router):
            os.makedirs(cfg.log_router)
        log = open(cfg.log_router + 'train' + r'.logs',
                   mode='a+',
                   encoding='utf-8')
        # 迭代训练
        num = len(img_file_list)
        # 开始训练
        for i in tqdm(range(cfg.iter_num)):
            # 随机打乱
            np.random.shuffle(img_file_list)
            for j in tqdm(range(num)):
                img_path = img_root_dir + img_file_list[j]
                gt_path = gt_root_dir + 'GT_' + img_file_list[j].split(r'.')[0]
                img, dmp, cnt = tool.read_train_data(img_path,
                                                     gt_path,
                                                     use_knn=True)
                feed_dict = {INPUT: img, GT_DMP: dmp, GT_CNT: cnt}

                _, est_dmp, est_cnt, train_loss = sess.run(
                    [optimizer, EST_DMP, EST_CNT, loss], feed_dict=feed_dict)
                format_time = str(
                    time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
                format_str = 'step %d, loss = %.3f, gt = %d, inference = %.3f'
                log_line = format_time, img_file_list[j], format_str % (
                    (i * num + j), train_loss, cnt, est_cnt)
                print(log_line)
                log.writelines(str(log_line) + '\n')
                sess.run(lr, feed_dict={global_step: i * num + j})

            if i % cfg.snap == 0:
                # 保存模型
                saver.save(sess, cfg.ckpt_router + '/v1', global_step=i)
                # 进行测试
                print('testing', i, '> > > > > > > > > > > > > > > > > > >')
                total_mae = 0.0
                total_mse = 0.0
                num = len(test_img_list)
                log_line = ''
                for k in tqdm(range(num - 2, num)):
                    img_path = test_img_dir + test_img_list[k]
                    gt_path = test_gt_dir + 'GT_' + test_img_list[k].split(
                        r'.')[0]

                    img, dmp, cnt = tool.read_test_data(img_path,
                                                        gt_path,
                                                        use_knn=True)

                    feed_dict = {INPUT: img, GT_DMP: dmp, GT_CNT: cnt}

                    est_cnt, test_loss = sess.run([EST_CNT, loss],
                                                  feed_dict=feed_dict)

                    format_time = str(
                        time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
                    format_str = 'step %d, joint loss = %.3f, gt = %d, inference = %.3f'
                    line = str(format_time + ' ' + test_img_list[k] + ' ' +
                               format_str % (k, test_loss, cnt, est_cnt) +
                               '\n')
                    log_line += line
                    total_mae += tool.mae_metrix(cnt, est_cnt)
                    total_mse += tool.mse_metrix(cnt, est_cnt)
                    print(line)

                avg_mae = total_mae / num
                avg_mse = pow(total_mse / num, 0.5)
                result = str('_MAE_%.5f_MSE_%.5f' % (avg_mae, avg_mse))
                test_log = open(cfg.log_router + 'test_' + str(i) + result +
                                r'.logs',
                                mode='a+',
                                encoding='utf-8')
                test_log.writelines(result + '\n')
                test_log.writelines(str(log_line) + '\n')
                test_log.close()
        log.close()
Ejemplo n.º 43
0
# defining hyperparameters
NUM_EPOCHS = 1
LEARNING_RATE = 0.01
GRADIENT_NORM = 5
EMBEDDING_DIM = 32
TOP_K = 5
HIDDEN_DIM = 32
BATCH_SIZE = 16
CHUNK_SIZE = 32
TRAIN_PATH = "data/french.txt"

# instantiating dataset, model, loss function, and optimizer
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dataset = CorpusDataset(TRAIN_PATH, CHUNK_SIZE, BATCH_SIZE)
model = Model(EMBEDDING_DIM, HIDDEN_DIM, len(dataset.vocabulary), device)
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)

iteration = 0

for i in range(NUM_EPOCHS):
    hidden_state, cell_state = model.init_hidden(BATCH_SIZE)

    for text, target in dataset:
        model.train()
        optimizer.zero_grad()

        output, (hidden_state, cell_state) = model(text,
                                                   (hidden_state, cell_state))
        loss = criterion(output.transpose(1, 2), target).to(device)
def eval_robustness(ARGS, verbose=True):
    #############################################
    # Load pre-trained model
    #############################################

    if verbose:
        print('\n- Loading pre-trained model...')

    # Build evaluation graph
    eval_graph = tf.Graph()
    config = tf.ConfigProto(log_device_placement=False,
                            allow_soft_placement=True)
    config.gpu_options.allow_growth = True
    sess = tf.Session(graph=eval_graph, config=config)

    # Define input TF placeholder
    with eval_graph.as_default():
        with tf.device('/gpu:0'):
            # Define placeholders
            with tf.name_scope('Placeholders'):
                x = tf.placeholder(dtype=tf.float32,
                                   shape=input_shape,
                                   name='inputs')
                y = tf.placeholder(dtype=tf.float32,
                                   shape=(None, n_classes),
                                   name='labels')
                is_training = tf.placeholder_with_default(False,
                                                          shape=(),
                                                          name='is-training')

            # Define model
            with tf.name_scope('Model'):
                model = Model(nb_classes=n_classes,
                              input_shape=input_shape,
                              is_training=is_training)

            # Define forward-pass
            with tf.name_scope('Logits'):
                logits = model.get_logits(x)
            with tf.name_scope('Probs'):
                preds = tf.nn.softmax(logits)

            # Restore the pre-trained model
            with sess.as_default():
                saver = tf.train.Saver()
                saver.restore(sess, ARGS.restore_path + '/model.ckpt')

            # Define accuracy ops
            with tf.name_scope('Accuracy'):
                ground_truth = tf.argmax(y, axis=1)
                predicted_label = tf.argmax(preds, axis=1)
                correct_prediction = tf.equal(predicted_label, ground_truth)
                clean_acc = tf.reduce_mean(tf.to_float(correct_prediction),
                                           name='accuracy')

            # Define PGD adversary
            if ARGS.attack == 'PGD':
                if verbose:
                    print('\n- Building {:s} attack graph...'.format(
                        ARGS.attack))

                with tf.name_scope('PGD-Attacker'):
                    pgd_params = {
                        'ord': np.inf,
                        'y': y,
                        'eps': ARGS.eps / 255,
                        'eps_iter': ARGS.eps_iter / 255,
                        'nb_iter': ARGS.nb_iter,
                        'rand_init': ARGS.rand_init,
                        'rand_minmax': ARGS.eps / 255,
                        'clip_min': 0.,
                        'clip_max': 1.,
                        'sanity_checks': True
                    }

                    pgd = ProjectedGradientDescent(model, sess=None)
                    adv_x = pgd.generate(x, **pgd_params)

            # Define SPSA adversary
            elif ARGS.attack == 'SPSA':
                if verbose:
                    print('\n- Building {:s} attack graph...'.format(
                        ARGS.attack))

                with tf.name_scope('PGD-Attacker'):
                    spsa_params = {
                        'y': y,
                        'eps': ARGS.eps / 255,
                        'nb_iter': ARGS.nb_iter,
                        'spsa_samples': ARGS.spsa_samples,
                        'spsa_iters': ARGS.spsa_iters,
                        'clip_min': 0.,
                        'clip_max': 1.,
                        'learning_rate': ARGS.spsa_lr,
                        'delta': ARGS.spsa_delta
                    }

                    spsa = SPSA(model, sess=sess)
                    adv_x = spsa.generate(x, **spsa_params)
            else:
                raise NotImplementedError

            with tf.name_scope('Logits'):
                adv_logits = model.get_logits(adv_x)
            with tf.name_scope('Probs'):
                adv_preds = tf.nn.softmax(adv_logits)

            adv_loss = tf.nn.softmax_cross_entropy_with_logits(
                logits=adv_logits, labels=y)
            adv_predicted_label = tf.argmax(adv_preds, axis=1)
            correct_prediction = tf.equal(adv_predicted_label, ground_truth)
            adv_accuracy = tf.reduce_mean(tf.to_float(correct_prediction),
                                          name='adv-accuracy')
            is_adv_example = tf.not_equal(ground_truth, adv_predicted_label)

    #############################################
    # Run evaluation
    #############################################

    if verbose:
        print('\n- Running robustness evaluation against {:s} attacker...\n'.
              format(ARGS.attack))

    if ARGS.attack == 'PGD':
        clean, adv_mean, adv_worstcase = run_pgd_eval(x,
                                                      y,
                                                      is_training,
                                                      sess,
                                                      adv_testloader,
                                                      clean_acc,
                                                      adv_accuracy,
                                                      adv_loss,
                                                      is_adv_example,
                                                      ARGS,
                                                      save_loss_dist=False,
                                                      verbose=verbose)

    elif ARGS.attack == 'SPSA':
        clean, adv_mean = run_spsa_eval(x,
                                        y,
                                        is_training,
                                        sess,
                                        adv_testloader,
                                        clean_acc,
                                        adv_accuracy,
                                        adv_loss,
                                        is_adv_example,
                                        ARGS,
                                        save_loss_dist=False,
                                        verbose=verbose)
        adv_worstcase = adv_mean
    else:
        raise NotImplementedError

    return clean, adv_mean, adv_worstcase
Ejemplo n.º 45
0
Archivo: user.py Proyecto: daeyun/ppbot
 def __init__(self, access=None, **kwargs):
     Model.__init__(self)
     Base.__init__(self)
     self.access = access
Ejemplo n.º 46
0
USE_MQTT = False  # To be able to test without MQTT
DEFAULT_CONFIDENCE = 0.5
DEFAULT_LOGFILE = "logs/infer_and_publish.log"
DEFAULT_LOGLEVEL = "DEBUG"

DEFAULT_DEVICE = "MYRIAD"  #CPU
if DEFAULT_DEVICE == "CPU":
    DEFAULT_PREC = 32
else:
    DEFAULT_PREC = 16

DEFAULT_INPUT = 'resources/Pedestrian_Detect_2_1_1.mp4'
isImage = False
allow_print = False  #if --dev: print() else log.debug()

model = Model().get_default()
ini_time = time.perf_counter()


def build_argparser():
    """
    Parse command line arguments.

    :return: command line arguments
    """
    parser = ArgumentParser()
    parser.add_argument("-m",
                        "--model",
                        required=False,
                        type=str,
                        default=model['path'],
Ejemplo n.º 47
0
 def populate(self):
   new_model = Model()
   return new_model.save()
Ejemplo n.º 48
0
        batch_inputs = torch.zeros((batch_size, max_doc_len, max_sent_len),
                                   dtype=torch.int64)
        batch_masks = torch.zeros((batch_size, max_doc_len, max_sent_len),
                                  dtype=torch.int64)
        batch_labels = torch.LongTensor(doc_labels)

        for b in range(batch_size):
            for sent_idx in range(doc_lens[b]):
                sent_data = batch_data[b][2][sent_idx]  # 表示一个句子
                for word_idx in range(sent_data[0]):
                    batch_inputs[b, sent_idx,
                                 word_idx] = sent_data[1][word_idx]
                    batch_masks[b, sent_idx, word_idx] = 1

        if use_cuda:
            batch_inputs = batch_inputs.to(device)
            batch_masks = batch_masks.to(device)
            batch_labels = batch_labels.to(device)

        return (batch_inputs, batch_masks), batch_labels


if __name__ == '__main__':
    train_data, dev_data, test_data = get_data(fold_num, dev_fold)
    vocab = Vocab(train_data)
    model = Model(vocab)
    trainer = Trainer(model, vocab, train_data, dev_data, test_data)
    trainer.train()
    print(vocab._label2id)
Ejemplo n.º 49
0
class Pipeline():
    
    def __init__( self, feature_engineer=None, model=None ):
        # configure logging
        self.logger = logging.getLogger("Pipeline")
        handler = logging.FileHandler(settings.LOG_PATH)
        handler.setFormatter(logging.Formatter(
            '%(asctime)s %(levelname)s %(name)s: %(message)s'))
        self.logger.addHandler(handler)
        self.logger.setLevel(logging.DEBUG)
        
        self.settings = Settings()        
        self.feature_engineer = FeatureEngineer( self.settings )
        self.model = Model().get_model( Settings() )
            
        
    def read( self, file_path ): 
        self.settings.read( file_path )
        self.read_feature_engineer( settings.feature_engineer_path )
        self.read_model( settings.model_path )
        
    def read_feature_engineer( self, file_path ):
        print('implement this')
        
    def read_model( self, file_path ):
        self.model = joblib.load( file_path+'_main.pkl' ) 
        self.model.load_model(file_path)          
        
        
    def write( self, file_path ):
        self.settings.write( file_path )
        self.write_feature_engineer( settings.feature_engineer_path )
        self.write_model( settings.model_path )
        
    def write_feature_engineer( self, file_path ):
        print('implement this')
        
    def write_model( self, file_path ):
        joblib.dump( self.model, file_path+'_main.pkl' ) 
        self.model.store_model(file_path)  
        
        
    def set_feature_engineer( self, feature_engineer ):
        self.feature_engineer = feature_engineer
        
    def set_model( self, model ):
        self.model = model


    def train( self ):
        # Create a "local" train and testset
        initial_preparation.create_test_and_train_set()

        # Feature engineer ( Transform features and create new ones )
        self.logger.info('Start Feature Engineering')
        train_data = self.feature_engineer.engineer_features(settings.LOKAL_TRAIN, settings.PROCESSED_TRAIN)
        self.feature_engineer.train_state = False
        test_data = self.feature_engineer.engineer_features (settings.LOKAL_TEST, settings.PROCESSED_TEST )

        # Train model and evaluate 
        self.logger.info('Train and Evaluate Model')
        score = self.model.train_model()
        self.model.store_model()
        self.logger.info('Model Score is %f', score)
        
    def predict( self ):
        # Feature engineer ( Transform features and create new ones )
        self.logger.info('Start Feature Engineering')
        self.feature_engineer.train_state = False
        test_data = self.feature_engineer.engineer_features( settings.GLOBAL_TEST, settings.PROCESSED_GLOBAL )
        
        #test_data.drop( settings.ID_FIELD, inplace=True, axis=1 )
        
        # Train model and evaluate 
        self.logger.info('Train and Evaluate Model')
        self.model.load_model()
        ids, prediction = self.model.predict_submission()
        
        submission_writer.create_submission( ids, prediction )
Ejemplo n.º 50
0

#
if __name__ == '__main__':

    n_burn_in = 10
    n_samples = 100

    pot = Ring_Potential(scale=5, bias=-1)

    x0 = np.asarray([[0.], [0.]])
    rng = np.random.RandomState()
    model = Model(x0,
                  rng=rng,
                  pot=pot,
                  n_steps=50,
                  step_size=0.1,
                  verbose=True,
                  rand_steps=True)

    # adjust for nice plotting
    print "Running Model: {}".format(__file__)
    model.run(n_samples=n_samples,
              n_burn_in=n_burn_in,
              mixing_angle=np.pi / 6.)
    print "Finished Running Model: {}".format(__file__)

    # change shape from (n, 2) -> (2, n)
    samples = model.samples
    burn_in = model.burn_in
Ejemplo n.º 51
0
	def __init__(self, dbpath):
		Model.__init__(self, dbpath, "proxy_users")
Ejemplo n.º 52
0
def main(opts):
    if not opts.do_train and not opts.do_eval:
        raise ValueError(
            'At least one of `do_train` or `do_eval` must be True.')
    if os.path.exists(opts.output_dir) and os.listdir(
            opts.output_dir) and opts.do_train:
        raise ValueError(
            'Output directory ({}) already exists and is not empty.'.format(
                opts.output_dir))

    # Create the output directory (if not exists)
    create_dir_if_not_exists(opts.output_dir)

    # Device device type
    opts.device = torch.device(
        'cuda' if torch.cuda.is_available() and not opts.no_cuda else 'cpu')
    opts.n_gpus = torch.cuda.device_count() if str(
        opts.device) == 'cuda' else 0
    print('Device Type: {} | Number of GPUs: {}'.format(
        opts.device, opts.n_gpus),
          flush=True)

    # Load Datasets and Ontology
    if opts.language == 'cn':
        dataset, ontology = load_dataset_cn(opts.data_dir)
    else:
        dataset, ontology = load_dataset(opts.data_dir)
    print('Loaded Datasets and Ontology', flush=True)
    print('Number of Train Dialogues: {}'.format(len(dataset['train'])),
          flush=True)
    print('Number of Dev Dialogues: {}'.format(len(dataset['dev'])),
          flush=True)
    print('Number of Test Dialogues: {}'.format(len(dataset['test'])),
          flush=True)

    if opts.do_train:
        # Load model from scratch
        model = Model.from_scratch(opts.bert_model)
        model.move_to_device(opts)
        print('Number of model parameters is: {}'.format(get_n_params(model)))

        # Start Training
        print('Start Training', flush=True)
        model.run_train(dataset, ontology, opts)

        # Free up all memory pytorch is taken from gpu memory
        del model
        torch.cuda.empty_cache()

    if opts.do_eval:
        if not (os.path.exists(opts.output_dir)
                and os.listdir(opts.output_dir)):
            raise ValueError(
                'Output directory ({}) is empty. Cannot do evaluation'.format(
                    opts.output_dir))

        # Load trained model
        model = Model.from_model_path(opts.output_dir)
        model.move_to_device(opts)
        print('Number of model parameters is: {}'.format(get_n_params(model)))

        # Start evaluating
        print('Start Evaluating', flush=True)
        print(model.run_dev(dataset, ontology, opts), flush=True)
        print(model.run_test(dataset, ontology, opts), flush=True)
 def __init__(self, train):
   Model.__init__(self, train)
   dayage = train['dayage'].values
   self.begin = dayage[0]
   self.end = dayage[-1]
   self.mean = train['count'].mean()
Ejemplo n.º 54
0
class Evaluation(object):
    def __init__(self, hparams, model=None):

        self.hparams = hparams
        self.model = model
        self._logger = logging.getLogger(__name__)
        self.device = (torch.device("cuda", self.hparams.gpu_ids[0]) if
                       self.hparams.gpu_ids[0] >= 0 else torch.device("cpu"))
        self.split = hparams.evaluate_data_type
        print("Evaluation Split :", self.split)
        do_valid, do_test = False, False

        if self.split == "dev":
            do_valid = True
        else:
            do_test = True
        self._build_dataloader(do_valid=do_valid, do_test=do_test)
        self._dataloader = self.valid_dataloader if self.split == 'dev' else self.test_dataloader

        if model is None:
            print("No pre-defined model!")
            self._build_model()

    def _build_dataloader(self, do_valid=False, do_test=False):

        if do_valid:
            self.valid_dataset = ResponseSelectionDataset(
                self.hparams,
                split="dev",
            )
            self.valid_dataloader = DataLoader(
                self.valid_dataset,
                batch_size=self.hparams.eval_batch_size,
                num_workers=self.hparams.cpu_workers,
                drop_last=False,
            )

        if do_test:
            self.test_dataset = ResponseSelectionDataset(
                self.hparams,
                split="test",
            )

            self.test_dataloader = DataLoader(
                self.test_dataset,
                batch_size=self.hparams.eval_batch_size,
                num_workers=self.hparams.cpu_workers,
                drop_last=False,
            )

    def _build_model(self):
        self.model = Model(self.hparams)
        self.model = self.model.to(self.device)
        # Use Multi-GPUs
        if -1 not in self.hparams.gpu_ids and len(self.hparams.gpu_ids) > 1:
            self.model = nn.DataParallel(self.model, self.hparams.gpu_ids)

    def run_evaluate(self, evaluation_path):
        self._logger.info("Evaluation")
        model_state_dict, optimizer_state_dict = load_checkpoint(
            evaluation_path)
        print(evaluation_path)
        if isinstance(self.model, nn.DataParallel):
            self.model.module.load_state_dict(model_state_dict)
        else:
            self.model.load_state_dict(model_state_dict)

        k_list = self.hparams.recall_k_list
        total_mrr, total_prec_at_one, total_map = 0, 0, 0
        total_examples, total_correct = 0, 0

        self.model.eval()

        with torch.no_grad():
            for batch_idx, batch in enumerate(tqdm(self._dataloader)):
                buffer_batch = batch.copy()
                for task_key in batch:
                    for key in buffer_batch[task_key]:
                        buffer_batch[task_key][key] = buffer_batch[task_key][
                            key].to(self.device)
                # for key in buffer_batch["res_sel"]:
                # 	buffer_batch["res_sel"][key] = buffer_batch["res_sel"][key].to(self.device)

                logits, loss = self.model(buffer_batch)
                pred = torch.sigmoid(logits).to("cpu").tolist()

                rank_by_pred, pos_index, stack_scores = \
                  calculate_candidates_ranking(np.array(pred), np.array(buffer_batch["res_sel"]["label"].to("cpu").tolist()),
                                               self.hparams.evaluate_candidates_num)

                num_correct = logits_recall_at_k(pos_index, k_list)

                if self.hparams.task_name in ["douban", "kakao"]:
                    total_prec_at_one += precision_at_one(rank_by_pred)
                    total_map += mean_average_precision(pos_index)
                    for pred in rank_by_pred:
                        if sum(pred) == 0:
                            total_examples -= 1

                total_mrr += logits_mrr(pos_index)

                total_correct = np.add(total_correct, num_correct)
                total_examples += math.ceil(
                    buffer_batch["res_sel"]["label"].size()[0] /
                    self.hparams.evaluate_candidates_num)

                recall_result = ""
                if (batch_idx + 1) % self.hparams.evaluate_print_step == 0:
                    for i in range(len(k_list)):
                        recall_result += "Recall@%s : " % k_list[
                            i] + "%.2f%% | " % (
                                (total_correct[i] / total_examples) * 100)
                    else:
                        print(
                            "%d[th] | %s | MRR : %.3f | P@1 : %.3f | MAP : %.3f"
                            % (batch_idx + 1, recall_result,
                               float(total_mrr / total_examples),
                               float(total_prec_at_one / total_examples),
                               float(total_map / total_examples)))
                    self._logger.info(
                        "%d[th] | %s | MRR : %.3f | P@1 : %.3f | MAP : %.3f" %
                        (batch_idx + 1, recall_result,
                         float(total_mrr / total_examples),
                         float(total_prec_at_one / total_examples),
                         float(total_map / total_examples)))

            avg_mrr = float(total_mrr / total_examples)
            avg_prec_at_one = float(total_prec_at_one / total_examples)
            avg_map = float(total_map / total_examples)
            recall_result = ""

            for i in range(len(k_list)):
                recall_result += "Recall@%s : " % k_list[i] + "%.2f%% | " % (
                    (total_correct[i] / total_examples) * 100)
            print(recall_result)
            print("MRR: %.4f" % avg_mrr)
            print("P@1: %.4f" % avg_prec_at_one)
            print("MAP: %.4f" % avg_map)

            self._logger.info(recall_result)
            self._logger.info("MRR: %.4f" % avg_mrr)
            self._logger.info("P@1: %.4f" % avg_prec_at_one)
            self._logger.info("MAP: %.4f" % avg_map)
Ejemplo n.º 55
0
class Execute:
    def __init__(self, path):
        self.config = Configuration.construct(path)
        self.env = Environment(self.config)
        self.memory = ReplayMemory(self.config)
        self.model = Model(self.config)
        self.ep = None

    def get_epsilon(self, is_play):
        if is_play:
            return self.config.play.ep
        ep_start = self.config.train.ep.start
        ep_final = self.config.train.ep.final
        ep_num_frames = self.config.train.ep.num_frames
        decay = (ep_start - ep_final) / ep_num_frames
        if self.ep is None:
            self.ep = ep_start
        self.ep = max(self.ep - decay, ep_final)
        return self.ep

    def log(self, **kawrgs):
        log = ""
        for name, value in kawrgs.items():
            log += f"{name}: {value}, "
        print(log)

    def run_episode(self, episode=1, steps=0, is_play=True, debug=False):
        config = self.config

        self.env.reset()
        action = 1
        _, _, curr_state, is_done = self.env.step(action)
        total_reward = 0
        update_net = 0; C = config.train.network_update_freq
        t = 0; T = config.max_episode_length

        while not is_done and t < T:
            if t % config.action_repeat == 0:
                ep = self.get_epsilon(is_play)
                action = self.model.choose_action(curr_state, ep)
            prev_state, reward, curr_state, is_done = self.env.step(action)
            total_reward += reward
            t += 1

            if is_play:
                self.env.render("human")
                if debug and t % config.play.debug.time == 0:
                    self.log(ftype=self.env.get_frame_type(), action=action, reward=total_reward)
                continue

            self.memory.add((prev_state, action, reward, curr_state, is_done))
            if self.memory.get_size() > config.train.replay_start_size:
                for i in range(config.train.batch_run):
                    batch = self.memory.sample()
                    self.model.optimize(batch)
                    steps = (steps + 1) % C
                if steps % C == 0:
                    self.model.update_qhat()
                    update_net += 1

        if not is_play and debug and episode % config.train.debug.time == 0:
            self.log(ftype=self.env.get_frame_type(), total_reward=total_reward, network_update_steps=update_net, episode_time=t, ep=ep)

        return total_reward, steps

    def load_model(self):
        ftype = self.env.get_frame_type()
        in_size = self.env.get_in_size()
        num_actions = self.env.get_num_actions()
        self.model.load_model(ftype, in_size, num_actions)

    def play(self, debug=False):
        self.load_model()
        for ep in range(1):
            self.run_episode(is_play=True, debug=debug)

    def train(self, debug=False):
        self.load_model()
        optimize_steps = 0
        episodes = self.config.train.episodes
        for episode in range(1, episodes+1):
            reward, steps = self.run_episode(episode=episode, steps=optimize_steps, is_play=False, debug=debug)
            optimize_steps += steps
            if episode % self.config.train.save_model_episode == 0:
                self.model.save_model()
        self.model.update_qhat()
        self.model.save_model()

    def close(self):
        self.env.close()
        self.memory.close()
Ejemplo n.º 56
0
 def _build_model(self):
     self.model = Model(self.hparams)
     self.model = self.model.to(self.device)
     # Use Multi-GPUs
     if -1 not in self.hparams.gpu_ids and len(self.hparams.gpu_ids) > 1:
         self.model = nn.DataParallel(self.model, self.hparams.gpu_ids)
Ejemplo n.º 57
0
	def __init__(self, dbpath):
		Model.__init__(self, dbpath, "downloads")
		self.Status = Status
Ejemplo n.º 58
0
class Agent:
    def __init__(self, action_dims, model_version=Config.model_version,
                                    q_version=Config.q_version,
                                    target_version=Config.target_version,
                                    loss_version=Config.loss_version):
        # self.action_num = action_num
        self.action_dims = action_dims
        self.epsilon = Config.initial_epsilon
        self.epsilon_final = Config.epsilon_final
        self.epsilon_start = Config.epsilon_start
        self.epsilon_decay = Config.epsilon_decay
        self.model_version = model_version
        self.q_version = q_version
        self.target_version = target_version
        self.loss_version = loss_version
        self.build_network()

    def build_network(self):
        self.Q_network = Model(self.action_dims, self.model_version, self.loss_version)
        self.target_network = Model(self.action_dims, self.model_version, self.loss_version)
        # Change learning rate for commen net !!!! Start from here
        if self.loss_version == 0:
            self.optimizers = optim.Adam(self.Q_network.parameters(), lr=Config.lr)
        elif self.loss_version == 1:
            if self.model_version == 1:
                self.optimizers = [optim.Adam([
                    {'params': self.Q_network.multi_output_1.parameters(), 'lr':Config.lr},
                    {'params': self.Q_network.fc2.parameters()},
                    {'params': self.Q_network.fc1.parameters()},
                    {'params': self.Q_network.lstm1.parameters()}
                    ], lr=0.5*Config.lr), 
                    optim.Adam([
                    {'params': self.Q_network.multi_output_2.parameters(), 'lr':Config.lr},
                    {'params': self.Q_network.fc2.parameters()},
                    {'params': self.Q_network.fc1.parameters()},
                    {'params': self.Q_network.lstm1.parameters()}
                    ], lr=0.5*Config.lr)]
            elif self.model_version == 2:
                self.optimizers = [optim.Adam([
                    {'params': self.Q_network.multi_output_1.parameters(), 'lr':Config.lr},
                    {'params': self.Q_network.fc2.parameters()},
                    {'params': self.Q_network.fc1.parameters()},
                    {'params': self.Q_network.lstm1.parameters()},
                    {'params': self.Q_network.dueling.parameters()}
                    ], lr=0.5*Config.lr), 
                    optim.Adam([
                    {'params': self.Q_network.multi_output_2.parameters(), 'lr':Config.lr},
                    {'params': self.Q_network.fc2.parameters()},
                    {'params': self.Q_network.fc1.parameters()},
                    {'params': self.Q_network.lstm1.parameters()},
                    {'params': self.Q_network.dueling.parameters()}
                    ], lr=0.5*Config.lr)]
        
    
    def update_target_network(self):
        # copy current_network to target network
        self.target_network.load_state_dict(self.Q_network.state_dict())
    
    def update_Q_network_v0(self, state, action, reward, state_new, terminal):
        state = torch.from_numpy(state).float()
        action = torch.from_numpy(action).float()
        state_new = torch.from_numpy(state_new).float()
        terminal = torch.from_numpy(terminal).float()
        reward = torch.from_numpy(reward).float()
        state = Variable(state)
        action = Variable(action)                  # shape (batch, 6*7)
        state_new = Variable(state_new)
        terminal = Variable(terminal)
        reward = Variable(reward)
        self.Q_network.eval()
        self.target_network.eval()
        
        # use current network to evaluate action argmax_a' Q_current(s', a')_
        actions_new = self.Q_network.forward(state_new).max(dim=1)[1].cpu().data.view(-1, 1)
        actions_new_onehot = torch.zeros(Config.sampling_batch_size, self.action_dims[0]*self.action_dims[1]) 
        actions_new_onehot = Variable(actions_new_onehot.scatter_(1, actions_new, 1.0))
        
        # Different loss and object
        # use target network to evaluate value y = r + discount_factor * Q_tar(s', a')
        y = reward + torch.mul(((self.target_network.forward(state_new)*actions_new_onehot).sum(dim=1)*terminal),Config.discount_factor)
        self.Q_network.train()
        Q = (self.Q_network.forward(state)*action).sum(dim=1)
        losses = []
        # y = reward + torch.mul(((self.target_network.forward(state_new)[action_idx]*actions_new_onehot[action_idx]).sum(dim=1)*terminal), Config.discount_factor)
        # Q = (self.Q_network.forward(state)[action_idx]*actions[action_idx]).sum(dim=1)
        loss = mse_loss(input=Q, target=y.detach())
        self.optimizers.zero_grad()
        loss.backward()
        self.optimizers.step()
        losses.append(loss.item())
        # print(losses)
        return losses

    def update_Q_network_v1(self, state, action_1, action_2, reward, state_new, terminal):
        state = torch.from_numpy(state).float()
        action_1 = torch.from_numpy(action_1).float()
        action_2 = torch.from_numpy(action_2).float()
        state_new = torch.from_numpy(state_new).float()
        terminal = torch.from_numpy(terminal).float()
        reward = torch.from_numpy(reward).float()
        state = Variable(state)
        action_1 = Variable(action_1)                 
        action_2 = Variable(action_2)
        state_new = Variable(state_new)
        terminal = Variable(terminal)
        reward = Variable(reward)
        self.Q_network.eval()
        self.target_network.eval()
        
        # use current network to evaluate action argmax_a' Q_current(s', a')_
        new_q_values = self.Q_network.forward(state_new)
        actions_new = [torch.max(q_value, 1)[1].cpu().data.view(-1, 1) for q_value in new_q_values] 
        actions_new_onehot = [torch.zeros(Config.sampling_batch_size, action_dim) for action_dim in self.action_dims]
        actions_new_onehot = [Variable(actions_new_onehot[action_idx].scatter_(1, actions_new[action_idx], 1.0)) for action_idx in range(len(self.action_dims))]
        
        # Different loss and object
        # use target network to evaluate value y = r + discount_factor * Q_tar(s', a')
        actions = [action_1, action_2]
        raw_y = self.target_network.forward(state_new)
        y = []
        for new_q_idx in range(len(raw_y)):
            y.append(reward + torch.mul(((raw_y[new_q_idx]*actions_new_onehot[new_q_idx]).sum(dim=1)*terminal),Config.discount_factor))
        self.Q_network.train()
        raw_Q = self.Q_network.forward(state)
        Q = []
        for q_idx in range(len(raw_Q)):
            Q.append((raw_Q[q_idx]*actions[q_idx]).sum(dim=1))
        
        # Calculate loss
        losses = []
        if self.loss_version == 0:
            loss = 0.0
            for action_idx in range(len(self.action_dims)):
                loss += mse_loss(input=Q[action_idx], target=y[action_idx].detach())
            loss /= len(self.action_dims)
            self.optimizers.zero_grad()
            loss.backward()
            self.optimizers.step()
            losses.append(loss.item())

        elif self.loss_version == 1:
            for action_idx in range(len(self.action_dims)):
                loss = mse_loss(input=Q[action_idx], target=y[action_idx].detach())
                self.optimizers[action_idx].zero_grad()
                if action_idx < len(self.action_dims)-1:
                    loss.backward(retain_graph=True)
                else:
                    loss.backward()
                self.optimizers[action_idx].step()
                losses.append(loss.item())
        # print(losses)    
        return losses

    def update_Q_network_v2(self, state, action_1, action_2, reward, state_new, terminal):
        state = torch.from_numpy(state).float()
        action_1 = torch.from_numpy(action_1).float()
        action_2 = torch.from_numpy(action_2).float()
        state_new = torch.from_numpy(state_new).float()
        terminal = torch.from_numpy(terminal).float()
        reward = torch.from_numpy(reward).float()
        state = Variable(state)
        action_1 = Variable(action_1)                 
        action_2 = Variable(action_2)
        state_new = Variable(state_new)
        terminal = Variable(terminal)
        reward = Variable(reward)
        self.Q_network.eval()
        self.target_network.eval()
        
        # use current network to evaluate action argmax_a' Q_current(s', a')_
        # Get argmax action does not need aggregation
        new_q_values = self.Q_network.forward(state_new)
        # print(torch.max(new_q_values[0],1)[0], torch.max(new_q_values[0],1)[0].data)
        # print(new_q_values[0], new_q_values[0] + new_q_values[2], new_q_values[2])   
        actions_new = [torch.max(q_value, 1)[1].cpu().data.view(-1, 1) for q_value in new_q_values[:2]] 
        actions_new_onehot = [torch.zeros(Config.sampling_batch_size, action_dim) for action_dim in self.action_dims]
        actions_new_onehot = [Variable(actions_new_onehot[action_idx].scatter_(1, actions_new[action_idx], 1.0)) for action_idx in range(len(self.action_dims))]

        actions = [action_1, action_2]
        raw_y = self.target_network.forward(state_new)
        if self.q_version == 0:
            # Q = V + A
            # Get Q(s',a')
            aggregation = [raw_y[0]+ raw_y[2], raw_y[1]+raw_y[2]]
            # Get Q(s,a)
            self.Q_network.train()
            raw_Q = self.Q_network.forward(state)
            Q = []
            for q_idx in range(len(raw_Q[:2])):
                Q.append(((raw_Q[q_idx]+raw_Q[2])*actions[q_idx]).sum(dim=1))
        elif self.q_version == 1:
            # Q = V + Ad-max(Ad)
            # print(raw_y[0])
            # print(raw_y[1])
            # print(raw_y[2])
            # print(torch.max(raw_y[0],1, keepdim=True)[0])
            branch_0 = raw_y[0]-torch.max(raw_y[0], 1, keepdim=True)[0].data + raw_y[2] 
            branch_1 = raw_y[1]-torch.max(raw_y[1], 1, keepdim=True)[0].data + raw_y[2]
            aggregation = [branch_0, branch_1]
            print(aggregation)
            # Get Q(s,a)
            self.Q_network.train()
            raw_Q = self.Q_network.forward(state)
            Q = []
            for q_idx in range(len(raw_Q[:2])):
                Q.append(((raw_Q[q_idx]-torch.max(raw_y[0], 1, keepdim=True)[0].data+raw_Q[2])*actions[q_idx]).sum(dim=1))
        elif self.q_version == 2:
            # Q = V + Ad - mean(Ad)
            branch_0 = raw_y[0]-torch.mean(raw_y[0], 1, keepdim=True)[0].data + raw_y[2] 
            branch_1 = raw_y[1]-torch.mean(raw_y[1], 1, keepdim=True)[0].data + raw_y[2]
            aggregation = [branch_0, branch_1]
            # Get Q(s,a)
            self.Q_network.train()
            raw_Q = self.Q_network.forward(state)
            Q = []
            for q_idx in range(len(raw_Q[:2])):
                Q.append(((raw_Q[q_idx]-torch.mean(raw_y[0], 1, keepdim=True)[0].data+raw_Q[2])*actions[q_idx]).sum(dim=1))
       
        # Calculate target indep or global and then do loss (v0 or v1)
        losses = []
        if self.target_version == 0:
            # Independent target, 2 branches 
            y = []
            for new_q_idx in range(len(aggregation)):
                y.append(reward + torch.mul(((aggregation[new_q_idx]*actions_new_onehot[new_q_idx]).sum(dim=1)*terminal),Config.discount_factor))
            # print(y)

            # Calculate loss
            if self.loss_version == 0:
                loss = 0.0
                for action_idx in range(len(self.action_dims)):
                    loss += mse_loss(input=Q[action_idx], target=y[action_idx].detach())
                loss/=len(self.action_dims)                
                self.optimizers.zero_grad()
                loss.backward()
                self.optimizers.step()
                losses.append(loss.item())
            elif self.loss_version == 1:
                for action_idx in range(len(self.action_dims)):
                    loss = mse_loss(input=Q[action_idx], target=y[action_idx].detach())
                    self.optimizers[action_idx].zero_grad()
                    if action_idx < len(self.action_dims)-1:
                        loss.backward(retain_graph=True)
                    else:
                        loss.backward()
                    self.optimizers[action_idx].step()
                    losses.append(loss.item())
        elif self.target_version == 1:
            # Global target using max of branches, one y
            # Torch.max support find max over two 300*6 and 300*7 tensors
            local_max_0 = torch.mul(((aggregation[0]*actions_new_onehot[0]).sum(dim=1)*terminal),Config.discount_factor)
            local_max_1 = torch.mul(((aggregation[1]*actions_new_onehot[1]).sum(dim=1)*terminal),Config.discount_factor)
            # print(local_max_0, local_max_1)
            y = reward + torch.max(local_max_0, local_max_1)
            # print(y)
            # Calculate loss
            if self.loss_version == 0:
                loss = 0.0
                for action_idx in range(len(self.action_dims)):
                    loss += mse_loss(input=Q[action_idx], target=y.detach())
                loss/=len(self.action_dims)                
                self.optimizers.zero_grad()
                loss.backward()
                self.optimizers.step()
                losses.append(loss.item())
            elif self.loss_version == 1:
                for action_idx in range(len(self.action_dims)):
                    loss = mse_loss(input=Q[action_idx], target=y.detach())
                    self.optimizers[action_idx].zero_grad()
                    if action_idx < len(self.action_dims)-1:
                        loss.backward(retain_graph=True)
                    else:
                        loss.backward()
                    self.optimizers[action_idx].step()
                    losses.append(loss.item())

        # Global target using global average
        elif self.target_version == 2:
            local_max_0 = torch.mul(((aggregation[0]*actions_new_onehot[0]).sum(dim=1)*terminal),Config.discount_factor)
            local_max_1 = torch.mul(((aggregation[1]*actions_new_onehot[1]).sum(dim=1)*terminal),Config.discount_factor)
            # print(local_max_0)
            # print(local_max_1)
            # print(torch.mean(torch.stack((local_max_0, local_max_1)),0))
            y = reward + torch.mean(torch.stack((local_max_0, local_max_1)),dim=0)
            # Calculate loss
            if self.loss_version == 0:
                loss = 0.0
                for action_idx in range(len(self.action_dims)):
                    loss += mse_loss(input=Q[action_idx], target=y.detach())
                loss/=len(self.action_dims)                
                self.optimizers.zero_grad()
                loss.backward()
                self.optimizers.step()
                losses.append(loss.item())
            elif self.loss_version == 1:
                for action_idx in range(len(self.action_dims)):
                    loss = mse_loss(input=Q[action_idx], target=y.detach())
                    self.optimizers[action_idx].zero_grad()
                    if action_idx < len(self.action_dims)-1:
                        loss.backward(retain_graph=True)
                    else:
                        loss.backward()
                    self.optimizers[action_idx].step()
                    losses.append(loss.item())
        # print(losses)
        return losses

    def take_action(self, state):
        state = torch.from_numpy(state).float()
        state = Variable(state)
        self.Q_network.eval()

        if self.model_version == 0:
            estimate = torch.max(self.Q_network.forward(state), 1)[1].data[0]
            if np.random.random() < self.epsilon:
                return np.random.randint(0, self.action_dims[0]*self.action_dims[1])
            else:
                return estimate

        elif self.model_version == 1:
            outputs = self.Q_network.forward(state)
            estimate = [torch.max(q_value, 1)[1].data[0] for q_value in outputs] 
            # with epsilon prob to choose random action else choose argmax Q estimate action
            if np.random.random() < self.epsilon:
                return [np.random.randint(0, self.action_dims[action_idx]) for action_idx in range(len(self.action_dims))]
            else:
                return estimate

        elif self.model_version == 2:
            outputs = self.Q_network.forward(state)[:2]
            estimate = [torch.max(q_value, 1)[1].data[0] for q_value in outputs] 
            # with epsilon prob to choose random action else choose argmax Q estimate action
            if np.random.random() < self.epsilon:
                return [np.random.randint(0, self.action_dims[action_idx]) for action_idx in range(len(self.action_dims))]
            else:
                return estimate

    def testing_take_action(self, state):
        state = torch.from_numpy(state).float()
        state = Variable(state)
        self.Q_network.eval()
        if self.model_version == 0:
            estimate = torch.max(self.Q_network.forward(state), 1)[1].data[0]
            return estimate

        elif self.model_version == 1:
            q_values = self.Q_network.forward(state)
            estimate = [torch.max(q_value, 1)[1].data[0] for q_value in q_values] 
            # with epsilon prob to choose random action else choose argmax Q estimate action
            return estimate

        elif self.model_version == 2:
            q_values = self.Q_network.forward(state)[:2]
            estimate = [torch.max(q_value, 1)[1].data[0] for q_value in q_values] 
            # with epsilon prob to choose random action else choose argmax Q estimate action
            return estimate

    def update_epsilon_by_epoch(self, epoch):
        self.epsilon = self.epsilon_final+(self.epsilon_start - self.epsilon_final) * math.exp(-1.*epoch/self.epsilon_decay)   
    
    def save(self, step, logs_path):
        os.makedirs(logs_path, exist_ok=True)
        model_list =  glob.glob(os.path.join(logs_path, '*.pth'))
        if len(model_list) > Config.maximum_model - 1 :
            min_step = min([int(li.split('/')[-1][6:-4]) for li in model_list]) 
            os.remove(os.path.join(logs_path, 'model-{}.pth' .format(min_step)))
        logs_path = os.path.join(logs_path, 'model-{}.pth' .format(step))
        self.Q_network.save(logs_path, step=step, optimizers=self.optimizers)
        print('=> Save {}' .format(logs_path)) 

    def train_restore(self, logs_path):
        model_list =  glob.glob(os.path.join(logs_path, '*.pth'))
        max_step = max([int(li.split('/')[-1][6:-4]) for li in model_list]) 
        model_path = os.path.join(logs_path, 'model-{}.pth' .format(max_step))
        self.Q_network.load(model_path, self.optimizers)
        self.target_network.load(model_path, self.optimizers)
        print('=> Restore {}' .format(model_path))
        return max_step + 1
    
    def restore(self, logs_path):
        if self.loss_version == 0:
            self.Q_network.load(logs_path, self.optimizers)
            self.target_network.load(logs_path, self.optimizers)
            print('=> Restore {}' .format(logs_path))
            # self.optimizers = optim.Adam(self.Q_network.parameters(), lr=Config.lr)
        elif self.loss_version == 1:
            self.Q_network.load(logs_path, self.optimizers)
            self.target_network.load(logs_path, self.optimizers)
            print('=> Restore {}' .format(logs_path)) 
Ejemplo n.º 59
0
 def __init__(self, env, alpha, gamma, eps):
     super().__init__(Model(), alpha, gamma)
     self.env = env
     self.eps = eps
     self.reset()
Ejemplo n.º 60
0
    mask = K.cast(K.greater(targ, 0),
                  dtype='float32')  #cast bool-tensor to float
    n_correct = K.sum(mask * correct)  #
    n_total = K.sum(mask)
    return n_correct / n_total


#custom loss because we dont want to consider padding
def loss(y_true, y_pred):
    # both are of shape ( _, Ty, VOCAB_SIZE )
    mask = K.cast(y_true > 0, dtype='float32')
    out = mask * y_true * K.log(y_pred)  #cross entopy loss
    return -K.sum(out) / K.sum(mask)


models = Model(VOCAB_SIZE, MAX_LEN, embedding_matrix)
train_model = models.model_train_teacher_forcing()
train_model.compile(optimizer="adam", loss=loss, metrics=[acc])

print("Training Started")
all_metrics = []
BATCH_SIZE = 224
TRAIN_BATCHES, VAL_BATCHES = len(x_train) // BATCH_SIZE, len(
    x_val) // BATCH_SIZE
for epoch in range(len(all_metrics) + 1, 15):
    tloss, tacc, ti, t0 = 0, 0, 0, time.time()
    for x, y in batch_generator(x_train,
                                y_train,
                                ytf_train,
                                batch_size=BATCH_SIZE):
        metrics_train = train_model.train_on_batch(x, y)