问题描述
我希望我的 ANN 模型给出 0 和 1 的输出,而不是 0 和 1 之间的概率,因为我需要将我的模型作为 permutation_importance( ) 的输入。我的数据集有二元结果。
我尝试为输出层设计定制的二元阶跃函数,但运气不佳,错误很多。
在下面的代码中,我插入了 grad 函数只是因为没有它我会收到错误消息 ValueError: No gradients provided for any variable: ['dense/kernel:0','dense/bias:0','dense_1/kernel:0','dense_1/bias:0','dense_2/kernel:0','dense_2/bias:0'].
我的代码:
from keras.layers import Activation
from keras import backend as K
from keras.utils.generic_utils import get_custom_objects
def custom_activation(x):
x = K.round(x)
def grad(dx):
return dx
return x,grad
get_custom_objects().update({'custom_activation':
Activation(custom_activation)})
#model
model = keras.models.Sequential()
model.add(keras.layers.Dense(32,input_dim=a,activation='relu'))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(64,activation="relu"))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(2,activation="softmax"))
model.add(Activation(custom_activation,name='custom_activation'))
model.compile(loss="binary_crossentropy",optimizer='adam',metrics=["accuracy"])
model.fit(train_feats,train_labels,epochs=10)
我得到的错误是
021-06-07 12:38:11.506383: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (onednN) to use the following cpu instructions in performance-critical operations: AVX2 FMA
To enable them in other operations,rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
File "<input>",line 1,in <module>
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_umd.py",line 197,in runfile
pydev_imports.execfile(filename,global_vars,local_vars) # execute the script
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py",line 18,in execfile
exec(compile(contents+"\n",file,'exec'),glob,loc)
File "/Users/himanideshpande/PycharmProjects/hello/ANN_sttpcode.py",line 203,in <module>
model.add(Activation(custom_activation,name='custom_activation'))
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/training/tracking/base.py",line 522,in _method_wrapper
result = method(self,*args,**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/engine/sequential.py",line 223,in add
output_tensor = layer(self.outputs[0])
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/engine/base_layer.py",line 945,in __call__
return self._functional_construction_call(inputs,args,kwargs,File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/engine/base_layer.py",line 1083,in _functional_construction_call
outputs = self._keras_tensor_symbolic_call(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/engine/base_layer.py",line 816,in _keras_tensor_symbolic_call
return self._infer_output_signature(inputs,input_masks)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/engine/base_layer.py",line 861,in _infer_output_signature
outputs = tf.nest.map_structure(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/util/nest.py",line 867,in map_structure
structure[0],[func(*x) for x in entries],File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/util/nest.py",in <listcomp>
structure[0],File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/engine/keras_tensor.py",line 580,in keras_tensor_from_tensor
out = keras_tensor_cls.from_tensor(tensor)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/engine/keras_tensor.py",line 172,in from_tensor
type_spec = tf.type_spec_from_value(tensor)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/framework/type_spec.py",line 579,in type_spec_from_value
raise TypeError("Could not build a typespec for %r with type %s" %
TypeError: Could not build a typespec for <function custom_activation.<locals>.grad at 0x7fd44b0ed4c0> with type function
请帮忙解决这个问题
提前致谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)