코드입니다


import tensorflow as tf
from tensorflow.keras.models import Sequential # type: ignore
from tensorflow.keras.layers import Dense # type: ignore
from tensorflow.keras.optimizers import Adam # type: ignore
from tensorflow.keras.datasets import mnist # type: ignore
from tensorflow.keras.utils import to_categorical # type: ignore
import numpy as np

# 사용자 정의 활성화 함수: SiLU
def SiLU(x):
    return x / (1 + np.exp(-x))

# 사용자 정의 활성화 함수를 Keras에 등록
from tensorflow.keras.layers import Activation # type: ignore
from tensorflow.keras.utils import get_custom_objects # type: ignore

get_custom_objects().update({'SiLU': Activation(SiLU)})

# MNIST 데이터셋 로드
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# 데이터 전처리
X_train = X_train.reshape(-1, 28*28).astype('float32') / 255.0
X_test = X_test.reshape(-1, 28*28).astype('float32') / 255.0

y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

# 모델 정의
model = Sequential([
    Dense(128, activation='SiLU', input_shape=(784,)),  # 첫 번째 은닉층: 128개의 뉴런, Leaky ReLU 활성화 함수 적용
    Dense(64, activation='SiLU'),   # 두 번째 은닉층: 64개의 뉴런, Leaky ReLU 활성화 함수 적용
    Dense(10, activation='softmax')       # 출력층: 10개의 뉴런 (10개의 클래스), Softmax 활성화 함수 적용
])

# 모델 컴파일
model.compile(optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

# 모델 훈련
model.fit(X_train, y_train, epochs=10, batch_size=32, validation_split=0.2)

# 모델 평가
loss, accuracy = model.evaluate(X_test, y_test)
print(f"Test Accuracy: {accuracy * 100:.2f}%")





출력은 대충 이렇게 나옵니다.


2024-07-12 12:07:30.344063: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.

2024-07-12 12:07:30.923519: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.

c:\Users\d4143\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\layers\core\dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.

  super().__init__(activity_regularizer=activity_regularizer, **kwargs)

Traceback (most recent call last):

", line 31, in <module>

    Dense(128, activation='SiLU', input_shape=(784,)),  # 첫 번째 은닉층: 128개의 뉴런, Leaky ReLU 활성화 함수 적용

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  line 89, in __init__

    self.activation = activations.get(activation)

                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^

  line 104, in get

    raise ValueError(

ValueError: Could not interpret activation function identifier: SiLU



stackoverflow에 검색해도 나오지 않아 여기에 질문드립니다. sigmoid함수나 tanh같은 건 잘 돌아가는데 왜 swish만 안 될까요?