In [2]:
import sys, os
import getpass
import cntk.ops as C
from cntk.models import Sequential
from cntk.layers import Dense
from cntk.trainer import Trainer
from cntk.learner import sgd
from cntk.io import StreamConfiguration, text_format_minibatch_source
from cntk.utils import get_train_loss, get_train_eval_criterion
import numpy as np
import matplotlib.pyplot as plt
In [3]:
mnist_dir = '/home/' + getpass.getuser() + '/repos/cntk/Examples/Image/DataSets/MNIST/'
train_file = mnist_dir + "Train-28x28_cntk_text.txt"
test_file = mnist_dir + "Test-28x28_cntk_text.txt"
input_dim = 784
hidden_layers_dim = 400
num_output_classes = 10
features_stream_name = 'features'
labels_stream_name = 'labels'
mb_source = text_format_minibatch_source(train_file, [
StreamConfiguration(features_stream_name, input_dim),
StreamConfiguration(labels_stream_name, num_output_classes)
])
features_si = mb_source[features_stream_name]
labels_si = mb_source[labels_stream_name]
print (mb_source)
print (features_si)
In [4]:
input = C.input_variable(input_dim, np.float32)
label = C.input_variable(num_output_classes, np.float32)
scaled_input = C.element_times(C.constant(0.00390625), input)
#scaled_input = input
model = Sequential([Dense(hidden_layers_dim, activation=C.relu),
Dense(hidden_layers_dim, activation=C.relu),
Dense(num_output_classes)])
model = model(scaled_input)
cross_entropy = C.cross_entropy_with_softmax(model, label)
error = C.classification_error(model, label)
trainer = Trainer(model=model, loss_function=cross_entropy, eval_function=error,
parameter_learners=sgd(model.parameters, lr=0.003125))
In [5]:
def print_training_progress(trainer, ii, freq, verbose=0):
previous_minibatch_loss = 'NA'
previous_minibatch_eval = 'NA'
if ii % freq == 0:
previous_minibatch_loss = get_train_loss(trainer)
previous_minibatch_eval = get_train_eval_criterion(trainer)
if verbose:
print ('mb_num: {0}, loss: {1}, eval: {2}'.format(ii, previous_minibatch_loss, previous_minibatch_eval))
return ii, previous_minibatch_loss, previous_minibatch_eval
#Initialize the parameters for the trainer
minibatch_size = 64
num_samples_per_sweep = 60000
num_sweeps_to_train_with = 10
num_minibatches_to_train = (num_samples_per_sweep * num_sweeps_to_train_with) / minibatch_size
In [6]:
training_progress_output_freq = 500
plotdata = {"batchsize":[], "loss":[], "error":[]}
for i in range(0, int(num_minibatches_to_train)):
mb = mb_source.next_minibatch(minibatch_size)
# Specify the input variables mapping in the model to actual minibatch data to be trained
arguments = {input: mb[features_si],
label: mb[labels_si]}
trainer.train_minibatch(arguments)
batchsize, loss, error = print_training_progress(trainer, i, training_progress_output_freq, verbose=1)
if not (loss == "NA" or error =="NA"):
plotdata["batchsize"].append(batchsize)
plotdata["loss"].append(loss)
plotdata["error"].append(error)
In [19]:
plt.plot(plotdata['batchsize'], plotdata['loss'], '--')
plt.plot(plotdata['batchsize'], plotdata['error'], '--', c='r')
plt.show()
In [20]:
mb_source_test = text_format_minibatch_source(test_file, [
StreamConfiguration(features_stream_name, input_dim),
StreamConfiguration(labels_stream_name, num_output_classes)
])
features_si_test = mb_source_test[features_stream_name]
labels_si_test = mb_source_test[labels_stream_name]
In [21]:
# Test data for trained model
test_minibatch_size = 512
num_samples = 10000
num_minibatches_to_test = num_samples / test_minibatch_size
test_result = 0.0
for ii in range(0, int(num_minibatches_to_test)):
mb_test = mb_source_test.next_minibatch(test_minibatch_size)
error_per_sample = trainer.test_minibatch({input: mb_test[features_si_test], label: mb_test[labels_si_test]})
test_result += error_per_sample
print ('test error per sample: {0}'.format(test_result/int(num_minibatches_to_test)))
In [23]:
output = C.softmax(model)
In [59]:
mb = mb_source_test.next_minibatch(20)
y_pred = output.eval({input: mb[features_si_test]})[:, 0, :]
y_true = np.asarray(mb[labels_si_test].m_data)[:, 0, :]
y_pred = np.argmax(y_pred, axis=1)
y_true = np.argmax(y_true, axis=1)
prd_wroung = [ii for ii in range(len(y_pred)) if y_pred[ii]!=y_true[ii]]
print (y_pred)
print (y_true)
print (prd_wroung)
In [64]:
if len(prd_wroung) > 0:
idx = prd_wroung[0]
else:
idx = 0
mat = mb[features_si_test].value
plt.imshow(mat[idx, :, :].reshape(28, 28), cmap='gray_r')
plt.axis('off')
plt.show()
In [80]:
import sys
sys.path.append('/home/' + getpass.getuser() + '/git_test/')
from Teemo.examples.mnist import load_data
from Teemo.algorithm.utils import matrixops
trn_x, trn_y, valid_x, valid_y = load_data.mnist()
trn_x = np.asarray(trn_x, dtype=np.float32)
trn_y = np.asarray(trn_y, dtype=np.float32)
valid_x = np.asarray(valid_x, dtype=np.float32)
valid_y = np.asarray(valid_y, dtype=np.float32)
print ('load data sucessfully')
In [81]:
input = C.input_variable(input_dim, np.float32)
label = C.input_variable(num_output_classes, np.float32)
#scaled_input = C.element_times(C.constant(0.00390625), input)
scaled_input = input
model = Sequential([Dense(hidden_layers_dim, activation=C.relu),
Dense(hidden_layers_dim, activation=C.relu),
Dense(num_output_classes)])
model = model(scaled_input)
cross_entropy = C.cross_entropy_with_softmax(model, label)
error = C.classification_error(model, label)
trainer = Trainer(model=model, loss_function=cross_entropy, eval_function=error,
parameter_learners=sgd(model.parameters, lr=0.003125))
In [82]:
training_progress_output_freq = 500
plotdata = {"batchsize":[], "loss":[], "error":[]}
max_jj = 0
for ii in range(10):
jj = 0
for mb_x, mb_y in matrixops.iterate_minibatches(minibatch_size, trn_x, trn_y, shuffle=True):
trainer.train_minibatch({input:mb_x, label:mb_y})
batchsize, loss, error = print_training_progress(trainer, ii*max_jj + jj, training_progress_output_freq, verbose=1)
jj += 1
if not (loss == "NA" or error =="NA"):
plotdata["batchsize"].append(batchsize)
plotdata["loss"].append(loss)
plotdata["error"].append(error)
max_jj = jj
In [83]:
plt.plot(plotdata['batchsize'], plotdata['loss'], '--')
plt.plot(plotdata['batchsize'], plotdata['error'], '--', c='r')
plt.show()
In [84]:
# Test data for trained model
test_minibatch_size = 512
num_samples = 10000
num_minibatches_to_test = num_samples / test_minibatch_size
test_result = 0.0
for mb_x, mb_y in matrixops.iterate_minibatches(test_minibatch_size, valid_x, valid_y, shuffle=True):
error_per_sample = trainer.test_minibatch({input: mb_x, label: mb_y})
test_result += error_per_sample
print ('test error per sample: {0}'.format(test_result/int(num_minibatches_to_test)))
In [85]:
output = C.softmax(model)
In [116]:
num_test = 500
mb_x = valid_x[:num_test]
mb_y = valid_y[:num_test]
y_pred = output.eval({input: mb_x})[0, :, :]
y_true = mb_y
y_pred = np.argmax(y_pred, axis=1)
y_true = np.argmax(y_true, axis=1)
prd_wroung = [ii for ii in range(len(y_pred)) if y_pred[ii]!=y_true[ii]]
print ('predicted value: {0}'.format([y_pred[ii] for ii in prd_wroung]))
print ('ground truth value: {0}'.format([y_true[ii] for ii in prd_wroung]))
In [117]:
if len(prd_wroung) > 0:
idx = prd_wroung[1]
print ('wroung')
else:
idx = 0
plt.imshow(mb_x[idx].reshape(28, 28), cmap='gray_r')
plt.axis('off')
plt.show()
In [ ]: