- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的 X_train 和 Y_train 数据:
x_train = [
array([ 6, 1, 9, 10, 7, 7, 1, 9, 10, 3, 10, 1, 4]),
array([ 2, 8, 8, 1, 1, 4, 2, 5, 1, 2, 7, 2, 1, 1, 4, 5, 10, 4])
]
y_train = [23, 17]
数组是 numpy 数组。我现在正尝试使用 tf.data.Dataset
类将它们加载为张量。在我使用以下代码成功完成类似操作之前:
dataset = data.Dataset.from_tensor_slices((x_train, y_train))
由于此输入被送入 RNN,我在第一个 RNN 层中使用了 expand_dims 方法(expand_dimension 作为函数传递以克服 tensorflow 中的一个明显错误:参见 https://github.com/keras-team/keras/issues/5298#issuecomment-281914537):
def expand_dimension(x):
from tensorflow import expand_dims
return expand_dims(x, axis=-1)
model = models.Sequential(
[
layers.Lambda(expand_dimension,
input_shape=[None]),
layers.LSTM(units=64, activation='tanh'),
layers.Dense(units=1)
]
)
这很有效,因为我有等长的数组。在我发布的示例中,第一个数组有 13 个数字,第二个数组有 18 个。在这种情况下,上面的方法不起作用,推荐的方法似乎是使用 tf.data.Dataset.from_generator
。阅读此 How to use the Tensorflow Dataset Pipeline for Variable Length Inputs? , 接受的解决方案显示类似以下的内容可行(为简单起见,我在这里不关心 y_train
):
dataset = tf.data.Dataset.from_generator(lambda: x_train,
tf.as_dtype(x_train[0].dtype),
tf.TensorShape([None, ]))
但是,自此答案以来,tensorflow 中的语法发生了变化,现在它需要使用 output_signature
参数(参见 https://www.tensorflow.org/api_docs/python/tf/data/Dataset#from_generator)。
我尝试了不同的方法,但我发现很难从 tensorflow 文档中理解 output_signature
在我的情况下究竟应该是什么。任何帮助将不胜感激。
最佳答案
简短的回答是,您可以按如下方式定义 output_signature
。
import tensorflow as tf
import numpy as np
x_train = [
np.array([ 6, 1, 9, 10, 7, 7, 1, 9, 10, 3, 10, 1, 4]),
np.array([ 2, 8, 8, 1, 1, 4, 2, 5, 1, 2, 7, 2, 1, 1, 4, 5, 10, 4])
]
y_train = [23, 17]
dataset = tf.data.Dataset.from_generator(
lambda: x_train,
output_signature=tf.TensorSpec(
[None, ],
dtype=tf.as_dtype(x_train[0].dtype)
)
)
我还将扩展和改进您在这里所做的一些事情,以改进您的管道。
dataset = tf.data.Dataset.from_generator(
lambda: zip(x_train, y_train),
output_signature=(
tf.TensorSpec([None, ], dtype=tf.as_dtype(x_train[0].dtype)),
tf.TensorSpec([], dtype=tf.as_dtype(y_train.dtype))
)
)
for x in dataset:
print(x)
哪个会输出,
(<tf.Tensor: shape=(13,), dtype=int64, numpy=array([ 6, 1, 9, 10, 7, 7, 1, 9, 10, 3, 10, 1, 4])>, <tf.Tensor: shape=(), dtype=int64, numpy=23>)
(<tf.Tensor: shape=(18,), dtype=int64, numpy=
array([ 2, 8, 8, 1, 1, 4, 2, 5, 1, 2, 7, 2, 1, 1, 4, 5, 10,
4])>, <tf.Tensor: shape=(), dtype=int64, numpy=17>)
警告:如果您尝试 tf.data.Dataset.batch()
项目,这可能会稍微复杂一些。那么你需要使用 RaggedTensorSpec
而不是 TensorSpec
。此外,我还没有对将参差不齐的张量输入 RNN 进行过太多实验。但我认为这些超出了您提出的问题的范围。
关于python - tensorflow : how to feed a variable-time-step input to a RNN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73165980/
在我用 OpenCascade 生成的 STEP 文件中,我可以看到: #58 = VERTEX_POINT('',#59); #59 = CARTESIAN_POINT('',(-0.5,-0.5,
这个问题在这里已经有了答案: How to use Firebug's debugger functions? (1 个回答) 关闭 8 年前。 我是 FireBug 调试器的新手,谁能告诉我什么是
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我想知道 Eclipse IDE 中是否存在任何功能,可以在执行 selenium 脚本 时“从步骤进行调试” “单步执行”、“单步执行”。 “QTP” 和大多数专有功能自动化工具中都提供此功能。我假
我正在寻找 .stp/.step 文件的某种规范、文档、解释等。 更多的是关于每行包含哪些信息而不是一般信息。 我似乎无法自己弄清楚每个值的含义。 有谁知道一些关于 STEP 文件的好读物? 我已经搜
我写了下面的代码: Mockup TOPdesk
我正在尝试研究 java String 类中的方法如何工作,因此我创建了一些调用该 String 类方法的自定义代码。 如您所见,我在自己的代码中设置了一个断点,并且在 java String 类源代
我有某些功能,需要我进行一个 api 调用来检索第二个 api 调用中使用的某个 id,逻辑上它看起来像这样: componentWillMount () { this.props.functio
在本地使用 AppEngine(即使用 dev_appserver.py)时,是否可以进行逐步调试?使用 logging.info() 或类似的函数来显示代码中所有变量的值并确定错误的位置是一种过时的
我正在使用 JQuery Steps,它非常好。但事实证明有些事情很棘手。 假设我在第 2 步。根据我在表单中选择的内容,我可能想在单击“下一步”时直接跳到第 4 步,并且进一步禁用第 3 步 anc
我已经包含了 jquery-steps 插件。 如何更改按钮文本? 现在它说“完成”我想把它改成“去” 谢谢 最佳答案 查看以下内容 link .您可以在初始化时更改所有标签。 var setting
在(现已弃用的) Angular 场景测试运行器中,有一个选项可以创建一个 runner.html 页面,该页面将在 iFrame 中运行测试,同时在主页中逐步报告进度。 有没有办法获得类似的 Pro
我有使用标签 的 primefaces 步骤像下面这样: 结果是这样的: 我可以点击步骤 1,但不能点击步骤 3 和 4。如何为所有步骤启用点击? 最佳答案 哇,
给定一个使用分区的 Spring Batch 作业,是否可以有多个分区步骤? 例如: 在上面的示例中,是否可以将另一个分区
假设您要从数据库中获取一条记录,该记录返回大量数据并且需要多次连接。 所以我的问题是,使用单个查询来检查数据是否存在并在存在时获取结果是否更好?或者做一个更简单的查询,检查数据是否存在然后id记录是否
我如何创建一个具有整数输入类型而不是字符串的任务,例如我想启动一个状态机: "SomeStage": { "Comment": "Getting busy
我还没有看到有人问过这个问题——尽管我确实阅读了大约 100 个关于类似主题的 jQuery 步骤——似乎没有一个能解决我的问题。 我正在使用 jQuery-steps 并希望在第一步完成后添加一个“
我想知道是否可以在 Laravel 5.3 中逐个迁移。 我无法使 --step 选项正常工作,文档仅提及此选项与 rollback 或 refresh 一起使用命令。 // this works p
我已按照 AWS 文档中的步骤在本地设置和运行 AWS Step Functions:https://docs.aws.amazon.com/step-functions/latest/dg/sfn-
我已按照 AWS 文档中的步骤在本地设置和运行 AWS Step Functions:https://docs.aws.amazon.com/step-functions/latest/dg/sfn-
我是一名优秀的程序员,十分优秀!