我正在尝试通过实现梯度始终为1的自定义激活函数,在Keras中模拟Rosenblatt的感知器。
在阅读了文档以及有关实现自定义激活功能的几个示例之后,我编写了以下代码
import numpy as np
import tensorflow as tf
@tf.custom_gradient
def step(x):
def grad(*dy):
return 1.0
return (tf.math.sign(x) + 1.0) / 2.0, grad
class RosenLayer(tf.keras.layers.Layer):
def __init__(self):
super(RosenLayer, self).__init__()
def call(self, x):
return step(x)
X = np.random.normal(0, 1, 100)
X = X.astype(np.float32)
X = X.reshape([100,1])
y = (np.sign(X) + 1)/2
inputs = tf.keras.layers.Input(shape=(1,))
results = tf.keras.layers.Dense(1,activation='linear')(inputs)
preds = RosenLayer()(results)
model = tf.keras.models.Model(inputs=inputs,outputs=preds)
sgd = tf.keras.optimizers.SGD()
model.compile(optimizer=sgd ,loss='mse',metrics=['mse'])
model.fit(X,y, batch_size=1, epochs=30, shuffle=False)
我正在对正态分布进行随机抽样,然后根据数据是正数还是负数对数据进行分类。这导致两个线性可分离的类,Rosenblatt感知器应将其分离。但是,运行这段代码,我得到了一个很大的回溯,结尾是
File "[...]/anaconda3/envs/aaut/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1613, in _create_c_op
raise ValueError(str(e))
ValueError: Shape must be at least rank 2 but is rank 0 for 'BiasAddGrad' (op: 'BiasAddGrad') with input shapes: [].
此错误可能是什么原因?