有没有办法将计数器作为输入传递到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 (将#修改为@)