def mnist(layers, # pylint: disable=invalid-name activation="sigmoid", batch_size=128, mode="train"): """Mnist classification with a multi-layer perceptron.""" if activation == "sigmoid": activation_op = tf.sigmoid elif activation == "relu": activation_op = tf.nn.relu else: raise ValueError("{} activation not supported".format(activation)) # Data. data = mnist_dataset.load_mnist() data = getattr(data, mode) images = tf.constant(data.images, dtype=tf.float32, name="MNIST_images") images = tf.reshape(images, [-1, 28, 28, 1]) labels = tf.constant(data.labels, dtype=tf.int64, name="MNIST_labels") # Network. mlp = nn.MLP(list(layers) + [10], activation=activation_op, initializers=_nn_initializers) network = nn.Sequential([nn.BatchFlatten(), mlp]) def build(): indices = tf.random_uniform([batch_size], 0, data.num_examples, tf.int64) batch_images = tf.gather(images, indices) batch_labels = tf.gather(labels, indices) output = network(batch_images) return _xent_loss(output, batch_labels) return build
def _init_(self, modelName='MNIST.model'): self.modelName = modelName self.root = self.windows() self.image1 = Image.new("RGB", (window_width, window_height), (255, 255, 255)) self.draw = ImageDraw.Draw(self.image1) self.mlp = nn.MLP(784, 1000, 10) model = cLinks.Classifier(self.mlp) serializers.load_hdf5(self.modelNamem, model)
def main(): # CSVファイルを読込 csv = pd.read_csv('./' + DATASET_PATH, engine='python') # データをx(入力値)とy(出力値)に分割する x = pd.DataFrame() y = pd.DataFrame() for i, key in enumerate(csv.columns): if i == 0: y[key] = csv[key] else: x[key] = csv[key] # スケーリングしない場合 # ax = x ay = y # 標準化する場合 ax = (x - x.mean()) / x.std() # ay = (y - y.mean()) / y.std() # x, yを教師データとテストデータに分割する train_len = TRAIN_LENGTH train_x = train_y = test_x = test_y = pd.DataFrame() train_x = ax[0:train_len] train_y = ay[0:train_len] test_x = ax[train_len + 1:len(x)] test_y = ay[train_len + 1:len(y)] # indexを修正 train_x = train_x.reset_index() train_y = train_y.reset_index() del train_x['index'] del train_y['index'] test_x = test_x.reset_index() test_y = test_y.reset_index() del test_x['index'] del test_y['index'] model = nn.MLP(LAYER_NUM, OUTPUT_LAYER_NUM) chainer.serializers.load_npz(MODEL_PATH, model) # テストを実行 test(train_x, train_y, 'train', model) test(test_x, test_y, 'test', model)
def main(): np.random.seed(100) X_train, y_train = read_data('images_train.csv', 'labels_train.csv') y_train = one_hot_labels(y_train).T print(X_train.shape) print(y_train.shape) p = np.random.permutation(60000) X_train = X_train[:, p] y_train = y_train[:, p] X_dev = X_train[:, 0:10000] y_dev = y_train[:, 0:10000] X_train = X_train[:, 10000:] y_train = y_train[:, 10000:] mean = np.mean(X_train) std = np.std(X_train) X_train = (X_train - mean) / std X_dev = (X_dev - mean) / std X_test, y_test = read_data('images_test.csv', 'labels_test.csv') y_test = one_hot_labels(y_test).T X_test = (X_test - mean) / std model = nn.MLP(hidden_sizes=[300], hidden_activation='sigmoid', output_activation='softmax', cost_function='cross_entropy') costs = model.fit(X_train, y_train, k=10, learning_rate=1, num_epochs=30, lmbd=0, batch_size=1000, shuffle=False) for item in costs: print(item) preds = model.predict(X_test) acc = accuracy(y_test, preds) print('Accuracy: {}'.format(acc))
def cifar10( path, # pylint: disable=invalid-name conv_channels=None, linear_layers=None, batch_norm=True, batch_size=128, num_threads=4, min_queue_examples=1000, mode="train"): """Cifar10 classification with a convolutional network.""" # Data. _maybe_download_cifar10(path) # Read images and labels from disk. if mode == "train": filenames = [ os.path.join(path, CIFAR10_FOLDER, "data_batch_{}.bin".format(i)) for i in xrange(1, 6) ] elif mode == "test": filenames = [os.path.join(path, "test_batch.bin")] else: raise ValueError("Mode {} not recognised".format(mode)) depth = 3 height = 32 width = 32 label_bytes = 1 image_bytes = depth * height * width record_bytes = label_bytes + image_bytes reader = tf.FixedLengthRecordReader(record_bytes=record_bytes) _, record = reader.read(tf.train.string_input_producer(filenames)) record_bytes = tf.decode_raw(record, tf.uint8) label = tf.cast(tf.slice(record_bytes, [0], [label_bytes]), tf.int32) raw_image = tf.slice(record_bytes, [label_bytes], [image_bytes]) image = tf.cast(tf.reshape(raw_image, [depth, height, width]), tf.float32) # height x width x depth. image = tf.transpose(image, [1, 2, 0]) image = tf.div(image, 255) queue = tf.RandomShuffleQueue( capacity=min_queue_examples + 3 * batch_size, min_after_dequeue=min_queue_examples, dtypes=[tf.float32, tf.int32], shapes=[image.get_shape(), label.get_shape()]) enqueue_ops = [queue.enqueue([image, label]) for _ in xrange(num_threads)] tf.train.add_queue_runner(tf.train.QueueRunner(queue, enqueue_ops)) # Network. def _conv_activation(x): # pylint: disable=invalid-name return tf.nn.max_pool(tf.nn.relu(x), ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") conv = nn.ConvNet2D(output_channels=conv_channels, kernel_shapes=[5], strides=[1], paddings=[nn.SAME], activation=_conv_activation, activate_final=True, initializers=_nn_initializers, use_batch_norm=batch_norm) if batch_norm: linear_activation = lambda x: tf.nn.relu(nn.BatchNorm()(x)) else: linear_activation = tf.nn.relu mlp = nn.MLP(list(linear_layers) + [10], activation=linear_activation, initializers=_nn_initializers) network = nn.Sequential([conv, nn.BatchFlatten(), mlp]) def build(): image_batch, label_batch = queue.dequeue_many(batch_size) label_batch = tf.reshape(label_batch, [batch_size]) output = network(image_batch) return _xent_loss(output, label_batch) return build
def main(): # 学習処理 def train(batch_size, epoch_size): np.random.seed(seed=4) loss_log = [] for epoch in range(epoch_size): # ミニバッチ処理 sffindx = np.random.permutation(train_len) for i in range(0, train_len, batch_size): xx = Variable( train_x.iloc[sffindx[i:(i + batch_size)], range(len(train_x.columns))].values.astype( np.float32)) yy = Variable( train_y.iloc[sffindx[i:(i + batch_size)], range(len(train_y.columns))].values.astype( np.int32).flatten()) model.cleargrads() pred_y = model(xx) loss = F.softmax_cross_entropy(pred_y, yy) loss.backward() optimizer.update() loss_log.append(loss.data) print(str(epoch + 1) + '/' + str(epoch_size) + ' ステップ完了') print(' loss = ' + str(loss_log[epoch])) output_list = [] for loss in loss_log: output_list.append(float(str(round(np.average(loss), 2)))) json.dump( np.array(output_list).tolist(), codecs.open('./output/loss.json', 'w', encoding='utf-8')) plt.plot(loss_log) plt.show() # CSVファイルを読込 csv = pd.read_csv(DATASET_PATH, engine='python') # データをx(入力値)とy(出力値)に分割する x = pd.DataFrame() y = pd.DataFrame() for i, key in enumerate(csv.columns): if i == 0: y[key] = csv[key] else: x[key] = csv[key] print(csv.corr()['rank']) # スケーリングしない場合 # ax = x ay = y # 標準化する場合 ax = (x - x.mean()) / x.std() # ay = (y - y.mean()) / y.std() # x, yを教師データとテストデータに分割する train_len = TRAIN_LENGTH train_x = train_y = test_x = test_y = pd.DataFrame() # 前半のデータを教師データとする場合 train_x = ax[0:train_len] train_y = ay[0:train_len] test_x = ax[train_len + 1:len(x)] test_y = ay[train_len + 1:len(y)] # indexを修正 train_x = train_x.reset_index() train_y = train_y.reset_index() del train_x['index'] del train_y['index'] test_x = test_x.reset_index() test_y = test_y.reset_index() del test_x['index'] del test_y['index'] # Optimizerの設定 model = nn.MLP(LAYER_NUM, OUTPUT_LAYER_NUM) optimizer = optimizers.Adam(alpha=LEARNING_RATE) optimizer.setup(model) # 学習を実行 train(BATCH_SIZE, EPOCH_SIZE) chainer.serializers.save_npz(MODEL_PATH, model) # テストを実行 test(train_x, train_y, 'train', model) test(test_x, test_y, 'test', model)