def build(self, input_tensor, net): with self.scope(): input_tensor = utils.ensure_tensor_dimensionality(input_tensor, 2) self.labels_placeholder = tf.placeholder( tf.float32, shape=(None, int(input_tensor.shape[1])), name='Y') loss = tf.reduce_mean( self.activation(labels=self.labels_placeholder, logits=input_tensor)) for layer in net.layers: loss = layer.contribute_to_loss(loss) output = tf.identity(loss, 'loss') self.register_train_summary(tf.summary.scalar('train_loss', loss)) self.register_test_summary(tf.summary.scalar('test_loss', loss)) with self.scope('accuracy_layer'): correct_prediction = tf.equal( tf.argmax(self.labels_placeholder, 1), tf.argmax(input_tensor, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name='accuracy') self.register_train_summary( tf.summary.scalar('train_accuracy', accuracy)) self.register_test_summary(tf.summary.scalar('test_accuracy', accuracy)) return output
def verify_input_tensor_dimensionality(self, input_tensor): if self.input_tensor_shape_list != input_tensor.shape.as_list(): raise ValueError( "Tensors' shape during building and applying are different: build={}, " "apply={}".format(self.input_tensor_shape_list, input_tensor.shape.as_list())) return utils.ensure_tensor_dimensionality(input_tensor, self.n_dimensions)
def apply_reverse(self, input_tensor): if self.weights is None or self.bias is None: raise RuntimeError('Layer is not built') with self.reverse_scope(): input_tensor = utils.ensure_tensor_dimensionality(input_tensor, 2) output = tf.nn.bias_add(input_tensor, tf.negative(self.bias)) output = tf.matmul(output, self.weights, transpose_b=True) if self.activation: output = self.activation(output) return self.restore_dimensionality_after_reverse(output)
def apply_reverse(self, input_tensor): if self.weights is None or self.bias is None: raise RuntimeError('Layer is not built') with self.reverse_scope(): input_tensor = utils.ensure_tensor_dimensionality(input_tensor, 4) output = tf.nn.bias_add(input_tensor, tf.negative(self.bias)) output_shape = tf.stack([tf.shape(input_tensor)[0]] + self.input_shape[1:]) output = tf.nn.conv2d_transpose(output, self.weights, output_shape=output_shape, strides=self.strides, padding='SAME') if self.activation: output = self.activation(output) return self.restore_dimensionality_after_reverse(output)
def build(self, input_tensor, net): with self.scope(): input_tensor = utils.ensure_tensor_dimensionality(input_tensor, 2) loss = tf.reduce_mean( tf.squared_difference(net.input_layer.input_placeholder, input_tensor), name='loss') for layer in net.layers: loss = layer.contribute_to_loss(loss) output = tf.identity(loss, 'loss') self.register_train_summary(tf.summary.scalar('train_loss', loss)) self.register_test_summary(tf.summary.scalar('test_loss', loss)) # TODO: this should not be here, probably in the network self.register_test_summary( tf.summary.image( 'input', tf.reshape(input_tensor, [-1, 28, 28, 1]))) return output
def apply(self, input_tensor): input_tensor = utils.ensure_tensor_dimensionality(input_tensor, 4) return tf.nn.max_pool(input_tensor, self.size, strides=[1, 2, 2, 1], padding='SAME')