-
RNN LSTM 을 활용한 제조 데이터 분석 사례 및 동향 조사Trend 2023. 12. 2. 15:33728x90
순환신경망을 활용하여 이상감지를 수행하는 과정은 지금까지 봐왔던 이상감지 알고리즘과 는 사뭇 다르게 느껴졌다.
아래 참고자료에서는 RNN을 예시로 들었다.
하지만 RNN의 단점인
과거의 정보가 시간이 지나면서 희미해 짐 (vanishing gradient problem)의 발생으로 LSTM 의 사용이 가능하다.
순환신경망 구조는 위, 아래(RNN, LSTM)와 같다.
이를 활용하여
LSTM-AutoEncoder 이상감지 모델에 적용할 수 있다.(LSTM-AE)
auto encoder를 사용하면 raw 데이터의 전처리 등을 통하여 분석, 데이터 활용에 적합한 수준으로 변환할 수 있는 장점이 있다.
아래 소스코드는 gpt로 작성한 코드이다.
import numpy as np import matplotlib.pyplot as plt from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense from sklearn.preprocessing import MinMaxScaler from matplotlib.animation import FuncAnimation # Generate some example data np.random.seed(42) data = np.random.normal(0, 1, 1000).reshape(-1, 1) # Normalize the data scaler = MinMaxScaler(feature_range=(0, 1)) data_normalized = scaler.fit_transform(data) # Create sequences for training sequence_length = 10 def create_sequences(data, sequence_length): sequences = [] for i in range(len(data) - sequence_length + 1): seq = data[i:i+sequence_length] sequences.append(seq) return np.array(sequences) # Build the LSTM autoencoder model model = Sequential() model.add(LSTM(100, activation='relu', input_shape=(sequence_length, 1), return_sequences=True)) model.add(LSTM(50, activation='relu', return_sequences=False)) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') # Set up the plot fig, ax = plt.subplots() line, = ax.plot([], [], label='Original Data', linestyle='--', marker='o') line_reconstructed, = ax.plot([], [], label='Reconstructed Data', linestyle='--', marker='o') ax.legend() # Function to initialize the plot def init(): ax.set_xlim(0, len(data)) ax.set_ylim(-1, 1) return line, line_reconstructed # Function to update the plot in real-time def update(frame): new_data_point = np.random.normal(0, 1) data[-1] = new_data_point data[:-1] = data[1:] # Update the model with the new data new_sequence = create_sequences(data_normalized[-sequence_length:].reshape(1, -1), sequence_length) predicted_data = model.predict(new_sequence.reshape(1, sequence_length, 1)) # Update the plot line.set_data(range(len(data)), data) line_reconstructed.set_data(range(len(data) - 1, len(data)), scaler.inverse_transform(predicted_data).flatten()) return line, line_reconstructed # Set up the animation ani = FuncAnimation(fig, update, frames=range(100), init_func=init, blit=True) plt.show()
colab에서의 실행 결과이다.
에러로 인해 제대로 그려지지 않았다.
발생 에러는 아래와 같은 원인으로 나타났다.
Exception in Tkinter callback Traceback (most recent call last): predicted_data = model.predict(new_sequence.reshape(1, sequence_length, 1)) ValueError: cannot reshape array of size 0 into shape (1,10,1)
참고자료 :
https://inspaceai.github.io/2019/03/21/Anomaly_Detection_With_LSTM-AE/
LSTM-AutoEncoder 이상감지 모델
Content Similar Posts Comments
inspaceai.github.io
카이스트 기계공학과 유승화 교수 이메일: ryush@kaist.ac.kr 홈페이지: https://sites.google.com/site/seunghwalab
반응형'Trend' 카테고리의 다른 글
(동향 조사) 新 디지털 제조혁신 추진전략 (2) (4) 2023.12.08 (동향 조사) 新 디지털 제조혁신 추진전략 (1) (4) 2023.12.06 국내외 스마트제조ㆍ지능형로봇ㆍ드론 시장분석과 해외진출 전략 (리뷰) (2) 2023.09.10 (관련 보고서 발굴, 정리) 제조분야에서의 융합연구 리뷰 보고서 요약 (2) 2023.09.10 2023, 스마트 팩토리 및 스마트 제조산업 관련 자료 정리 및 등대공장 현황 등 (2) 2023.09.03