def net_model(feature): with default_options(init=cntk.glorot_uniform()): layers = Dense(output_classes, activation=None)(feature) return layers
from cntk.io import MinibatchSource, CTFDeserializer, StreamDef, StreamDefs from cntk.ops import input_variable from cntk.layers import Dense from cntk.ops import relu from cntk.metrics import classification_error from cntk.losses import cross_entropy_with_softmax path = "dataset.txt" featuresShapeValue = 6 labelsShapeValue = 1 featuresShape = input_variable(featuresShapeValue) labelsShape = input_variable(labelsShapeValue) ctfdResult = CTFDeserializer( path, StreamDefs(features=StreamDef(field='features', shape=featuresShapeValue), labels=StreamDef(field='labels', shape=labelsShapeValue))) reader = MinibatchSource(ctfdResult) hiddenLayerDimension = 4 hiddenLayerOne = Dense(hiddenLayerDimension, activation=relu)(featuresShape) outputLayer = Dense(labelsShapeValue, activation=relu)(hiddenLayerOne) crossEntropy = cross_entropy_with_softmax(outputLayer, labelsShape) classificationError = classification_error(outputLayer, labelsShape)
plt.xlabel("X") plt.ylabel("Y") plt.show() # adding one dimension for further processing. Input must be formatted as (batch_size,1). features = features[:, None] predictions = predictions[:, None] ################### ##### Network ##### ################### # Output is a single node with a linear operation. input = cntk.input_variable(input_dim) label = cntk.input_variable(num_outputs) pred = Dense(num_outputs)(input) ################## ###### Loss ###### ################## # Defining loss function and evaluation metric loss = cntk.squared_error(pred, label) eval_fun = cntk.squared_error(pred, label) ###################### ###### Training ###### ###################### # Instantiate the trainer object to drive the model training learning_rate = learning_rate_schedule(args.initial_learning_rate,
def simple_mnist(tensorboard_logdir=None): input_dim = 19 num_output_classes = 2 num_hidden_layers = 2 hidden_layers_dim = 1024 # Input variables denoting the features and label data feature = C.input_variable(input_dim, np.float32) label = C.input_variable(num_output_classes, np.float32) # Instantiate the feedforward classification model # scaled_input = element_times(constant(0.00390625), feature) z = Sequential([For(range(num_hidden_layers), lambda i: Dense(hidden_layers_dim, activation=relu)), Dense(num_output_classes)])(feature) ce = cross_entropy_with_softmax(z, label) pe = classification_error(z, label) data_dir = r"." path = os.path.normpath(os.path.join(data_dir, "train.ctf")) check_path(path) reader_train = create_reader(path, True, input_dim, num_output_classes) input_map = { feature : reader_train.streams.features, label : reader_train.streams.labels } # Training config minibatch_size = 512 num_samples_per_sweep = 1825000 num_sweeps_to_train_with = 100 # Instantiate progress writers. progress_writers = [ProgressPrinter( tag='Training', num_epochs=num_sweeps_to_train_with)] if tensorboard_logdir is not None: tensorboard_writer = TensorBoardProgressWriter(freq=10, log_dir=tensorboard_logdir, model=z) progress_writers.append(tensorboard_writer) # Instantiate the trainer object to drive the model training lr = learning_parameter_schedule_per_sample(0.001) learner = create_learner(model=z) trainer = Trainer(z, (ce, pe), learner, progress_writers) num_minibatches_to_train = int(num_samples_per_sweep / minibatch_size * num_sweeps_to_train_with) model_dir = "model" for i in range(num_minibatches_to_train): mb = reader_train.next_minibatch(minibatch_size, input_map=input_map) trainer.train_minibatch(mb) freq = int(num_samples_per_sweep / minibatch_size) if i > 0 and i % freq == 0: timestamp = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%f") current_trainer_cp = os.path.join(model_dir, timestamp + "_epoch_" + str(freq) + ".trainer") trainer.save_checkpoint(current_trainer_cp) train_error = get_error_rate(os.path.join(data_dir, "train_subset.ctf"), input_map, input_dim, num_output_classes, trainer) valid_error = get_error_rate(os.path.join(data_dir, "validation.ctf"), input_map, input_dim, num_output_classes, trainer) if train_error > 0: tensorboard_writer.write_value("train_error", train_error, i) if valid_error > 0: tensorboard_writer.write_value("valid_error", valid_error, i) feat_path = os.path.normpath(os.path.join(data_dir, "test.ctf")) return get_error_rate(feat_path, input_map, input_dim, num_output_classes, trainer)
K = len(set(Y)) # we want one-hot encoded labels Y = y2indicator(Y) # split the data X = X.astype(np.float32) Y = Y.astype(np.float32) Xtrain = X[:-1000, ] Ytrain = Y[:-1000] Xtest = X[-1000:, ] Ytest = Y[-1000:] # the model will be a sequence of layers model = Sequential([ Dense(500, activation=relu), Dense(300, activation=relu), Dense(K, activation=None), ]) # define the inputs and labels inputs = C.input_variable(D, np.float32, name='inputs') labels = C.input_variable(K, np.float32, name='labels') # get the output logits = model(inputs) # define loss / metrics # like Tensorflow the softmax is done # internally (if needed), so all we need are the logits ce = cross_entropy_with_softmax(logits, labels)
def do_demo(): # create NN, train, test, predict input_dim = 4 hidden_dim = 2 output_dim = 3 train_file = "trainData_cntk.txt" test_file = "testData_cntk.txt" input_Var = C.ops.input(input_dim, np.float32) label_Var = C.ops.input(output_dim, np.float32) print("Creating a 4-2-3 tanh softmax NN for Iris data ") with default_options(init=glorot_uniform()): hLayer = C.layers.Dense(hidden_dim, activation=C.ops.tanh, name='hidLayer')(input_Var) oLayer = Dense(output_dim, activation=C.ops.softmax, name='outLayer')(hLayer) nnet = oLayer print("Creating a cross entropy mini-batch Trainer \n") ce = C.cross_entropy_with_softmax(nnet, label_Var) pe = C.classification_error(nnet, label_Var) fixed_lr = 0.05 lr_per_batch = learning_rate_schedule(fixed_lr, UnitType.minibatch) learner = C.sgd(nnet.parameters, lr_per_batch) trainer = C.Trainer(nnet, (ce, pe), [learner]) max_iter = 5000 # Máximo de iterações para o treino batch_size = 5 # Define o tamanho para o mini-batch progress_freq = 1000 # Exibe o erro a cada n mini-batches reader_train = create_reader(train_file, True, input_dim, output_dim) my_input_map = { input_Var: reader_train.streams.features, label_Var: reader_train.streams.labels } pp = ProgressPrinter(progress_freq) print("Starting training \n") for i in range(0, max_iter): currBatch = reader_train.next_minibatch(batch_size, input_map=my_input_map) trainer.train_minibatch(currBatch) pp.update_with_trainer(trainer) print("\nTraining complete") # ---------------------------------- print("\nEvaluating test data \n") reader_test = create_reader(test_file, False, input_dim, output_dim) numTestItems = 30 allTest = reader_test.next_minibatch(numTestItems, input_map=my_input_map) test_error = trainer.test_minibatch(allTest) print("Classification error on the 30 test items = %f" % test_error) # ---------------------------------- # Faz a predição para uma flor desconhecida unknown = np.array([[6.9, 3.1, 4.6, 1.3]], dtype=np.float32) print( "\nPrevisão de espécies de Íris para as características de entrada:") my_print(unknown[0], 1) # 1 decimal predicted = nnet.eval({input_Var: unknown}) print("Prediction is: ") my_print(predicted[0], 3) # 3 decimais # --------------------------------- print("\nTrained model input-to-hidden weights:\n") print(hLayer.hidLayer.W.value) print("\nTrained model hidden node biases:\n") print(hLayer.hidLayer.b.value) print("\nTrained model hidden-to-output weights:\n") print(oLayer.outLayer.W.value) print("\nTrained model output node biases:\n") print(oLayer.outLayer.b.value) save_weights("weights.txt", hLayer.hidLayer.W.value, hLayer.hidLayer.b.value, oLayer.outLayer.W.value, oLayer.outLayer.b.value) return 0 # success
def termination_gate(init=glorot_uniform(), name=''): return Dense(1, activation=sigmoid, init=init, name=name)
lr_schedule = C.learning_rate_schedule(lr=0.1, unit=C.UnitType.sample) m_schedule = C.momentum_schedule(0.99) vm_schedule = C.momentum_schedule(0.999) optimizer = C.adam([W1, W2], lr_schedule, momentum=m_schedule, variance_momentum=vm_schedule) # Create a buffer to manually accumulate gradients gradBuffer = dict((var.name, np.zeros(shape=var.shape)) for var in loss.parameters if var.name in ['W1', 'W2', 'b1', 'b2']) # Define the critic network critic = Sequential([ Dense(128, activation=C.relu, init=C.glorot_uniform()), Dense(1, activation=None, init=C.glorot_uniform(scale=.01)) ])(observations) # Define target and Squared Error Loss Function, adam optimizier, and trainer for the Critic. critic_target = C.sequence.input_variable(1, np.float32, name="target") critic_loss = C.squared_error(critic, critic_target) critic_lr_schedule = C.learning_rate_schedule(lr=0.1, unit=C.UnitType.sample) critic_optimizer = C.adam(critic.parameters, critic_lr_schedule, momentum=m_schedule, variance_momentum=vm_schedule) critic_trainer = C.Trainer(critic, (critic_loss, None), critic_optimizer) def discount_rewards(r, gamma=0.999):
def Blocked_Dense(dim, activation=None): dense = Dense(dim, activation=activation) @C.layers.BlockFunction('blocked_dense', 'blocked_dense') def func(x): return dense(x) return func
from cntk import cross_entropy_with_softmax, classification_error, Trainer from cntk.logging import ProgressPrinter #define network input_dim = 784 num_output_classes = 10 hidden_layers_dim = 200 feature = C.input_variable(input_dim) label = C.input_variable(num_output_classes) scaled_input = element_times(constant(0.00390625), feature) #define network topology h1 = Dense(hidden_layers_dim, activation=relu)(scaled_input) h2 = Dense(hidden_layers_dim, activation=relu)(h1) z = Dense(num_output_classes, activation=None)(h2) #define loss and error functions ce = cross_entropy_with_softmax(z, label) pe = classification_error(z, label) #Data source for training and testing path_train = "D:/MachineLearning/CNTK-2.0/Examples/Image/DataSets/MNIST/Train-28x28_cntk_text.txt" path_test = "D:/MachineLearning/CNTK-2.0/Examples/Image/DataSets/MNIST/Test-28x28_cntk_text.txt" #train model reader_train = MinibatchSource( CTFDeserializer( path_train,
def inception_v3_cifar_model(input, labelDim, bnTimeConst): # 32 x 32 x 3 conv1 = conv_bn_relu_layer(input, 32, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 32 conv2 = conv_bn_relu_layer(conv1, 32, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 32 conv3 = conv_bn_relu_layer(conv2, 64, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 64 conv4 = conv_bn_relu_layer(conv3, 80, (1,1), (1,1), True, bnTimeConst) # 32 x 32 x 80 conv5 = conv_bn_relu_layer(conv4, 128, (3,3), (1,1), True, bnTimeConst) # 32 x 32 x 128 pool1 = MaxPooling(filter_shape=(3,3), strides=(2,2), pad=True)(conv5) # # Inception Blocks # 16 x 16 x 128 mixed1 = inception_block_1(pool1, 32, [32, 48], [48, 64, 64], 32, bnTimeConst) # 16 x 16 x 176 mixed2 = inception_block_1(mixed1, 32, [32, 48], [48, 64, 64], 32, bnTimeConst) # 16 x 16 x 176 mixed3 = inception_block_1(mixed2, 32, [32, 48], [48, 64, 64], 32, bnTimeConst) # 16 x 16 x 176 mixed4 = inception_block_pass_through(mixed3, 0, 32, 48, 32, 48, 0, bnTimeConst) # 8 x 8 x 256 mixed5 = inception_block_3(mixed4, 64, [48, 64, 64], [48, 48, 48, 48, 64], 64, bnTimeConst) # 8 x 8 x 256 mixed6 = inception_block_3(mixed5, 64, [48, 64, 64], [48, 48, 48, 48, 64], 64, bnTimeConst) # 8 x 8 x 256 mixed7 = inception_block_3(mixed6, 64, [48, 64, 64], [48, 48, 48, 48, 64], 64, bnTimeConst) # 8 x 8 x 256 mixed8 = inception_block_3(mixed7, 80, [48, 64, 64], [48, 48, 48, 48, 64], 80, bnTimeConst) # 8 x 8 x 288 mixed9 = inception_block_3(mixed8, 128, [64, 128, 128], [64, 64, 64, 64, 128], 128, bnTimeConst) # 8 x 8 x 512 mixed10 = inception_block_5(mixed9, 128, [64, 128, 128], [64, 64, 64, 128], 128, bnTimeConst) # 8 x 8 x 512 mixed11 = inception_block_5(mixed10, 128, [64, 128, 128], [64, 64, 64, 128], 128, bnTimeConst) # 8 x 8 x 512 # # Prediction # pool2 = AveragePooling(filter_shape=(8,8))(mixed11) # 1 x 1 x 512 z = Dense(labelDim, init=he_normal())(pool2) # # Auxiliary # # 8 x 8 x 288 auxPool = AveragePooling(filter_shape=(3,3), strides=(1,1), pad=True)(mixed8) # 8 x 8 x 288 auxConv1 = conv_bn_relu_layer(auxPool, 320, (1,1), (1,1), True, bnTimeConst) # 8 x 8 x 320 auxConv2 = conv_bn_relu_layer(auxConv1, 512, (3,3), (1,1), True, bnTimeConst) # 8 x 8 x 512 aux = Dense(labelDim, init=he_normal())(auxConv2) return { 'z': z, 'aux': aux }
def inception_v3_cifar_model(input, labelDim, bnTimeConst): # 32 x 32 x 3 conv1 = conv_bn_relu_layer(input, 32, (3, 3), (1, 1), True, bnTimeConst) # 32 x 32 x 32 conv2 = conv_bn_relu_layer(conv1, 32, (3, 3), (1, 1), True, bnTimeConst) # 32 x 32 x 32 conv3 = conv_bn_relu_layer(conv2, 64, (3, 3), (1, 1), True, bnTimeConst) # 32 x 32 x 64 conv4 = conv_bn_relu_layer(conv3, 80, (1, 1), (1, 1), True, bnTimeConst) # 32 x 32 x 80 conv5 = conv_bn_relu_layer(conv4, 128, (3, 3), (1, 1), True, bnTimeConst) # 32 x 32 x 128 pool1 = MaxPooling(filter_shape=(3, 3), strides=(2, 2), pad=True)(conv5) # # Inception Blocks # 16 x 16 x 128 mixed1 = inception_block_1(pool1, 32, [32, 48], [48, 64, 64], 32, bnTimeConst) # 16 x 16 x 160 mixed2 = inception_block_1(mixed1, 32, [32, 48], [48, 64, 64], 64, bnTimeConst) # 16 x 16 x 160 mixed3 = inception_block_1(mixed2, 32, [32, 48], [48, 64, 64], 64, bnTimeConst) # 16 x 16 x 160 #mixed4 = inception_block_2(mixed3, 32, [48, 64, 64], bnTimeConst) mixed4 = inception_block_pass_through(mixed3, 0, 64, 80, 32, 48, 0, bnTimeConst) # 8 x 8 x 256 mixed5 = inception_block_3(mixed4, 192, [48, 64, 64], [128, 128, 128, 128, 192], 192, bnTimeConst) # 8 x 8 x mixed6 = inception_block_3(mixed5, 192, [160, 160, 192], [160, 160, 160, 160, 192], 192, bnTimeConst) # 8 x 8 x 768 mixed7 = inception_block_3(mixed6, 192, [160, 160, 192], [160, 160, 160, 160, 192], 192, bnTimeConst) # 8 x 8 x 768 mixed8 = inception_block_3(mixed7, 192, [192, 192, 192], [192, 192, 192, 192, 192], 192, bnTimeConst) # 8 x 8 x 768 mixed9 = inception_block_3(mixed8, 192, [192, 192, 192], [192, 192, 192, 192, 192], 192, bnTimeConst) # 8 x 8 x 1280 mixed10 = inception_block_5(mixed9, 320, [384, 384, 384], [448, 384, 384, 384], 192, bnTimeConst) # 8 x 8 x 2048 mixed11 = inception_block_5(mixed10, 320, [384, 384, 384], [448, 384, 384, 384], 192, bnTimeConst) # 8 x 8 x 2048 # # Prediction # pool3 = AveragePooling(filter_shape=(8, 8))(mixed11) # 1 x 1 x 2048 drop = Dropout(dropout_rate=0.2)(pool3) # 1 x 1 x 2048 z = Dense(labelDim, init=he_normal())(drop) # # Auxiliary # # 8 x 8 x 768 auxPool = AveragePooling(filter_shape=(3, 3), strides=(1, 1), pad=True)(mixed8) # 5 x 5 x 768 auxConv1 = conv_bn_relu_layer(auxPool, 128, (1, 1), (1, 1), True, bnTimeConst) # 5 x 5 x 128 auxConv2 = conv_bn_relu_layer(auxConv1, 256, (3, 3), (1, 1), True, bnTimeConst) # 1 x 1 x 768 aux = Dense(labelDim, init=he_normal())(auxConv2) return {'z': z, 'aux': aux}
x = [np.random.randint(2, size=(t, dim)).astype(np.float32) for t in r] y = np.array([arr.sum() for arr in x])[..., None] return x, y input_dim = 2 x, y = generate_variable_10(nb_samples=1000, dim=input_dim) hidden_dim = 100 input_tensor = C.sequence.input_variable(input_dim) target_tensor = C.input_variable(1) hidden = QRNN(window=2, hidden_dim=hidden_dim)(input_tensor) # hidden = Recurrence(LSTM(hidden_dim))(input_tensor) prediction = Dense(1)(C.sequence.last(hidden)) loss = C.squared_error(prediction, target_tensor) sgd_m = C.momentum_sgd(prediction.parameters, 0.1, 0.912) trainer = C.Trainer(prediction, (loss, ), [sgd_m]) n_epoch = 50 minibatch_size = 30 start = time.time() for epoch in range(n_epoch): for i in range(0, len(x), minibatch_size): lbound, ubound = i, i + minibatch_size x_mini = x[lbound:ubound] y_mini = y[lbound:ubound]
def inception_v3_model(input, labelDim, dropRate, bnTimeConst): # 299 x 299 x 3 conv1 = conv_bn_relu_layer(input, 32, (3, 3), (2, 2), False, bnTimeConst) # 149 x 149 x 32 conv2 = conv_bn_relu_layer(conv1, 32, (3, 3), (1, 1), False, bnTimeConst) # 147 x 147 x 32 conv3 = conv_bn_relu_layer(conv2, 64, (3, 3), (1, 1), True, bnTimeConst) # 147 x 147 x 64 pool1 = MaxPooling(filter_shape=(3, 3), strides=(2, 2), pad=False)(conv3) # 73 x 73 x 64 conv4 = conv_bn_relu_layer(pool1, 80, (1, 1), (1, 1), False, bnTimeConst) # 73 x 73 x 80 conv5 = conv_bn_relu_layer(conv4, 192, (3, 3), (1, 1), False, bnTimeConst) # 71 x 71 x 192 pool2 = MaxPooling(filter_shape=(3, 3), strides=(2, 2), pad=False)(conv5) # 35 x 35 x 192 # # Inception Blocks # mixed1 = inception_block_1(pool2, 64, [48, 64], [64, 96, 96], 32, bnTimeConst) # 35 x 35 x 256 mixed2 = inception_block_1(mixed1, 64, [48, 64], [64, 96, 96], 64, bnTimeConst) # 35 x 35 x 288 mixed3 = inception_block_1(mixed2, 64, [48, 64], [64, 96, 96], 64, bnTimeConst) # 35 x 35 x 288 mixed4 = inception_block_2(mixed3, 384, [64, 96, 96], bnTimeConst) # 17 x 17 x 768 mixed5 = inception_block_3(mixed4, 192, [128, 128, 192], [128, 128, 128, 128, 192], 192, bnTimeConst) # 17 x 17 x 768 mixed6 = inception_block_3(mixed5, 192, [160, 160, 192], [160, 160, 160, 160, 192], 192, bnTimeConst) # 17 x 17 x 768 mixed7 = inception_block_3(mixed6, 192, [160, 160, 192], [160, 160, 160, 160, 192], 192, bnTimeConst) # 17 x 17 x 768 mixed8 = inception_block_3(mixed7, 192, [192, 192, 192], [192, 192, 192, 192, 192], 192, bnTimeConst) # 17 x 17 x 768 mixed9 = inception_block_4(mixed8, [192, 320], [192, 192, 192, 192], bnTimeConst) # 8 x 8 x 1280 mixed10 = inception_block_5(mixed9, 320, [384, 384, 384], [448, 384, 384, 384], 192, bnTimeConst) # 8 x 8 x 2048 mixed11 = inception_block_5(mixed10, 320, [384, 384, 384], [448, 384, 384, 384], 192, bnTimeConst) # 8 x 8 x 2048 # # Prediction # pool3 = AveragePooling(filter_shape=(8, 8), pad=False)(mixed11) # 1 x 1 x 2048 drop = Dropout(dropout_rate=dropRate)(pool3) # 1 x 1 x 2048 z = Dense(labelDim, init=he_normal())(drop) # # Auxiliary # # 17 x 17 x 768 auxPool = AveragePooling(filter_shape=(5, 5), strides=(3, 3), pad=False)(mixed8) # 5 x 5 x 768 auxConv1 = conv_bn_relu_layer(auxPool, 128, (1, 1), (1, 1), True, bnTimeConst) # 5 x 5 x 128 auxConv2 = conv_bn_relu_layer(auxConv1, 768, (5, 5), (1, 1), False, bnTimeConst) # 1 x 1 x 768 aux = Dense(labelDim, init=he_normal())(auxConv2) return {'z': z, 'aux': aux}
random_state=0) t_test = np.eye(3)[list(map(int, _t_test))] # 各種パラメータ定義 n_input = 4 n_hidden = 10 n_output = 3 # 学習データと正解ラベルの入力変数を定義する。 features = C.input_variable((n_input)) label = C.input_variable((n_output)) # ネットワークを構築する。 model = Sequential([ Dense(n_hidden, activation=C.relu), Dense(n_hidden, activation=C.relu), Dense(n_hidden, activation=C.relu), Dense(n_output) ])(features) ce = C.cross_entropy_with_softmax(model, label) pe = C.classification_error(model, label) minibatch = C.learning_parameter_schedule(0.125) progress_printer = ProgressPrinter(0) trainer = C.Trainer(model, (ce, pe), [sgd(model.parameters, lr=minibatch)], [progress_printer]) # 学習する。 n_epoch = 30
input_folder_path = "src/ml/data/autcar_training" output_folder_path = "src/ml/data/autcar_training_balanced" image_width = 224 image_height = 168 trainer = Trainer(deeplearning_framework="cntk", image_height=image_height, image_width=image_width) trainer.create_balanced_dataset(input_folder_path, output_folder_path=output_folder_path) model = Sequential([ Convolution2D(filter_shape=(5,5), num_filters=32, strides=(1,1), pad=True, name="first_conv"), BatchNormalization(map_rank=1), Activation(relu), MaxPooling(filter_shape=(3,3), strides=(2,2), name="first_max"), Convolution2D(filter_shape=(3,3), num_filters=48, strides=(1,1), pad=True, name="second_conv"), BatchNormalization(map_rank=1), Activation(relu), MaxPooling(filter_shape=(3,3), strides=(2,2), name="second_max"), Convolution2D(filter_shape=(3,3), num_filters=64, strides=(1,1), pad=True, name="third_conv"), BatchNormalization(map_rank=1), Activation(relu), MaxPooling(filter_shape=(3,3), strides=(2,2), name="third_max"), Convolution2D(filter_shape=(5,5), num_filters=32, strides=(1,1), pad=True, name="fourth_conv"), BatchNormalization(map_rank=1), Activation(relu), Dense(100, activation=relu), Dropout(0.1), Dense(12, activation=softmax) ]) trainer.train(output_folder_path, model, epochs=4, output_model_path="driver_cntk.onnx") trainer.test("driver_cntk.onnx", output_folder_path+"/test_map.txt")
def z(): return Sequential([ Dense(hidden_dimension, activation=C.relu), Dense(outputs)])
def main(env_name='KungFuMasterNoFrameskip-v0', train_freq=4, target_update_freq=10000, checkpoint_freq=100000, log_freq=1, batch_size=32, train_after=200000, max_timesteps=5000000, buffer_size=50000, vmin=-10, vmax=10, n=51, gamma=0.99, final_eps=0.1, final_eps_update=1000000, learning_rate=0.00025, momentum=0.95): env = gym.make(env_name) env = wrap_env(env) state_dim = (4, 84, 84) action_count = env.action_space.n with C.default_options(activation=C.relu, init=C.he_uniform()): model_func = Sequential([ Convolution2D((8, 8), 32, strides=4, name='conv1'), Convolution2D((4, 4), 64, strides=2, name='conv2'), Convolution2D((3, 3), 64, strides=1, name='conv3'), Dense(512, name='dense1'), Dense((action_count, n), activation=None, name='out') ]) agent = CategoricalAgent(state_dim, action_count, model_func, vmin, vmax, n, gamma, lr=learning_rate, mm=momentum, use_tensorboard=True) logger = agent.writer epsilon_schedule = LinearSchedule(1.0, final_eps, final_eps_update) replay_buffer = ReplayBuffer(buffer_size) try: obs = env.reset() episode = 0 rewards = 0 steps = 0 for t in range(max_timesteps): # Take action if t > train_after: action = agent.act(obs, epsilon=epsilon_schedule.value(t)) else: action = np.random.choice(action_count) obs_, reward, done, _ = env.step(action) # Store transition in replay buffer replay_buffer.add(obs, action, reward, obs_, float(done)) obs = obs_ rewards += reward if t > train_after and (t % train_freq) == 0: # Minimize error in projected Bellman update on a batch sampled from replay buffer experience = replay_buffer.sample(batch_size) agent.train(*experience) # experience is (s, a, r, s_, t) tuple logger.write_value('loss', agent.trainer.previous_minibatch_loss_average, t) if t > train_after and (t % target_update_freq) == 0: agent.update_target() if t > train_after and (t % checkpoint_freq) == 0: agent.checkpoint('checkpoints/model_{}.chkpt'.format(t)) if done: episode += 1 obs = env.reset() if episode % log_freq == 0: steps = t - steps + 1 logger.write_value('rewards', rewards, episode) logger.write_value('steps', steps, episode) logger.write_value('epsilon', epsilon_schedule.value(t), episode) logger.flush() rewards = 0 steps = t finally: agent.save_model('checkpoints/{}.cdqn'.format(env_name))
def __call__(self, x): return Dense(x)
Y = np.eye(vocab_size, dtype=np.float32)[yi] return [X], [Y] sample(0) input_seq_axis = Axis('inputAxis') input_sequence = sequence.input_variable(shape=vocab_size, sequence_axis=input_seq_axis) label_sequence = sequence.input_variable(shape=vocab_size, sequence_axis=input_seq_axis) # model = Sequential([Dense(300),Dense(vocab_size)]) model = Sequential([ For(range(2), lambda: Sequential([Stabilizer(), Recurrence(LSTM(256), go_backwards=False)])), Dense(vocab_size)]) z = model(input_sequence) ce = cross_entropy_with_softmax(z, label_sequence) errs = classification_error(z, label_sequence) lr_per_sample = learning_rate_schedule(0.001, UnitType.sample) momentum_time_constant = momentum_as_time_constant_schedule(1100) clipping_threshold_per_sample = 5.0 gradient_clipping_with_truncation = True learner = momentum_sgd(z.parameters, lr_per_sample, momentum_time_constant, gradient_clipping_threshold_per_sample=clipping_threshold_per_sample, gradient_clipping_with_truncation=gradient_clipping_with_truncation) progress_printer = ProgressPrinter(freq=100, tag='Training') trainer = Trainer(z, (ce, errs), learner, progress_printer)
def simple_mnist(tensorboard_logdir=None): input_dim = 784 num_output_classes = 10 num_hidden_layers = 2 hidden_layers_dim = 200 # Input variables denoting the features and label data feature = C.input_variable(input_dim, np.float32) label = C.input_variable(num_output_classes, np.float32) # Instantiate the feedforward classification model scaled_input = element_times(constant(0.00390625), feature) z = Sequential([ For(range(num_hidden_layers), lambda i: Dense(hidden_layers_dim, activation=relu)), Dense(num_output_classes) ])(scaled_input) ce = cross_entropy_with_softmax(z, label) pe = classification_error(z, label) data_dir = os.path.dirname(os.path.abspath(__file__)) path = os.path.join(data_dir, 'Train-28x28_cntk_text.txt') reader_train = create_reader(path, True, input_dim, num_output_classes) input_map = { feature: reader_train.streams.features, label: reader_train.streams.labels } # Training config minibatch_size = 64 num_samples_per_sweep = 60000 num_sweeps_to_train_with = 10 # Instantiate progress writers. # training_progress_output_freq = 100 progress_writers = [ ProgressPrinter( # freq=training_progress_output_freq, tag='Training', num_epochs=num_sweeps_to_train_with) ] if tensorboard_logdir is not None: progress_writers.append( TensorBoardProgressWriter(freq=10, log_dir=tensorboard_logdir, model=z)) # Instantiate the trainer object to drive the model training lr = 0.001 trainer = Trainer(z, (ce, pe), sgd(z.parameters, lr), progress_writers) training_session(trainer=trainer, mb_source=reader_train, mb_size=minibatch_size, model_inputs_to_streams=input_map, max_samples=num_samples_per_sweep * num_sweeps_to_train_with, progress_frequency=num_samples_per_sweep).train() # Load test data path = os.path.normpath(os.path.join(data_dir, "Test-28x28_cntk_text.txt")) check_path(path) reader_test = create_reader(path, False, input_dim, num_output_classes) input_map = { feature: reader_test.streams.features, label: reader_test.streams.labels } # Test data for trained model C.debugging.start_profiler() C.debugging.enable_profiler() C.debugging.set_node_timing(True) test_minibatch_size = 1024 num_samples = 10000 num_minibatches_to_test = num_samples / test_minibatch_size test_result = 0.0 for i in range(0, int(num_minibatches_to_test)): mb = reader_test.next_minibatch(test_minibatch_size, input_map=input_map) eval_error = trainer.test_minibatch(mb) test_result = test_result + eval_error C.debugging.stop_profiler() trainer.print_node_timing() # Average of evaluation errors of all test minibatches return test_result * 100 / num_minibatches_to_test
xi = [char_to_ix[ch] for ch in data[p:p + nchars]] yi = [char_to_ix[data[p + 1]]] X = np.eye(vocab_size, dtype=np.float32)[xi] Y = np.eye(vocab_size, dtype=np.float32)[yi] return X, Y get_sample(0) input_text = C.input_variable((nchars, vocab_size)) output_char = C.input_variable(shape=vocab_size) model = Sequential( [Dense(3000, activation=C.relu), Dense(vocab_size, activation=None)]) z = model(input_text) ce = cross_entropy_with_softmax(z, output_char) errs = classification_error(z, output_char) lr_per_sample = learning_rate_schedule(0.01, UnitType.minibatch) momentum_time_constant = momentum_as_time_constant_schedule(1100) learner = C.learners.adam(z.parameters, lr_per_sample, momentum=momentum_time_constant) progress_printer = ProgressPrinter(freq=100, tag='Training') trainer = Trainer(z, (ce, errs), learner, progress_printer)
def __init__( self, input_shape, nb_actions, gamma=0.99, explorer=LinearEpsilonAnnealingExplorer( 1, 0.1, 10), #start, end, steps 100000 learning_rate=0.00025, momentum=0.95, minibatch_size=16, memory_size=500000, train_after=10000, train_interval=4, target_update_interval=10000, monitor=True): self.input_shape = input_shape self.nb_actions = nb_actions self.gamma = gamma self._train_after = train_after self._train_interval = train_interval self._target_update_interval = target_update_interval self._explorer = explorer self._minibatch_size = minibatch_size self._history = History(input_shape) self._memory = ReplayMemory(memory_size, input_shape[1:], 4) self._num_actions_taken = 0 # Metrics accumulator self._episode_rewards, self._episode_q_means, self._episode_q_stddev = [], [], [] # Action Value model (used by agent to interact with the environment) with default_options(activation=relu, init=he_uniform()): self._action_value_net = Sequential([ Convolution2D((8, 8), 16, strides=4), Convolution2D((4, 4), 32, strides=2), Convolution2D((3, 3), 32, strides=1), Dense(256, init=he_uniform(scale=0.01)), Dense(nb_actions, activation=None, init=he_uniform(scale=0.01)) ]) #dense 256 self._action_value_net.update_signature(Tensor[input_shape]) # Target model used to compute the target Q-values in training, updated # less frequently for increased stability. self._target_net = self._action_value_net.clone(CloneMethod.freeze) # Function computing Q-values targets as part of the computation graph @Function @Signature(post_states=Tensor[input_shape], rewards=Tensor[()], terminals=Tensor[()]) def compute_q_targets(post_states, rewards, terminals): return element_select( terminals, rewards, gamma * reduce_max(self._target_net(post_states), axis=0) + rewards, ) # Define the loss, using Huber Loss (more robust to outliers) @Function @Signature(pre_states=Tensor[input_shape], actions=Tensor[nb_actions], post_states=Tensor[input_shape], rewards=Tensor[()], terminals=Tensor[()]) def criterion(pre_states, actions, post_states, rewards, terminals): # Compute the q_targets q_targets = compute_q_targets(post_states, rewards, terminals) # actions is a 1-hot encoding of the action done by the agent q_acted = reduce_sum(self._action_value_net(pre_states) * actions, axis=0) # Define training criterion as the Huber Loss function return huber_loss(q_targets, q_acted, 1.0) # Adam based SGD lr_schedule = learning_rate_schedule(learning_rate, UnitType.minibatch) m_schedule = momentum_schedule(momentum) vm_schedule = momentum_schedule(0.999) l_sgd = adam(self._action_value_net.parameters, lr_schedule, momentum=m_schedule, variance_momentum=vm_schedule) self._metrics_writer = TensorBoardProgressWriter( freq=1, log_dir='metrics', model=criterion) if monitor else None self._learner = l_sgd self._trainer = Trainer(criterion, (criterion, None), l_sgd, self._metrics_writer) # if os.path.isfile('./cnnNetwork_CheckpointRightGoal.dnn'): # if os.path.isfile('./cnnNetwork_Checkpoint.dnn'): #if os.path.isfile('./cnnNetwork_CheckpointLeftGoal.dnn') if os.path.isfile('./savednetwork.dnn'): self._action_value_net = load_model("./savednetwork.dnn") self._target_net = self._action_value_net.clone(CloneMethod.freeze)
#Now we need to define a critic network which will estimate the value function $V_\phi(s_t)$. #You can likely reuse code from the policy network or look at other CNTK network examples. #Additionally, you'll need to define a squared error loss function, optimizer, and trainer for this newtork. # TODO 1: Define the critic network that learns the value function V(s). # Hint: you can use a similar architecture as the policy network, except # the output should just be a scalar value estimate. Also, since the value # function learning is more standard, it's possible to use stanard CNTK # wrappers such as Trainer(). critic_input = None # Modify this critic_output = None # Modify this critic = Sequential([ Dense(critic_input, activation=C.relu, init=C.glorot_uniform()), Dense(critic_output, activation=None, init=C.glorot_uniform(scale=.01)) ])(observations) # TODO 2: Define target and Squared Error Loss Function, adam optimizier, and trainer for the Critic. critic_target = None # Modify this critic_loss = None # Modify this critic_lr_schedule = C.learning_rate_schedule(lr=0.1, unit=C.UnitType.sample) critic_optimizer = C.adam(critic.parameters, critic_lr_schedule, momentum=m_schedule, variance_momentum=vm_schedule) critic_trainer = C.Trainer(critic, (critic_loss, None), critic_optimizer) #The main training loop is nearly identical except you'll need to train the critic to minimize the difference #between the predicted and observed return at each step. Additionally, you'll need to update the Reinforce update to subtract the baseline. running_variance = RunningVariance() reward_sum = 0
def create_vgg19(): # Input variables denoting the features and label data feature_var = input((num_channels, image_height, image_width)) label_var = input((num_classes)) # apply model to input # remove mean value input = minus(feature_var, constant([[[104]], [[117]], [[124]]]), name='mean_removed_input') with default_options(activation=None, pad=True, bias=True): z = Sequential([ # we separate Convolution and ReLU to name the output for feature extraction (usually before ReLU) For( range(2), lambda i: [ Convolution2D((3, 3), 64, name='conv1_{}'.format(i)), Activation(activation=relu, name='relu1_{}'.format(i)), ]), MaxPooling((2, 2), (2, 2), name='pool1'), For( range(2), lambda i: [ Convolution2D((3, 3), 128, name='conv2_{}'.format(i)), Activation(activation=relu, name='relu2_{}'.format(i)), ]), MaxPooling((2, 2), (2, 2), name='pool2'), For( range(4), lambda i: [ Convolution2D((3, 3), 256, name='conv3_{}'.format(i)), Activation(activation=relu, name='relu3_{}'.format(i)), ]), MaxPooling((2, 2), (2, 2), name='pool3'), For( range(4), lambda i: [ Convolution2D((3, 3), 512, name='conv4_{}'.format(i)), Activation(activation=relu, name='relu4_{}'.format(i)), ]), MaxPooling((2, 2), (2, 2), name='pool4'), For( range(4), lambda i: [ Convolution2D((3, 3), 512, name='conv5_{}'.format(i)), Activation(activation=relu, name='relu5_{}'.format(i)), ]), MaxPooling((2, 2), (2, 2), name='pool5'), Dense(4096, name='fc6'), Activation(activation=relu, name='relu6'), Dropout(0.5, name='drop6'), Dense(4096, name='fc7'), Activation(activation=relu, name='relu7'), Dropout(0.5, name='drop7'), Dense(num_classes, name='fc8') ])(input) # loss and metric ce = cross_entropy_with_softmax(z, label_var) pe = classification_error(z, label_var) pe5 = classification_error(z, label_var, topN=5) log_number_of_parameters(z) print() return { 'feature': feature_var, 'label': label_var, 'ce': ce, 'pe': pe, 'pe5': pe5, 'output': z }
def build_model(self): cntk.debugging.set_checked_mode(True) # Defining the input variables for training and evaluation. self.stacked_frames = cntk.input_variable( (1, self.STATE_WIDTH, self.STATE_HEIGHT), dtype=np.float32) #self.stacked_frames = cntk.input_variable((1, 84, 84), dtype=np.float32) self.action = cntk.input_variable(self.num_actions) self.R = cntk.input_variable(1, dtype=np.float32) self.v_calc = cntk.input_variable( 1, dtype=np.float32 ) # In the loss of pi, the parameters of V(s) should be fixed. # Creating the value approximator extension. conv1_v = Convolution2D((8, 8), num_filters=16, pad=False, strides=4, activation=cntk.relu, name='conv1_v') conv2_v = Convolution2D((4, 4), num_filters=32, pad=False, strides=2, activation=cntk.relu, name='conv2_v') dense_v = Dense(256, activation=cntk.sigmoid, name='dense_v', init=cntk.xavier()) v = Sequential([ conv1_v, conv2_v, dense_v, Dense(1, activation=cntk.sigmoid, name='outdense_v', init=cntk.xavier()) ]) # Creating the policy approximator extension. conv1_pi = Convolution2D((8, 8), num_filters=16, pad=False, strides=4, activation=cntk.relu, name='conv1_pi') conv2_pi = Convolution2D((4, 4), num_filters=32, pad=False, strides=2, activation=cntk.relu, name='conv2_pi') dense_pi = Dense(256, activation=cntk.sigmoid, name='dense_pi', init=cntk.xavier()) pi = Sequential([ conv1_v, conv2_v, dense_pi, Dense(self.num_actions, activation=cntk.softmax, name='outdense_pi', init=cntk.xavier()) ]) self.pi = pi(self.stacked_frames) self.pms_pi = self.pi.parameters # List of cntk Parameter types (containes the function's parameters) self.v = v(self.stacked_frames) self.pms_v = self.v.parameters cntk.debugging.debug_model(v)
def __init__(self, input_shape, nb_actions, gamma=0.7, explorer=LinearEpsilonAnnealingExplorer(1, 0.1, 100000), learning_rate=0.001, momentum=0.2, minibatch_size=32, memory_size=500000, train_after=100, train_interval=100, target_update_interval=100, monitor=True): self.input_shape = input_shape self.nb_actions = nb_actions self.gamma = gamma self._train_after = train_after self._train_interval = train_interval self._target_update_interval = target_update_interval self._explorer = explorer self._minibatch_size = minibatch_size self._history = History(input_shape) self._memory = ReplayMemory(memory_size, input_shape[1:], 4) self._num_actions_taken = 0 self._num_trains = 0 # Metrics accumulator self._episode_rewards, self._episode_q_means, self._episode_q_stddev = [], [], [] with default_options(activation=relu, init=he_uniform()): self._action_value_net = Sequential([ Dense(3, init=he_uniform(scale=0.01)), Dense(4, init=he_uniform(scale=0.01)), #Dense(4, init=he_uniform(scale=0.01)), #Dense(32, init=he_uniform(scale=0.01)), Dense(nb_actions, activation=None, init=he_uniform(scale=0.01)) ]) self._action_value_net.update_signature(Tensor[input_shape]) # Target model used to compute the target Q-values in training, updated # less frequently for increased stability. self._target_net = self._action_value_net.clone(CloneMethod.freeze) # Function computing Q-values targets as part of the computation graph @Function @Signature(post_states=Tensor[input_shape], rewards=Tensor[()], terminals=Tensor[()]) def compute_q_targets(post_states, rewards, terminals): return element_select( terminals, rewards, gamma * reduce_max(self._target_net(post_states), axis=0) + rewards, ) # Define the loss, using Huber Loss (more robust to outliers) @Function @Signature(pre_states=Tensor[input_shape], actions=Tensor[nb_actions], post_states=Tensor[input_shape], rewards=Tensor[()], terminals=Tensor[()]) def criterion(pre_states, actions, post_states, rewards, terminals): # Compute the q_targets q_targets = compute_q_targets(post_states, rewards, terminals) # actions is a 1-hot encoding of the action done by the agent q_acted = reduce_sum(self._action_value_net(pre_states) * actions, axis=0) # Define training criterion as the Huber Loss function return huber_loss(q_targets, q_acted, 1.0) # Adam based SGD lr_schedule = learning_rate_schedule(learning_rate, UnitType.minibatch) m_schedule = momentum_schedule(momentum) vm_schedule = momentum_schedule(0.3) l_sgd = adam(self._action_value_net.parameters, lr_schedule, momentum=m_schedule, variance_momentum=vm_schedule) self._metrics_writer = TensorBoardProgressWriter(freq=1, log_dir='metrics', model=criterion) if monitor else None self._learner = l_sgd self._trainer = Trainer(criterion, (criterion, None), l_sgd, self._metrics_writer)
def LSTM_sequence_classifier_net(feature, num_output_classes, embedding_dim, LSTM_dim, cell_dim): lstm_classifier = Sequential([Embedding(embedding_dim), Recurrence(LSTM(LSTM_dim, cell_dim))[0], sequence.last, Dense(num_output_classes)]) return lstm_classifier(feature)
def create_alexnet(): # Input variables denoting the features and label data feature_var = input((num_channels, image_height, image_width)) label_var = input((num_classes)) # apply model to input # remove mean value mean_removed_features = minus(feature_var, constant(114), name='mean_removed_input') with default_options(activation=None, pad=True, bias=True): z = Sequential([ # we separate Convolution and ReLU to name the output for feature extraction (usually before ReLU) Convolution2D((11, 11), 96, init=normal(0.01), pad=False, strides=(4, 4), name='conv1'), Activation(activation=relu, name='relu1'), LocalResponseNormalization(1.0, 2, 0.0001, 0.75, name='norm1'), MaxPooling((3, 3), (2, 2), name='pool1'), Convolution2D((5, 5), 192, init=normal(0.01), init_bias=0.1, name='conv2'), Activation(activation=relu, name='relu2'), LocalResponseNormalization(1.0, 2, 0.0001, 0.75, name='norm2'), MaxPooling((3, 3), (2, 2), name='pool2'), Convolution2D((3, 3), 384, init=normal(0.01), name='conv3'), Activation(activation=relu, name='relu3'), Convolution2D((3, 3), 384, init=normal(0.01), init_bias=0.1, name='conv4'), Activation(activation=relu, name='relu4'), Convolution2D((3, 3), 256, init=normal(0.01), init_bias=0.1, name='conv5'), Activation(activation=relu, name='relu5'), MaxPooling((3, 3), (2, 2), name='pool5'), Dense(4096, init=normal(0.005), init_bias=0.1, name='fc6'), Activation(activation=relu, name='relu6'), Dropout(0.5, name='drop6'), Dense(4096, init=normal(0.005), init_bias=0.1, name='fc7'), Activation(activation=relu, name='relu7'), Dropout(0.5, name='drop7'), Dense(num_classes, init=normal(0.01), name='fc8') ])(mean_removed_features) # loss and metric ce = cross_entropy_with_softmax(z, label_var) pe = classification_error(z, label_var) pe5 = classification_error(z, label_var, topN=5) log_number_of_parameters(z) print() return { 'feature': feature_var, 'label': label_var, 'ce': ce, 'pe': pe, 'pe5': pe5, 'output': z }
def model_ind_rnn(input_tensor, hidden_dim): hidden = Recurrence(IndRNN(hidden_dim, C.relu))(input_tensor) prediction = Dense(1)(C.sequence.last(hidden)) return prediction