项目作者: dnwjddl

项目描述 :
🌱CNN, GAN, RNN, AE, GAN, UNET
高级语言: Jupyter Notebook
项目地址: git://github.com/dnwjddl/keras-in-DeepLearning.git
创建时间: 2021-02-10T17:58:13Z
项目社区:https://github.com/dnwjddl/keras-in-DeepLearning

开源协议:

下载


keras-in-DeepLearning

파이썬 라이브러리 KERAS

  • 엔진으로는 Tensorflow, Theano, CNTK 등이 있음
  • 케라스는 인공지능 엔진 프로그램을 호출하여 인공지능 알고리즘을 수행한다
  • 케라스는 특정 엔진에 국한되지 않는 공통 백엔드 함수도 제공해준다.

CODE

  • models는 인공신경망의 각 게층을 연결하여 하나의 모델을 만든 후 컴파일, 학습, 예측을 담당
  • layers는 인공신경망의 각 계층을 만드는 클래스 제공
  1. # 케라스로 인공 신경망 모델 만듦. models.Sequential()을 사용하여 파이썬 프로세스에게 알림
  2. ## models.Sequential은 파이썬의 클래스
  3. ## model이라는 인스턴스 만듦
  4. model = keras.models.Sequential()
  5. # 모델 인스턴스가 생성되면 멤버 함수 add()를 이용하여 인공지능 계층 추가
  6. model.add(keras.layers.Dense(1, input_shape =(1,))
  7. # 학습에 사용되는 최적화 알고리즘 -> 확률적 경사하강법, 손실함수 -> 평균제곱 오차
  8. model.compile('SGD', 'mse')
  9. # 모델을 주어진 데이터로 학습
  10. ## verbose는 학습 진행사항 표시 여부
  11. model.fit(x[:2], y[:2], epochs = 1000, verbose = 0)
  12. # 성능 평가
  13. print("Targets:", y[2:])
  14. print("Predictions:", model.predict(x[2:]).flatten())

객체지향형 구현

  • 분산 방식 모델링
  1. class ANN(models.Model):
  2. def __init__(self, Nin, Nh, Nout):
  3. # Prepare network layers and activate functions
  4. hidden = layers.Dense(Nh)
  5. output = layers.Dense(Nout)
  6. relu = layers.Activation('relu')
  7. softmax = layers.Activation('softmax')
  8. # Connect network elements
  9. x = layers.Input(shape = (Nin,))
  10. h = relu(hidden(x))
  11. y = softmax(output(h))
  12. super().__init__(x, y)
  13. self.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
  • 연쇄 방식 모델링
  1. class ANN(models.Sequential):
  2. def __init__(self, Nin, Nh, Nout):
  3. super().__init__()
  4. self.add(layers.Dense(Nh, activation = 'relu', input_shape = (Nin,)))
  5. self.add(layers.Dense(Nout, activation = 'softmax'))
  6. self.compil (loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

함수형 구현

  • 분산 방식 모델링
  1. def ANN(Nin, Nh, Nout):
  2. x = layers.Input(shape = (Nin,))
  3. h = layers.Activation('relu')(layers.Dense(Nh)(x))
  4. ...
  5. model = models.Model(x,y)
  6. model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy']
  7. return model
  • 연쇄 방식 모델링
  1. def ANN(Nin, Nh, Nout):
  2. model = models.Sequential()
  3. model.add(layers.Dense(Nh, activation = 'relu', input_shape = (Nin,)))
  4. model.add(layers.Dense(Nout, avtivation = 'softmax'))
  5. model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
  6. return model