new_contents: #coding: utf-8
  import numpy as np
  import chainer
  from chainer import cuda
  import chainer.functions as F
  from chainer import optimizers
  import time
  from sklearn.datasets import fetch_mldata
  from sklearn.cross_validation import train_test_split
  import pylab
  import matplotlib.pyplot as plt
  
  gpu_flag = -1
  
  if gpu_flag >= 0:
      cuda.check_cuda_available()
  xp = cuda.cupy if gpu_flag >= 0 else np
  
  batchsize = 100
  n_epoch = 20
  
  
  print "load MNIST dataset"
  mnist = fetch_mldata('MNIST original', data_home=".")
  X = mnist.data
  y = mnist.target
  X = X.astype(xp.float32)
  y = y.astype(xp.int32)
  
  # Normalize
  X /= X.max()
  
  # Divide data
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)
  
  N = y_train.size
  N_test = y_test.size
  
  # MNISTはチャンネル数が1なのでreshapeだけでOK
  X_train = X_train.reshape((len(X_train), 1, 28, 28))
  X_test = X_test.reshape((len(X_test), 1, 28, 28))
  # plt.imshow(X_train[1][0], cmap=pylab.cm.gray_r, interpolation='nearest')
  # plt.show()
  
  model = chainer.FunctionSet(conv1=F.Convolution2D(1, 20, 5),   
                              conv2=F.Convolution2D(20, 50, 5),  
                              l1=F.Linear(800, 500),             
                              l2=F.Linear(500, 10))              
  
  if gpu_flag >= 0:
      cuda.get_device(gpu_flag).use()
      model.to_gpu()
  
  def forward(x_data, y_data, train=True):
      x, t = chainer.Variable(x_data), chainer.Variable(y_data)
      h = F.max_pooling_2d(F.relu(model.conv1(x)), 2)
      h = F.max_pooling_2d(F.relu(model.conv2(h)), 2)
      h = F.dropout(F.relu(model.l1(h)), train=train)
      y = model.l2(h)
      if train:
          return F.softmax_cross_entropy(y, t)
      else:
          return F.accuracy(y, t)
  
  optimizer = optimizers.Adam()
  optimizer.setup(model)
  
  fp1 = open("accuracy.txt", "w")
  fp2 = open("loss.txt", "w")
  
  fp1.write("epoch\ttest_accuracy\n")
  fp2.write("epoch\ttrain_loss\n")
  
  
  for epoch in range(1, n_epoch + 1):
      print "epoch: %d" % epoch
  
      perm = np.random.permutation(N)
      sum_loss = 0
      for i in range(0, N, batchsize):
          x_batch = xp.asarray(X_train[perm[i:i + batchsize]])
          y_batch = xp.asarray(y_train[perm[i:i + batchsize]])
  
          optimizer.zero_grads()
          loss = forward(x_batch, y_batch)
          loss.backward()
          optimizer.update()
          sum_loss += float(loss.data) * len(y_batch)
  
      print "train mean loss: %f" % (sum_loss / N)
      fp2.write("%d\t%f\n" % (epoch, sum_loss / N))
      fp2.flush()
  
      sum_accuracy = 0
      for i in range(0, N_test, batchsize):
          x_batch = xp.asarray(X_test[i:i + batchsize])
          y_batch = xp.asarray(y_test[i:i + batchsize])
  
          acc = forward(x_batch, y_batch, train=False)
          sum_accuracy += float(acc.data) * len(y_batch)
  
      print "test accuracy: %f" % (sum_accuracy / N_test)
      fp1.write("%d\t%f\n" % (epoch, sum_accuracy / N_test))
      fp1.flush()
  
  
  fp1.close()
  fp2.close()
  
  import cPickle
  # CPU環境でも学習済みモデルを読み込めるようにCPUに移してからダンプ
  model.to_cpu()
  cPickle.dump(model, open("model.pkl", "wb"), -1)
  
subject: add learn