在 Tensorflow 中,我想将多维数组保存到 TFRecord。例如:
[[1, 2, 3], [1, 2], [3, 2, 1]]
由于我要解决的任务是顺序的,因此我尝试使用 Tensorflow 的 tf.train.SequenceExample() 并在写入数据时成功将数据写入 TFRecord 文件.但是,当我尝试使用 tf.parse_single_sequence_example 从 TFRecord 文件中加载数据时,我遇到了大量神秘错误:
W tensorflow/core/framework/op_kernel.cc:936] Invalid argument: Name: , Key: input_characters, Index: 1. Number of int64 values != expected. values size: 6 but output shape: []
E tensorflow/core/client/tensor_c_api.cc:485] Name: , Key: input_characters, Index: 1. Number of int64 values != expected. values size: 6 but output shape: []
我用来加载数据的函数如下:
def read_and_decode_single_example(filename):
filename_queue = tf.train.string_input_producer([filename],
num_epochs=None)
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
context_features = {
"length": tf.FixedLenFeature([], dtype=tf.int64)
}
sequence_features = {
"input_characters": tf.FixedLenSequenceFeature([], dtype=tf.int64),
"output_characters": tf.FixedLenSequenceFeature([], dtype=tf.int64)
}
context_parsed, sequence_parsed = tf.parse_single_sequence_example(
serialized=serialized_example,
context_features=context_features,
sequence_features=sequence_features
)
context = tf.contrib.learn.run_n(context_parsed, n=1, feed_dict=None)
print context
我用来保存数据的函数在这里:
# http://www.wildml.com/2016/08/rnns-in-tensorflow-a-practical-guide-and-undocumented-features/
def make_example(input_sequence, output_sequence):
"""
Makes a single example from Python lists that follows the
format of tf.train.SequenceExample.
"""
example_sequence = tf.train.SequenceExample()
# 3D length
sequence_length = sum([len(word) for word in input_sequence])
example_sequence.context.feature["length"].int64_list.value.append(sequence_length)
input_characters = example_sequence.feature_lists.feature_list["input_characters"]
output_characters = example_sequence.feature_lists.feature_list["output_characters"]
for input_character, output_character in izip_longest(input_sequence,
output_sequence):
# Extend seems to work, therefore it replaces append.
if input_sequence is not None:
input_characters.feature.add().int64_list.value.extend(input_character)
if output_characters is not None:
output_characters.feature.add().int64_list.value.extend(output_character)
return example_sequence
欢迎任何帮助。
最佳答案
我遇到了同样的问题。我认为它是完全可以解决的,但你必须决定输出格式,然后弄清楚你将如何使用它。
首先 你的错误是什么?
错误消息告诉您,您尝试读取的内容不适合您指定的特征大小。那么你在哪里指定呢?就在这里:
sequence_features = {
"input_characters": tf.FixedLenSequenceFeature([], dtype=tf.int64),
"output_characters": tf.FixedLenSequenceFeature([], dtype=tf.int64)
}
这说“我的 input_characters 是一个单值序列”,但这不是真的;你所拥有的是一系列单值序列,因此是一个错误。
第二 你能做什么?
如果您改为使用:
a = [[1,2,3], [2,3,1], [3,2,1]]
sequence_features = {
"input_characters": tf.FixedLenSequenceFeature([3], dtype=tf.int64),
"output_characters": tf.FixedLenSequenceFeature([3], dtype=tf.int64)
}
您的代码不会出错,因为您已指定顶级序列的每个元素都是 3 个元素长。
或者,如果您没有固定长度的序列,那么您将不得不使用不同类型的功能。
sequence_features = {
"input_characters": tf.VarLenFeature(tf.int64),
"output_characters": tf.VarLenFeature(tf.int64)
}
VarLenFeature 告诉它在读取之前长度是未知的。不幸的是,这意味着您的 input_characters 不能再一步被读取为密集向量。相反,它将是 SparseTensor默认。您可以使用 tf.sparse_tensor_to_dense 将其转换为密集张量例如:
input_densified = tf.sparse_tensor_to_dense(sequence_parsed['input_characters'])
如 the article 中所述您一直在查看,如果您的数据并不总是具有相同的长度,则您的词汇表中必须有一个“not_really_a_word”单词,您将其用作默认索引。例如假设您将索引 0 映射到“not_really_a_word”单词,然后使用您的
a = [[1,2,3], [2,3], [3,2,1]]
python 列表最终会成为一个
array((1,2,3), (2,3,0), (3,2,1))
张量。
被警告;我不确定反向传播对稀疏张量“是否有效”,就像它对密集张量一样。 wildml article谈论每个序列填充 0 以掩盖“not_actually_a_word”单词的损失(参见:“旁注:在他们的文章中使用 0'S 在你的词汇/类别中小心”)。这似乎表明第一种方法更容易实现。
请注意,这与此处描述的情况不同,其中每个示例都是一系列序列。据我了解,这种方法之所以没有得到很好的支持,是因为它是滥用本应支持的情况;直接加载固定大小的嵌入。
我假设您接下来要做的就是将这些数字转换为词嵌入。您可以使用 tf.nn.embedding_lookup
关于python - tf.SequenceExample 与多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39524323/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
我正在尝试在Ruby中制作一个cli应用程序,它接受一个给定的数组,然后将其显示为一个列表,我可以使用箭头键浏览它。我觉得我已经在Ruby中看到一个库已经这样做了,但我记不起它的名字了。我正在尝试对soundcloud2000中的代码进行逆向工程做类似的事情,但他的代码与SoundcloudAPI的使用紧密耦合。我知道cursesgem,我正在考虑更抽象的东西。广告有没有人见过可以做到这一点的库或一些概念证明的Ruby代码可以做到这一点? 最佳答案 我不知道这是否是您正在寻找的,但也许您可以使用我的想法。由于我没有关于您要完成的工作
我使用Ember作为我的前端和GrapeAPI来为我的API提供服务。前端发送类似:{"service"=>{"name"=>"Name","duration"=>"30","user"=>nil,"organization"=>"org","category"=>nil,"description"=>"description","disabled"=>true,"color"=>nil,"availabilities"=>[{"day"=>"Saturday","enabled"=>false,"timeSlots"=>[{"startAt"=>"09:00AM","endAt"=>
我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']