我只是想让一个简单的RNNCell工作。这个简单的代码:
with tf.Session() as sess:
x = tf.Variable(np.ones((2, 3)))
tf.initialize_all_variables().run()
out, state = BasicRNNCell(4)(x, x)
引发以下错误:
Traceback (most recent call last):
File "scrap.py", line 38, in <module>
g, _ = tf.nn.rnn_cell.BasicRNNCell(2)(x, x)
File "/Users/ethan/env/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell.py", line 199, in __call__
output = self._activation(_linear([inputs, state], self._num_units, True))
File "/Users/ethan/env/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell.py", line 903, in _linear
"Matrix", [total_arg_size, output_size], dtype=dtype)
File "/Users/ethan/env/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1022, in get_variable
custom_getter=custom_getter)
File "/Users/ethan/env/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 849, in get_variable
custom_getter=custom_getter)
File "/Users/ethan/env/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 345, in get_variable
validate_shape=validate_shape)
File "/Users/ethan/env/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 330, in _true_getter
caching_device=caching_device, validate_shape=validate_shape)
File "/Users/ethan/env/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 676, in _get_single_variable
validate_shape=validate_shape)
File "/Users/ethan/env/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 215, in __init__
dtype=dtype)
File "/Users/ethan/env/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 288, in _init_from_args
initial_value(), name="initial_value", dtype=dtype)
File "/Users/ethan/env/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 666, in <lambda>
shape.as_list(), dtype=dtype, partition_info=partition_info)
File "/Users/ethan/env/lib/python2.7/site-packages/tensorflow/python/ops/init_ops.py", line 280, in _initializer
dtype, seed=seed)
File "/Users/ethan/env/lib/python2.7/site-packages/tensorflow/python/ops/random_ops.py", line 232, in random_uniform
minval = ops.convert_to_tensor(minval, dtype=dtype, name="min")
File "/Users/ethan/env/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 671, in convert_to_tensor
dtype.name, ret.dtype.name))
RuntimeError: min: Conversion function <function _constant_tensor_conversion_function at 0x112053c08> for type <type 'object'> returned incompatible dtype: requested = float64_ref, actual = float64
这是pip show tensorflow
:
元数据 - 版本:2.0 名称:tensorflow 版本:0.11.0rc1 简介:TensorFlow有助于张量流动 主页:http://tensorflow.org/ 作者:谷歌公司 作者电子邮件:opensource@google.com 安装人员:pip 许可证:Apache 2.0 位置:/Users/ethan/env/lib/python2.7/site-packages 需要:模拟,protobuf,numpy,wheel,six 分类: 发展现状:: 4 - Beta 目标受众::开发人员 目标受众::教育 目标受众::科学/研究 许可证:: OSI批准:: Apache软件许可证 编程语言:: Python :: 2.7 主题::科学/工程::数学 主题::软件开发::库:: Python模块 主题::软件开发::库 切入点: [console_scripts] tensorboard = tensorflow.tensorboard.tensorboard:main
谢谢!
答案 0 :(得分:2)
我认为这是Tensorflow中的一个错误,我邀请您open an issue on GitHub。
要解决此问题,您可以使用tf.identity
创建float64_ref
而不是float64
x
,并将此值作为inputs
参数传递。
import tensorflow as tf
import numpy as np
with tf.Session() as sess:
x = tf.Variable(np.ones((2, 3)))
sess.run(tf.initialize_all_variables())
out, state = tf.nn.rnn_cell.BasicRNNCell(4)(tf.identity(x), x)
答案 1 :(得分:0)
在TensorFlow中,类Tensor的dtype为float64_ref,类Variable的dtype为float64。并且BasicRNNCell需要除变量之外的Tensor作为输入。
在你的代码中,
x = tf.Variable(...)
应改为
x = tf.convert_to_tensor(...)