TensorFlow和Keras中的符号张量是什么? [英] What are symbolic tensors in TensorFlow and Keras?

查看:662
本文介绍了TensorFlow和Keras中的符号张量是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TensorFlow和Keras中的符号张量是什么?它们与其他张量有何不同?为什么它们甚至存在?他们在TensorFlow和Keras中出现在哪里?

What are symbolic tensors in TensorFlow and Keras? How are they different than other tensors? Why do they even exist? Where do they come up in TensorFlow and Keras? How should we deal with them or what problems can we face when dealing with them?

过去,我曾遇到过一些与符号张量相关的问题,例如 _SymbolicException ,但文档未描述此概念。也有另一篇文章,该问题也被问到了,但是在这篇文章中,我将重点放在这个特定问题上,因此

In the past, I had faced certain issues related to symbolic tensors, such as the _SymbolicException, but the documentation does not describe this concept. There's also another post where this question is also asked, but, in this post, I am focusing on this specific question, so that answers can be later used as a reference.

推荐答案

根据 blog.tensorflow.org 符号张量与其他张量不同

让我们考虑一个简单的例子。

Let's consider a simple example.

>>> a = tf.Variable(5, name="a")
>>> b = tf.Variable(7, name="b")
>>> c = (b**2 - a**3)**5
>>> print(c)

输出如下:

tf.Tensor(1759441920, shape=(), dtype=int32)

对于上述内容,这些值特别以 tf.Variable 格式定义,并且输出为Tensor格式。但是,张量必须包含一个值才能被认为是这样。

For the above, the values are specifically defined in tf.Variable format, and the output is in Tensor format. However, the tensor must contain a value in order to be considered as such.

符号张量不同,因为不需要显式值来定义张量,并且这意味着使用TensorFlow 2.0构建神经网络,现在使用 Keras 作为默认API。

Symbolic tensors are different in that no explicit values are required to define the tensor, and this has implications in terms of building neural networks with TensorFlow 2.0, which now uses Keras as the default API.

这里是顺序神经的一个示例用于建立预测酒店取消发生率的分类模型的网络(完整的Jupyter Notebook 此处(如果有兴趣):

Here is an example of a Sequential neural network that is used to build a classification model for predicting hotel cancellation incidences (full Jupyter Notebook here if interested):

from tensorflow.keras import models
from tensorflow.keras import layers

model = models.Sequential()
model.add(layers.Dense(8, activation='relu', input_shape=(4,)))
model.add(layers.Dense(1, activation='sigmoid'))

这是一个符号定义的模型,因为在网络中未明确定义任何值。相反,创建了一个框架,供网络读取输入变量,然后生成预测。

This is a symbolically defined model, as no values are explicitly being defined in the network. Rather, a framework is created for the input variables to be read by the network, and then generate predictions.

在这方面,Keras变得非常流行,因为它允许用于使用符号张量构建图,同时保持命令式布局。

In this regard, Keras has become quite popular given that it allows for building of graphs using symbolic tensors, while at the same time maintaining an imperative layout.

这篇关于TensorFlow和Keras中的符号张量是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆