• 大小: 12.68MB
    文件类型: .zip
    金币: 2
    下载: 0 次
    发布日期: 2024-02-04
  • 语言: 其他
  • 标签: 深度学习  吴岸城  

资源简介

深度学习算法实践-电子工业出版社-吴岸城-2017年1月-随书源码

资源截图

代码片段和文件信息

# -*- coding:utf-8 -*-
import os
os.environ[‘KERAS_BACKEND‘] = “theano“
# os.environ[‘THEANO_FLAGS‘] = “device=cpu“
from keras.models import Sequential
from keras.layers.core import Dense Activation
from keras.layers.recurrent import LSTM
from keras.layers.wrappers import TimeDistributed Bidirectional
from keras.layers import FlattenLambdaK
from keras.layers.embeddings import embedding
from keras.layers.pooling import MaxPooling2D GlobalMaxPooling2DMaxPooling1D
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import MergeDropout
from keras.datasets import imdb
import numpy as np
# from keras.utils import plot_model
from keras.utils.visualize_util import plot

from keras.preprocessing import sequence

def text_feature_extract_model1(embedding_size=128 hidden_size=256):
    ‘‘‘
    this is a model use normal Bi-LSTM and maxpooling extract feature

    examples:
这个货很好,很流畅 [  1.62172219e-05]
这个东西真好吃, [  1.65377696e-05]
服务太糟糕味道差 [ 1.]
这个贴花的款式好看 [  1.76498161e-05]
看着不错,生产日期也是新的是16年12月份的,就是有点小贵 [  1.59666997e-05]
一股淡淡的腥味。每次喝完都会吃一口白糖 [ 1.]
还没喝,不过,看着应该不错哟 [  1.52662833e-05]
用来看电视还是不错的,就是有些大打字不习惯,要是可以换输入法就好了! [ 1.]
嗯,中间出了点小问题已经联系苹果客服解决了,打游戏也没有卡顿,总体来讲还不错吧! [  1.52281245e-05]
下软件下的多的时候死了一回机,强制重启之后就恢复了。 [ 1.]
东西用着还可以很流畅! [  1.59881820e-05]


    :return:
    ‘‘‘
    model = Sequential()
    model.add(embedding(input_dim=max_features
                        output_dim=embedding_size
                        input_length=max_seq))
    model.add(Bidirectional(LSTM(hidden_size return_sequences=True)))
    model.add(TimeDistributed(Dense(embedding_size/2)))
    model.add(Activation(‘softplus‘))
    model.add(MaxPooling1D(5))
    model.add(Flatten())
    # model.add(Dense(2048 activation=‘softplus‘))
    # model.add(Dropout(0.2))
    model.add(Dense(1 activation=‘sigmoid‘))

    model.compile(loss=‘binary_crossentropy‘ optimizer=‘adam‘ metrics=[‘accuracy‘])
    model.summary()
    plot(model to_file=“text_feature_extract_model1.png“ show_shapes=True)
    return model

if __name__ == “__main__“:
    #prepare y 1 negtive 0 postive
    y = []

    txt_path = ‘../data/corpus/reviews/‘

    with open(txt_path+‘1_point.txt‘ ‘rb‘) as fp:
        lines = fp.readlines()

    len_temp_lines = len(lines)
    for i in range(len(lines)):
        y.append(1)

    with open(txt_path+‘5_point.txt‘ ‘rb‘) as fp:
        lines += fp.readlines()

    for i in range(len(lines[len_temp_lines:])):
        y.append(0)

    def create_dict():
        dict = open(txt_path+‘1_point.txt‘ ‘rb‘).read()
        # dict += open(txt_path + ‘2_point.txt‘ ‘rb‘).read()
        dict += open(txt_path+‘5_point.txt‘ ‘rb‘).read()
        dict_list = set(list(dict.decode(‘utf8‘)))
        dicts = {}
        for i d in enumerate(dict_list):
            dicts[d] = i
        return dicts

    def create_X(lines):
        len_seq = []
        dicts = create_dict()
        sequences = []
        for line 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\
     文件           7  2017-04-20 11:24  Book_DeepLearning_Practice-master\.gitignore
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\DeepTextClf\
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\DeepTextClf\algorithm\
     文件        6086  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\DeepTextClf\algorithm\RNN-CNN_feature_extract.py
     文件           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\DeepTextClf\algorithm\__init__.py
     文件       71891  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\DeepTextClf\algorithm\text_feature_extract_model1.png
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\DeepTextClf\data\
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\DeepTextClf\data\corpus\
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\DeepTextClf\data\corpus\reviews\
     文件    16765967  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\DeepTextClf\data\corpus\reviews\1_point.txt
     文件    14183087  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\DeepTextClf\data\corpus\reviews\5_point.txt
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\dialogue\
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\dialogue\提取和选择\
     文件       12292  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\dialogue\提取和选择\answer_find_out.py
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\dialogue\爬取\
     文件       38276  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\dialogue\爬取\fromSE.py
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\dialogue\相似提取答案\
     文件        4433  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\dialogue\相似提取答案\Compress.py
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\dialogue\蕴含\
     文件        8415  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\dialogue\蕴含\textual_entailment.py
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\mail\
     文件        1789  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\mail\mail_classily.py
     文件        4660  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\mail\stopwords.txt
     文件         452  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\mail\text_preprocess.py
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\sentiment\
     目录           0  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\sentiment\LSTM\
     文件        5410  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\sentiment\LSTM\extract_feature.py
     文件         931  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\sentiment\LSTM\lstm_model.py
     文件         340  2017-04-20 11:24  Book_DeepLearning_Practice-master\2_Chapter\sentiment\LSTM\test_lstm.py
............此处省略64个文件信息

评论

共有 条评论