Exemplo n.º 1
0
    def __init__(self):
        print("tensorflow version: ", tf.__version__)

        self.dict_file = 'data/word_dict.txt'
        self.data_map = "data/map.pkl"  # pkl是cpickle模块生成的文件,用于长久保存字符串、列表、字典等数据

        self.batch_size = 20  # 每次喂进20个
        self.max_epoch = 10000  # 最大100000轮
        self.show_batch = 1  #
        self.model_path = 'model/'
        # jieba导入词典
        jieba.load_userdict(self.dict_file)

        self.location = ["杭州", "重庆", "上海", "北京"]
        self.user_info = {"__UserName__": "yw", "__Location__": "重庆"}
        self.robot_info = {"__RobotName__": "xw"}

        # 获取输入输出
        if os.path.isfile(self.data_map):
            with open(self.data_map, "rb") as f:
                data_map = cPickle.load(
                    f)  # 使用cpickle读取map.pkl文件内容返回,注意写入是什么类型数据,读取是就是什么类型数据
        else:
            p = Preprocess()
            p.main()  # 如果不存在data_map则调用Preprocess()方法重新创建向量和map
            data_map = p.data_map  # data_map是全局变量的dict,在这里可以取到

        # 从data_map中查找各个键值对并赋值,其中也存在字典嵌套
        self.encoder_vocab = data_map.get("Q_vocab")
        self.encoder_vec = data_map.get("Q_vec")
        self.encoder_vocab_size = data_map.get("Q_vocab_size")
        self.char_to_vec = self.encoder_vocab

        self.decoder_vocab = data_map.get("A_vocab")
        self.decoder_vec = data_map.get("A_vec")
        self.decoder_vocab_size = data_map.get("A_vocab_size")
        self.vec_to_char = {v: k for k, v in self.decoder_vocab.items()}

        print("encoder_vocab_size {}".format(self.encoder_vocab_size))
        print("decoder_vocab_size {}".format(self.decoder_vocab_size))
        # 调用DynamicSeq2seq()方法,将编码解码词典长度导入,初始化模型
        self.model = DynamicSeq2seq(
            encoder_vocab_size=self.encoder_vocab_size + 1,
            decoder_vocab_size=self.decoder_vocab_size + 1,
        )
        #优先给程序分配显存
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
        self.sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
        self.restore_model()
Exemplo n.º 2
0
    def __init__(self):
        print("tensorflow version: ", tf.__version__)

        self.dict_file = 'data/word_dict.txt'
        self.data_map = "data/map.pkl"

        self.batch_size = 20
        #self.max_epoch = 100000
        self.max_epoch = 10
        self.show_batch = 1
        self.model_path = 'model/'
        # jieba导入词典
        jieba.load_userdict(self.dict_file)

        self.location = ["杭州", "重庆", "上海", "北京"]
        self.user_info = {"__UserName__": "yw", "__Location__": "重庆"}
        self.robot_info = {"__RobotName__": "Rr"}

        # 获取输入输出
        if os.path.isfile(self.data_map):
            with open(self.data_map, "rb") as f:
                data_map = cPickle.load(f)
        else:
            p = Preprocess()
            p.main()
            data_map = p.data_map

        self.encoder_vocab = data_map.get("Q_vocab")
        self.encoder_vec = data_map.get("Q_vec")
        self.encoder_vocab_size = data_map.get("Q_vocab_size")
        self.char_to_vec = self.encoder_vocab

        self.decoder_vocab = data_map.get("A_vocab")
        self.decoder_vec = data_map.get("A_vec")
        self.decoder_vocab_size = data_map.get("A_vocab_size")
        self.vec_to_char = {v: k for k, v in self.decoder_vocab.items()}

        print "encoder_vocab_size {}".format(self.encoder_vocab_size)
        print "decoder_vocab_size {}".format(self.decoder_vocab_size)
        self.model = DynamicSeq2seq(
            encoder_vocab_size=self.encoder_vocab_size + 1,
            decoder_vocab_size=self.decoder_vocab_size + 1,
        )
        self.sess = tf.Session()
        self.restore_model()