I am trying to run an a code in Python for self-organizing map (SOM), which uses TensorFlow. I got the code from here, but when I run it, I get an error:
Error: Argument must be a dense tensor: range(2, 3) - got shape 1, but wanted []
I think the code relevant is:
s = SOM( (3,), 30, num_training, sess )
and then:
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])
or:
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 whole error message is:
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 [].
Does anyone know why am I getting this error?