• 小白的第一个RNN(情感分析模型)


    平台:window10,python3.11.4,pycharm

    框架:keras

    编写日期:20230903

    数据集:英语,自编,训练集和测试集分别有4个样本,标签有积极和消极两种

    环境搭建

    新建文件夹,进入目录

    创建虚拟环境

    virtualenv venv

    激活虚拟环境

    venv\Scripts\activate

    安装依赖库

    pip install tensorflow

    代码编写

    目录下创建main.py,进入pycharm打开文件夹,编写代码

    包引入

    1. import numpy as np
    2. from keras.preprocessing.text import Tokenizer
    3. from keras.preprocessing.sequence import pad_sequences
    4. from keras.models import Sequential
    5. from keras.layers import SimpleRNN, Dense

    数据集处理

    1. # 训练集
    2. train_texts = ['I love this movie',
    3. 'This is the worst film I have ever seen.',
    4. 'An enjoyable and thought-provoking experience.',
    5. "I think it is boring"
    6. ]
    7. train_labels = np.array([1, 0, 1, 0]) # 0代表消极,1代表积极
    8. # 测试集
    9. test_texts = ["What a waste of my time",
    10. "One of the best movies I've seen in a long time",
    11. "Amazing acting!",
    12. "This movie look awful"
    13. ]
    14. test_labels = np.array([0, 1, 1, 0])
    1. # 构建分词器
    2. tokenizer = Tokenizer(num_words=100)
    3. # 用训练集与测试集训练分词器
    4. tokenizer.fit_on_texts(train_texts + test_texts)
    5. # 数据集序列化,将文本转成数字,便于机器处理
    6. train_sequences = tokenizer.texts_to_sequences(train_texts)
    7. test_sequences = tokenizer.texts_to_sequences(test_texts)
    8. # 数据填充到20,超过的就截断,post:在末尾填充
    9. # 由于每个训练文本有不同的单词数,需要统一
    10. train_data = pad_sequences(train_sequences, maxlen=20, padding='post')
    11. test_data = pad_sequences(test_sequences, maxlen=20, padding='post')

    模型搭建和训练

    1. # 创建一个线性模型容器
    2. model = Sequential()
    3. #添加RNN层,神经元数量为100,输入数据形状为(20,1)
    4. model.add(SimpleRNN(100, input_shape=(20, 1)))
    5. # 添加1个输出,激活函数为sigmoid的全连接层
    6. model.add(Dense(1, activation='sigmoid'))
    7. # 优化器:Adam,损失计算方法:二元交叉熵,评估依据:准确率
    8. model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    9. # 输出模型结构
    10. model.summary()
    11. #训练模型,训练5轮,每次训练2个样本
    12. model.fit(train_data, train_labels, epochs=5, batch_size=2, validation_data=(test_data, test_labels))

    模型评估

    1. # 打印评估信息
    2. print('Evaluating the model...')
    3. #进行评估
    4. model.evaluate(test_data, test_labels)

    所有代码集合

    1. import numpy as np
    2. from keras.preprocessing.text import Tokenizer
    3. from keras.preprocessing.sequence import pad_sequences
    4. from keras.models import Sequential
    5. from keras.layers import SimpleRNN, Dense
    6. train_texts = ['I love this movie',
    7. 'This is the worst film I have ever seen.',
    8. 'An enjoyable and thought-provoking experience.',
    9. "I think it is boring"
    10. ]
    11. train_labels = np.array([1, 0, 1, 0])
    12. test_texts = ["What a waste of my time",
    13. "One of the best movies I've seen in a long time",
    14. "Amazing acting!",
    15. "This movie look awful"
    16. ]
    17. test_labels = np.array([0, 1, 1, 0])
    18. tokenizer = Tokenizer(num_words=1000)
    19. tokenizer.fit_on_texts(train_texts + test_texts)
    20. train_sequences = tokenizer.texts_to_sequences(train_texts)
    21. test_sequences = tokenizer.texts_to_sequences(test_texts)
    22. train_data = pad_sequences(train_sequences, maxlen=20, padding='post')
    23. test_data = pad_sequences(test_sequences, maxlen=20, padding='post')
    24. model = Sequential()
    25. model.add(SimpleRNN(100, input_shape=(20, 1)))
    26. model.add(Dense(1, activation='sigmoid'))
    27. model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    28. model.summary()
    29. model.fit(train_data, train_labels, epochs=5, batch_size=2, validation_data=(test_data, test_labels))
    30. print('Evaluating the model...')
    31. model.evaluate(test_data, test_labels)

    运行图片截取

    文件目录

    控制台

     

  • 相关阅读:
    SOM网络1:原理讲解
    中秋节的广西甘蔗——智蔗见智·向新而生
    渗透测试-Kali Linux 正确清理垃圾的姿势
    GEEM2引擎微端架设基本教程
    4种常见的鉴权方式及说明
    第十一章 api mgmnt API 参考
    jQuery、vue、小程序、uni-app中的本地存储数据和接受数据是什么?
    基于深度学习网络的人员吸烟行为检测算法matlab仿真
    Android SELinux
    低蓝光认证:TUV莱茵与TUV南德 有啥区别?
  • 原文地址:https://blog.csdn.net/supermodule/article/details/132655811