Ejemplo n.º 1
0
    def __init__(self,
                 config,
                 model_path='./runs/1539678339/checkpoints/model-200',
                 word_to_index='./vocabs/word_to_index.json',
                 index_to_label='./vocabs/index_to_label.json'):
        self.word_to_index = load_json(word_to_index)
        self.index_to_label = load_json(index_to_label)

        graph = tf.Graph()
        with graph.as_default():
            session_conf = tf.ConfigProto(
                allow_soft_placement=config['allow_soft_placement'],
                log_device_placement=config['log_device_placement'])
            self.sess = tf.Session(config=session_conf)
            with self.sess.as_default():
                # Load the saved meta graph and restore variables
                saver = tf.train.import_meta_graph(
                    "{}.meta".format(model_path))
                saver.restore(self.sess, model_path)

                # Get the placeholders from the graph by name
                self.input_x = graph.get_operation_by_name(
                    "input_x").outputs[0]

                self.dropout_keep_prob = graph.get_operation_by_name(
                    "dropout_keep_prob").outputs[0]

                # Tensors we want to evaluate
                self.predictions = graph.get_operation_by_name(
                    "output/predictions").outputs[0]
Ejemplo n.º 2
0
    def __init__(self,
                 config,
                 model_path='./runs/1548754630/checkpoints/model-1500',
                 word2index='./vocabs/word2index.json',
                 index2label='./vocabs/index2label.json'):
        self.word2index = load_json(word2index)
        self.index2label = load_json(index2label)

        graph = tf.Graph()
        with graph.as_default():
            session_conf = tf.ConfigProto(
                allow_soft_placement=config['allow_soft_placement'],
                log_device_placement=config['log_device_placement'])
            self.sess = tf.Session(config=session_conf)
            with self.sess.as_default():  # 使用as_default(),当退出上下文的时候会话不会关闭
                # load model
                saver = tf.train.import_meta_graph(
                    '{}.meta'.format(model_path))
                saver.restore(self.sess, model_path)

                # get the placeholders from graph by name
                self.input_x = graph.get_operation_by_name(
                    'input_x').outputs[0]

                self.dropout_keep_prob = graph.get_operation_by_name(
                    'dropout_keep_prob').outputs[0]

                # tensors we want to evaluate
                self.predictions = graph.get_operation_by_name(
                    'output/predictions').outputs[0]
Ejemplo n.º 3
0
def generating_training_time_series(lower_range, upper_range):
    # Read stream data
    _main_stream = dh.load_json(PATH + 'james.json')  # All main stream data.
    #reply_stream = dh.load_json('james_reply.json') # All reply stream data.
    _reply_stream = [
        dh.load_json(PATH + "james/{}.json".format(i))
        for i in range(len(_main_stream))
    ]

    # main posts time series
    main_stream_time_series = dh.main_stream_time_lists(
        _main_stream)[lower_range:upper_range]
    reply_stream_time_series = dh.reply_stream_time_lists(
        _reply_stream)[lower_range:upper_range]

    return main_stream_time_series, reply_stream_time_series
Ejemplo n.º 4
0
def load_data_all():

    # glyce embedding
    fin = "../data/wvec.hf"
    init_vec = load_hdf(fin)

    # char vec from training
    fin = "../data/trn_wvec.hf"
    train_vec = load_hdf(fin)

    # glyce char2vec idx
    fdic = "../data/dictionary.json"
    fdic = load_json(fdic)
    init_ch2idx = fdic["char2idx"]

    # char2vec idx from training
    ch2idx = load_json("../md/char2idx.json")
    return init_ch2idx, init_vec, ch2idx, train_vec
Ejemplo n.º 5
0
def doc2vec(fci):
    dic = load_json(fci)
    init_ch2idx, init_vec, ch2idx, train_vec = load_data_all()

    raw_dic, dic = make_data(dic, ch2idx, train_vec, init_ch2idx, init_vec)

    fout = "../data/char2vec_with_glyce.pickle"
    write2pickle(fout, dic)

    fout = "../data/char2vec_raw.pickle"
    write2pickle(fout, raw_dic)
Ejemplo n.º 6
0
def generating_reply_time_series(upper_range, number):
    # Read stream data
    _main_stream = dh.load_json(PATH + 'james.json')  # All main stream data.
    #reply_stream = dh.load_json('james_reply.json') # All reply stream data.
    _reply_stream = [
        dh.load_json(PATH + "james/{}.json".format(i))
        for i in range(len(_main_stream))
    ]

    # main posts time series
    main_stream_time_series = dh.main_stream_time_lists(
        _main_stream)[upper_range:upper_range + number]
    reply_stream_time_series = dh.reply_stream_time_lists(
        _reply_stream)[upper_range:upper_range + number]

    for i in range(len(reply_stream_time_series)):
        temp = np.array(reply_stream_time_series[i])
        reply_stream_time_series[i] = temp[np.where(
            temp < main_stream_time_series[-1])]

    return main_stream_time_series, reply_stream_time_series
Ejemplo n.º 7
0
            text_list[i] = ''
    
    return text_list


def searchkw(text_list, kw_lists):
    """Search for targeted elements in the list based on the lw_lists"""
    mentions_list = []
    for i, x in enumerate(text_list):
        if any(n in x.lower() for n in kw_lists):
            mentions_list.append(i)
            
    return mentions_list


main_stream = dh.load_json("data/Avengers/avengers.json")

texts = generate_text(main_stream)

mentions = searchkw(texts, ['lbj', 'lebron', 'lebron james'])

# Get all posts related to James
james_dataframe = main_stream.iloc[mentions]
james_main_id = list(james_dataframe.sub_id)

# Generate James related 
james_reply_stream = pd.DataFrame([])
for i in range(8):
    df = dh.load_json('data/NBA_1904/reply_stream_{}.json'.format(i+1))
    for i in range(len(df)):
        if df.iloc[i].link_id in james_main_id: