首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TensorFlow2.x中的session.run()等效项

TensorFlow2.x中的session.run()等效项
EN

Stack Overflow用户
提问于 2021-06-14 03:41:19
回答 1查看 39关注 0票数 2

此函数采用预先训练好的imagenet-vgg-verydeep-19.mat模型,并使用其权重和输入返回图( model )。

代码语言:javascript
复制
def load_vgg_model(path):
    ...
    ...
    ...
    # Constructs the graph model.
    graph = {}
    graph['input']   = tf.Variable(np.zeros((1, 400, 300, 3)), dtype = 'float32')
    graph['conv1_1']  = _conv2d_relu(graph['input'], 0, 'conv1_1')      # returns tf.nn.relu
    graph['conv1_2']  = _conv2d_relu(graph['conv1_1'], 2, 'conv1_2')
    graph['avgpool1'] = _avgpool(graph['conv1_2'])                    # returns tf.nn.avg_pool
    graph['conv2_1']  = _conv2d_relu(graph['avgpool1'], 5, 'conv2_1')
    ....
    ....
    graph['conv5_4']  = _conv2d_relu(graph['conv5_3'], 34, 'conv5_4')
    graph['avgpool5'] = _avgpool(graph['conv5_4'])
    
    return graph

下面是TensorFlow 1中相同的实现

代码语言:javascript
复制
model = load_vgg_model("imagenet-vgg-verydeep-19.mat")
sess = tf.compat.v1.InteractiveSession()
content_image = imread("images/louvre_small.jpg")

sess.run(model['input'].assign(content_image))
out = model['conv4_2']
a_C = sess.run(out)
a_G = out

但是我想知道TensorFlow 2.x中的实现

我已经阅读了这些文档https://www.tensorflow.org/guide/effective_tf2

https://www.tensorflow.org/guide/migrate

EN

回答 1

Stack Overflow用户

发布于 2021-07-27 11:23:21

正如在this doc中提到的,Tensorflow 2具有默认的紧急执行。代码通过构建中间图来执行。

在Tensorflow 2中,会话的创建不是required.You,可以使用Autograph函数来代替会话来创建图形。

文件上说,

代码语言:javascript
复制
TensorFlow 1.x requires users to manually stitch together an abstract syntax tree (the graph) by making tf.* API calls. It then requires users to manually compile the abstract syntax tree by passing a set of output tensors and input tensors to a session.run call. TensorFlow 2.0 executes eagerly (like Python normally does) and in 2.0, graphs and sessions should feel like implementation details.

One notable byproduct of eager execution is that tf.control_dependencies is no longer required, as all lines of code execute in order (within a tf.function, code with side effects executes in the order written).
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67962056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档