已知语料划分训练集和测试集,已知词向量预训练模型,比较三种深度学习模型完整代码
时间: 2023-08-07 12:35:23 浏览: 94
好的,下面是三种深度学习模型的完整代码,包括数据预处理、模型定义、训练和测试过程。
## 1. 模型一: LSTM
### 1.1 数据预处理
```python
import numpy as np
import pandas as pd
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
# 读取数据
train_data = pd.read_csv("train.csv")
test_data = pd.read_csv("test.csv")
# 将文本转换为数字序列
tokenizer = Tokenizer(num_words=5000)
tokenizer.fit_on_texts(train_data['text'])
X_train = tokenizer.texts_to_sequences(train_data['text'])
X_test = tokenizer.texts_to_sequences(test_data['text'])
# 对数字序列进行填充
maxlen = 100
X_train = pad_sequences(X_train, padding='post', maxlen=maxlen)
X_test = pad_sequences(X_test, padding='post', maxlen=maxlen)
# 将标签转换为分类
y_train = pd.get_dummies(train_data['label']).values
y_test = pd.get_dummies(test_data['label']).values
```
### 1.2 模型定义
```python
from keras.models import Sequential
from keras.layers import LSTM, Dense, Embedding
# 定义模型
model = Sequential()
model.add(Embedding(5000, 32, input_length=maxlen))
model.add(LSTM(64))
model.add(Dense(2, activation='softmax'))
# 编译模型
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# 打印模型结构
print(model.summary())
```
### 1.3 训练和测试
```python
# 训练模型
model.fit(X_train, y_train, epochs=10, batch_size=64)
# 测试模型
score = model.evaluate(X_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])
```
## 2. 模型二: CNN
### 2.1 数据预处理
```python
import numpy as np
import pandas as pd
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
# 读取数据
train_data = pd.read_csv("train.csv")
test_data = pd.read_csv("test.csv")
# 将文本转换为数字序列
tokenizer = Tokenizer(num_words=5000)
tokenizer.fit_on_texts(train_data['text'])
X_train = tokenizer.texts_to_sequences(train_data['text'])
X_test = tokenizer.texts_to_sequences(test_data['text'])
# 对数字序列进行填充
maxlen = 100
X_train = pad_sequences(X_train, padding='post', maxlen=maxlen)
X_test = pad_sequences(X_test, padding='post', maxlen=maxlen)
# 将标签转换为分类
y_train = pd.get_dummies(train_data['label']).values
y_test = pd.get_dummies(test_data['label']).values
```
### 2.2 模型定义
```python
from keras.models import Sequential
from keras.layers import Embedding, Conv1D, MaxPooling1D, Flatten, Dense
# 定义模型
model = Sequential()
model.add(Embedding(5000, 32, input_length=maxlen))
model.add(Conv1D(64, 3, activation='relu'))
model.add(MaxPooling1D(3))
model.add(Conv1D(64, 3, activation='relu'))
model.add(MaxPooling1D(3))
model.add(Flatten())
model.add(Dense(2, activation='softmax'))
# 编译模型
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# 打印模型结构
print(model.summary())
```
### 2.3 训练和测试
```python
# 训练模型
model.fit(X_train, y_train, epochs=10, batch_size=64)
# 测试模型
score = model.evaluate(X_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])
```
## 3. 模型三: BERT
### 3.1 数据预处理
```python
import numpy as np
import pandas as pd
from transformers import BertTokenizer
# 读取数据
train_data = pd.read_csv("train.csv")
test_data = pd.read_csv("test.csv")
# 初始化BERT分词器
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
# 将文本转换为BERT输入格式
X_train = [tokenizer.encode(str(text), add_special_tokens=True) for text in train_data['text']]
X_test = [tokenizer.encode(str(text), add_special_tokens=True) for text in test_data['text']]
# 对BERT输入进行填充和截断
maxlen = 100
X_train = np.array([x[:maxlen] + [0] * (maxlen - len(x)) for x in X_train])
X_test = np.array([x[:maxlen] + [0] * (maxlen - len(x)) for x in X_test])
# 将标签转换为分类
y_train = pd.get_dummies(train_data['label']).values
y_test = pd.get_dummies(test_data['label']).values
```
### 3.2 模型定义
```python
from transformers import TFBertModel
import tensorflow as tf
from tensorflow.keras.layers import Input, Dropout, Dense
# 加载BERT模型
bert = TFBertModel.from_pretrained('bert-base-uncased')
# 定义模型
input_ids = Input(shape=(maxlen,), dtype=tf.int32)
inputs = {'input_ids': input_ids}
bert_layer = bert(inputs)[1]
dropout = Dropout(0.2)(bert_layer)
outputs = Dense(2, activation='softmax')(dropout)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
# 编译模型
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# 打印模型结构
print(model.summary())
```
### 3.3 训练和测试
```python
# 训练模型
model.fit(X_train, y_train, epochs=10, batch_size=64)
# 测试模型
score = model.evaluate(X_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])
```
阅读全文