tensorflow - 使用 Keras 构建神经网络的奇怪结果
问题描述
我使用 Keras 构建了一个情感分类器来预测一个句子的情感分数是否为 1、2、3、4 或 5。但是我得到了一些奇怪的结果。我将首先显示我的代码:
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import EarlyStopping
import pandas as pd
import numpy as np
# the data only reflects the structure of the actual data
# the real data has way larger text and more entries
X_train = ['i am glad i heard about that', 'that is one ugly bike']
y_train = pd.Series(np.array([1, 4])) # pandas series
X_test = ['that hurted me']
y_test = pd.Series(np.array([1, 4])) # pandas series
# tokenizing
tokenizer = Tokenizer(num_words = 5)
tokenizer.fit_on_texts(X_train)
X_train_seq = tokenizer.texts_to_sequences(X_train)
X_test_seq = tokenizer.texts_to_sequences(X_test)
# performing some padding
padding_len = 4
X_train_seq_padded = pad_sequences(X_train_seq, maxlen = padding_len)
X_test_seq_padded = pad_sequences(X_test_seq, maxlen = padding_len)
# building the model
model = Sequential()
model.add(Dense(16, input_dim = padding_len, activation = 'relu', name = 'hidden-1'))
model.add(Dense(16, activation = 'relu', name = 'hidden-2'))
model.add(Dense(16, activation = 'relu', name = 'hidden-3'))
model.add(Dense(6, activation='softmax', name = 'output_layer'))
# compiling the model
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics=['accuracy'])
# training the model
callbacks = [EarlyStopping(monitor = 'accuracy', patience = 5, mode = 'max')]
my_model = model.fit(X_train_seq_padded, to_categorical(y_train), epochs = 100, batch_size = 1000, callbacks = callbacks, validation_data = (X_test, to_categorical(y_test)))
使用实际数据,我不断得到大约 0.67xx(xx 随机数)的结果,无论我引入的代码有什么变化(有些是极端的),在 1/2 epochs 之后就会达到这个结果。
- 我尝试将填充更改为 1、10、100、1000。
- 我尝试删除隐藏层 2 和隐藏层 3。
- 我尝试在标记化之前添加停用词删除。
- 我尝试在隐藏层中使用 tahn 激活函数。
- 我使用了 sgd 优化器。
现在我的问题是,我的代码有问题还是这些实际可能的结果?
解决方案
推荐阅读
- node.js - Node.JS Express - POST 请求不起作用(返回 404) - 在端口 8080 的子目录中运行
- ios - 使用 SwiftUI 和 UIKit 视图时如何仅在第一个视图中隐藏导航栏
- pointers - 是否有替代原始指针的不安全 slice::from_raw_parts 的安全替代方法?
- php - 无法针对复选框获取正确的值
- oracle - NVL函数不使用索引而不是FTS,是否可以修改查询
- ios - 如何将不同的字符串值放入 UITableView 中的多行
- python - 计算文本文件中单词的出现次数
- javascript - Array.prototype.map,以及这两个例子的区别
- sql - Sum() Over(ROWS n Preceding) 语句在 python3.7 sqlite3 中不起作用
- django - 在 Django 表单中制作所需的单选按钮