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))
。
如果要实现我们期望的判断,有三种主要的写法:
if X is None
;if not X
;
当X为None, False, 空字符串”“, 0, 空列表[], 空字典{}, 空元组()这些时,not X为真,即无法分辨出他们之间的不同。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)
那么模型经过build
、compile
后,运行,能够得到相应的结果。但如果想更改这个mask
,直接使用在外部对这个属性进行修改:
myLayer = myDense()myLayer.mask = bar
再次执行上面的相乘,发现结果还是没有更改之前的,证明修改没有作用。但如果输出此时对象的mask
属性,其实的确是修改之后的值。
是否与build操作有关呢?
为了验证做了一些实验:
- 重新执行
model.compile
无效 - 将
myLayer
中的self.built
置为False
,重新model.compile()
无效 - 将
model
中的self.built
置为False
,重新model.compile()
无效 - 清除目前所构建的图,重新定义网络,重新
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)
这个对于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重组。(好像之前博客里也说过)
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
- keras tips&problems
- Tips for solving CSS problems in IE7
- apache and php common tips and problems
- STL vector usage tips and problems
- keras中的一些小tips(一)
- Problems
- Problems
- Problems
- keras
- keras
- keras
- Keras
- keras
- Keras
- keras
- Shiny tips & tricks for improving your apps and solving common problems
- Tips
- Tips
- HashSet HashMap Hashtable的区别
- SRS + ffmpeg 推即时视频流
- 人工神经网络
- Visual Studio 2012 控制台应用程序,打开记事本,打开网址的方法 02
- minimum-depth-of-binary-tree
- keras tips&problems
- Feign:web service client(译)
- 使用list和tuple
- bzoj5027 数学题(扩欧)
- point 问题
- MyEclipse的silksvn插件如何清除本地已有的svn账号
- 第3步:打包flaskr
- 大白菜的java开发学习之旅---c3p0和dbcp的配置文件
- Filter(第一节)