我希望我的网络在训练时即时计算加权标准偏差(或方差)。
权重必须来自常数向量,例如:
PTRACE_CONT
输入的大小与weights = np.array([0.1,0.4,0.5,0.6,0.1,0.3,0.6,0.9])
相同。
我如何在Keras中做到这一点?
我已经得出了均值的公式
weights
但是即使崩溃也会导致错误:
weights = K.variable(weights)
width = dot([in, weights],axes=-1, normalize=False)
编辑:
我弄错了,我想用输入作为权重来计算常数向量的方差。
我遵循@Sharky的建议:
File "/sps/atlas/a/aghosh/miniconda3/envs/cpuApril19/lib/python2.7/site-packages/keras/layers/merge.py", line 668, in dot
return Dot(axes=axes, normalize=normalize, **kwargs)(inputs)
File "~/envs/cpuApril19/lib/python2.7/site-packages/keras/engine/base_layer.py", line 474, in __call__
output_shape = self.compute_output_shape(input_shape)
File "~/envs/cpuApril19/lib/python2.7/site-packages/keras/layers/merge.py", line 512, in compute_output_shape
shape2.pop(0)
IndexError: pop from empty list
它给出此错误: AttributeError:“ NoneType”对象没有属性“ _inbound_nodes”
如果将张量切换为:
constVector = np.array([-0.1,-0.4,-0.5,0.6,0.1,0.3,0.6,0.9])
....
in = Input(shape=(8,), name='Input')
width = Lambda(lambda x: tf.nn.weighted_moments(x,axes=-1,frequency_weights=in)[1])(constVector)
Model = Model(inputs=[in], outputs= width)
它可以编译,但是我需要对tf.nn.weighted_moments(x,axes=-1,frequency_weights=constVector)[1])(in )
加权的constVector
进行变异
Edit2: 只需正确实施Lamda层
in
答案 0 :(得分:0)
在@Sharky的评论之后,我将TensorFlow的weighted_moments()
函数包装在Keras中:
constVector = np.array([-0.1,-0.4,-0.5,0.6,0.1,0.3,0.6,0.9])
constVector = K.variable(constVector)
def wWidthFunc(x, y=constVector):
# only works for 1-D vector inputs
z = tf.nn.weighted_moments(y,axes=[1],frequency_weights=x)[1]
z = K.expand_dims(z, -1) # z of shape (batch_size, 1)
return z
def make_width_model():
m_input = Input(shape=(8,), name='Input_')
width = Lambda(wWidthFunc)(m_input) # m_input values must be >0
M_w = Model(inputs=[m_input], outputs= width)
return M_w