如何解决有没有办法将计数器作为输入传递到Tensorflow?
我有一个计数器字典(来自collections,)所有键都是整数。我正在尝试编写一个将计数器作为输入的自动编码器。
像tf.data.Dataset.from_tensor_slices(datastructure)
这样的幼稚尝试失败。我当前的方法是使每个计数器通过手动循环并创建稀疏矩阵,但是这非常消耗RAM和CPU。我希望有一个更好的解决方案。想法?
注意1:如果需要的话,我正在使用Keras界面。
这里是一个示例,它产生的结构与我的数据相同,然后将其低效地转换为Tensorflow可接受的数据结构:
from collections import Counter
import numpy as np
import random
random.seed(1)
datastructure = dict()
for i in range(5):
x_key = int(random.uniform(1,200))
if x_key in datastructure:
continue
else:
datastructure[x_key] = Counter()
for j in range(3):
y_key = int(random.uniform(1,200))
if y_key not in datastructure:
datastructure[y_key] = Counter()
value = int(random.uniform(1,4))
datastructure[x_key][y_key] += value
datastructure[y_key][x_key] += value
print("Original structure:")
print(datastructure)
max_key = max(datastructure.keys())
trainable_datastructure = np.zeros_like([],shape = (max_key + 1,max_key + 1))
for i in datastructure:
for j in datastructure[i]:
trainable_datastructure[i][j] = datastructure[i][j]
trainable_datastructure[j][i] = datastructure[i][j]
print("Trainable and inefficient structure:")
print(trainable_datastructure)
为了使事情简单,我使用了较小的范围,在我的情况下,数据大小接近2000万乘以2000万,稀疏度约为99.993%
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。