kri*_*nab 5 python keras tensorflow
我正在尝试协调来自 TF“Graphs and Sessions”指南和 TF“Keras”指南和 TF Estimators 指南的信息。现在在前者中它说 tf.Session 使计算图可以访问物理硬件以执行图并训练模型。就像学习 TF 的初始教程要求您使用 Session 来运行任何东西:变量指南、张量指南等。但是,在 TF Keras 指南中,示例似乎在没有任何显式调用tf.Session或通常with tf.Session() as sess:的情况下运行Keras 模型也没有使用 Eager Execution。Estimators API 也是如此。
我有几个代码示例。其中一些使用对会话的调用,而另一些则不使用。我希望有人能澄清使用tf.SessionKeras 层或估算器的要求规则是什么。我的意思是,您似乎可以run_configs为 keras 估算器或标准 TF.estimator 进行设置,并为多 GPU 等进行设置。
以下是功能 API 的 TF Keras 指南中的示例。请注意,没有调用会话:
inputs = tf.keras.Input(shape=(32,)) # Returns a placeholder tensor
# A layer instance is callable on a tensor, and returns a tensor.
x = layers.Dense(64, activation='relu')(inputs)
x = layers.Dense(64, activation='relu')(x)
predictions = layers.Dense(10, activation='softmax')(x)
model = tf.keras.Model(inputs=inputs, outputs=predictions)
# The compile step specifies the training configuration.
model.compile(optimizer=tf.train.RMSPropOptimizer(0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
# Trains for 5 epochs
model.fit(data, labels, batch_size=32, epochs=100)
Run Code Online (Sandbox Code Playgroud)
感谢您提供任何信息或澄清。
tf.Session()仅使用 Keras 时,不需要调用。这是在使用tensorflow后端时调用的,如这里所示。仅当使用tensorflow后端时才调用此函数,而不是使用theanoor时调用CNTK。
关于使用 Tensorflow 接口进行调用,这里对此tf.Session()进行了解释,它只是使用Keras 和纯 TensorFlow张量和/或函数之间的桥梁。tf.Session()
tf.Session()您可以看到使用Keras的示例,使用set_session()后端函数:
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
from keras import backend as K
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)
Run Code Online (Sandbox Code Playgroud)