2017-05-24 32 views
1

我想在Python中運行一個代碼用於自組織映射(SOM),它使用TensorFlow。我得到了代碼here,但是當我運行它,我得到一個錯誤:錯誤:參數必須是一個密集的張量:範圍(2,3) - 形狀[1],但想要[]

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) 

The整個錯誤消息是:

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 [].

有誰知道爲什麼我會收到這個錯誤?

回答

0

我遇到同樣的問題。我改變了

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

相關問題