Ejemplo n.º 1
0
 def test_sparse_softmax_cross_entropy(self):
   batch_size = 10
   n_features = 5
   logit_tensor = np.random.rand(batch_size, n_features)
   label_tensor = np.random.rand(batch_size)
   with self.session() as sess:
     logit_tensor = tf.convert_to_tensor(logit_tensor, dtype=tf.float32)
     label_tensor = tf.convert_to_tensor(label_tensor, dtype=tf.int32)
     out_tensor = SparseSoftMaxCrossEntropy()(label_tensor, logit_tensor)
     out_tensor = out_tensor.eval()
     assert out_tensor.shape == (batch_size,)
Ejemplo n.º 2
0
 def test_sparse_softmax_cross_entropy(self):
   batch_size = 10
   n_features = 5
   logit_tensor = np.random.rand(batch_size, n_features)
   label_tensor = np.random.rand(batch_size)
   with self.session() as sess:
     logit_tensor = tf.convert_to_tensor(logit_tensor, dtype=tf.float32)
     label_tensor = tf.convert_to_tensor(label_tensor, dtype=tf.int32)
     out_tensor = SparseSoftMaxCrossEntropy()(label_tensor, logit_tensor)
     out_tensor = out_tensor.eval()
     assert out_tensor.shape == (batch_size,)
Ejemplo n.º 3
0
  def create_loss(self, layer, label, weight):
    task_label = Squeeze(squeeze_dims=1, in_layers=[label])
    task_label = Cast(dtype=tf.int32, in_layers=[task_label])
    task_weight = Squeeze(squeeze_dims=1, in_layers=[weight])

    loss = SparseSoftMaxCrossEntropy(in_layers=[task_label, layer])
    weighted_loss = WeightedError(in_layers=[loss, task_weight])
    return weighted_loss
Ejemplo n.º 4
0
def test_SparseSoftmaxCrossEntropy_pickle():
    tg = TensorGraph()
    logits = Feature(shape=(tg.batch_size, 5))
    labels = Feature(shape=(tg.batch_size, ), dtype=tf.int32)
    layer = SparseSoftMaxCrossEntropy(in_layers=[labels, logits])
    tg.add_output(layer)
    tg.set_loss(layer)
    tg.build()
    tg.save()
Ejemplo n.º 5
0
    def build_graph(self):
        # inputs placeholder
        self.inputs = Feature(shape=(None, self.image_size, self.image_size,
                                     3),
                              dtype=tf.float32)
        # data preprocessing and augmentation
        in_layer = DRAugment(self.augment,
                             self.batch_size,
                             size=(self.image_size, self.image_size),
                             in_layers=[self.inputs])
        # first conv layer
        in_layer = Conv2D(int(self.n_init_kernel),
                          kernel_size=7,
                          activation_fn=None,
                          in_layers=[in_layer])
        in_layer = BatchNorm(in_layers=[in_layer])
        in_layer = ReLU(in_layers=[in_layer])

        # downsample by max pooling
        res_in = MaxPool2D(ksize=[1, 3, 3, 1],
                           strides=[1, 2, 2, 1],
                           in_layers=[in_layer])

        for ct_module in range(self.n_downsample - 1):
            # each module is a residual convolutional block
            # followed by a convolutional downsample layer
            in_layer = Conv2D(int(self.n_init_kernel * 2**(ct_module - 1)),
                              kernel_size=1,
                              activation_fn=None,
                              in_layers=[res_in])
            in_layer = BatchNorm(in_layers=[in_layer])
            in_layer = ReLU(in_layers=[in_layer])
            in_layer = Conv2D(int(self.n_init_kernel * 2**(ct_module - 1)),
                              kernel_size=3,
                              activation_fn=None,
                              in_layers=[in_layer])
            in_layer = BatchNorm(in_layers=[in_layer])
            in_layer = ReLU(in_layers=[in_layer])
            in_layer = Conv2D(int(self.n_init_kernel * 2**ct_module),
                              kernel_size=1,
                              activation_fn=None,
                              in_layers=[in_layer])
            res_a = BatchNorm(in_layers=[in_layer])

            res_out = res_in + res_a
            res_in = Conv2D(int(self.n_init_kernel * 2**(ct_module + 1)),
                            kernel_size=3,
                            stride=2,
                            in_layers=[res_out])
            res_in = BatchNorm(in_layers=[res_in])

        # max pooling over the final outcome
        in_layer = ReduceMax(axis=(1, 2), in_layers=[res_in])

        for layer_size in self.n_fully_connected:
            # fully connected layers
            in_layer = Dense(layer_size,
                             activation_fn=tf.nn.relu,
                             in_layers=[in_layer])
            # dropout for dense layers
            #in_layer = Dropout(0.25, in_layers=[in_layer])

        logit_pred = Dense(self.n_tasks * self.n_classes,
                           activation_fn=None,
                           in_layers=[in_layer])
        logit_pred = Reshape(shape=(None, self.n_tasks, self.n_classes),
                             in_layers=[logit_pred])

        weights = Weights(shape=(None, self.n_tasks))
        labels = Label(shape=(None, self.n_tasks), dtype=tf.int32)

        output = SoftMax(logit_pred)
        self.add_output(output)
        loss = SparseSoftMaxCrossEntropy(in_layers=[labels, logit_pred])
        weighted_loss = WeightedError(in_layers=[loss, weights])

        # weight decay regularizer
        # weighted_loss = WeightDecay(0.1, 'l2', in_layers=[weighted_loss])
        self.set_loss(weighted_loss)