如何查看Keras NN回归模型的模型输出?

时间:2017-06-27 15:31:24

标签: python neural-network keras

以下是代码:

import numpy as np
import pandas as pd
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline


dataframe=pd.read_csv('C:/Users/joe/Desktop/BIS/bon.csv', header=0)
dataset=dataframe.values

#splitting into input and output variables
X=dataset[:,0:11]
Y=dataset[:,11]


#defining baseline model
def baseline_model():
    #creating model
    model=Sequential()
    model.add(Dense(11, input_dim=11, kernel_initializer='normal', activation='relu'))
    model.add(Dense(1,kernel_initializer='normal'))
    #compiling model
    model.compile(loss='mean_squared_error', optimizer='adam')
    #testing model
    return model

#fixing random seed
seed=7
np.random.seed(seed)

estimator = KerasRegressor(build_fn=baseline_model, nb_epoch=100, batch_size=5, verbose=0)
kfold=KFold(n_splits=10, random_state=seed)
results=cross_val_score(estimator, X, Y, cv=kfold)
print("Results: %.2f (%.2f) MSE" % (results.mean(), results.std()))

我想将真实值(存储在Y中)与模型的预测值进行比较,我该怎么做?

我试过了     在return model块中的def baseline model之前打印(model.layers [-1] .output)。但这就是我得到的结果:

Tensor("dense_42/BiasAdd:0", shape=(?, 1), dtype=float32)
Tensor("dense_44/BiasAdd:0", shape=(?, 1), dtype=float32)
Tensor("dense_46/BiasAdd:0", shape=(?, 1), dtype=float32)
Tensor("dense_48/BiasAdd:0", shape=(?, 1), dtype=float32)
Tensor("dense_50/BiasAdd:0", shape=(?, 1), dtype=float32)
Tensor("dense_52/BiasAdd:0", shape=(?, 1), dtype=float32)
Tensor("dense_54/BiasAdd:0", shape=(?, 1), dtype=float32)
Tensor("dense_56/BiasAdd:0", shape=(?, 1), dtype=float32)
Tensor("dense_58/BiasAdd:0", shape=(?, 1), dtype=float32)
Tensor("dense_60/BiasAdd:0", shape=(?, 1), dtype=float32)
Results: 0.09 (0.09) MSE

1 个答案:

答案 0 :(得分:0)

您没有获得model.layers[-1].output中的值的原因是因为这只是模型结构(图形),而不是实际运行模型的会话。

您可以简单地使用函数cross_val_predict代替cross_val_score来获取预测,我认为:

http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.cross_val_predict.html#sklearn.model_selection.cross_val_predict