小编Dam*_*pov的帖子

来自tf.session.run的网络输出与使用keras.Model.predict获得的输出大不相同

我正试图通过Tensorflow会话使用Keras模型.但结果形式model.predictsess.run不同.有没有办法通过Tensorflow会话使用Kers模型?

Tensorflow版本:1.4.0 Keras
版本:2.1.1

from sklearn.datasets.samples_generator import make_circles
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
import numpy as np
import tensorflow as tf
from keras import backend as K

sess = tf.Session()
K.tensorflow_backend.set_session(sess)

X, y = make_circles(n_samples=1000,
                    noise=0.1,
                    factor=0.2,
                    random_state=0)

model = Sequential()
model.add(Dense(4, input_shape=(2,), activation='tanh'))
model.add(Dense(1, activation='sigmoid'))
model.compile(SGD(lr=0.5), 'binary_crossentropy', metrics=['accuracy'])
model.fit(X, y, epochs=20)

print("Keras model. First prediction: " + str(model.predict(np.c_[0, 0])))
print("Keras model. Second prediction: " + str(model.predict(np.c_[1.5, 1.5])))

with …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow

3
推荐指数
1
解决办法
599
查看次数

标签 统计

keras ×1

python ×1

tensorflow ×1