def main_wrapper(data_dir='/data', sensor='mynt_eye', intention_type='dlm'): global IMG_TOPICS global TOPICS global TOPICS_IDX assert (sensor in SENSORS), f'Must be valid sensor in {SENSORS}' RGBS = SENSOR_TOPIC[sensor]['RGBS'] DEPTHS = SENSOR_TOPIC[sensor]['DEPTHS'] bagfns = glob.glob(data_dir + '/*.bag') print('bags:', bagfns) bags = chain(*[parse_bag(bagfn, intention_type) for bagfn in bagfns]) it = ThreadedGenerator(bags, queue_maxsize=6500) # make dirs for images gendir = 'data' if os.path.exists(osp.join(data_dir, gendir)) and os.path.isdir( osp.join(data_dir, gendir)): shutil.rmtree(osp.join(data_dir, gendir)) os.mkdir(osp.join(data_dir, gendir)) with open(osp.join(data_dir, gendir, 'README.txt'), 'w+') as f: f.write('THIS DATA IS PARSED TO SERVE PYTORCH MODEL') topic_save_path = [] for idx, rgb_topic in enumerate(RGBS): fn = osp.join(data_dir, gendir, f'rgb_{idx}') os.mkdir(fn) TOPICS.append(rgb_topic) TOPICS_IDX[rgb_topic] = len(TOPICS) - 1 topic_save_path.append(fn) for idx, depth_topic in enumerate(DEPTHS): fn = osp.join(data_dir, gendir, f'depth_{idx}') os.mkdir(fn) TOPICS.append(depth_topic) TOPICS_IDX[depth_topic] = len(TOPICS) - 1 topic_save_path.append(fn) if intention_type == 'lpe': fn = osp.join(data_dir, gendir, 'intention_img') os.mkdir(fn) TOPICS.append(INTENTION) TOPICS_IDX[INTENTION] = len(TOPICS) - 1 topic_save_path.append(fn) IMG_TOPICS = TOPICS[:] else: IMG_TOPICS = TOPICS[:] TOPICS.append(INTENTION) TOPICS.append(CONTROL) f = open(osp.join(data_dir, gendir, 'label.txt'), 'w') labelwriter = csv.writer(f, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) labelwriter.writerow([ 'frame', 'intention_type', 'current_velocity', 'steering_wheel_angle', 'dlm' ]) for chunk in partition_all(CHUNK_SIZE, tqdm(it)): for c in chunk: for idx, fn in enumerate(topic_save_path): cv2.imwrite(osp.join(fn, f'{c.frame}.jpg'), c.imgs[idx]) labelwriter.writerow( [c.frame, intention_type, c.vel, c.steer, c.dlm])
def generate_lpe_intention(intention_map, pixels, thetas, offset, files, max_plot_samples, line_thick=4, steps=120): lpes = [] intention_map = cv2.copyMakeBorder(intention_map, offset, offset, offset, offset, cv2.BORDER_CONSTANT, value=[1, 1, 1, 1]) it = ThreadedGenerator(range(len(pixels)), queue_maxsize=6400) for idx in partition_all(1, tqdm(it)): i = idx[0] pixel = pixels[i] theta = thetas[i] img = np.copy(intention_map[pixel[1]:pixel[1] + 2 * offset, pixel[0]:pixel[0] + 2 * offset]) # add history for h in range(max(0, i - steps), i): h_pixel = (offset + pixels[h][0] - pixel[0], offset + pixels[h][1] - pixel[1]) if h_pixel[0] > 0 and h_pixel[0] < offset * 2 and h_pixel[ 1] > 0 and h_pixel[1] < offset * 2: img[h_pixel[1] - line_thick:h_pixel[1] + line_thick, h_pixel[0] - line_thick:h_pixel[0] + line_thick] = [1.0, 0, 0, 1] # add ahead path for h in range(i, min(i + steps, len(pixels))): h_pixel = (offset + pixels[h][0] - pixel[0], offset + pixels[h][1] - pixel[1]) if h_pixel[0] > 0 and h_pixel[0] < offset * 2 and h_pixel[ 1] > 0 and h_pixel[1] < offset * 2: img[h_pixel[1] - line_thick:h_pixel[1] + line_thick, h_pixel[0] - line_thick:h_pixel[0] + line_thick] = [0, 0, 1.0, 1] col, row, channel = img.shape M = cv2.getRotationMatrix2D((col / 2, row / 2), 90 + theta, 1) img = cv2.warpAffine(img, M, (col, row), cv2.INTER_LINEAR, cv2.BORDER_CONSTANT, 1) img = cv2.resize(img, (224, 224)) mpimg.imsave(files[i], img) if i < max_plot_samples: lpes.append(img) return lpes
def main_wrapper(data_dir): bagfns = glob.glob(data_dir + '/*.bag') print('bags:', bagfns) bags = chain(*[parse_bag(bagfn) for bagfn in bagfns]) it = ThreadedGenerator(bags, queue_maxsize=6500) # make dirs for images gendir = 'route_gendata' data_dir = osp.join(data_dir, 'data') if os.path.exists(osp.join(data_dir, gendir)) and os.path.isdir( osp.join(data_dir, gendir)): shutil.rmtree(osp.join(data_dir, gendir)) os.mkdir(osp.join(data_dir, gendir)) os.mkdir(osp.join(data_dir, gendir, 'camera_img')) os.mkdir(osp.join(data_dir, gendir, 'camera_img', 'front_60')) os.mkdir(osp.join(data_dir, gendir, 'camera_img', 'front_96_left')) os.mkdir(osp.join(data_dir, gendir, 'camera_img', 'side_96_left')) os.mkdir(osp.join(data_dir, gendir, 'camera_img', 'side_96_right')) os.mkdir(osp.join(data_dir, gendir, 'intention_img')) f = open(osp.join(data_dir, gendir, 'LabelData_VehicleData_PRT.txt'), 'w') labelwriter = csv.writer(f, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) labelwriter.writerow([ 'intention_type', 'current_velocity', 'steering_wheel_angle', 'ax', 'img_front_60_frame', 'intention_img' ]) for chunk in partition_all(CHUNK_SIZE, tqdm(it)): for c in chunk: cv2.imwrite( osp.join(data_dir, gendir, 'camera_img', 'front_60', '{}.jpg'.format(c.t)), c.img) cv2.imwrite( osp.join(data_dir, gendir, 'camera_img', 'front_96_left', '{}.jpg'.format(c.t)), c.front_96_img) cv2.imwrite( osp.join(data_dir, gendir, 'camera_img', 'side_96_left', '{}.jpg'.format(c.t)), c.left_96_img) cv2.imwrite( osp.join(data_dir, gendir, 'camera_img', 'side_96_right', '{}.jpg'.format(c.t)), c.right_96_img) cv2.imwrite( osp.join(data_dir, gendir, 'intention_img', '{}.jpg'.format(c.t)), c.lpe) labelwriter.writerow([c.dlm, c.speed, c.steer, c.acc, c.t, c.t])
def test(bidirectional, cell_type, depth, attention_type, use_residual, use_dropout, time_major, hidden_units): """测试不同参数在生成的假数据上的运行结果""" from sequence_to_sequence import SequenceToSequence from data_utils import batch_flow_bucket as batch_flow from word_sequence import WordSequence # pylint: disable=unused-variable from threadedgenerator import ThreadedGenerator emb = pickle.load(open('emb.pkl', 'rb')) x_data, y_data, ws = pickle.load(open('chatbot.pkl', 'rb')) # 训练部分 n_epoch = 5 batch_size = 128 # x_data, y_data = shuffle(x_data, y_data, random_state=0) # x_data = x_data[:100000] # y_data = y_data[:100000] steps = int(len(x_data) / batch_size) + 1 config = tf.ConfigProto( # device_count={'CPU': 1, 'GPU': 0}, allow_soft_placement=True, log_device_placement=False) save_path = './s2ss_chatbot_anti.ckpt' tf.reset_default_graph() with tf.Graph().as_default(): random.seed(0) np.random.seed(0) tf.set_random_seed(0) with tf.Session(config=config) as sess: model = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=batch_size, bidirectional=bidirectional, cell_type=cell_type, depth=depth, attention_type=attention_type, use_residual=use_residual, use_dropout=use_dropout, hidden_units=hidden_units, time_major=time_major, learning_rate=0.001, optimizer='adam', share_embedding=True, dropout=0.2, pretrained_embedding=True) init = tf.global_variables_initializer() sess.run(init) # 加载训练好的embedding model.feed_embedding(sess, encoder=emb) # print(sess.run(model.input_layer.kernel)) # exit(1) flow = ThreadedGenerator(batch_flow([x_data, y_data], ws, batch_size), queue_maxsize=30) dummy_encoder_inputs = np.array( [np.array([WordSequence.PAD]) for _ in range(batch_size)]) dummy_encoder_inputs_lengths = np.array([1] * batch_size) for epoch in range(1, n_epoch + 1): costs = [] bar = tqdm(range(steps), total=steps, desc='epoch {}, loss=0.000000'.format(epoch)) for _ in bar: x, xl, y, yl = next(flow) x = np.flip(x, axis=1) add_loss = model.train(sess, dummy_encoder_inputs, dummy_encoder_inputs_lengths, y, yl, loss_only=True) add_loss *= -0.5 # print(x, y) cost, lr = model.train(sess, x, xl, y, yl, return_lr=True, add_loss=add_loss) costs.append(cost) bar.set_description( 'epoch {} loss={:.6f} lr={:.6f}'.format( epoch, np.mean(costs), lr)) model.save(sess, save_path) flow.close() # 测试部分 tf.reset_default_graph() model_pred = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=1, mode='decode', beam_width=12, bidirectional=bidirectional, cell_type=cell_type, depth=depth, attention_type=attention_type, use_residual=use_residual, use_dropout=use_dropout, hidden_units=hidden_units, time_major=time_major, parallel_iterations=1, learning_rate=0.001, optimizer='adam', share_embedding=True, pretrained_embedding=True) init = tf.global_variables_initializer() with tf.Session(config=config) as sess: sess.run(init) model_pred.load(sess, save_path) bar = batch_flow([x_data, y_data], ws, 1) t = 0 for x, xl, y, yl in bar: x = np.flip(x, axis=1) pred = model_pred.predict(sess, np.array(x), np.array(xl)) print(ws.inverse_transform(x[0])) print(ws.inverse_transform(y[0])) print(ws.inverse_transform(pred[0])) t += 1 if t >= 3: break tf.reset_default_graph() model_pred = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=1, mode='decode', beam_width=1, bidirectional=bidirectional, cell_type=cell_type, depth=depth, attention_type=attention_type, use_residual=use_residual, use_dropout=use_dropout, hidden_units=hidden_units, time_major=time_major, parallel_iterations=1, learning_rate=0.001, optimizer='adam', share_embedding=True, pretrained_embedding=True) init = tf.global_variables_initializer() with tf.Session(config=config) as sess: sess.run(init) model_pred.load(sess, save_path) bar = batch_flow([x_data, y_data], ws, 1) t = 0 for x, xl, y, yl in bar: pred = model_pred.predict(sess, np.array(x), np.array(xl)) print(ws.inverse_transform(x[0])) print(ws.inverse_transform(y[0])) print(ws.inverse_transform(pred[0])) t += 1 if t >= 3: break
def test(params): from sequence_to_sequence import SequenceToSequence from data_utils import batch_flow_bucket as batch_flow from word_sequence import WordSequence from threadedgenerator import ThreadedGenerator x_data, y_data = pickle.load(open('chatbot.pkl', 'rb')) ws = pickle.load(open('ws.pkl', 'rb')) n_epoch = 2 batch_size = 128 steps = int(len(x_data) / batch_size) + 1 config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) save_path = 'model/s2ss_chatbot_anti.ckpt' tf.reset_default_graph() with tf.Graph().as_default(): random.seed(0) np.random.seed(0) tf.set_random_seed(0) with tf.Session(config=config) as sess: model = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=batch_size, **params) init = tf.global_variables_initializer() sess.run(init) flow = ThreadedGenerator(batch_flow([x_data, y_data], ws, batch_size, add_end=[False, True]), queue_maxsize=30) dummy_encoder_inputs = np.array( [np.array([WordSequence.PAD]) for _ in range(batch_size)]) dummy_encoder_inputs_length = np.array([1] * batch_size) for epoch in range(1, n_epoch + 1): costs = [] bar = tqdm(range(steps), total=steps, desc='epoch {}, loss=0.000000'.format(epoch)) for _ in bar: x, xl, y, yl = next(flow) x = np.flip(x, axis=1) add_loss = model.train(sess, dummy_encoder_inputs, dummy_encoder_inputs_length, y, yl, loss_only=True) add_loss *= -0.5 cost, lr = model.train(sess, x, xl, y, yl, return_lr=True, add_loss=add_loss) costs.append(cost) bar.set_description( 'epoch {} loss={:.6f} lr={:.6f}'.format( epoch, np.mean(costs), lr)) model.save(sess, save_path) flow.close() tf.reset_default_graph() model_pred = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=1, mode='decode', beam_width=12, **params) init = tf.global_variables_initializer() with tf.Session(config=config) as sess: sess.run(init) model_pred.load(sess, save_path) bar = batch_flow([x_data, y_data], ws, 1, add_end=False) t = 0 for x, xl, y, yl in bar: x = np.flip(x, axis=1) pred = model_pred.predict(sess, np.array(x), np.array(xl)) print(ws.inverse_transform(x[0])) print(ws.inverse_transform(y[0])) print(ws.inverse_transform(pred[0])) t += 1 if t >= 3: break tf.reset_default_graph() model_pred = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=1, mode='decode', beam_width=1, **params) init = tf.global_variables_initializer() with tf.Session(config=config) as sess: sess.run(init) model_pred.load(sess, save_path) bar = batch_flow([x_data, y_data], ws, 1, add_end=False) t = 0 for x, xl, y, yl in bar: x = np.flip(x, axis=1) pred = model_pred.predict(sess, np.array(x), np.array(xl)) print(ws.inverse_transform(x[0])) print(ws.inverse_transform(y[0])) print(ws.inverse_transform(pred[0])) t += 1 if t >= 3: break
def test(params): from sequence_to_sequence import SequenceToSequence from data_utils import batch_flow_bucket as batch_flow from threadedgenerator import ThreadedGenerator x_data, y_data = pickle.load(open('chatbot.pkl', 'rb')) #extract_conv.py 生成 ws = pickle.load(open('ws.pkl', 'rb')) #训练 """ 1.n_epoch是训练的轮次数; 2.理论上来讲训练的轮次数越大,那么训练的精度越高; 3.如果训练的轮次数特别大,比如1000,那么有可能会发生过拟合的现象,但是是否过拟合也和训练的数据有关 4.n_epoch越大,训练的时间越长; 5.办公室电脑用P5000的GPU训练40轮,训练了大概3天,训练2轮,大概一个半小时, 如果使用CPU来训练的话,速度会特别特别的慢,可能一轮就要几个小时 """ n_epoch = 2 batch_size = 128 #每一轮的步数 steps = int(len(x_data) / batch_size) + 1 #日志 config = tf.ConfigProto( allow_soft_placement=True, #是不是由系统选择CPU还是GPU log_device_placement=False #是不是打印设备的日志 ) #训练完后的模型保存路径 save_path = './model/s2ss_chatbot.ckpt' #重置默认的图 tf.reset_default_graph() #定义图的信息 with tf.Graph().as_default(): random.seed(0) np.random.seed(0) tf.set_random_seed(0) #传入定义的配置 with tf.Session(config=config) as sess: # 定义模型 model = SequenceToSequence( input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=batch_size, **params #传入的参数 ) init = tf.global_variables_initializer() sess.run(init) #定义数据流 放在线程生成器里做 flow = ThreadedGenerator(batch_flow([x_data, y_data], ws, batch_size, add_end=[False, True]), queue_maxsize=30) for epoch in range(1, n_epoch + 1): costs = [] #进度条 bar = tqdm(range(steps), total=steps, desc='epoch {}, loss=0.000000'.format(epoch)) #变量没啥意义 或者 之后用不到的 使用下划线'_'来来表示 for _ in bar: x, xl, y, yl = next(flow) #[[1,2], [3,4]] #[[3,4], [1,2]] #对输出进行翻转 这个实际上是在训练时的一个技巧,这个技巧出自于encoder的论文 x = np.flip(x, axis=1) #得到损失和学习率 cost, lr = model.train(sess, x, xl, y, yl, return_lr=True) costs.append(cost) #进行信息的打印 bar.set_description( 'epoch {} loss={:.6f} lr={:.6f}'.format( epoch, np.mean(costs), lr)) #保存模型 model.save(sess, save_path) #测试 模型的正确性 tf.reset_default_graph() #默认的图reset model_pred = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=1, mode='decode', beam_width=12, parallel_iterations=1, **params) init = tf.global_variables_initializer() with tf.Session(config=config) as sess: sess.run(init) model_pred.load(sess, save_path) bar = batch_flow([x_data, y_data], ws, 1, add_end=False) t = 0 for x, xl, y, yl in bar: x = np.flip(x, axis=1) pred = model_pred.predict(sess, np.array(x), np.array(xl)) #打印出预测的文字 print(ws.inverse_transform(x[0])) print(ws.inverse_transform(y[0])) print(ws.inverse_transform(pred[0])) t += 1 #仅测试三轮查看是否能正常对话 if t >= 3: break
def test(params): from sequence_to_sequence import SequenceToSequence from data_utils import batch_flow_bucket as batch_flow from threadedgenerator import ThreadedGenerator x_data, y_data = pickle.load(open('chatbot.pkl', 'rb')) ws = pickle.load(open('ws.pkl', 'rb')) #训练 """ 1.n_epoch是训练的轮次数; 2.理论上来讲训练的轮次数越大,那么训练的精度越高; 3.如果训练的轮次数特别大,比如1000,那么有可能会发生过拟合的现象,但是是否过拟合也和训练的数据有关 4.n_epoch越大,训练的时间越长; 5.办公室电脑用P5000的GPU训练40轮,训练了大概3天,训练2轮,大概一个半小时, 如果使用CPU来训练的话,速度会特别特别的慢,可能一轮就要几个小时 """ n_epoch = 2 batch_size = 128 steps = int(len(x_data) / batch_size) + 1 config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) save_path = './model/s2ss_chatbot.ckpt' tf.reset_default_graph() with tf.Graph().as_default(): random.seed(0) np.random.seed(0) tf.set_random_seed(0) with tf.Session(config=config) as sess: # 定义模型 model = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=batch_size, **params) init = tf.global_variables_initializer() sess.run(init) flow = ThreadedGenerator(batch_flow([x_data, y_data], ws, batch_size, add_end=[False, True]), queue_maxsize=30) for epoch in range(1, n_epoch + 1): costs = [] bar = tqdm(range(steps), total=steps, desc='epoch {}, loss=0.000000'.format(epoch)) for _ in bar: x, xl, y, yl = next(flow) #[[1,2], [3,4]] #[[3,4], [1,2]] x = np.flip(x, axis=1) cost, lr = model.train(sess, x, xl, y, yl, return_lr=True) costs.append(cost) bar.set_description( 'epoch {} loss={:.6f} lr={:.6f}'.format( epoch, np.mean(costs), lr)) model.save(sess, save_path) #测试 tf.reset_default_graph() model_pred = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=1, mode='decode', beam_width=12, parallel_iterations=1, **params) init = tf.global_variables_initializer() with tf.Session(config=config) as sess: sess.run(init) model_pred.load(sess, save_path) bar = batch_flow([x_data, y_data], ws, 1, add_end=False) t = 0 for x, xl, y, yl in bar: x = np.flip(x, axis=1) pred = model_pred.predict(sess, np.array(x), np.array(xl)) print(ws.inverse_transform(x[0])) print(ws.inverse_transform(y[0])) print(ws.inverse_transform(pred[0])) t += 1 if t >= 3: break
def test(params): """测试不同参数在生成的假数据上的运行结果""" from sequence_to_sequence import SequenceToSequence from data_utils import batch_flow_bucket as batch_flow from word_sequence import WordSequence # pylint: disable=unused-variable from threadedgenerator import ThreadedGenerator x_data, y_data = pickle.load(open('chatbot.pkl', 'rb')) ws = pickle.load(open('ws.pkl', 'rb')) # 训练部分 n_epoch = 5 batch_size = 32 # x_data, y_data = shuffle(x_data, y_data, random_state=0) # x_data = x_data[:10000] # y_data = y_data[:10000] steps = int(len(x_data) / batch_size) + 1 config = tf.ConfigProto( # device_count={'CPU': 1, 'GPU': 0}, allow_soft_placement=True, log_device_placement=False ) save_path = './s2ss_chatbot.ckpt' tf.reset_default_graph() with tf.Graph().as_default(): random.seed(0) np.random.seed(0) tf.set_random_seed(0) with tf.Session(config=config) as sess: model = SequenceToSequence( input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=batch_size, **params ) init = tf.global_variables_initializer() sess.run(init) # print(sess.run(model.input_layer.kernel)) # exit(1) flow = ThreadedGenerator( batch_flow([x_data, y_data], ws, batch_size, add_end=[False, True]), queue_maxsize=30) for epoch in range(1, n_epoch + 1): costs = [] bar = tqdm(range(steps), total=steps, desc='epoch {}, loss=0.000000'.format(epoch)) for _ in bar: x, xl, y, yl = next(flow) x = np.flip(x, axis=1) # print(x, y) # print(xl, yl) # exit(1) cost, lr = model.train(sess, x, xl, y, yl, return_lr=True) costs.append(cost) bar.set_description('epoch {} loss={:.6f} lr={:.6f}'.format( epoch, np.mean(costs), lr )) model.save(sess, save_path) flow.close() # 测试部分 tf.reset_default_graph() model_pred = SequenceToSequence( input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=1, mode='decode', beam_width=12, parallel_iterations=1, **params ) init = tf.global_variables_initializer() with tf.Session(config=config) as sess: sess.run(init) model_pred.load(sess, save_path) bar = batch_flow([x_data, y_data], ws, 1, add_end=False) t = 0 for x, xl, y, yl in bar: x = np.flip(x, axis=1) pred = model_pred.predict( sess, np.array(x), np.array(xl) ) print(ws.inverse_transform(x[0])) print(ws.inverse_transform(y[0])) print(ws.inverse_transform(pred[0])) t += 1 if t >= 3: break tf.reset_default_graph() model_pred = SequenceToSequence( input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=1, mode='decode', beam_width=1, parallel_iterations=1, **params ) init = tf.global_variables_initializer() with tf.Session(config=config) as sess: sess.run(init) model_pred.load(sess, save_path) bar = batch_flow([x_data, y_data], ws, 1, add_end=False) t = 0 for x, xl, y, yl in bar: pred = model_pred.predict( sess, np.array(x), np.array(xl) ) print(ws.inverse_transform(x[0])) print(ws.inverse_transform(y[0])) print(ws.inverse_transform(pred[0])) t += 1 if t >= 3: break
def test(bidirectional, cell_type, depth, attention_type, use_residual, use_dropout, time_major, hidden_units, optimizer, embedding_size): ''' 测试不同参数在生成的假数据上的运行结果 :param bidirectional: :param cell_type: :param depth: 模型深度 :param attention_type: attention的类型 :param use_residual: :param use_dropout: :param time_major: :param hidden_units: :return: ''' emb_path = 'D:\project\Chatbot_CN\Chatbot_Data\Text_generator\emb.pkl' chatbot_path = 'D:\project\Chatbot_CN\Chatbot_Data\Text_generator\chatbot.pkl' emb = pickle.load(open(emb_path, 'rb')) x_data, y_data, ws = pickle.load(open(chatbot_path, 'rb')) # 训练部分 n_epoch = 40 batch_size = 60 # 每训练一轮将数据打乱。 [shuffle的重要性] # np.random.permutation 和 shuffle的区别: # 函数shuffle与permutation都是对原来的数组进行重新洗牌(即随机打乱原来的元素顺序); # 区别在于shuffle直接在原来的数组上进行操作,改变原来数组的顺序,无返回值。 # 而permutation不直接在原来的数组上进行操作,而是返回一个新的打乱顺序的数组,并不改变原来的数组。 x_data, y_data = shuffle(x_data, y_data, random_state=0) # x_data = x_data[:10000] # y_data = y_data[:10000] steps = int(len(x_data) / batch_size) + 1 # 控制训练的步数 config = tf.ConfigProto( # device_count={'CPU': 1, 'GPU': 0}, allow_soft_placement=True, log_device_placement=False ) save_path = './chatbots/S2S_Chatbot.ckpt' tf.reset_default_graph() with tf.Graph().as_default(): random.seed(0) np.random.seed(0) tf.set_random_seed(0) with tf.Session(config=config) as sess: model = SequenceToSequence( input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=batch_size, bidirectional=bidirectional, cell_type=cell_type, depth=depth, attention_type=attention_type, use_residual=use_residual, use_dropout=use_dropout, hidden_units=hidden_units, time_major=time_major, learning_rate=0.001, optimizer= optimizer, embedding_size= embedding_size, share_embedding=True, dropout=0.2, pretrained_embedding=True ) init = tf.global_variables_initializer() sess.run(init) # 加载训练好的embedding model.feed_embedding(sess, encoder=emb) flow = ThreadedGenerator( batch_flow_bucket([x_data, y_data], ws, batch_size), queue_maxsize=30 ) for epoch in range(1, n_epoch + 1): costs = [] bar = tqdm(range(steps), total=steps, desc='epoch {}, loss=0.000000'.format(epoch)) for _ in bar: x, xl, y, yl = next(flow) x = np.flip(x, axis=1) # print(x, y) cost, lr = model.train(sess, x, xl, y, yl, return_lr=True) costs.append(cost) bar.set_description('epoch {} loss={:.6f} lr={:.6f}'.format( epoch, np.mean(costs), lr )) model.save(sess, save_path) flow.close() # 测试部分 tf.reset_default_graph() model_pred = SequenceToSequence( input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=1, mode='decode', beam_width=12, bidirectional=bidirectional, cell_type=cell_type, depth=depth, attention_type=attention_type, use_residual=use_residual, use_dropout=use_dropout, hidden_units=hidden_units, time_major=time_major, parallel_iterations=1, learning_rate=0.001, optimizer='adam', share_embedding=True, pretrained_embedding=True ) init = tf.global_variables_initializer() with tf.Session(config=config) as sess: sess.run(init) model_pred.load(sess, save_path) bar = batch_flow_bucket([x_data, y_data], ws, 1) t = 0 for x, xl, y, yl in bar: x = np.flip(x, axis=1) pred = model_pred.predict( sess, np.array(x), np.array(xl) ) print(ws.inverse_transform(x[0])) print(ws.inverse_transform(y[0])) print(ws.inverse_transform(pred[0])) t += 1 if t >= 3: break tf.reset_default_graph() model_pred = SequenceToSequence( input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=1, mode='decode', beam_width=1, bidirectional=bidirectional, cell_type=cell_type, depth=depth, attention_type=attention_type, use_residual=use_residual, use_dropout=use_dropout, hidden_units=hidden_units, time_major=time_major, parallel_iterations=1, learning_rate=0.001, optimizer='adam', share_embedding=True, pretrained_embedding=True ) init = tf.global_variables_initializer() with tf.Session(config=config) as sess: sess.run(init) model_pred.load(sess, save_path) bar = batch_flow_bucket([x_data, y_data], ws, 1) t = 0 for x, xl, y, yl in bar: pred = model_pred.predict( sess, np.array(x), np.array(xl) ) print(ws.inverse_transform(x[0])) print(ws.inverse_transform(y[0])) print(ws.inverse_transform(pred[0])) t += 1 if t >= 3: break
def train(params): from seq_2_seq import SequenceToSequence from data_utils import batch_flow_bucket as batch_flow from word_sequence import WordSequence from threadedgenerator import ThreadedGenerator x_data, y_data = pickle.load(open('chatbot.pkl', 'rb')) ws = pickle.load(open('ws.pkl', 'rb')) n_epoch = 40 batch_size = 128 steps = int(len(x_data) / batch_size) + 1 config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) save_path = 'model_anti/s2ss_chatbot_anti.ckpt' best_save_path = 'model_anti_best/best_cost.ckpt' # 训练模式 # loss下降较慢,不至于出现严重的梯度消散 tf.reset_default_graph() with tf.Graph().as_default(): random.seed(0) np.random.seed(0) tf.set_random_seed(0) with tf.Session(config=config) as sess: model = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=batch_size, **params) init = tf.global_variables_initializer() sess.run(init) # 是否继续训练 if tf.train.checkpoint_exists( './model_anti/s2ss_chatbot_anti.ckpt'): model.load(sess, save_path) print('>>>=Having restored model') flow = ThreadedGenerator(batch_flow([x_data, y_data], ws, batch_size, add_end=[False, True]), queue_maxsize=30) dummy_encoder_inputs = np.array( [np.array([WordSequence.PAD]) for _ in range(batch_size)]) dummy_encoder_inputs_length = np.array([1] * batch_size) temp_loss = 30 for epoch in range(1, n_epoch + 1): costs = [] bar = tqdm(range(steps), total=steps, desc='epoch {}, loss=0.000000'.format(epoch)) for _ in bar: x, xl, y, yl = next(flow) # permutation = np.random.permutation(batch_size) # dummy_encoder_inputs = x[permutation, :] # dummy_encoder_inputs_length = xl[permutation] x = np.flip(x, axis=1) dummy_encoder_inputs = np.flip(dummy_encoder_inputs, axis=1) add_loss = model.train(sess, dummy_encoder_inputs, dummy_encoder_inputs_length, y, yl, loss_only=True) add_loss *= -0.5 # 此处相当于减去加入负样本所带来的损失 cost, lr = model.train(sess, x, xl, y, yl, return_lr=True, add_loss=add_loss) costs.append(cost) bar.set_description( 'epoch {} loss={:.6f} lr={:.6f}'.format( epoch, np.mean(costs), lr)) model.save(sess, save_path) mean_loss = np.mean(costs) if mean_loss <= temp_loss: model.save(sess, best_save_path) temp_loss = mean_loss with open('./model_anti/globalstep.txt', 'a+') as f: f.write('global step is:{}\n'.format(epoch)) flow.close() # 预测模式(beam_width=200) tf.reset_default_graph() model_pred = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=1, mode='decode', beam_width=200, **params) init = tf.global_variables_initializer() with tf.Session(config=config) as sess: sess.run(init) model_pred.load(sess, save_path) bar = batch_flow([x_data, y_data], ws, 1, add_end=False) t = 0 for x, xl, y, yl in bar: x = np.flip(x, axis=1) pred = model_pred.predict(sess, np.array(x), np.array(xl)) print('输入问句(倒序):', ws.inverse_transform(x[0])) print('输入答句:', ws.inverse_transform(y[0])) print('预测答句:', ws.inverse_transform(pred[0][0])) t += 1 if t >= 3: break # 预测模式(beam_width=1) tf.reset_default_graph() model_pred = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=1, mode='decode', beam_width=1, **params) init = tf.global_variables_initializer() with tf.Session(config=config) as sess: sess.run(init) model_pred.load(sess, save_path) bar = batch_flow([x_data, y_data], ws, 1, add_end=False) t = 0 for x, xl, y, yl in bar: x = np.flip(x, axis=1) pred = model_pred.predict(sess, np.array(x), np.array(xl)) print('输入问句(倒序):', ws.inverse_transform(x[0])) print('输入答句:', ws.inverse_transform(y[0])) print('预测答句:', ws.inverse_transform(pred[0][0])) t += 1 if t >= 3: break
def train(params): from seq_2_seq import SequenceToSequence from data_utils import batch_flow_bucket as batch_flow from threadedgenerator import ThreadedGenerator x_data, y_data = pickle.load(open('chatbot.pkl', 'rb')) ws = pickle.load(open('ws.pkl', 'rb')) # 训练模式 n_epoch = 200 batch_size = 256 steps = int(len(x_data) / batch_size) + 1 config = tf.ConfigProto( allow_soft_placement=True, # 系统自动选择运行cpu或者gpu log_device_placement=False # 是否需要打印设备日志 ) save_path = './model/s2ss_chatbot.ckpt' # 重置默认的图 tf.reset_default_graph() # 定义图的基本信息 with tf.Graph().as_default(): random.seed(0) np.random.seed(0) tf.set_random_seed(0) with tf.Session(config=config) as sess: # 定义模型 model = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=batch_size, **params) init = tf.global_variables_initializer() sess.run(init) if tf.train.checkpoint_exists('./model/s2ss_chatbot.ckpt'): model.load(sess, save_path) print('>>>=Having restored model') flow = ThreadedGenerator(batch_flow([x_data, y_data], ws, batch_size, add_end=[False, True]), queue_maxsize=30) for epoch in range(1, n_epoch + 1): costs = [] bar = tqdm(range(steps), total=steps, desc='epoch {}, loss=0.000000'.format(epoch)) for _ in bar: x, xl, y, yl = next(flow) # 此处效果为每个seq倒序 x = np.flip(x, 1) cost, lr = model.train(sess, x, xl, y, yl, return_lr=True) costs.append(cost) bar.set_description( 'epoch {} loss={:.6f} lr={:.6f}'.format( epoch, np.mean(costs), lr)) model.save(sess, save_path) flow.close() # 测试模式 tf.reset_default_graph() model_pred = SequenceToSequence(input_vocab_size=len(ws), target_vocab_size=len(ws), batch_size=1, mode='decode', beam_width=200, parallel_iterations=1, **params) init = tf.global_variables_initializer() with tf.Session(config=config) as sess: sess.run(init) model_pred.load(sess, save_path) bar = batch_flow([x_data, y_data], ws, 1, add_end=False) t = 0 for x, xl, y, yl in bar: x = np.flip(x, axis=1) pred = model_pred.predict(sess, np.array(x), np.array(xl)) print(ws.inverse_transform(x[0])) print(ws.inverse_transform(y[0])) print(ws.inverse_transform(pred[0])) t += 1 # 此处只测试了3次 if t >= 3: break