示例#1
0
    return (np.argmax(tpy, axis=1) == np.argmax(
        ty, axis=1)).sum() * 1.0 / tpy.shape[0]


# load the data: x, y, tx, ty, graph
NAMES = ['x', 'y', 'tx', 'ty', 'graph']
objects = {}
for name in NAMES:
    data = pkl.load(open("data/trans.{}.{}".format(DATASET, name), 'rb'),
                    encoding='latin1')
    objects[name] = data
#     objects.append(cPickle.load(open("data/trans.{}.{}".format(DATASET, name))))
# x, y, tx, ty, graph = tuple(objects)

# initialize the model
m = model(args)
# add data
m.add_data(objects['x'], objects['y'], objects['graph'])
# build the model
m.build()
# pre-training
m.init_train(init_iter_label=2000, init_iter_graph=70)
iter_cnt, max_accu = 0, 0
for _ in range(10000):
    # perform a training step
    m.step_train(max_iter=1, iter_graph=0, iter_inst=1, iter_label=0)
    # predict the dev set
    tpy = m.predict(objects['tx'])
    # compute the accuracy on the dev set
    accu = comp_accu(tpy, objects['ty'])
    print(iter_cnt, accu, max_accu)
示例#2
0
def comp_accu(tpy, ty):
    import numpy as np
    return (np.argmax(tpy, axis=1) == np.argmax(
        ty, axis=1)).sum() * 1.0 / tpy.shape[0]


# load the data: x, y, tx, ty, graph
NAMES = ['x', 'y', 'tx', 'ty', 'graph']
OBJECTS = []
for i in range(len(NAMES)):
    OBJECTS.append(
        cPickle.load(open("data/trans.{}.{}".format(DATASET, NAMES[i]))))
x, y, tx, ty, graph = tuple(OBJECTS)

m = model(args)  # initialize the model
m.add_data(x, y, graph)  # add data
m.build()  # build the model
m.init_train(init_iter_label=2000, init_iter_graph=70)  # pre-training
iter_cnt, max_accu = 0, 0
while True:
    m.step_train(max_iter=1, iter_graph=0, iter_inst=1,
                 iter_label=0)  # perform a training step
    tpy = m.predict(tx)  # predict the dev set
    accu = comp_accu(tpy, ty)  # compute the accuracy on the dev set
    print iter_cnt, accu, max_accu
    iter_cnt += 1
    if accu > max_accu:
        m.store_params()  # store the model if better result is obtained
        max_accu = max(max_accu, accu)
示例#3
0
parser.add_argument('--use_feature', help = 'whether use input features', type = bool, default = True)
parser.add_argument('--update_emb', help = 'whether update embedding when optimizing supervised loss', type = bool, default = True)
parser.add_argument('--layer_loss', help = 'whether incur loss on hidden layers', type = bool, default = True)
args = parser.parse_args()

def comp_accu(tpy, ty):
    import numpy as np
    return (np.argmax(tpy, axis = 1) == np.argmax(ty, axis = 1)).sum() * 1.0 / tpy.shape[0]

# load the data: x, y, tx, ty, graph
NAMES = ['x', 'y', 'tx', 'ty', 'graph']
OBJECTS = []
for i in range(len(NAMES)):
    OBJECTS.append(cPickle.load(open("data/trans.{}.{}".format(DATASET, NAMES[i]))))
x, y, tx, ty, graph = tuple(OBJECTS)

m = model(args)                                             # initialize the model
m.add_data(x, y, graph)                                     # add data
m.build()                                                   # build the model
m.init_train(init_iter_label = 2000, init_iter_graph = 70)  # pre-training
iter_cnt, max_accu = 0, 0
while True:
    m.step_train(max_iter = 1, iter_graph = 0, iter_inst = 1, iter_label = 0)   # perform a training step
    tpy = m.predict(tx)                                                         # predict the dev set
    accu = comp_accu(tpy, ty)                                                   # compute the accuracy on the dev set
    print iter_cnt, accu, max_accu
    iter_cnt += 1
    if accu > max_accu:
        m.store_params()                                                        # store the model if better result is obtained
        max_accu = max(max_accu, accu)