<jupyter_start><jupyter_text>准备数据不能直接把数据输入到神经网络，需要将列表转换成张量，转换方式有以下两种：- 填充列表，使其具有相同的长度，再将列表转换成形状为（samples ,word_indices）的整数张量，然后网络第一层使用能处理这种整数张量的层（Embedding）- 对列表进行one-hot编码，将其转换为0和1组成的向量。<jupyter_code>import numpy as np

def vectorize_sequences(sequences,dimension=10000):
    '''
    转换成张量
    '''
    results = np.zeros((len(sequences),dimension))
    for i, sequence in enumerate(sequences):
        results[i,sequence] = 1.
    return results

x_train = vectorize_sequences(train_data)
x_test = vectorize_sequences(test_data)
x_train[0]
#标签也需要向量化
y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')<jupyter_output><empty_output><jupyter_text>构建网络这种Dense层的堆叠，需要确认两个关键架构- 网络有多少层- 每层有多少个隐藏单元<jupyter_code>from keras import models
from keras import layers

model = models.Sequential()
model.add(layers.Dense(16, activation='relu',input_shape=(10000,)))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1,activation='sigmoid'))
#编译模型

#配置优化器
from keras import optimizers

# model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy']) #方式一

# model.compile(optimizer=optimizers.RMSprop(lr=0.001),loss='binary_crossentropy',metrics=['accuracy'])

#使用自定义的损失和指标
from keras import losses
from keras import metrics

model.compile(optimizer=optimizers.RMSprop(lr=0.001), loss=losses.binary_crossentropy, metrics=[metrics.binary_accuracy])
#留出验证集
x_val = x_train[:10000]
partial_x_train = x_train[10000:]

y_val = y_train[:10000]
partial_y_train = y_train[10000:]
#训练模型
history = model.fit(partial_x_train,partial_y_train,epochs=20,batch_size=512,validation_data=(x_val,y_val))
history_dict = history.history
history_dict.keys()
## 绘制训练损失和验证损失
history_dict = history.history
loss_values = history_dict['loss']
val_loss_value = history_dict['val_loss']

epochs = range(1, len(loss_values) + 1)


import matplotlib.pyplot as plt

plt.plot(epochs,loss_values,'bo',label ='Training loss')#bo表示蓝色原点
plt.plot(epochs,val_loss_value,'b',label = 'Validation loss')# b表示蓝色实线
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()

plt.show()
## 绘制训练精度和验证精度
plt.clf # 清空图像
acc = history_dict['binary_accuracy']
val_acc = history_dict['val_binary_accuracy']

plt.plot(epochs,acc,'bo', label='Training acc')
plt.plot(epochs,val_acc,'b', label = 'Validation acc')
plt.title('Training and  validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()

plt.show()<jupyter_output><empty_output><jupyter_text>上图过拟合<jupyter_code>## 重头开始训练，到第四轮停止
model = models.Sequential()
model.add(layers.Dense(16, activation='relu',input_shape=(10000,)))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1,activation='sigmoid'))

model.compile(optimizer=optimizers.RMSprop(lr=0.001), loss=losses.binary_crossentropy, metrics=[metrics.binary_accuracy])

model.fit(x_train,y_train,epochs=4,batch_size=512)
result = model.evaluate(x_test,y_test)
result<jupyter_output><empty_output><jupyter_text>用训练好的模型在新数据上进行预测<jupyter_code>model.predict(x_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14221364221364222, 'fraction_numerical': 0.02445302445302445, 'mean_word_length': 9.57482993197279, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '351252', 'license': 'MIT', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1407, 'n_words': 150, 'path': 'IMDB/IMDB.ipynb', 'repo_name': 'chenmengjie-xiaomi/Deep_Learning_with_Python', 'size': '3108'}