问题标签 [tensorflow2.0]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
tensorflow - tensorflow 2.0, variable_scope(), TypeError: __call__() got an unexpected keyword argument 'partition_info'
我已经使用 将 CNN 模型从 tf1.x 转换为 tf2.0 tf_upgrade_v2
,但是当我使用这个转换后的模型时,我得到了一个错误:
File "/home/hsw/virtual_env/tf2.0/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 2492, in default_variable_creator
import_scope=import_scope, distribute_strategy=distribute_strategy)
File "/home/hsw/virtual_env/tf2.0/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 216, in __call__
return super(VariableMetaclass, cls).__call__(*args, **kwargs)
File "/home/hsw/virtual_env/tf2.0/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 422, in __init__
constraint=constraint)
File "/home/hsw/virtual_env/tf2.0/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 545, in _init_from_args
initial_value() if init_from_fn else initial_value,
File "/home/hsw/virtual_env/tf2.0/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 886, in <lambda>
shape.as_list(), dtype=dtype, partition_info=partition_info)
TypeError: __call__() got an unexpected keyword argument 'partition_info'
似乎有问题variables.py
,转换后的模型如下:
应该怎么做才能解决这个问题?
python - Tensorflow 2.0 - AttributeError:模块'tensorflow'没有属性'Session'
当我sess = tf.Session()
在 Tensorflow 2.0 环境中执行命令时,我收到如下错误消息:
系统信息:
- 操作系统平台和发行版:Windows 10
- Python版本:3.7.1
- Tensorflow 版本:2.0.0-alpha0(用 pip 安装)
重现步骤:
安装:
- 点安装——升级点
- pip install tensorflow==2.0.0-alpha0
- 点安装 keras
- pip install numpy==1.16.2
执行:
- 执行命令:import tensorflow as tf
- 执行命令:sess = tf.Session()
tensorflow - 张量转换函数 numpy() 在 tf.estimator 模型函数中不起作用
我已经在 tensorflow v2.0 和 v1.12.0(带有tf.enable_eager_execution()
)上尝试过这个。显然,如果我numpy()
在函数中使用下面显示的代码片段进行main()
调用,它会完美运行。但是,如果我在我的估计器模型函数中使用它,model_fn(features, labels, mode, params)
那么它会抱怨'Tensor' object has no attribute 'numpy'
.
有没有其他人遇到过类似的问题?对于 tf.estimator 来说,这似乎是个大问题?
python - TensorFlow 2.0:你需要在每个函数之上添加一个 @tf.function 装饰器吗?
在 TensorFlow 2.0(现在仍然是 alpha 版本)中,我知道您可以使用装饰器@tf.function
将纯 Python 代码转换为图形。@tf.function
每次我想要的时候,我必须把每个功能放在上面吗?并且只@tf.function
考虑以下功能块?
image - TensorFlow 2.0 和图像处理
我只是将一些代码转换为 tensorflow v2,但我遇到了一个问题。我曾经在 tensorflow v1 中使用过一些 tf.contrib.image 函数(dense_image_warp 和 rotate)。我不知道如何在 v2 中替换它们。这些函数不是 tf.image 模块的一部分。
tf_upgrade_v2 脚本无法转换这些方法。
有人知道如何替换它们以兼容 v2 吗?
谢谢
python - 如何在 TensorFlow 2.0 中控制详细程度
在 TensorFlow 1.x 中,我可以自由选择在训练期间如何以及何时打印准确率/损失分数。例如,如果我想每 100 个 epoch 打印一次训练损失,tf.Session()
我会写:
在 TF 2.0 (alpha) 发布后,Keras API 似乎强制坚持其标准输出。有没有办法恢复这种灵活性?
python-3.x - valueerror 尝试将值(类型模型)转换为张量
我正在尝试使用 tensorflow 2 训练模型。
我收到错误:
当我尝试打电话时
return loss_object(y_true=y, y_pred=ypred)
函数内部loss
。
的类型ypred
是
<class'tensorflow.python.keras.engine.training.Model'>
它应该是一个张量。
如果我不使用该类并改为运行:
它运行正常!
的类型model(x)
是<class 'tensorflow.python.framework.ops.EagerTensor'>
但在类代码中,类型self.build_model(X)
是model
.
python - Tensorflow 2.0 Keras 的训练速度比 2.0 Estimator 慢 4 倍
我们最近切换到 TF 2.0 的 Keras,但是当我们将它与 2.0 上的 DNNClassifier Estimator 进行比较时,我们发现 Keras 的速度慢了大约 4 倍。但我终其一生都无法弄清楚为什么会发生这种情况。两者的其余代码是相同的,使用返回相同 tf.data.Dataset 的 input_fn(),并使用相同的 feature_columns。几天来一直在努力解决这个问题。任何帮助将不胜感激。谢谢
估算器代码:
凯拉斯代码:
更新:为了进一步测试,我编写了一个自定义子类模型(请参阅:专家入门),它的运行速度比 Keras 快,但比 Estimators 慢。如果 Estimator 在 100 秒内训练,自定义模型大约需要 ~180 秒,而 Keras 大约需要 ~350 秒。一个有趣的注意事项是,使用 Adam() 的 Estimator 运行速度比 Adagrad() 慢,而 Keras 似乎运行得更快。使用 Adam(),Keras 花费的时间不到 DNNClassifier 的两倍。假设我没有弄乱自定义代码,我开始认为 DNNClassifier 只是有很多后端优化/效率,使其比 Keras 运行得更快。
自定义代码:
更新:它可能似乎是数据集。当我在 train_input_fn() 中打印一行数据集时,在估计器中,它会打印出非急切的张量定义。在 Keras 中,它会打印出急切的值。通过 Keras 后端代码,当它接收到一个 tf.data.dataset 作为输入时,它会急切地(而且只是急切地)处理它,这就是我在 train_input_fn() 上使用 tf.function 时它崩溃的原因。基本上,我的猜测是 DNNClassifier 的训练速度比 Keras 快,因为它在图形模式下运行更多的数据集代码。将发布任何更新/发现。
tensorflow - 如何在 TF 2.0 数据集映射步骤中从张量中获取和使用值?
我正在使用 TensorFlow Alpha 2.0。
我有我正在读取的 TFRecords 文件,每个文件都包含一个短视频剪辑,每个帧都编码为 jpeg 字节字符串以节省空间:
我的 tf.data.Dataset 管道中有一个映射步骤,可以成功解析每个示例:
我的下一步是从 from 中读取帧数并在每个帧上numframes
运行 tf.io.decode_jpeg 函数:frames.values[i]
i
range(numframes)
我的数据集管道完整性:
如果我排除该dataset.map(parse_jpegs)
行,一切正常,向我展示类似{'frames': <tensorflow.python.framework.sparse_tensor.SparseTensor at 0x7f394c285518>, 'numframes': <tf.Tensor: id=2937, shape=(), dtype=int64, numpy=25>}
(请注意,numframes 张量包含 25 的 numpy 值。我可以使用 tensor.numpy() 方法在我的数据集管道之外获取它)
但是,在该映射函数中,我无法调用 .numpy() 从张量中获取值,并且在打印张量本身时,它尚未被评估或其他什么,因为还没有显示任何值。
在数据集管道中解析所有这些帧的最佳方法是什么?
TypeError: 'Tensor' object cannot be interpreted as an integer
编辑:尝试获取 numframes 时,我收到的错误消息位于 parse_jpegs 中。这对我来说很有意义,为什么不能将张量解释为 int,但是如何从该张量中获取值以用于设置范围?
我遇到的问题归结为每个“帧”对象具有不同数量的帧。如果我可以应用tf.io.decode_jpeg
到该列表中的每个帧而不需要单独记录帧数我会很好,但我在这里有“numframes”,所以我知道我的“frames”列表中需要解码多少帧。
编辑:我会向其他可能觉得有帮助的人提出这个问题,但我最终只是返回原始字节串并在数据集 API 之外的单独生成器函数中执行 decode_jpeg。这样会容易得多,即使它可能会更慢。
python-3.x - 使用 tensorflow 2 api 的简单模型会产生巨大的损失和 acc 值
我正在使用简单的数据和简单的模型。
我收到的结果是:
我无法找出问题所在。
我尝试了其他设置(更多单位、不同指标、损失),但结果是一样的。