- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下功能:
import random
lst = []
for i in range(100):
lst.append(random.randint(1, 10))
print(lst)
buffer = []
# This is the peace of code which I am interested to convert into tensorflow.
for a in lst:
buffer.append(a)
if len(buffer) > 5:
buffer.pop(0)
if len(buffer) == 5:
print(buffer)
因此,从代码中,我需要创建一个缓冲区(可以是 tensorflow 中的变量)。该缓冲区应保存从最后一个转换层
提取的特征。在我的例子中,变量
将是RNN
的输入。
这种方法的优点是,当我们有大图像时,并且当我们需要向 RNN 提供(图像批处理)*(序列长度)*(1 个图像的大小)
,这将需要将大量图像加载到主内存中。另一方面,根据上面的代码,我们将使用 tensorflow 中的数据集、输入队列或任何其他替代方案一次输入一张图像。因此,我们将在内存中存储大小为:batch_size *equence_length *feature space
的特征。此外,我们可以说:
if len(buffer) == n:
# empty out the buffer after using its elements
buffer = [] # Or any other alternative way
我知道我可以向我的网络批量
提供图像,但我需要根据一些文献来完成上述代码。
非常感谢任何帮助!!
最佳答案
我尝试使用tf.FIFOQueue ( https://www.tensorflow.org/api_docs/python/tf/FIFOQueue ) 重新生成您的输出。我在下面给出了我的代码,并在必要时添加了注释。
BATCH_SIZE = 20
lst = []
for i in range(BATCH_SIZE):
lst.append(random.randint(1, 10))
print(lst)
curr_data = np.reshape(lst, (BATCH_SIZE, 1)) # reshape the tensor so that [BATCH_SIZE 1]
# queue starts here
queue_input_data = tf.placeholder(tf.int32, shape=[1]) # Placeholder for feed the data
queue = tf.FIFOQueue(capacity=50, dtypes=[tf.int32], shapes=[1]) # Queue define here
enqueue_op = queue.enqueue([queue_input_data]) # enqueue operation
len_op = queue.size() # chek the queue size
#check the length of the queue and dequeue one if greater than 5
dequeue_one = tf.cond(tf.greater(len_op, 5), lambda: queue.dequeue(), lambda: 0)
#check the length of the queue and dequeue five elemts if equals to 5
dequeue_many = tf.cond(tf.equal(len_op, 5), lambda:queue.dequeue_many(5), lambda: 0)
with tf.Session() as session:
for i in range(BATCH_SIZE):
_ = session.run(enqueue_op, feed_dict={queue_input_data: curr_data[i]}) # enqueue one element each ietaration
len = session.run(len_op) # check the legth of the queue
print(len)
element = session.run(dequeue_one) # dequeue the first element
print(element)
但是,以下两个问题与上述代码相关,
只有使一个出列和使多个出列操作可用,并且您看不到队列内的元素(我认为您不需要这个,因为您正在寻找类似于管道的东西。
我认为tf.cond是实现条件操作的唯一方法(我找不到任何其他类似的合适函数)。然而,由于它类似于 if-then-else 语句,因此当语句为 false 时也必须定义一个操作(而不仅仅是只有 if 语句 而没有 <强>其他)。由于 Tensorflow 就是构建一个图,我认为有必要包含两个分支(当条件为 true 和 false 时)。
此外,可以在此处找到有关 Tensorflow 输入管道的详细解释 ( http://ischlag.github.io/2016/11/07/tensorflow-input-pipeline-for-large-datasets/ )。
希望这有帮助。
关于for-loop - tensorflow 中以下代码的等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47276087/
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
用作 mergetool for Git 时,vimdiff 中与 kdiff3 的“从 A/B/C 中选择行”等效的是什么? kdiff3 中是否有类似 Ctrl+1/2/3 的快捷方式? 最佳答案
什么是 Javascript 等同于 imgsrc = $("#content div form img").attr('src'); HTML 是
您好,我有一个数据库可以从中选择 IP 位置> 脚本是在 php 中,我正在将它转换为 java,但我不知道什么是 ip2long('127.0.0.1' )); 在 java 中的等价物 最佳答案
我有一个 C# 应用程序,我正试图将其转换为 Java。 C# 应用程序有几个类型为 ushort 的变量。 Java 中是否有等效项? 谢谢 最佳答案 在大小方面最接近的等价物是 char,因为 J
我正在 iOS 中寻找与 .NET 中的脉冲和等待模式相同的多线程模式。本质上,我希望后台线程处于休眠状态,直到设置标志为止,这实际上是将线程“踢”到行动中。 它是 loop+thread.sle
对于某些并发编程,我可以使用 Java 的 CountDownLatch概念。是否有 C++11 的等效项,或者该概念在 C++ 中称为什么? 我想要的是在计数达到零时调用一个函数。 如果还没有,我会
我正在用 Ruby 开发一个 CLI 应用程序,我想允许通过 /etc/appnamerc 的标准配置文件级联在 Unix 中进行配置。 , ~/.appnamerc .但是,该应用程序也意味着在 W
是否有与 JAXB 等效的 PHP?它被证明对 Java 开发非常有用,作为一个 PHP 新手,我想在 PHP 世界中使用 JAXB 提供的相同概念。 最佳答案 我之前也想找同样的东西,但是找不到。所
Python 有一个 urljoin 函数,它接受两个 URL 并智能地连接它们。有没有在c++中提供类似功能的库? urljoin 文档:http://docs.python.org/library
我有一个从另一种语言移植的功能,你能帮我把它变成“pythonic”吗? 这里的函数以“非pythonic”方式移植(这是一个有点人为的例子 - 每个任务都与一个项目相关联或“无”,我们需要一个不同项
我有 2 个相同类型的对象,我想将一种状态浅复制到另一种状态。在 C++ 中,我有很棒的 memcpy。我怎样才能在 C# 中做到这一点? MemberwiseClone() 不够好,因为它创建并返回
有什么方法可以在 CSS 中使用条件语句吗? 最佳答案 我想说 CSS 中最接近“IF”的是媒体查询,例如可用于响应式设计的媒体查询。对于媒体查询,您是在说“如果屏幕宽度在 440 像素到 660 像
我正在尝试在 Swift 的 iTunesU 中从“为 iphone 和 ipad 开发 ios7 应用程序”中复制 Stanford Matchismo 游戏。 第三讲77页slides ,它显示使
这个问题在这里已经有了答案: Store output of subprocess.Popen call in a string [duplicate] (15 个回答) 关闭4年前。 我想从 pyt
这个问题在这里已经有了答案: Is there a 'foreach' function in Python 3? (14 个回答) 关闭1年前。 我正在深入研究 Python,但我有一个关于 for
我想从 Java 中的这个 Kotlin 类访问信息。它是通过 Gradle 库导入的。 密封类: public sealed class Resource private constructor()
SWT 中的 JPanel 有什么等价物? 最佳答案 原始问题要求 SWT 等同于 JLabel。 还有一个 org.eclipse.swt.custom.CLabel . SWT 等价于 JPane
在诸如 postgres 之类的 SQL 数据库中,我们可以创建 SCHEMA,以便我们可以将我们的表作为 schema_name.table_name 引用。 mongodb 中有模式吗?谢谢 最佳
哪个模型是“GBTRegressor”Pyspark 模型的 Python 等效模型? 简要背景:我正在尝试将 pyspark 模型重新创建为 python 模型。现有管道中使用的模型是 GBTReg
我是一名优秀的程序员,十分优秀!