我正在尝试创建一个激活函数,以便在我的keras模型中使用。
基本上,我想要的是一个只有两位小数的sigmoid函数。所以我试图创建我自己的激活函数:
def mySigmoid(x):
return np.around(K.sigmoid(x), decimals=2)
然后:
get_custom_objects().update({'mySigmoid': Activation(mySigmoid)})
但出于某种原因,我得到错误。
有人可以帮帮我吗?
非常感谢
堆栈追踪:
AttributeError Traceback (most recent call last)
<ipython-input-52-891a9f63ca56> in <module>()
3 model.add(Dense(30, activation='softmax'))
4
----> 5 model.add(Dense(10, activation='mySigmoid'))
6 model.summary()
7 sgd = optimizers.SGD(lr=0.1, decay=1e-5, momentum=0.3, nesterov=True)
/usr/local/lib/python2.7/dist-packages/keras/models.pyc in add(self, layer)
473 output_shapes=[self.outputs[0]._keras_shape])
474 else:
--> 475 output_tensor = layer(self.outputs[0])
476 if isinstance(output_tensor, list):
477 raise TypeError('All layers in a Sequential model '
/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in __call__(self, inputs, **kwargs)
600
601 # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 602 output = self.call(inputs, **kwargs)
603 output_mask = self.compute_mask(inputs, previous_mask)
604
/usr/local/lib/python2.7/dist-packages/keras/layers/core.pyc in call(self, inputs)
843 output = K.bias_add(output, self.bias)
844 if self.activation is not None:
--> 845 output = self.activation(output)
846 return output
847
/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in __call__(self, inputs, **kwargs)
600
601 # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 602 output = self.call(inputs, **kwargs)
603 output_mask = self.compute_mask(inputs, previous_mask)
604
/usr/local/lib/python2.7/dist-packages/keras/layers/core.pyc in call(self, inputs)
284
285 def call(self, inputs):
--> 286 return self.activation(inputs)
287
288 def get_config(self):
<ipython-input-50-cc621aa5ea1b> in mySigmoid(x)
1 def mySigmoid(x):
----> 2 return np.around(K.sigmoid(x), decimals=2)
3 #return (K.sigmoid(x) * 5) - 1
/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.pyc in around(a, decimals, out)
2787
2788 """
-> 2789 return _wrapfunc(a, 'round', decimals=decimals, out=out)
2790
2791
/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.pyc in _wrapfunc(obj, method, *args, **kwds)
65 # a downstream library like 'pandas'.
66 except (AttributeError, TypeError):
---> 67 return _wrapit(obj, method, *args, **kwds)
68
69
/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.pyc in _wrapit(obj, method, *args, **kwds)
45 except AttributeError:
46 wrap = None
---> 47 result = getattr(asarray(obj), method)(*args, **kwds)
48 if wrap:
49 if not isinstance(result, mu.ndarray):
AttributeError: 'Tensor' object has no attribute 'rint'
我的任务:我正在尝试创建一个神经网络来预测住宅门窗的开启状态。
这些状态是从0到1的浮点数,最多有两位小数。
输入数据集组织为:
headerInput = ['hour', 'Temperature', 'Wind_Speed', 'Wind_Direction', 'Humidity', 'Air_Density', 'Rain_Status', 'Jardim_PMV','Jardim_Temp','livingRoom_PMV','livingRoom_Temp','mezzanine_PMV','mezzanine_Temp']
示例:
14.0,15.1,3.1,230.0,40.0,1.21136396241,0.0,-2.0925950832,15.2547144369,-1.59841620663,17.4451394848,-4.48642007828,17.7701378164,-1.87781304943,16.4544875583,-0.334880991824,20.9530677507,-3.98155421448,18.0000031279,-2.06816062239,16.9694428505,-1.27592184517,17.7946534879
输出数据集:
headerOutput = ['window_1','window_2','window_3','window_4','window_5','window_6','window_7','window_8','window_9','window_10']
示例:
0.0,0.94,0.82,0.0,0.4,0.67,0.0,1.0,1.0,0.95,0.0,0.64,0.0,0.75,0.78,0.77,0.23,0.78,0.21,0.29,0.7,0.48,0.0
现在我的网络拓扑结构如下,但必要时可以更改:
model = Sequential()
model.add(Dense(40, input_shape=(13, ), kernel_initializer='random_normal', activation='tanh'))
model.add(Dense(40, activation='tanh'))
model.add(Dense(40, activation='tanh'))
model.add(Dense(40, activation='tanh'))
model.add(Dense(40, activation='relu'))
model.add(Dense(10, activation='sigmoid'))
model.summary()
sgd = optimizers.SGD(lr=0.1, decay=1e-5, momentum=0.3, nesterov=True)
model.compile(optimizer=sgd, loss='mean_squared_error', metrics=['mae', 'acc'])
model.fit(Input_np, Output_Norm, validation_split=0.3, epochs=35, batch_size=100)
答案 0 :(得分:1)
这里有两个问题:
np.round
需要兼容的非符号对象,而K.sigmoid
则返回符号张量。 (更新这是您的错误来源)。np.round
返回一个非符号值。 (更新这将是下一个错误)。最重要的是,您必须在所有功能中坚持keras.backend
的功能。如果您不喜欢兼容性(即您只打算在特定后端运行模型,无论是Theano还是Tensorflow),您也可以直接使用后端定义的操作,因为keras.backend
相对有限。
在任何支持的后端中,也没有直接的方法来舍入浮点值。更重要的是,我认为舍入激活函数没有多大意义,因为它可能会严重损害渐变。
<强>更新强>
如果你这样做是为了得到一个圆形输出,就没有必要摆弄激活函数本身。您可以简单地对输出进行舍入:np.round(yournetwork.predict(...), 2)