Nice to meet you, this is my first post on qiita. I think there are many places that cannot be reached.
The sample code of the neural network framework is mostly a classification problem, and there are not many examples of regression, so I tried it.
There was a place where it fits. I want to know how to write well, so I will post it.
** I wrote more than 1000 lines to do the same thing with theano, but with chainer it was 98 lines. ** Migrate to chainer.
It worked fine on GPU.
_ ** Conclusion: chainer awesome ** _
python
#target is correct answer data
#mnist.If it is py, it is written like ↓
target = diabetes['target'].astype(np.float32) #With this, it will not work if the number of mini batches is 2 or more
If it is ↑, a Value Error like ↓ will appear (13 is batch size).
ValueError: non-broadcastable output operand with shape (1,30) doesn't match the broadcast shape (13,30)
As a result of trial and error, it worked.
python
#I have to reshape
target = diabetes['target'].astype(np.float32).reshape(len(diabetes['target']), 1)
python
n_units = 30
model = FunctionSet(l1=F.Linear(10, n_units),
l2=F.Linear(n_units, n_units),
l3=F.Linear(n_units, 1))
python
optimizer = optimizers.AdaDelta(rho=0.9)
python
# Neural net architecture
def forward(x_data, y_data, train=True):
x, t = Variable(x_data), Variable(y_data)
h1 = F.dropout(F.relu(model.l1(x)), train=train)
h2 = F.dropout(F.relu(model.l2(h1)), train=train)
y = model.l3(h2)
#Returns mean squared error and prediction results
#The prediction result is returned because the correlation coefficient is calculated later with the prediction result and the correct answer data.
return F.mean_squared_error(y, t), y
Since the correct answer rate cannot be given, the same evaluation as the classification cannot be performed.
Since it is difficult to understand if it is only an error, usually use the R 2 </ sup> value?
For personal reasons, we evaluated the correlation coefficient between the predicted value and the correct answer data.
The line that calculates the correlation coefficient becomes messy because of the addiction (see above).
python
pearson = np.corrcoef(np.asarray(preds).reshape(len(preds),), np.asarray(y_test).reshape(len(preds),))
Recommended Posts