tokenizer = Tokenizer(num_words=max_words) tokenizer.fit_on_texts(data['text']) sequences = tokenizer.texts_to_sequences(data['text']) word_index = tokenizer.word_index print('Found %s unique tokens.' % len(word_index)) data = pad_sequences(sequences,maxlen=maxlen) labels = np.array(data[:,:1]) print('Shape of data tensor:',data.shape) print('Shape of label tensor',labels.shape) indices = np.arange(data.shape[0]) np.random.shuffle(indices) data = data[indices] labels = labels[indices] x_train = data[:traing_samples] y_train = data[:traing_samples] x_val = data[traing_samples:traing_samples+validation_samples] y_val = data[traing_samples:traing_samples+validation_samples] model = Sequential() model.add(Embedding(max_words,100,input_length=maxlen)) model.add(Flatten()) model.add(Dense(32,activation='relu')) model.add(Dense(10000,activation='sigmoid')) model.summary() model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc']) history = model.fit(x_train,y_train, epochs=1, batch_size=128, validation_data=[x_val,y_val]) import matplotlib.pyplot as plt acc = history.history['acc'] val_acc = history.history['val_acc'] loss = history.history['loss'] val_loss = history.history['val_loss'] epoachs = range(1,len(acc) + 1) plt.plot(epoachs,acc,'bo',label='Training acc') plt.plot(epoachs,val_acc,'b',label = 'Validation acc') plt.title('Training and validation accuracy') plt.legend() plt.figure() plt.plot(epoachs,loss,'bo',label='Training loss') plt.plot(epoachs,val_loss,'b',label = 'Validation loss') plt.title('Training and validation loss') plt.legend() plt.show() max_len = 10000 x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=max_len) x_test = data[10000:,0:] x_test = keras.preprocessing.sequence.pad_sequences(x_test, maxlen=max_len) # 将标签转换为独热编码 y_train = np.eye(2)[y_train] y_test = data[10000:,:1] y_test = np.eye(2)[y_test]
时间: 2024-02-05 12:03:57 浏览: 92
这段代码中存在几个问题:
1. `labels` 的赋值有误。应该是 `labels = np.array(data[:, 1:])`,因为标签是从第二列开始的。
2. `validation_data` 的格式有误。应该是 `validation_data=(x_val, y_val)`,而不是 `validation_data=[x_val, y_val]`。
3. 在使用 `Sequential` 模型时,需要指定输入的维度。这里应该是 `model.add(Dense(10000, activation='sigmoid', input_dim=maxlen))`,而不是 `model.add(Dense(10000, activation='sigmoid'))`。
4. 绘制图表时,应该使用 `val_acc` 和 `val_loss`,而不是 `test_acc` 和 `test_loss`。
5. 在将标签转换为独热编码时,应该使用 `y_train[:, 0]` 和 `y_test[:, 0]`,而不是 `y_train` 和 `y_test`。
以下是修改后的代码:
```python
tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(data['text'])
sequences = tokenizer.texts_to_sequences(data['text'])
word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))
data = pad_sequences(sequences, maxlen=maxlen)
labels = np.array(data[:, 1:])
print('Shape of data tensor:', data.shape)
print('Shape of label tensor', labels.shape)
indices = np.arange(data.shape[0])
np.random.shuffle(indices)
data = data[indices]
labels = labels[indices]
x_train = data[:training_samples]
y_train = labels[:training_samples]
x_val = data[training_samples:training_samples+validation_samples]
y_val = labels[training_samples:training_samples+validation_samples]
model = Sequential()
model.add(Embedding(max_words, 100, input_length=maxlen))
model.add(Flatten())
model.add(Dense(32, activation='relu'))
model.add(Dense(10000, activation='sigmoid', input_dim=maxlen))
model.summary()
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['acc'])
history = model.fit(x_train, y_train,
epochs=1,
batch_size=128,
validation_data=(x_val, y_val))
import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
max_len = 10000
x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=max_len)
x_test = data[10000:, 0:]
x_test = keras.preprocessing.sequence.pad_sequences(x_test, maxlen=max_len)
# 将标签转换为独热编码
y_train = np.eye(2)[y_train[:, 0]]
y_test = data[10000:, 1:]
y_test = np.eye(2)[y_test[:, 0]]
```
阅读全文