示例#1
0
文件: layers.py 项目: linklab/aiclass
 def forward(self, forward_final_output_value, target_value, is_numba):
     self.target_value = target_value
     self.y = tff.softmax(forward_final_output_value, is_numba=is_numba)
     loss = tff.cross_entropy_error(self.y,
                                    self.target_value,
                                    is_numba=is_numba)
     return loss
示例#2
0
    def draw_filtered_images(self, test_inputs, figsize=(20, 5)):
        self.feed_forward(input_data=test_inputs, is_train=False, is_numba=False)

        plt.figure(figsize=figsize)
        for idx in range(len(test_inputs)):
            plt.subplot(100 + len(test_inputs) * 10 + idx + 1)
            img = test_inputs[idx]
            img.shape = (28, 28)
            print(img.shape)
            plt.imshow(img, cmap='gray')
        plt.show()

        for layer_name, layer in self.layers.items():
            if layer_name.startswith("conv"):
                print("[Convolution Layer: {:s}]".format(layer_name))
                plt.figure(figsize=(layer.output[0].shape[2], int(layer.output[0].shape[1] / len(layer.output))))
                for idx in range(len(layer.output)):
                    plt.subplot(100 + len(layer.output) * 10 + idx + 1)
                    img = layer.output[idx]
                    if (img.shape[0] == 1):
                        img = np.reshape(img, (img.shape[1], img.shape[2]))
                        print(img.shape)
                        plt.imshow(img, cmap='gray')
                    elif (img.shape[0] == 3 or img.shape[0] == 4):
                        img = np.transpose(img, (1, 2, 0))
                        print(img.shape)
                        plt.imshow(img, cmap=cm.PRGn)
                    else:
                        print("Image Channel Size (Filter Num) should be 1, 3, 4")
                plt.show()

                activation_layer_name = "activation" + layer_name[4:]
                activation_layer = self.layers[activation_layer_name]

                print("[Activation Layer: {:s}]".format(activation_layer_name))
                plt.figure(figsize=(activation_layer.output[0].shape[2], int(activation_layer.output[0].shape[1] / len(activation_layer.output))))
                for idx in range(len(activation_layer.output)):
                    plt.subplot(100 + len(activation_layer.output) * 10 + idx + 1)
                    img = activation_layer.output[idx]
                    if (img.shape[0] == 1):
                        img = np.reshape(img, (img.shape[1], img.shape[2]))
                        print(img.shape)
                        plt.imshow(img, cmap='gray')
                    elif (img.shape[0] == 3 or img.shape[0] == 4):
                        img = np.transpose(img, (1, 2, 0))
                        print(img.shape)
                        plt.imshow(img, cmap=cm.PRGn)
                    else:
                        print("Image Channel Size (Filter Num) should be 1, 3, 4")
                plt.show()

            if layer_name.startswith("pooling"):
                print("[Pooling Layer: {:s}]".format(layer_name))
                plt.figure(figsize=(layer.output[0].shape[2], int(layer.output[0].shape[1] / len(layer.output))))
                for idx in range(len(layer.output)):
                    plt.subplot(100 + len(layer.output) * 10 + idx + 1)
                    img = layer.output[idx]
                    if (img.shape[0] == 1):
                        img = np.reshape(img, (img.shape[1], img.shape[2]))
                        print(img.shape)
                        plt.imshow(img, cmap='gray')
                    elif (img.shape[0] == 3 or img.shape[0] == 4):
                        img = np.transpose(img, (1, 2, 0))
                        print(img.shape)
                        plt.imshow(img, cmap=cm.PRGn)
                    else:
                        print("Image Channel Size (Filter Num) should be 1, 3, 4")
                plt.show()

            if layer_name.startswith("reshape"):
                print("[Pooling Layer: {:s}]".format(layer_name))
                for idx in range(len(layer.output)):
                    plt.figure(figsize=(layer.output.shape[1], int(layer.output.shape[0])))
                    img = layer.output[idx]
                    img.shape = (1, len(img))
                    print(img.shape)
                    plt.imshow(img, cmap='gray')
                plt.show()

            if layer_name.startswith("affine"):
                print("[Affine Layer: {:s}]".format(layer_name))
                for idx in range(len(layer.output)):
                    plt.figure(figsize=(layer.output.shape[1], int(layer.output.shape[0])))
                    img = layer.output[idx]
                    img.shape = (1, len(img))
                    print(img.shape)
                    plt.imshow(img, cmap='gray')
                plt.show()

        print("[Softmax Layer]")
        np.set_printoptions(precision=3)
        for idx in range(len(layer.output)):
            plt.figure(figsize=(layer.output.shape[1], int(layer.output.shape[0])))
            img = layer.output[idx]
            softmax_img = tff.softmax(img, is_numba=False)
            softmax_img.shape = (1, len(softmax_img))
            print(softmax_img.shape, ":", softmax_img)
            plt.imshow(softmax_img, cmap='gray')
        plt.show()