keras tips&problems

来源:互联网 发布:ant java fork 编辑:程序博客网 时间:2024/05/16 08:03

写了一下keras的层,出现了一些问题,值得总结一下~

Python中对变量是否为None的判断


这个问题出在以下代码段:

self.mask = np.zeros(shape)if self.mask == None:    passelse:    pass

直接运行会报错,原因在于对于numpy数组,对None的判断是对于其中元素的,而不是对于mask这个对象的。

如果比较相同的对象实例,is总是返回True 而 == 最终取决于 eq()

>>> class foo(object):    def __eq__(self, other):        return True>>> f = foo()>>> f == NoneTrue>>> f is NoneFalse>>> list1 = [1, 2, 3]>>> list2 = [1, 2, 3]>>> list1==list2True>>> list1 is list2False

另外(ob1 is ob2)等价于(id(ob1) == id(ob2))

如果要实现我们期望的判断,有三种主要的写法:

  1. if X is None;
  2. if not X
    当X为None, False, 空字符串”“, 0, 空列表[], 空字典{}, 空元组()这些时,not X为真,即无法分辨出他们之间的不同。
  3. if not X is None;

在Python中,None、空列表[]、空字典{}、空元组()、0等一系列代表空和无的对象会被转换成False。除此之外的其它对象都会被转化成True。

在命令if not 1中,1便会转换为bool类型的True。not是逻辑运算符非,not 1则恒为False。因此if语句if not 1之下的语句,永远不会执行。

stackoverflow-py-top-qa
python代码if not x:if x is not None:if not x is None:使用

模型build后无法更改属性??


这其实还是未解决的一个疑问。
mask=foo作为输入传入自定义由Dense()继承而来的类myDense(),并用self.mask作为属性保存。之后在call函数中使用:

def call(self, inputs):    output = K.dot(inputs, self.mask)

那么模型经过buildcompile后,运行,能够得到相应的结果。但如果想更改这个mask,直接使用在外部对这个属性进行修改:

myLayer = myDense()myLayer.mask = bar

再次执行上面的相乘,发现结果还是没有更改之前的,证明修改没有作用。但如果输出此时对象的mask属性,其实的确是修改之后的值。
是否与build操作有关呢?

为了验证做了一些实验:

  1. 重新执行model.compile
    无效
  2. myLayer中的self.built置为False,重新model.compile()
    无效
  3. model中的self.built置为False,重新model.compile()
    无效
  4. 清除目前所构建的图,重新定义网络,重新model.compile()
    有效

最后的方法基本就等于关掉程序重新运行了,但其他方法也确实不行,感觉就像层的属性只在第一次compile()时才被应用。个人感觉不应该这么麻烦,希望有大神指点= =

一些新的functions&tricks


一个心得:很多想法用numpy数学库很好解决,但在Tensorflow中,由于Tensor类型的限制,某些操作无法直接进行,特别是通常最简单的赋值(当然也可能是我还没有了解到更多的方法)。

这样有些操作需要搜索半天实现方法,真的挺费劲。。

非排序分割和


# unsorted_segment_sum(...): Computes the sum along segments of a tensor.tf.unsorted_segment_sum(    data, # 数据    segment_ids, # 不同类的mask    num_segments, # 分类总数    name=None)

unsorted_segment_sum

这个对于group by的reduce可是太好用了!赞美

用一个tensor作为另一个tensor的下标


在numpy中,对array的操作是否简单:

x = np.asarray([1,2,3,3,2,5,6,7,1,3])e = np.asarray([0,1,0,1,1,1,0,1])print x * e[x]

得到:

[1 0 3 3 0 5 0 7 1 3]

而在Tensorflow,则需要tf.gather的帮助:

gather(    params,    indices,    validate_indices=None,    name=None)

通过gather,将params重组。(好像之前博客里也说过)

gather

x = np.asarray([1,2,3,3,2,5,6,7,1,3])e = np.asarray([0,1,0,1,1,1,0,1])x_t = tf.constant(x)e_t = tf.constant(e)result = x_t * tf.gather(e_t, x_t)with tf.Session() as sess:    print sess.run(result)  # ==> 'array([1, 0, 3, 3, 0, 5, 0, 7, 1, 3])'

TensorFlow: using a tensor to index another tensor

相似的选值还有 tf.boolean_mask

# 1-D exampletensor = [0, 1, 2, 3]mask = np.array([True, False, True, False])boolean_mask(tensor, mask) # ==> [0, 2]

按mask对Tensor赋值


给定一个mask:inds ,mask上的值代表着sumed 数组中的下标,经过更新后得到输出:

sumed = [1.,-2.,3.]inds = [[2.,1.,0.],        [0.,1.,2.]]out = [[ 3. -2.  1.]       [ 1. -2.  3.]]

这个问题只是目前找到了暂时的解决方法,应该还有改进的地方。

sumed = np.asarray([1.,-2.,3.])inds = np.asarray([[2.,1.,0.],[0.,1.,2.]])sumed_tensor = tf.convert_to_tensor(sumed)inds_tensor = tf.convert_to_tensor(inds)with tf.Session() as sess:    with tf.name_scope('my'):        # 这个循环感觉很蠢。。        for i in range(3):            mask = tf.equal(inds_tensor, i * tf.ones_like(inds_tensor))            casted = tf.cast(mask, inds_tensor.dtype)            temp = tf.multiply(sumed[int(i)], casted)            if i == 0:                new_tensor = temp            else:                new_tensor = temp + new_tensor
原创粉丝点击