ben*_*ben 72 python theano deep-learning keras
我正在玩路透社示例数据集,它运行正常(我的模型已经过培训).我读到了如何保存模型,所以我可以稍后加载它再次使用.但是如何使用此保存的模型来预测新文本?我用models.predict()吗?
我是否必须以特殊方式准备此文本?
我试过了
import keras.preprocessing.text
text = np.array(['this is just some random, stupid text'])
print(text.shape)
tk = keras.preprocessing.text.Tokenizer(
nb_words=2000,
filters=keras.preprocessing.text.base_filter(),
lower=True,
split=" ")
tk.fit_on_texts(text)
pred = tk.texts_to_sequences(text)
print(pred)
model.predict(pred)
Run Code Online (Sandbox Code Playgroud)
但我总是得到
(1L,)
[[2, 4, 1, 6, 5, 7, 3]]
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-83-42d744d811fb> in <module>()
7 print(pred)
8
----> 9 model.predict(pred)
C:\Users\bkey\Anaconda2\lib\site-packages\keras\models.pyc in predict(self, x, batch_size, verbose)
457 if self.model is None:
458 self.build()
--> 459 return self.model.predict(x, batch_size=batch_size, verbose=verbose)
460
461 def predict_on_batch(self, x):
C:\Users\bkey\Anaconda2\lib\site-packages\keras\engine\training.pyc in predict(self, x, batch_size, verbose)
1132 x = standardize_input_data(x, self.input_names,
1133 self.internal_input_shapes,
-> 1134 check_batch_dim=False)
1135 if self.stateful:
1136 if x[0].shape[0] > batch_size and x[0].shape[0] % batch_size != 0:
C:\Users\bkey\Anaconda2\lib\site-packages\keras\engine\training.pyc in standardize_input_data(data, names, shapes, check_batch_dim, exception_prefix)
79 for i in range(len(names)):
80 array = arrays[i]
---> 81 if len(array.shape) == 1:
82 array = np.expand_dims(array, 1)
83 arrays[i] = array
AttributeError: 'list' object has no attribute 'shape'
Run Code Online (Sandbox Code Playgroud)
您对如何使用经过训练的模型进行预测有任何建议吗?
nem*_*emo 50
model.predict()期望第一个参数是一个numpy数组.您提供了一个列表,该列表没有shapenumpy数组所具有的属性.
否则你的代码看起来很好,除了你没有对预测做任何事情.确保将其存储在变量中,例如:
prediction = model.predict(np.array(tk.texts_to_sequences(text)))
print(prediction)
Run Code Online (Sandbox Code Playgroud)
您可以使用正确形状的数组“调用”您的模型:
model(np.array([[6.7, 3.3, 5.7, 2.5]]))
Run Code Online (Sandbox Code Playgroud)
完整示例:
from sklearn.datasets import load_iris
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
import numpy as np
X, y = load_iris(return_X_y=True)
model = Sequential([
Dense(16, activation='relu'),
Dense(32, activation='relu'),
Dense(1)])
model.compile(loss='mean_absolute_error', optimizer='adam')
history = model.fit(X, y, epochs=10, verbose=0)
print(model(np.array([[6.7, 3.3, 5.7, 2.5]])))
Run Code Online (Sandbox Code Playgroud)
<tf.Tensor: shape=(1, 1), dtype=float64, numpy=array([[1.92517677]])>
Run Code Online (Sandbox Code Playgroud)
model.predict_classes(<numpy_array>)
Run Code Online (Sandbox Code Playgroud)
样本https://gist.github.com/alexcpn/0683bb940cae510cf84d5976c1652abd
您必须使用用于构建模型的相同 Tokenizer!
否则,这将为每个单词提供不同的向量。
然后,我正在使用:
phrase = "not good"
tokens = myTokenizer.texts_to_matrix([phrase])
model.predict(np.array(tokens))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
117196 次 |
| 最近记录: |