【发布时间】:2018-10-23 06:21:01
【问题描述】:
我正在尝试让 HashMap 类型的功能与 tensorflow 一起使用。当键和值是 int 类型时,我让它工作。但是当它们是数组时,它会给出错误 - ValueError: Shapes (2,) and () are not compatible 在线 default_value)
import numpy as np
import tensorflow as tf
input_tensor = tf.constant([1, 1], dtype=tf.int64)
keys = tf.constant(np.array([[1, 1],[2, 2],[3, 3]]), dtype=tf.int64)
values = tf.constant(np.array([[4, 1],[5, 1],[6, 1]]), dtype=tf.int64)
default_value = tf.constant(np.array([1, 1]), dtype=tf.int64)
table = tf.contrib.lookup.HashTable(
tf.contrib.lookup.KeyValueTensorInitializer(keys, values),
default_value)
out = table.lookup(input_tensor)
with tf.Session() as sess:
table.init.run()
print(out.eval())
【问题讨论】:
-
default_value应该是一个标量值,而不是一个数组。 -
但我的值是数组。这有什么意义?它也给出了错误:
ValueError: Shape must be rank 1 but is rank 2 for 'key_value_init_4' (op: 'InitializeTable') with input shapes: [2], [3,2], [3,2]. -
我投票只是因为@MihkelL。居然发了MCVE,真爽! :)
标签: python numpy tensorflow