本文整理汇总了Python中model.Model.test方法的典型用法代码示例。如果您正苦于以下问题:Python Model.test方法的具体用法?Python Model.test怎么用?Python Model.test使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类model.Model
的用法示例。
在下文中一共展示了Model.test方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_full
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import test [as 别名]
def run_full():
train = get_data('tmp/train.csv')
test = get_data('tmp/test.csv')
w = [True] * len(train['X'][0])
C = 0.03
#C = 0.3
m1 = Model(has_none=w, C=C)
m1.fit(train['X'], train['Y'])
results = m1.test(test['X'])
error = 0
tp = 0
fp = 0
tn = 0
fn = 0
for i in range(len(results)):
if results[i] != test['Y'][i]:
error += 1
if results[i] == 1:
fp += 1
else:
fn += 1
elif results[i] == 1:
tp += 1
else:
tn += 1
print('tp = {}, fp = {}, tn = {}, fn = {}'.format(tp, fp, tn, fn))
print('error rate: {}'.format(float(error) / len(results)))
print('precision: {}'.format(float(tp) / (tp + fp + 1)))
print('recall: {}'.format(float(tp) / (tp + fn + 1)))
print('specificity: {}'.format(float(tn) / (tn + fp + 1)))
示例2: main
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import test [as 别名]
def main():
args = [i.lower() for i in sys.argv]
if 'help' in args or len(args) is 1:
print_help()
if 'download' in args:
down = Downloader()
down.download()
down.preprocess()
down.write_out(train="train.dat",test="test.dat")
if 'tag' in args:
t = Tagger()
t.tag("test.dat")
t.write_out("test_tagged.dat")
if 'train' in args:
m = Model()
m.train("train.dat")
m.write_out()
if 'test' in args:
m = Model("model.mdl")
m.test("test_tagged.dat")
示例3: run_compare
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import test [as 别名]
def run_compare():
train = get_data('tmp/train.csv')
test = get_data('tmp/test.csv')
C = 0.03
#C = 0.3
m1 = Model(C = C)
m1.fit(train['X'], train['Y'])
for judged_class in range(2):
m1.judged_class = judged_class
if judged_class == 0:
m1.threshold = 0.25
else:
m1.threshold = 0.69
results = m1.test(test['X'])
error = 0
tp = 0
fp = 0
tn = 0
fn = 0
for i in range(len(results)):
if results[i] != test['Y'][i]:
error += 1
if results[i] == 1:
fp += 1
else:
fn += 1
elif results[i] == 1:
tp += 1
else:
tn += 1
err = float(error) / len(results)
precision = float(tp) / (tp + fp + 1)
recall = float(tp) / (tp + fn + 1)
spec = float(tn) / (tn + fp + 1)
print('Judged class: {}'.format(judged_class))
print('tp = {}, fp = {}, tn = {}, fn = {}'.format(tp, fp, tn, fn))
print('error rate: {}'.format(err))
print('precision: {}'.format(precision))
print('recall: {}'.format(recall))
print('specificity: {}'.format(spec))
'''
示例4: run_statistics
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import test [as 别名]
def run_statistics(judged_class = 0, threshold = 0.6):
train = get_data('tmp/train.csv')
test = get_data('tmp/test.csv')
global out_stats
C = 0.03
#C = 0.3
m1 = Model(judged_class = judged_class, threshold = threshold, C = C)
m1.fit(train['X'], train['Y'])
for w0 in frange(0.1, 0.9, 0.1):
for w1 in frange(0.1, 0.9, 0.1):
m1.w0 = w0
m1.w1 = w1
print('({0}, {1}) passed'.format(w0, w1))
results = m1.test(test['X'])
error = 0
tp = 0
fp = 0
tn = 0
fn = 0
for i in range(len(results)):
if results[i] != test['Y'][i]:
error += 1
if results[i] == 1:
fp += 1
else:
fn += 1
elif results[i] == 1:
tp += 1
else:
tn += 1
err = float(error) / len(results)
precision = float(tp) / (tp + fp + 1)
recall = float(tp) / (tp + fn + 1)
specificity = float(tn) / (tn + fp + 1)
out_stats.write('({0}, {1})\t{2}\t{3}\t{4}\t{5}\n'.format(w0, w1, err, precision, recall, specificity))
示例5: run_crossval
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import test [as 别名]
def run_crossval():
splits = get_crossval_data()
results = []
for i in range(2):
s = splits[i]
other_s = splits[1 - i]
z = [True] * len(s[0][0])
w = [True] * len(s[0][0])
remove_features_rfc = [19,20]
remove_features_lr = [19,20,21,22,23,24,25,26,29,30,31,32]
for i in remove_features_rfc:
z[i] = False
for i in remove_features_lr:
w[i] = False
m1 = Model(compress=z, has_none=w)
m1.fit(s[0], s[1])
X = other_s[0]
predictions = m1.test(X)
keys = other_s[4]
pred_dict = {}
for j in xrange(len(keys)):
uid, eid = keys[j]
if uid not in pred_dict:
pred_dict[uid] = []
pred_dict[uid].append((eid, predictions[j]))
for uid, l in pred_dict.iteritems():
l.sort(key=lambda x: -x[1])
l = [e[0] for e in l]
results.append(apk(other_s[3][uid], l))
print sum(results) / len(results)
示例6: Model
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import test [as 别名]
FLAGS = flags.FLAGS
flags.DEFINE_boolean('test', False, 'If true, test against a random strategy.')
flags.DEFINE_boolean('play', False, 'If true, play against a trained TD-Gammon strategy.')
flags.DEFINE_boolean('restore', False, 'If true, restore a checkpoint before training.')
model_path = os.environ.get('MODEL_PATH', 'models/')
summary_path = os.environ.get('SUMMARY_PATH', 'summaries/')
checkpoint_path = os.environ.get('CHECKPOINT_PATH', 'checkpoints/')
if not os.path.exists(model_path):
os.makedirs(model_path)
if not os.path.exists(checkpoint_path):
os.makedirs(checkpoint_path)
if not os.path.exists(summary_path):
os.makedirs(summary_path)
if __name__ == '__main__':
graph = tf.Graph()
sess = tf.Session(graph=graph)
with sess.as_default(), graph.as_default():
model = Model(sess, model_path, summary_path, checkpoint_path, restore=FLAGS.restore)
if FLAGS.test:
model.test(episodes=1000)
elif FLAGS.play:
model.play()
else:
model.train()
示例7: Preprocessor
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import test [as 别名]
from preprocessor import Preprocessor
from model import Model
from optimizer import Optimizer
from global_constants import *
prep = Preprocessor()
train_x, train_y, test_x, test_y = prep.parse_data(PREPROCESS_TYPE["RM_PAD"])
I = train_x.shape[1] # input dim
K = 10 # output dim
model = Model(I, K)
model.set_hidden(HIDDEN_TYPE["EXPAND"])
model.set_activation(ACTIVATION_TYPE["SOFTMAX"])
optimizer = Optimizer(layer=2, loss=LOSS_TYPE["CROSS_ENTROPY"], lr=5, epoch=100000, batch_size=500)
model.set_optimizer(optimizer)
te, ta, ve, va = model.learn(train_x, train_y, time_limit=9.5)
test_err, test_acc = model.test(test_x, test_y)
示例8: Trainer
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import test [as 别名]
class Trainer(object):
def __init__(self, config, rng):
self.config = config
self.rng = rng
self.task = config.task
self.model_dir = config.model_dir
self.gpu_memory_fraction = config.gpu_memory_fraction
self.log_step = config.log_step
self.max_step = config.max_step
self.num_log_samples = config.num_log_samples
self.checkpoint_secs = config.checkpoint_secs
if config.task.lower().startswith('tsp'):
self.data_loader = TSPDataLoader(config, rng=self.rng)
else:
raise Exception("[!] Unknown task: {}".format(config.task))
self.model = Model(
config,
inputs=self.data_loader.x,
labels=self.data_loader.y,
enc_seq_length=self.data_loader.seq_length,
dec_seq_length=self.data_loader.seq_length,
mask=self.data_loader.mask)
self.build_session()
show_all_variables()
def build_session(self):
self.saver = tf.train.Saver()
self.summary_writer = tf.summary.FileWriter(self.model_dir)
sv = tf.train.Supervisor(logdir=self.model_dir,
is_chief=True,
saver=self.saver,
summary_op=None,
summary_writer=self.summary_writer,
save_summaries_secs=300,
save_model_secs=self.checkpoint_secs,
global_step=self.model.global_step)
gpu_options = tf.GPUOptions(
per_process_gpu_memory_fraction=self.gpu_memory_fraction,
allow_growth=True) # seems to be not working
sess_config = tf.ConfigProto(allow_soft_placement=True,
gpu_options=gpu_options)
self.sess = sv.prepare_or_wait_for_session(config=sess_config)
def train(self):
tf.logging.info("Training starts...")
self.data_loader.run_input_queue(self.sess)
summary_writer = None
for k in trange(self.max_step, desc="train"):
fetch = {
'optim': self.model.optim,
}
result = self.model.train(self.sess, fetch, summary_writer)
if result['step'] % self.log_step == 0:
self._test(self.summary_writer)
summary_writer = self._get_summary_writer(result)
self.data_loader.stop_input_queue()
def test(self):
tf.logging.info("Testing starts...")
self.data_loader.run_input_queue(self.sess)
for idx in range(10):
self._test(None)
self.data_loader.stop_input_queue()
def _test(self, summary_writer):
fetch = {
'loss': self.model.total_inference_loss,
'pred': self.model.dec_inference,
'true': self.model.dec_targets,
}
result = self.model.test(self.sess, fetch, summary_writer)
tf.logging.info("")
tf.logging.info("test loss: {}".format(result['loss']))
for idx in range(self.num_log_samples):
pred, true = result['pred'][idx], result['true'][idx]
tf.logging.info("test pred: {}".format(pred))
tf.logging.info("test true: {} ({})".format(true, np.array_equal(pred, true)))
if summary_writer:
summary_writer.add_summary(result['summary'], result['step'])
def _get_summary_writer(self, result):
if result['step'] % self.log_step == 0:
return self.summary_writer
else:
#.........这里部分代码省略.........