python 我的第一个合奏SPAGHETTI由Keras和示例代码提供支持
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 我的第一个合奏SPAGHETTI由Keras和示例代码提供支持相关的知识,希望对你有一定的参考价值。
import os
import argparse
import numpy as np
import tensorflow as tf
import keras.backend.tensorflow_backend as K
from util import *
paths = [
('LSTM', 'hw4-rnn-owo', 'model/fresh/model-002-0.80900.h5'),
('LSTM', 'hw4-rnn-owo', 'model/fresh/model-001-0.80987.h5'),
('LSTM', 'hw4-rnn-owo', 'model/fresh/model-001-0.80987.h5'),
('GRU', 'hw4-rnn-z', 'model/noob-0/model-002-0.80620.h5'),
('GRU', 'hw4-rnn-z', 'model/noob-0/model-002-0.80617.h5'),
('GRU', 'hw4-rnn-z', 'model/noob-0/model-001-0.80770.h5'),
('GRU', 'hw4-rnn-z', 'model/noob-0/model-001-0.80770.h5'),
('GRU', 'hw4-rnn-trad', 'model/noob/model-002-0.80767.h5'),
('GRU', 'hw4-rnn-trad', 'model/noob/model-002-0.80900.h5'),
('GRU', 'hw4-rnn-trad', 'model/model-002-0.81060.h5'),
('GRU', 'hw4-rnn-trad', 'model/model-002-0.81060.h5'),
('GRU', 'hw4-rnn-trad', 'model/model-002-0.81060.h5'),
('GRU', 'hw4-rnn-trad', 'model/model-deepq-0.8104.h5'),
('GRU', 'hw4-rnn-trad', 'model/model-deepq-0.8104.h5'),
('GRU', 'hw4-rnn-trad', 'model/model-deepq-best-2.h5'),
('GRU', 'hw4-rnn-trad', 'model/model-gpu-best.h5'),
('GRU', 'hw4-rnn-trad', 'model/model-gpu-best.h5'),
('LSTM', 'hw4-rnn-fresh', 'model/fresh-rmsprop/model-003-0.79623.h5'),
('LSTM', 'hw4-rnn-fresh', 'model/fresh-rmsprop/model-002-0.79787.h5'),
('LSTM', 'hw4-rnn-fresh', 'model/fresh-rmsprop/model-003-0.79733.h5'),
]
def makeModel(spec):
print('loading model', spec)
cell_type, defin, path = spec
if defin == 'hw4-rnn-z':
args.embedding_dim = 192
args.hidden_size = 320
elif defin == 'hw4-rnn-owo':
args.embedding_dim = 128
args.hidden_size = 512
else:
args.embedding_dim = 128
args.hidden_size = 384
args.cell = cell_type
model = __import__(defin).simpleRNN(args)
model.load_weights(path)
return model
def yieldVal(values):
return ((values.sum(axis=0) / len(paths)) >= 0.5).astype(int)
def main(args):
# limit gpu memory usage
def get_session(gpu_fraction):
gpu_options = tf.GPUOptions(
per_process_gpu_memory_fraction=gpu_fraction,
allow_growth=True)
return tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
K.set_session(get_session(args.gpu_fraction))
models = [ makeModel(p) for p in paths ]
dm = DataManager()
dm.add_data('train_data', args.data_train_path, True)
dm.add_testing_data('test_data', args.data_test_path)
dm.load_tokenizer(os.path.join('model', 'noob', 'token.pk'))
dm.to_sequence(40)
(X, Y), (X_val, Y_val) = dm.split_data('train_data', args.val_ratio)
[test_X] = dm.get_data('test_data')
probs = []
probs_eval = []
jj = 0
for m in models:
print('Iter #{}/{}'.format(jj, len(paths)))
probs.append(m.predict(test_X, batch_size=1024, verbose=True).squeeze())
probs_eval.append(m.predict(X_val, batch_size=1024, verbose=True).squeeze())
jj += 1
real_pred = yieldVal(np.array(probs))
real_pred_eval = yieldVal(np.array(probs_eval))
print('vali acc', sum((real_pred_eval == Y_val).astype(int)) / len(Y_val))
with open('result_ensemble.csv', 'w') as f:
f.write('id,label\n')
i = 0
for x in real_pred:
f.write('{},{}\n'.format(i, x))
i += 1
# print(pred.squeeze(), Y_val[:1024], ((pred.squeeze() > 0.5).astype(int) == Y_val[:1024]).astype(int).sum())
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Sentiment classification',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# training argument
parser.add_argument('--batch_size', help=' ', default=128, type=float)
parser.add_argument('--nb_epoch', help=' ', default=20, type=int)
parser.add_argument('--val_ratio', help=' ', default=0.15, type=float)
parser.add_argument('--gpu_fraction', help=' ', default=0.9, type=float)
parser.add_argument('--vocab_size', help=' ', default=20000, type=int)
parser.add_argument('--max_length', help=' ', default=40, type=int)
# model parameter
parser.add_argument('--loss_function', help=' ',
default='binary_crossentropy')
parser.add_argument('--cell', help=' ', default='LSTM',
choices=['LSTM', 'GRU'])
parser.add_argument('-emb_dim', '--embedding_dim',
help=' ', default=128, type=int)
parser.add_argument('-hid_siz', '--hidden_size',
help=' ', default=384, type=int)
parser.add_argument('--dropout_rate', help=' ', default=0.4, type=float)
parser.add_argument('-lr', '--learning_rate', help=' ',
default=0.001, type=float)
parser.add_argument('--threshold', help=' ', default=0.1, type=float)
# output path for your prediction
parser.add_argument('--result_path', help=' ', default='result.csv',)
# put model in the same directory
parser.add_argument('--save_dir', help=' ', default='model/')
# model to predict, i.e. to load weights from
parser.add_argument('--model_path', help=' ', default=None)
# paths
parser.add_argument('--data_train_path', help=' ', default='data/training_label.txt')
parser.add_argument('--data_test_path', help=' ', default='data/testing_data.txt')
parser.add_argument('--data_semi_path', help=' ', default='data/training_nolabel.txt')
args = parser.parse_args()
main(args)
以上是关于python 我的第一个合奏SPAGHETTI由Keras和示例代码提供支持的主要内容,如果未能解决你的问题,请参考以下文章
如何:Unity3D 或 Spaghetti 代码的项目架构? [关闭]