本文整理汇总了Python中model.Model.fit方法的典型用法代码示例。如果您正苦于以下问题:Python Model.fit方法的具体用法?Python Model.fit怎么用?Python Model.fit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类model.Model
的用法示例。
在下文中一共展示了Model.fit方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_full
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import fit [as 别名]
def run_full():
splits = get_crossval_data()
X = splits[0][0] + splits[1][0]
Y1 = splits[0][1] + splits[1][1]
Y2 = splits[0][2] + splits[1][2]
test_data = get_test_data()
remove_features_rfc = [19,20,34]
remove_features_lr = [19,20,21,22,23,24,25,26,29,30,31,32,34]
not_useful_rfc = [8,11,22,24,28,33,30,31,32]#9,21#30,31,32
remove_features_rfc.extend(not_useful_rfc)
not_useful_lr = [3,4,9,11,14,15,16,17,27,28,30,31,32]
remove_features_lr.extend(not_useful_lr)
z = [True] * len(X[0])
w = [True] * len(X[0])
for i in remove_features_rfc:
z[i] = False
for i in remove_features_lr:
w[i] = False
C = 0.03
#C = 0.3
m1 = Model(compress=z, has_none=w, C=C)
m1.fit(X, Y1)
final = False
results = run_model(m1, None, test_data, is_final=final)
if not final:
print evaluate_test_results(results)
示例2: run_full
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import fit [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)))
示例3: analyze
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import fit [as 别名]
def analyze(judged_class = 0):
train = get_data('tmp/train.csv')
test = get_data('tmp/test.csv')
C = 0.03
#C = 0.3
m1 = Model(judged_class = judged_class, C = C)
m1.fit(train['X'], train['Y'])
m1.analyze_threshold(test['X'], test['Y'])
示例4: image_train
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import fit [as 别名]
def image_train(x_train, y_train, x_test, model_path="model.json", weight_path="weights.h5"):
netModel = Model().image_model()
#sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
#netModel.compile(loss='mean_squared_error', optimizer=sgd, metrics=["accuracy"])
netModel.compile(loss='mean_squared_error', optimizer="rmsprop", metrics=["accuracy"])
print "STARTING TRAINING"
netModel.fit(x_train, y_train, nb_epoch=1, batch_size=10, shuffle=True, verbose=1)
# model.fit(data, label, batch_size=100,nb_epoch=10,shuffle=True,verbose=1,show_accuracy=True,validation_split=0.2)
y_predict = netModel.predict(x_test, batch_size=10)
save_model(netModel, model_path, weight_path)
return y_predict
示例5: run_compare
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import fit [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))
'''
示例6: run_statistics
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import fit [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))
示例7: __init__
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import fit [as 别名]
class Experiment:
"""
Machine Learning Experiment Interface
"""
def __init__(self):
self.model = Model()
self.description = "awesome-experiment"
pass
def set_description(self, description):
"""
:type description: str
"""
self.description = description
def get_data(self):
raise NotImplementedError()
def set_model(self, model):
"""
:type model: Model
"""
self.model = model
def run(self):
print " ====================================== "
print "| Data Gathering & Preparation |"
print " ====================================== "
self.get_data()
print " ====================================== "
print "| Model Building |"
print " ====================================== "
self.model.fit()
print " ====================================== "
print "| Model Evaluate |"
print " ====================================== "
self.model.evaluate()
示例8: run_crossval
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import fit [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)
示例9: Model
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import fit [as 别名]
seq, targets, tokens = c.encode(n_seq, n_steps, tokens, fs)
_, __, n_in = seq.shape
t0 = time.time()
#Creates the model to run the RNN.
params = {
'n_in': n_in,
'n_hid': n_hid,
'n_out': n_out,
'n_epochs': 250
}
model = Model(logger, params)
#Trains the RNN and runs the softmax signal.
while seq is not None and targets is not None:
model.fit(seq, targets, validation_freq=1000)
seqs = xrange(n_seq)
for seq_num in seqs:
tsm = time.time()
guess = model.predict_probability(seq[seq_num])
tsm = time.time() - tsm
softmax_time += tsm
logger.info("Softmax elapsed time: %f" % (tsm))
seq, targets, tokens = c.encode(n_seq, n_steps, tokens, fs)
logger.info("Total elapsed time: {} and softmax time: {} ".format(time.time() - t0, softmax_time))
示例10: main
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import fit [as 别名]
def main(args):
if args.gpu >= 0:
cuda.check_cuda_available()
xp = cuda.cupy if args.gpu >= 0 else np
model_id = build_model_id(args)
model_path = build_model_path(args, model_id)
setup_model_dir(args, model_path)
sys.stdout, sys.stderr = setup_logging(args)
x_train, y_train = load_model_data(args.train_file,
args.data_name, args.target_name,
n=args.n_train)
x_validation, y_validation = load_model_data(
args.validation_file,
args.data_name, args.target_name,
n=args.n_validation)
rng = np.random.RandomState(args.seed)
N = len(x_train)
N_validation = len(x_validation)
n_classes = max(np.unique(y_train)) + 1
json_cfg = load_model_json(args, x_train, n_classes)
print('args.model_dir', args.model_dir)
sys.path.append(args.model_dir)
from model import Model
model_cfg = ModelConfig(**json_cfg)
model = Model(model_cfg)
setattr(model, 'stop_training', False)
if args.gpu >= 0:
cuda.get_device(args.gpu).use()
model.to_gpu()
best_accuracy = 0.
best_epoch = 0
def keep_training(epoch, best_epoch):
if model_cfg.n_epochs is not None and epoch > model_cfg.n_epochs:
return False
if epoch > 1 and epoch - best_epoch > model_cfg.patience:
return False
return True
epoch = 1
while True:
if not keep_training(epoch, best_epoch):
break
if args.shuffle:
perm = np.random.permutation(N)
else:
perm = np.arange(N)
sum_accuracy = 0
sum_loss = 0
pbar = progressbar.ProgressBar(term_width=40,
widgets=[' ', progressbar.Percentage(),
' ', progressbar.ETA()],
maxval=N).start()
for j, i in enumerate(six.moves.range(0, N, model_cfg.batch_size)):
pbar.update(j+1)
x_batch = xp.asarray(x_train[perm[i:i + model_cfg.batch_size]].flatten())
y_batch = xp.asarray(y_train[perm[i:i + model_cfg.batch_size]])
pred, loss, acc = model.fit(x_batch, y_batch)
sum_loss += float(loss.data) * len(y_batch)
sum_accuracy += float(acc.data) * len(y_batch)
pbar.finish()
print('train epoch={}, mean loss={}, accuracy={}'.format(
epoch, sum_loss / N, sum_accuracy / N))
# Validation set evaluation
sum_accuracy = 0
sum_loss = 0
pbar = progressbar.ProgressBar(term_width=40,
widgets=[' ', progressbar.Percentage(),
' ', progressbar.ETA()],
maxval=N_validation).start()
for i in six.moves.range(0, N_validation, model_cfg.batch_size):
pbar.update(i+1)
x_batch = xp.asarray(x_validation[i:i + model_cfg.batch_size].flatten())
y_batch = xp.asarray(y_validation[i:i + model_cfg.batch_size])
pred, loss, acc = model.predict(x_batch, target=y_batch)
sum_loss += float(loss.data) * len(y_batch)
sum_accuracy += float(acc.data) * len(y_batch)
pbar.finish()
validation_accuracy = sum_accuracy / N_validation
validation_loss = sum_loss / N_validation
if validation_accuracy > best_accuracy:
#.........这里部分代码省略.........
示例11: week
# 需要导入模块: from model import Model [as 别名]
# 或者: from model.Model import fit [as 别名]
from config import *
from sklearn.metrics import confusion_matrix
def week():
X=[]
for i in range(7):
for t in range(24):
d = [0.0]*NUM_FEATURES
d[t]=1.0
d[NUM_INTERVALS+i]=1.0
# d[NUM_INTERVALS+NUM_DAY_OF_WEEK+loc]=1.0
X.append(d)
return np.array(X)
loc = pickle.load(open('./data/top_loc.dat','rb'))
loc = [loc[0]]
train_X, train_y, valid_X, valid_y, test_X, test_y = getTrainTestData()
print('train: {} valid: {}, test: {}'.format(len(train_X), len(valid_X), len(test_X)))
m = Model()
m.fit(train_X, train_y, valid_X, valid_y)
loss, acc = m.eva(test_X, test_y)
print('test: {}'.format(loss))
'''
X_week = week()
Y_week = m.predict(X_week)
visual_week = [Y_week[i] for i in range(len(Y_week))]
plt.plot(visual_week,'-')
plt.show()
'''