Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法将计数器作为输入传递到Tensorflow?_Python_Tensorflow_Keras Layer_Tf.keras_Autoencoder - Fatal编程技术网

Python 有没有办法将计数器作为输入传递到Tensorflow?

Python 有没有办法将计数器作为输入传递到Tensorflow?,python,tensorflow,keras-layer,tf.keras,autoencoder,Python,Tensorflow,Keras Layer,Tf.keras,Autoencoder,我有一个计数器字典(从),所有的键都是整数。我正在尝试写一个自动编码器,它将计数器作为输入 像tf.data.Dataset.from\u tensor\u切片(数据结构)。我目前的方法是将每个计数器通过一个手动循环,并创建一个稀疏矩阵,然而这是非常RAM和CPU密集型的。我希望有更好的解决办法。想法 注1:如果相关,我将使用Keras接口 下面是一个示例,它生成与我的数据具有相同结构的内容,然后低效地将其转换为Tensorflow可接受的数据结构: from collections impor

我有一个计数器字典(从),所有的键都是整数。我正在尝试写一个自动编码器,它将计数器作为输入

tf.data.Dataset.from\u tensor\u切片(数据结构)
。我目前的方法是将每个计数器通过一个手动循环,并创建一个稀疏矩阵,然而这是非常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%

您可以添加一些示例数据以及您尝试的预期输出和代码示例吗?@AniketBote我添加了一个示例。我不确定你所说的预期输出是什么意思,我正在尝试训练一个自动编码器。