错误:参数必须是密集张量:范围(2,3) - 得到形状[1],但想要[]

时间:2017-05-24 21:25:21

标签: python tensorflow som

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

  

错误:参数必须是密集张量:范围(2,3) - 得到形状1,   但想要[]

我认为相关的代码是:

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(最近一次调用最后一次):文件   " C:\ Users \用户的Jakub \应用程序数据\本地\程序\的Python \ Python35 \ lib中\站点包\ tensorflow \蟒\框架\ op_def_library.py&#34 ;,   第491行,在apply_op中       preferred_dtype = default_dtype)文件" C:\ Users \ jakub \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ framework \ ops.py",   第704行,在internal_convert_to_tensor中       ret = conversion_func(value,dtype = dtype,name = name,as_ref = as_ref)文件   " C:\ Users \用户的Jakub \应用程序数据\本地\程序\的Python \ Python35 \ lib中\站点包\ tensorflow \蟒\框架\ constant_op.py&#34 ;,   第113行,在_constant_tensor_conversion_function中       返回常量(v,dtype = dtype,name = name)文件" C:\ Users \ jakub \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ framework \ constant_op.py& #34 ;,   第102行,不变       tensor_util.make_tensor_proto(value,dtype = dtype,shape = shape,verify_shape = verify_shape))文件   " C:\ Users \用户的Jakub \应用程序数据\本地\程序\的Python \ Python35 \ lib中\站点包\ tensorflow \蟒\框架\ tensor_util.py&#34 ;,   第379行,在make_tensor_proto中       _GetDenseDimensions(values)))ValueError:参数必须是密集张量:范围(2,3) - 得到形状1,但想要[]。

     

在处理上述异常期间,发生了另一个异常:

     

Traceback(最近一次调用最后一次):文件   " C:\ Users \用户的Jakub \应用程序数据\本地\程序\的Python \ Python35 \ lib中\站点包\ tensorflow \蟒\框架\ op_def_library.py&#34 ;,   第505行,在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&# 34 ;,   第704行,在internal_convert_to_tensor中       ret = conversion_func(value,dtype = dtype,name = name,as_ref = as_ref)文件   " C:\ Users \用户的Jakub \应用程序数据\本地\程序\的Python \ Python35 \ lib中\站点包\ tensorflow \蟒\框架\ constant_op.py&#34 ;,   第113行,在_constant_tensor_conversion_function中       返回常量(v,dtype = dtype,name = name)文件" C:\ Users \ jakub \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ framework \ constant_op.py& #34 ;,   第102行,不变       tensor_util.make_tensor_proto(value,dtype = dtype,shape = shape,verify_shape = verify_shape))文件   " C:\ Users \用户的Jakub \应用程序数据\本地\程序\的Python \ Python35 \ lib中\站点包\ tensorflow \蟒\框架\ tensor_util.py&#34 ;,   第379行,在make_tensor_proto中       _GetDenseDimensions(values)))ValueError:参数必须是密集张量:范围(2,3) - 得到形状1,但想要[]。

     

在处理上述异常期间,发生了另一个异常:

     

Traceback(最近一次调用最后一次):文件   " C:\ Users \ jakub \ OneDrive \ UTP \ SztucznaInteligencja \ SOM \ SOM2.py",line   148,在       s = SOM((3,),30,num_training,sess)文件" C:\ Users \ jakub \ OneDrive \ UTP \ SztucznaInteligencja \ SOM \ SOM2.py",line   51,在 init 中       self.initialize_graph()文件" C:\ Users \ jakub \ OneDrive \ UTP \ SztucznaInteligencja \ SOM \ SOM2.py",line   76,在initialize_graph中       self.diff_sum = tf.reduce_sum(self.diff_sq,axis = range(2,2 + len(self.input_shape)))文件   " C:\ Users \用户的Jakub \应用程序数据\本地\程序\的Python \ Python35 \ lib中\站点包\ tensorflow \蟒\ OPS \ math_ops.py&#34 ;,   第1236行,在reduce_sum中       name = name)文件" C:\ Users \ jakub \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ ops \ gen_math_ops.py",   第2656行,在_sum中       keep_dims = keep_dims,name = name)文件" C:\ Users \ jakub \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ framework \ op_def_library.py",   第509行,在apply_op中       (input_name,err))ValueError:尝试转换' reduction_indices'到了张量并失败了。错误:参数必须是a   密集张量:范围(2,3) - 形状1,但想要[]。

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

1 个答案:

答案 0 :(得分:1)

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

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

相关问题