对不起,如果查询是原始查询。 我有一些代码试图对整数进行分类,如果它们不是质数。我已经使用Keras训练了模型。我正在尝试使用以下方法进行预测:
predict( x, batch_size=None, verbose=0, steps=None)
我不断收到以下错误消息:
---->预测(x = 5000003,batch_size = None,verbose = 0,steps = None)
NameError:名称'predict'未定义
当我使用以下命令时:“ model.predict(x = 5000003,batch_size = None,verbose = 0,steps = None)”,我收到此错误消息“ AttributeError:'KerasClassifier'对象没有属性'model '“
代码:
import numpy
from numpy import array
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import GridSearchCV
seed = 7
numpy.random.seed(seed)
def isPrime(number):
if number == 1:
return 0
elif number == 2:
return 1
elif number % 2 == 0:
return 0
for d in range(3, int(number**(0.5)+1), 2):
if number % d == 0:
return 0
else:
return 1
p=[]
N=[]
for i in range (1,10000):
p=[i,isPrime(i)]
N=N+[p]
a=array (N)
X=a[:10000,0]
Y=a[:10000,1]
def create_model(optimizer='rmsprop', init='glorot_uniform'):
# create model
model = Sequential()
model.add(Dense(2, input_dim=1, kernel_initializer=init, activation='selu'))
model.add(Dense(1, kernel_initializer=init, activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
# create model
model = KerasClassifier(build_fn=create_model, epochs=1000, batch_size=100, init='glorot_uniform', verbose=0)
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed)
results = cross_val_score(model, X, Y, cv=kfold)
print(results.mean())
predict(x=5000003, batch_size=None, verbose=0, steps=None)
答案 0 :(得分:0)
predict
是model
对象的功能,因此您可以将其用作:
model = KerasClassifier(build_fn=create_model, epochs=1000, batch_size=100, init='glorot_uniform', verbose=0)
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed)
results = cross_val_score(model, X, Y, cv=kfold)
print(results.mean())
# Call on model
model.predict(x=5000003, batch_size=None, verbose=0, steps=None)
这里是source code,以调查其幕后行为。