2017-10-13 3 views
7

실험으로 저는 행렬의 행렬식을 근사화하는 케라 (keras) 모델을 구축 중입니다. 그러나, 내가 그것을 실행할 때 모든 신기원에서 손실이 내려 가고 유효성 검사 손실이 증가합니다! 예를 들어 : 케라 (keras)로 행렬식을 근사하는 법

import numpy as np 
import sys 
from scipy.stats import pearsonr 
from scipy.linalg import det 
from sklearn.model_selection import train_test_split 
from tqdm import tqdm 
from sklearn.preprocessing import StandardScaler 
from sklearn.pipeline import Pipeline 
import math 
import tensorflow as tf 
from keras.models import Sequential 
from keras.layers import Dense 
from keras.wrappers.scikit_learn import KerasRegressor 
from keras import backend as K 

def baseline_model(): 
# create model 
     model = Sequential() 
     model.add(Dense(200, input_dim=n**2, kernel_initializer='normal', activation='relu')) 
     model.add(Dense(1, input_dim=n**2)) 
     #  model.add(Dense(1, kernel_initializer='normal')) 
     # Compile model 
     model.compile(loss='mean_squared_error', optimizer='adam') 
     return model 


n = 15 

print("Making the input data using seed 7", file=sys.stderr) 
np.random.seed(7) 
U = np.random.choice([0, 1], size=(n**2,n)) 
#U is a random orthogonal matrix 
X =[] 
Y =[] 
# print(U) 
for i in tqdm(range(100000)): 
     I = np.random.choice(n**2, size = n) 
     # Pick out the random rows and sort the rows of the matrix lexicographically. 
     A = U[I][np.lexsort(np.rot90(U[I]))] 
     X.append(A.ravel()) 
     Y.append(det(A)) 

X = np.array(X) 
Y = np.array(Y) 

print("Data created") 

estimators = [] 
estimators.append(('standardize', StandardScaler())) 
estimators.append(('mlp', KerasRegressor(build_fn=baseline_model, epochs=50, batch_size=32, verbose=2))) 
pipeline = Pipeline(estimators) 
X_train, X_test, y_train, y_test = train_test_split(X, Y, 
                train_size=0.75, test_size=0.25) 
pipeline.fit(X_train, y_train, mlp__validation_split=0.3) 

은 어떻게 중지 할 수 있습니다가 너무 심하게 overfitting : 여기
8s - loss: 7573.9168 - val_loss: 21831.5428 
Epoch 21/50 
8s - loss: 7345.0197 - val_loss: 23594.8540 
Epoch 22/50 
13s - loss: 7087.7454 - val_loss: 24718.3967 
Epoch 23/50 
7s - loss: 6851.8714 - val_loss: 25624.8609 
Epoch 24/50 
6s - loss: 6637.8168 - val_loss: 26616.7835 
Epoch 25/50 
7s - loss: 6446.8898 - val_loss: 28856.9654 
Epoch 26/50 
7s - loss: 6255.7414 - val_loss: 30122.7924 
Epoch 27/50 
7s - loss: 6054.5280 - val_loss: 32458.5306 
Epoch 28/50 

는 전체 코드는?


업데이트 1

는 좀 더 레이어와 L_2의 정규화를 추가했습니다. 그러나 차이가 거의 없거나 전혀 없습니다.

def baseline_model(): 
# create model 
     model = Sequential() 
     model.add(Dense(n**2, input_dim=n**2, kernel_initializer='glorot_normal', activation='relu')) 
     model.add(Dense(int((n**2)/2.0), kernel_initializer='glorot_normal', activation='relu', kernel_regularizer=regularizers.l2(0.01))) 
     model.add(Dense(int((n**2)/2.0), kernel_initializer='glorot_normal', activation='relu', kernel_regularizer=regularizers.l2(0.01))) 
     model.add(Dense(int((n**2)/2.0), kernel_initializer='glorot_normal', activation='relu', kernel_regularizer=regularizers.l2(0.01))) 
     model.add(Dense(1, kernel_initializer='glorot_normal')) 
     # Compile model 
     model.compile(loss='mean_squared_error', optimizer='adam') 
     return model 

제가

100 에포크의 수가 증가하고 함께 완료 :

19s - loss: 788.9504 - val_loss: 18423.2807 
Epoch 97/100 
24s - loss: 760.2046 - val_loss: 18305.9273 
Epoch 98/100 
20s - loss: 806.0941 - val_loss: 18174.8706 
Epoch 99/100 
24s - loss: 780.0487 - val_loss: 18356.7482 
Epoch 100/100 
27s - loss: 749.2595 - val_loss: 18331.5859 

가 keras를 이용한 행렬의 행렬식을 근사 할 수 있는가?

+0

이것은 지나치게 적합하지 않으며 모델이 데이터에 맞지 않습니다. 이 모델은 너무 단순합니다. –

+0

@MatiasValdenegro 내가 overfitting이라고 부르는 이유는 손실이 0을 향해 내려가고 있으며 validation_loss가 계속 진행되고 있기 때문입니다. 숨겨진 레이어의 노드 수를 늘리는 것은 전혀 도움이되지 않습니다. 다음에 무엇을 시도할까요? – eleanora

+0

숨겨진 레이어의 수를 늘립니다. 'glorot'을 사용하여 숨겨진 레이어를 초기화하십시오. 'dropout' 또는'l2 regularizer'를 사용하십시오. – Nain

답변

3

나는 당신의 코드를 테스트했고 같은 결과를 얻었다. 그러나 행렬식 (determinant)에 대한 기본적인 이해를 해보겠습니다. DET는 n! 따라서 신경망의 몇 층에서 n * n 가중치로 근사치를 구할 수는 없습니다. 이것은 15부터 n = 15로 확장되지 않는 가중치의 수를 필요로합니다! DET에서의 곱셈에 대한 용어는 1307674368000입니다.

+0

이것은 나에게 명확하지 않습니다. DET는 물론 n^3 시간에 계산 될 수 있습니다 (n!가 아님). 또한, 수백 개의 신기원에 대해 케라 모델을 실행하면 훈련 세트의 손실이 0에 가까워집니다. – eleanora

+0

사실, 웨이트는 +1과 -1 만 사용하지만 롯트는 많이 포함하는 잘 정의 된 공식입니다 입력의 곱셈의. 그것은 단순한 신경 네트워크를 시도하는 좋은 경우라고 확신하지 못합니다. –

+0

@eleanora 계산상의 복잡성 때문에 용어의 숫자를 혼동스럽게합니다. – denfromufa

관련 문제