gpt4 book ai didi

python - 错误 : Argument must be a dense tensor: range(2, 3) - 得到形状 [1],但想要 []

转载 作者:太空宇宙 更新时间:2023-11-03 15:11:42 29 4
gpt4 key购买 nike

我正在尝试在 Python 中运行用于自组织映射 (SOM) 的代码,该代码使用 TensorFlow。我从here获取代码,但是当我运行它时,出现错误:

Error: Argument must be a dense tensor: range(2, 3) - got shape 1, but wanted []

我认为相关的代码是:

s = SOM( (3,), 30, num_training, sess )

然后:

class SOM:
def __init__(self, input_shape, map_size_n, num_expected_iterations, session):
input_shape = tuple([i for i in input_shape if i is not None])

或者:

def initialize_graph(self):
self.weights = tf.Variable( tf.random_uniform((self.n*self.n, )+self.input_shape, 0.0, 1.0) )

self.input_placeholder = tf.placeholder(tf.float32, (None,)+self.input_shape)
self.current_iteration = tf.placeholder(tf.float32)

## Compute the current iteration's neighborhood sigma and learning rate alpha:
self.sigma_tmp = self.sigma * tf.exp( - self.current_iteration/self.timeconst_sigma )
self.sigma2 = 2.0*tf.multiply(self.sigma_tmp, self.sigma_tmp)

self.alpha_tmp = self.alpha * tf.exp( - self.current_iteration/self.timeconst_alpha )


self.input_placeholder_ = tf.expand_dims(self.input_placeholder, 1)
self.input_placeholder_ = tf.tile(self.input_placeholder_, (1,self.n*self.n,1) )

self.diff = self.input_placeholder_ - self.weights
self.diff_sq = tf.square(self.diff)
self.diff_sum = tf.reduce_sum( self.diff_sq, axis=range(2, 2+len(self.input_shape)) )

# Get the index of the best matching unit
self.bmu_index = tf.argmin(self.diff_sum, 1)

self.bmu_dist = tf.reduce_min(self.diff_sum, 1)
self.bmu_activity = tf.exp( -self.bmu_dist/self.sigma_act )

self.diff = tf.squeeze(self.diff)

self.diff_2 = tf.placeholder(tf.float32, (self.n*self.n,)+self.input_shape)
self.dist_sliced = tf.placeholder(tf.float32, (self.n*self.n,))

self.distances = tf.exp(-self.dist_sliced / self.sigma2 )
self.lr_times_neigh = tf.multiply( self.alpha_tmp, self.distances )
for i in range(len(self.input_shape)):
self.lr_times_neigh = tf.expand_dims(self.lr_times_neigh, -1)
self.lr_times_neigh = tf.tile(self.lr_times_neigh, (1,)+self.input_shape )

self.delta_w = self.lr_times_neigh * self.diff_2

self.update_weights = tf.assign_add(self.weights, self.delta_w)

整个错误消息是:

Traceback (most recent call last): File "C:\Users\jakub\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 491, in apply_op preferred_dtype=default_dtype) File "C:\Users\jakub\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 704, in internal_convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) File "C:\Users\jakub\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\constant_op.py", line 113, in _constant_tensor_conversion_function return constant(v, dtype=dtype, name=name) File "C:\Users\jakub\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\constant_op.py", line 102, in constant tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape)) File "C:\Users\jakub\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 379, in make_tensor_proto _GetDenseDimensions(values))) ValueError: Argument must be a dense tensor: range(2, 3) - got shape 1, but wanted [].

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\jakub\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 505, in apply_op values, as_ref=input_arg.is_ref).dtype.name File "C:\Users\jakub\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 704, in internal_convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) File "C:\Users\jakub\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\constant_op.py", line 113, in _constant_tensor_conversion_function return constant(v, dtype=dtype, name=name) File "C:\Users\jakub\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\constant_op.py", line 102, in constant tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape)) File "C:\Users\jakub\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 379, in make_tensor_proto _GetDenseDimensions(values))) ValueError: Argument must be a dense tensor: range(2, 3) - got shape 1, but wanted [].

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\jakub\OneDrive\UTP\SztucznaInteligencja\SOM\SOM2.py", line 148, in s = SOM( (3,), 30, num_training, sess ) File "C:\Users\jakub\OneDrive\UTP\SztucznaInteligencja\SOM\SOM2.py", line 51, in init self.initialize_graph() File "C:\Users\jakub\OneDrive\UTP\SztucznaInteligencja\SOM\SOM2.py", line 76, in initialize_graph self.diff_sum = tf.reduce_sum( self.diff_sq, axis=range(2, 2+len(self.input_shape)) ) File "C:\Users\jakub\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1236, in reduce_sum name=name) File "C:\Users\jakub\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 2656, in _sum keep_dims=keep_dims, name=name) File "C:\Users\jakub\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 509, in apply_op (input_name, err)) ValueError: Tried to convert 'reduction_indices' to a tensor and failed. Error: Argument must be a dense tensor: range(2, 3) - got shape 1, but wanted [].

有谁知道为什么我会收到此错误?

最佳答案

我也遇到同样的问题。我改变了

self.diff_sum = tf.reduce_sum( self.diff_sq, reduction_indices=range(2,2+len(self.input_shape)) )    

self.diff_sum = tf.reduce_sum( self.diff_sq,2 )

只是尝试让它运行。这是 400 次迭代后的结果: Kohonen_SOM_Colors_reduce_sum_bug

关于python - 错误 : Argument must be a dense tensor: range(2, 3) - 得到形状 [1],但想要 [],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44168709/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com