Tensorflowで自分で作った活性化関数をネットワークで実装したいのですがInputのエラーが出てがわかりません。
活性化関数はReLUを改良したものです。今まではKerasをずっと使っていたのですがTensorflowを使う際、そこで詰まりました。
エラー
ValueError: Shape must be rank 0 but is rank 2 for 'cond/Switch' (op: 'Switch') with input shapes: [1,1], [1,1].
関数のコード
python2.7
1def smooth_relu(tensor): 2 e=0.15 3 alpha=0.005 4 5 def smooth(tensor): 6 7 smoothtensor=tf.cond(tensor<(e+alpha) ,lambda: (tensor-alpha)*(tensor-alpha),lambda:e*((tensor-alpha)-self.e*0.5), tf.cond( 8 pred, 9 true_fn=None, 10 false_fn=None, 11 strict=False, 12 name=None, 13 fn1=None, 14 fn2=None 15 )) 16 17 18 return (smoothtensor) 19 20 21 22 newtensor=tf.cond(tensor<0 ,lambda :0, lambda:smooth(tensor)) 23 24 25 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())
あなたの回答
tips
プレビュー