[英]NLP, TfIdf output to CNN
我正在使用 TfIdf 进行 twitter 文本分析,我想将 TfIdfVectorizer 的输出输入到 CNN 中。 然而,我没有得到好的结果。 我将在这里提供我的代码:
def __init__(self):
self.name = ''
self.tknzr = TweetTokenizer()
self.model = TfidfVectorizer(max_features=10000, lowercase=False)
self.pca = PCA(n_components=100)
def fit(self, X):
print('Fitting the tfidf vectorizer...')
matrix = self.model.fit_transform(X)
print('Dimension of original tfidf matrix: ', matrix.shape)
print('Encoder fitting completed!')
return matrix.toarray()
def encode(self, X):
print('Encoding data...')
matrix = self.model.transform(X)
return matrix.toarray()
和 CNN 代码:
def __init__(self):
self.name = 'keras-sequential'
self.model = None
self.tokenizer = Tokenizer()
# self.earlystopping = callbacks.EarlyStopping(monitor="mae",
# mode="min", patience=5,
# restore_best_weights=True)
def fit(self, X, y):
print('Fitting into the neural net...')
# n_inputs = X.shape[1]
n_outputs = y.shape[1]
input = Input(shape=(10000, 1))
conv2 = Conv1D(filters=32, kernel_size=8, activation='relu')(input)
maxpool1 = Dropout(0.5)(conv2)
conv22 = Conv1D(filters=16, kernel_size=4, activation='relu')(maxpool1)
maxpool2 = Dropout(0.5)(conv22)
conv23 = Conv1D(filters=8, kernel_size=4, activation='relu')(maxpool2)
maxpool3 = Dropout(0.5)(conv23)
flat2 = Flatten()(maxpool3)
d1 = Dense(200, activation='relu')(flat2)
d2 = Dense(100, activation='relu')(d1)
out = Dense(n_outputs, activation='sigmoid')(d2)
self.model = Model(input, out)
self.model.summary()
self.model.compile(loss='mse', optimizer='adam', metrics=['mse', 'mae', custom_metric])
history = self.model.fit(X, y, verbose=1, epochs=10, validation_split=0.1)
我得到了糟糕的训练(验证)结果以及测试结果。 你知道我在这里做错了什么吗? 关于这个问题的更多信息:我有很多 Twitter 文本,对于每个文本,我有 5 个值。 目标是训练一种算法来预测这 5 个值。 我正在使用 tfidf 对文本进行编码,并且我想将编码适当地传递到 CNN 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.