2016-06-24 4 views
6

강화 학습을 사용하는 신경망을 만들려고했습니다. 나는 scikit-neuralnetwork를 라이브러리로 선택했습니다 (간단하기 때문에). 그래도 피팅이 Theano를 두 번 부러 뜨리는 것 같습니다.sknn - 두 번째 핏의 입력 치수 불일치

다음은 충돌이 발생 간단한 코드이다 (참고, 거기 어떤 층 중요하지 않으며, 학습 속도 나 n_iter 않습니다) :

import numpy as np 
from sknn.mlp import Classifier, Layer 

clf = Classifier(
    layers=[ 
     Layer("Softmax") 
     ], 
    learning_rate=0.001, 
    n_iter=1) 

clf.fit(np.array([[0.]]), np.array([[0.]])) # Initialize the network for learning 

X = np.array([[-1.], [1.]]) 
Y = np.array([[1.], [0.]]) 

clf.fit(X, Y) # crash 

을 그리고 여기에 내가 가진 오류는 다음과 같습니다

ValueError: Input dimension mis-match. (input[0].shape[1] = 2, input[1].shape[1] = 1) 
Apply node that caused the error: Elemwise{Mul}[(0, 1)](y, LogSoftmax.0) 
Toposort index: 12 
Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)] 
Inputs shapes: [(1L, 2L), (1L, 1L)] 
Inputs strides: [(16L, 8L), (8L, 8L)] 
Inputs values: [array([[ 1., 0.]]), array([[ 0.]])] 
Outputs clients: [[Sum{axis=[1], acc_dtype=float64}(Elemwise{Mul}[(0, 1)].0)]] 

는 sknn 피팅 여러 번 지원하지 않습니다, 또는 좀 바보를하고있는 중이 야 2.7.11

파이썬에서 테스트 실수? 그렇지 않은 경우, 어떻게 보강 학습을 구현해야합니까?

답변

1

나는 매우 자주 사용하지만 sklearn과 매우 유사하므로 도움을받을 수 있습니다.

첫 번째로 fit 메소드를 사용할 때 새 데이터를 기반으로 가중치를 업데이트하려는 경우 메소드를 사용해야합니다.

충돌과 관련해서는 X 배열이 두 번째가 아닌 첫 번째 차원에서 다른 모양이기 때문입니다.

import numpy as np 
from sknn.mlp import Classifier, Layer 

clf = Classifier(
    layers=[ 
     Layer("Softmax") 
     ], 
    learning_rate=0.001, 
    n_iter=1) 

# Original training data 
X = np.array([[0.]]) 
Y = np.array([[0.]]) 
print X.shape, Y.shape 

# Data used for second fitting 
X = np.array([[-1.], [1.]]) 
Y = np.array([[1.], [0.]]) 
print X.shape, Y.shape 


# Use the partial fit method to update weights 
clf.partial_fit(X, Y) # Initialize the network for learning 
clf.partial_fit(X, Y) # Update the weights 


# Multiple training examples by stacking two on top of each other 
X = np.concatenate((X, X)) 
Y = np.concatenate((Y, Y)) 
print X.shape, Y.shape 

clf.partial_fit(X, Y) 

출력 :

(1, 1) (1, 1) 
(2, 1) (2, 1) 
(4, 1) (4, 1) 
+0

sknn 더 partial_fit이 없다가 (방법이 질문을하기 전에 그것을 찾기 위해 노력했다). 오, 나는 sklearn의 신경 회로망을 전혀 작동시키지 못했습니다. – seequ

+0

코드가 잘 실행되기 때문에 실행중인 버전은 무엇입니까? – ncfirth

+1

sknn 문서에서 'partial_fit' 메소드를 볼 수 있습니다. http://scikit-neuralnetwork.readthedocs.io/en/latest/module_mlp.html#classifier – ncfirth