[파이썬] Keras 케라스 API 레퍼런스

Keras 케라스 is a popular deep learning framework that provides a high-level API for building and training neural networks. It is designed to be user-friendly, modular, and easy to understand. In this blog post, we will explore the various APIs available in Keras 케라스 and how to use them effectively in Python.

Sequential API

The Sequential API in Keras 케라스 allows you to build models by stacking layers on top of each other. Each layer in the model can be easily added using the add() function. Here is an example of how to create a simple neural network using the Sequential API:

import keras
from keras.models import Sequential
from keras.layers import Dense

# Create a sequential model
model = Sequential()

# Add layers to the model
model.add(Dense(64, activation='relu', input_shape=(784,)))
model.add(Dense(10, activation='softmax'))

# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

In the above example, we create a sequential model and add two layers to it - a fully connected layer with 64 units and ReLU activation, and a output layer with 10 units and softmax activation. We then compile the model by specifying the optimizer, loss function, and evaluation metric.

Functional API

The Functional API in Keras 케라스 allows you to build more complex models with shared layers and multiple inputs/outputs. Instead of creating models layer by layer, you define the entire computational graph of the model and then create the model using the defined graph. Here is an example of how to create a model using the Functional API:

import keras
from keras.models import Model
from keras.layers import Input, Dense

# Define input layer
inputs = Input(shape=(784,))

# Define layers
x = Dense(64, activation='relu')(inputs)
x = Dense(32, activation='relu')(x)
outputs = Dense(10, activation='softmax')(x)

# Create the model
model = Model(inputs=inputs, outputs=outputs)

# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

In the above example, we define the input layer, then pass it through two fully connected layers with ReLU activation, and finally connect it to the output layer. We then create the model using the defined inputs and outputs, and compile it as before.

Model Subclassing API

The Model Subclassing API in Keras 케라스 allows you to define custom models by subclassing the Model class and implementing the call() method. This gives you full flexibility in defining the model architecture and forward pass. Here is an example of how to create a custom model using the Model Subclassing API:

import keras
from keras.models import Model
from keras.layers import Input, Dense

class MyModel(Model):
    def __init__(self, num_classes):
        super(MyModel, self).__init__()
        self.dense1 = Dense(64, activation='relu')
        self.dense2 = Dense(num_classes, activation='softmax')

    def call(self, inputs):
        x = self.dense1(inputs)
        return self.dense2(x)

# Create an instance of the custom model
model = MyModel(num_classes=10)

# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

In the above example, we define a custom model MyModel by subclassing Model. We define the layers in the __init__() method and implement the forward pass in the call() method. We then create an instance of the custom model and compile it.

Conclusion

Keras 케라스 provides multiple APIs for building and training neural networks. The Sequential API is suitable for simple, linear architectures, while the Functional API is more flexible and allows for complex models. The Model Subclassing API is useful when you need to define custom models and have full control over the architecture and forward pass. Choose the API that suits your needs and start building powerful deep learning models with Keras 케라스!