我想用张量流向网络引入新的激活功能。但是,出现输入形状错误。我应该在哪里改变?
这是新的层代码。
def smooth_relu(tensor):
e=0.15
alpha=0.005
def smooth(tensor):
smoothtensor=tf.cond(tensor<(e+alpha) ,lambda: (tensor-alpha)*(tensor-alpha),lambda:e*((tensor-alpha)-self.e*0.5), tf.cond(
pred,
true_fn=None,
false_fn=None,
strict=False,
name=None,
fn1=None,
fn2=None
))
return (smoothtensor)
newtensor=tf.cond(tensor<0 ,lambda :0, lambda:smooth(tensor))
return (newtensor)
这是网络。
self.inputs = tf.placeholder(shape=[1,4], dtype=tf.float32)
self.weights1 = tf.Variable(tf.truncated_normal([4,4]))
self.bias1 = tf.Variable(tf.zeros(shape=[1,4]))
self.weights2 = tf.Variable(tf.truncated_normal([4,4]))
self.bias2 = tf.Variable(tf.zeros(shape=[1, 4]))
self.weights3 = tf.Variable(tf.truncated_normal([4,1]))
self.bias3 = tf.Variable(tf.zeros([1,1]))
self.layer1 = tf.tanh(tf.matmul(self.inputs, self.weights1) + self.bias1)
self.layer2 = tf.tanh(tf.matmul(self.layer1, self.weights2) + self.bias2)
self.layer3 = smooth_relu(tf.matmul(self.layer2, self.weights3) + self.bias3)
self.output_layer = self.layer3
self.optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
self.nextQ = tf.placeholder(shape=[1,1], dtype=tf.float32)
self.loss = tf.reduce_sum(tf.square((self.nextQ - self.output_layer)))
self.trainer = self.optimizer.minimize(self.loss)
self.sess = tf.Session()
self.sess.run(tf.global_variables_initializer())
错误
形状必须为0级,但“ cond / Switch”为2级(op:“ Switch”) 输入形状为[1,1],[1,1]。