Gon*_*oso 12 python pandas keras
我正在尝试使用Keras在Python中执行情绪分析.为此,我需要对我的文本进行嵌入.当我尝试将数据拟合到我的模型时,会出现问题:
model_1 = Sequential()
model_1.add(Embedding(1000,32, input_length = X_train.shape[0]))
model_1.add(Flatten())
model_1.add(Dense(250, activation='relu'))
model_1.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
Run Code Online (Sandbox Code Playgroud)
我的火车数据的形状是
(4834,)
Run Code Online (Sandbox Code Playgroud)
并且是熊猫系列的对象.当我尝试使用我的模型并使用其他一些数据验证它时,我收到此错误:
model_1.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=2, batch_size=64, verbose=2)
Run Code Online (Sandbox Code Playgroud)
ValueError:检查模型输入时出错:预期embedding_1_input具有形状(无,4834)但是具有形状的数组(4834,1)
如何重塑我的数据以使其适合Keras?我一直在尝试使用np.reshape,但我不能使用该函数放置None元素.
提前致谢
Dat*_*ran 15
None是进入培训的预期行数,因此您无法定义它.Keras也需要一个numpy数组作为输入而不是pandas数据帧.首先将df转换为numpy数组,df.values然后执行np.reshape((-1, 4834)).请注意,您应该使用np.float32.如果您在GPU上进行训练,这一点非常重要.
https://pypi.org/project/keras-pandas/
最简单的方法是使用keras_pandas包将pandas数据框装入keras.下面显示的代码是包文档中的一般示例.
from keras import Model
from keras.layers import Dense
from keras_pandas.Automater import Automater
from keras_pandas.lib import load_titanic
observations = load_titanic()
# Transform the data set, using keras_pandas
categorical_vars = ['pclass', 'sex', 'survived']
numerical_vars = ['age', 'siblings_spouses_aboard', 'parents_children_aboard', 'fare']
text_vars = ['name']
auto = Automater(categorical_vars=categorical_vars, numerical_vars=numerical_vars, text_vars=text_vars,
response_var='survived')
X, y = auto.fit_transform(observations)
# Start model with provided input nub
x = auto.input_nub
# Fill in your own hidden layers
x = Dense(32)(x)
x = Dense(32, activation='relu')(x)
x = Dense(32)(x)
# End model with provided output nub
x = auto.output_nub(x)
model = Model(inputs=auto.input_layers, outputs=x)
model.compile(optimizer='Adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train model
model.fit(X, y, epochs=4, validation_split=.2)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19910 次 |
| 最近记录: |