python - Keras标签的问题
问题描述
我正在使用预处理来标记数据并使用 em 拟合模型
我的资源:https ://machinelearningmastery.com/prepare-text-data-deep-learning-keras
这是我的代码(我想从负面词中检测正面词):
from keras.preprocessing.text import one_hot
from keras.preprocessing.text import text_to_word_sequence
import pandas as pd
import numpy as np
from keras.layers import Dense
from keras.models import Sequential
from keras.preprocessing.text import Tokenizer
from tensorflow.keras.layers.experimental import preprocessing
data = ["Good", "Great", 'Bad', 'Awefull']
t = Tokenizer()
# fit the tokenizer on the documents
t.fit_on_texts(data)
# integer encode documents
x = t.texts_to_matrix(data, mode='count')
print(x)
y = np.array([[1],[1],[0],[0]])
print(y)
model = Sequential()
model.add(Dense(16, input_dim=2, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['binary_accuracy'])
model.fit(x, y, verbose=2, epochs=500)
model.save("good_or_bad.h5")
我有这个错误:
raise ValueError(
ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 2 but received input with shape [None, 5]
我该如何解决这个错误?
解决方案
更改此行:
model.add(Dense(16, input_dim=5, activation='relu'))
推荐阅读
- windows - 为什么 Windows Defender 会延迟我们软件的启动?
- php - 函数 get_permalink() 在 if 语句(Wordpress)中无法正常工作
- vue.js - 如何将 Vue CLI 3.12.0 降级到 3.0.1
- visual-studio-code - 完成覆盖大括号
- python - 激活小部件时无法自动退出平移或缩放模式
- filter - 如何根据 Tableau 中的日期范围动态更改标志的值
- c++ - 即使对于左值也抛出调用移动构造函数
- r - 删除数据框列中的两个特定元素和所有元素
- c# - GetCurrentDirectory() 返回 AppData\Local\Temp
- c# - 如何根据年份制作自动增量编号