def train(cls, user_id): #默认加载data下的数据 print('user_id', user_id) FLAGS = tf.flags.FLAGS cls.my_data = dh.data_help(user_id=user_id) #cls.my_data = dh.data_help() checkpoint_file = tf.train.latest_checkpoint( os.path.join(cls.my_data.file_path, 'runs', 'checkpoints')) graph = tf.Graph() with graph.as_default(): session_conf = tf.ConfigProto( allow_soft_placement=FLAGS.allow_soft_placement, log_device_placement=FLAGS.log_device_placement) cls.sess = tf.Session(config=session_conf) with cls.sess.as_default(): saver = tf.train.import_meta_graph( "{}.meta".format(checkpoint_file)) saver.restore(cls.sess, checkpoint_file) cls.input_x = graph.get_operation_by_name("input_x").outputs[0] cls.dropout_keep_prob = graph.get_operation_by_name( "dropout_keep_prob").outputs[0] cls.predictions = graph.get_operation_by_name( "output/predictions").outputs[0] cls.probs = graph.get_operation_by_name( "output/probs").outputs[0]
"Allow device soft device placement") tf.flags.DEFINE_boolean("log_device_placement", False, "Log placement of ops on devices") parser = argparse.ArgumentParser() parser.add_argument('-u', '--user_id', help='mysql user-id', type=int, default='2') args = parser.parse_args() FLAGS = tf.flags.FLAGS print("user_id:%d" % (args.user_id)) my_data = data_help.data_help(user_id=args.user_id) x_train, y_train, x_dev, y_dev = my_data.get_test_train() #将测试数据写成pickle数据 my_data.write_to_pickle("test_data.pickle", x_dev, y_dev) with tf.Graph().as_default(): session_conf = tf.ConfigProto( allow_soft_placement=FLAGS.allow_soft_placement, log_device_placement=FLAGS.log_device_placement) sess = tf.Session(config=session_conf) with sess.as_default(): cnn = TextCNN(sequence_length=x_train.shape[1], num_classes=y_train.shape[1], vocab_size=my_data.Vocab_Size, embedding_size=FLAGS.embedding_dim,
tf.flags.DEFINE_boolean("eval_train", True, "Evaluate on all training data") # Misc Parameters tf.flags.DEFINE_boolean("allow_soft_placement", True, "Allow device soft device placement") tf.flags.DEFINE_boolean("log_device_placement", False, "Log placement of ops on devices") FLAGS = tf.flags.FLAGS FLAGS._parse_flags() print("\nParameters:") for attr, value in sorted(FLAGS.__flags.items()): print("{}={}".format(attr.upper(), value)) print("") my_data = dh.data_help() # CHANGE THIS: Load data. Load your own data here if FLAGS.eval_train: x_test, y_test = my_data.read_from_pickle("test_data.pickle") y_test = np.argmax(y_test, axis=1) else: x_raw = ["a masterpiece four years in the making", "everything is off."] y_test = [1, 0] print("\nEvaluating...\n") # Evaluation # ================================================== checkpoint_file = tf.train.latest_checkpoint( os.path.join(my_data.file_path, 'runs', 'checkpoints')) graph = tf.Graph()
tf.flags.DEFINE_boolean("log_device_placement", False, "Log placement of ops on devices") FLAGS = tf.flags.FLAGS FLAGS._parse_flags() Parameters = '' print("\nParameters:") for attr, value in sorted(FLAGS.__flags.items()): temp = "{}={}".format(attr.upper(), value) Parameters = temp + ' ' + Parameters print(temp) print("") # Load data print("Loading data...") my_data = data_help.data_help() y = my_data.build_labels() x, ans, subject = my_data.build_vocab() x_train, y_train, x_dev, y_dev = my_data.get_test_train(x, y) #将测试数据写成pickle数据 my_data.write_to_pickle("test_data.pickle", x_dev, y_dev) print(x_train.shape, x_dev.shape) with tf.Graph().as_default(): session_conf = tf.ConfigProto( allow_soft_placement=FLAGS.allow_soft_placement, log_device_placement=FLAGS.log_device_placement) sess = tf.Session(config=session_conf) with sess.as_default():
import json import xlrd import data_help s = requests data_lookup={"method":'lookup','id':1,'jsonrpc':2.0,'params':{'user_id':2}} data_chat={"method":'chat','id':1,'jsonrpc':2.0,'params':{'user_id':2,"quest":"银联二维码支付怎么用"}} data_retrain={"method":'retrain','id':1,'jsonrpc':2.0,'params':{'user_id':2}} right=0 total=0 def send_json(quest): data_chat={"method":'chat','id':1,'jsonrpc':2.0,'params':{'user_id':2,"quest":quest}} r = s.post('http://127.0.0.1:8000/deep_chat', json.dumps(data_chat)) r.encoding = 'utf-8' ans = eval(r.text) return ans['result']['answer'] my_data=data_help.data_help(bProcessData=False,user_id=2) for key,values in my_data.temp.items(): for value in values: total+=1 if(values.index(value)==0): right_answer=send_json(value) else: if right_answer == send_json(value): right+=1 print("%d/%d"%(right,total))