【发布时间】:2017-12-11 10:32:39
【问题描述】:
Keras 将始终为我给 hm 的每个输入预测相同的类。目前有四个班。 新闻、天气、体育和经济。
训练集由许多不同的文本组成,其中的类与其主题相同。分类为新闻和体育的文本比天气和经济的文本多得多。
- 新闻:12112条文本
- 天气:1685条短信
- 运动:13669 条文本
- 经济:1282 篇文章
我原以为该模型会偏向于体育和新闻,但它却完全偏向于天气,每个输入都被归类为至少 80% 置信度的天气。
让我更加困惑的是:在训练注释器时,准确率会达到 95% 到 100%(原文如此!)。我想我在这里做了一些非常愚蠢的事情,但我不知道它是什么。
这就是我如何称呼我的分类器。它在 Windows pc 上的 python 3 上运行。
with open('model.json') as json_data:
model_JSON = json.load(json_data)
model_JSON = json.dumps(model_JSON)
model = model_from_json(model_JSON)
model.load_weights('weights.h5')
text = str(text.decode())
encoded = one_hot(text, max_words, split=" ")
tokenizer = Tokenizer(num_words=max_words)
matrix = tokenizer.sequences_to_matrix([encoded], mode='binary')
result = model.predict(matrix)
legende = ["News", "Wetter", "Sport", "Wirtschaft"]
print(str(legende))
print(str(result))
cat = numpy.argmax(result)
return str(legende[cat]).encode()
这是我训练分类器的方法。我省略了从数据库中获取数据的部分。这是在 Linux VM 上完成的。 我已经尝试改变损失和激活,但什么也没发生。 此外,我目前正在尝试使用更多的时代,但到目前为止,这也没有帮助。
max_words = 10000
batch_size=32
epochs=15
rows = cursor.fetchall()
X = []
Y = []
# Einlesen der Rows
for row in rows:
X.append(row[5])
Y.append(row[1])
num_classes = len(set(Y))
Y = one_hot("$".join(Y), num_classes, split="$")
for i in range(len(X)):
X[i] = one_hot(str(X[i]), max_words, split=" ")
split = round(len(X) * 0.2)
x_test = np.asarray(X[0:int(split)])
y_test = np.asarray(Y[0:int(split)])
x_train = np.asarray(X[int(split):len(X)])
y_train = np.asarray(Y[int(split):len(X)])
print('x_test shape', x_test.shape)
print('y_test shape', y_test.shape)
print(num_classes, 'classes')
#vektorisieren
tokenizer = Tokenizer(num_words=max_words)
x_train = tokenizer.sequences_to_matrix(x_train, mode='binary')
x_test = tokenizer.sequences_to_matrix(x_test, mode='binary')
#klassenvektor zu binärer klassenmatrix
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
#model erstellen
model = Sequential()
model.add(Dense(512, input_shape=(max_words,)))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_split=0.1
)
score = model.evaluate(x_test, y_test,
batch_size=batch_size,
verbose=1
)
print('Test score', score[0])
print('Test accuracy', score[1])
#write model to json
print("writing model to json")
model_json = model.to_json()
with open("model.json", 'w') as json_file:
json_file.write(model_json)
#save weights as hdf5
print("saving weights to hdf5")
model.save_weights("weights.h5")
【问题讨论】:
-
您的模型很可能确实在预测所有事物的运动。你确定你以正确的顺序解释这四个类吗?您可能在
y_train和legende之间颠倒了一些东西。 -
这是可能的。但令我困惑的是,分类器的准确率接近 100%。无论如何,我将首先正确规范化数据。值得一试,
-
计算
y_train中有多少元素属于紧挨在fit之前的哪个类可能会很有趣。 -
@Junge,我对
Tokenizer很有经验,但也许您正试图以batch为基础,因此结果包含第一批的predictions(默认大小为32)?resultvar 的形状是什么? -
好吧,发生了一件有趣的事情,我确实在 Y 变量上使用了 one_hot。由于一个热点不是无碰撞的,因此发生了碰撞。他将除“天气”以外的所有内容都编码为 1,将“天气”编码为 2。所以现在他可以可靠地预测所有内容都是 2。我修复了这个问题,现在他几乎总是预测“新闻”,但这是我可能会得到控制的事情.
标签: python machine-learning keras prediction