为什么“tf.Variable([.3], tf.float32)"在张量流中工作? [英] Why does "tf.Variable([.3], tf.float32)" work in tensorflow?

查看:67
本文介绍了为什么“tf.Variable([.3], tf.float32)"在张量流中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准用法应该是

tf.Variable([.3], dtype=tf.float32),不是吗?

我在官方文档中看到了 tf.Variable([.3], tf.float32).tf.Variable 的构造函数原型为

I saw the tf.Variable([.3], tf.float32) in official documentation. The constructor function prototype of tf.Variable is

__init__(self,initial_value=None,trainable=True,collections=None,validate_shape=True,caching_device=None,name=None,variable_def=None,dtype=None,expected_shape=None,import_scope=None).

如果我们传递参数tf.float32 而不是dtype=tf.float32(关键参数),它怎么知道tf.float32code> 用于 dtype.python解释器会检查参数类型吗?

If we pass the parameter tf.float32 instead of dtype=tf.float32 (key parameter), how does it know the tf.float32 is employed for dtype. Does python interpreter check the parameter type?

推荐答案

来自 tf.Variable 的文档:

dtype:如果设置,initial_value 将被转换为给定的类型.如果 None,要么保留数据类型(如果 initial_value 是张量),或者 convert_to_tensor 将决定.

dtype: If set, initial_value will be converted to the given type. If None, either the datatype will be kept (if initial_value is a Tensor), or convert_to_tensor will decide.

以及来自 convert_to_tensor(value, dtype=None, ...) 的文档:

dtype:返回张量的可选元素类型.如果丢失,则类型是从 value 的类型推断出来的.

dtype: Optional element type for the returned tensor. If missing, the type is inferred from the type of value.

此外,convert_to_tensor 的文档中还提供了一个示例:

Also, there is an example given in the documentation to convert_to_tensor:

import numpy as np

def my_func(arg):
    arg = tf.convert_to_tensor(arg, dtype=tf.float32)
    return tf.matmul(arg, arg) + arg

# The following calls are equivalent.
value_1 = my_func(tf.constant([[1.0, 2.0], [3.0, 4.0]]))
value_2 = my_func([[1.0, 2.0], [3.0, 4.0]])
value_3 = my_func(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32))

所以,回到您的问题 - Tensorflow 不知道您打算使用 tf.float32,它恰好是 convert_to_tensor<默认选择的数据类型/代码> 功能.因此,返回的 Tensor 具有您期望的数据类型纯属巧合.如果例如您调用 tf.Variable([.3], tf.float64) 结果张量具有与调用 tf.Variable([.3] 时相同的 dtype, tf.float32).

So, coming back to your question - Tensorflow does not know that you intended to use tf.float32, it just happened to be the datatype that was chosen by default by the convert_to_tensor function. Hence, it was mere coincidence that the returned Tensor had the datatype that you expected. If e.g. you call tf.Variable([.3], tf.float64) the resulting Tensor has the same dtype as when calling tf.Variable([.3], tf.float32).

事实上,我相信两者都调用了 tf.Variable([.3], tf.float32)tf.Variable([.3], tf.float64)code> 是等价的,因为 tf.Variable 的第二个参数是一个布尔值,因此 tf.floatX 被转换为一个总是返回 True 的布尔值代码>.

In fact, I believe that both calls tf.Variable([.3], tf.float32) and tf.Variable([.3], tf.float64) are equivalent, as the second argument to tf.Variable is a boolean and thus tf.floatX is being converted to a boolean which always returns True.

这篇关于为什么“tf.Variable([.3], tf.float32)"在张量流中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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