Skip to content

Commit

Permalink
Integrate wilrich/alphaExampleFixes into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Project Philly committed Sep 9, 2016
2 parents cb85174 + 9f749f1 commit e33eeff
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
16 changes: 12 additions & 4 deletions bindings/python/examples/CifarResNet/CifarResNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@
import os
from cntk import learning_rates_per_sample, Trainer, sgd_learner, create_minibatch_source, DeviceDescriptor
from cntk.ops import input_variable, constant, parameter, cross_entropy_with_softmax, combine, classification_error, times, pooling, AVG_POOLING

abs_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(abs_path, "..", ".."))
from examples.common.nn import conv_bn_relu_layer, conv_bn_layer, resnet_node2, resnet_node2_inc, print_training_progress


# Instantiates the CNTK built-in minibatch source for reading images to be used for training the residual net
# The minibatch source is configured using a hierarchical dictionary of key:value pairs
def create_mb_source(features_stream_name, labels_stream_name, image_height, image_width, num_channels, num_classes):
map_file_rel_path = r"../../../../Examples/Image/Miscellaneous/CIFAR-10/cifar-10-batches-py/train_map.txt"
map_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), map_file_rel_path)
mean_file_rel_path = r"../../../../Examples/Image/Miscellaneous/CIFAR-10/cifar-10-batches-py/CIFAR-10_mean.xml"
mean_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), mean_file_rel_path)
map_file_rel_path = os.path.join(*"../../../../Examples/Image/Miscellaneous/CIFAR-10/cifar-10-batches-py/train_map.txt".split("/"))
map_file = os.path.normpath(os.path.join(abs_path, map_file_rel_path))
mean_file_rel_path = os.path.join(*"../../../../Examples/Image/Miscellaneous/CIFAR-10/cifar-10-batches-py/CIFAR-10_mean.xml".split("/"))
mean_file = os.path.normpath(os.path.join(abs_path, mean_file_rel_path))

if not os.path.exists(map_file) or not os.path.exists(mean_file):
cifar_py3 = "" if sys.version_info.major < 3 else "_py3"
raise RuntimeError("File '%s' or '%s' do not exist. Please run CifarDownload%s.py and CifarConverter%s.py from CIFAR-10 to fetch them"%(map_file, mean_file, cifar_py3, cifar_py3))

crop_transform_config = {"type" : "Crop", "cropType" : "Random", "cropRatio" : "0.8", "jitterType" : "uniRatio"}
scale_transform_config = {"type" : "Scale", "width" : image_width, "height" : image_height, "channels" : num_channels, "interpolations" : "linear"}
Expand Down
12 changes: 10 additions & 2 deletions bindings/python/examples/MNIST/SimpleMNIST.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import os
from cntk import learning_rates_per_sample, Trainer, sgd_learner, create_minibatch_source, StreamConfiguration, DeviceDescriptor, text_format_minibatch_source
from cntk.ops import input_variable, cross_entropy_with_softmax, combine, classification_error, sigmoid, element_times, constant

abs_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(abs_path, "..", ".."))
from examples.common.nn import fully_connected_classifier_net, print_training_progress

# Creates and trains a feedforward classification model for MNIST images
Expand All @@ -29,8 +32,11 @@ def simple_mnist():
ce = cross_entropy_with_softmax(netout, label)
pe = classification_error(netout, label)

rel_path = r"../../../../Examples/Image/MNIST/Data/Train-28x28_cntk_text.txt"
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), rel_path)
rel_path = os.path.join(*"../../../../Examples/Image/MNIST/Data/Train-28x28_cntk_text.txt".split("/"))
path = os.path.normpath(os.path.join(abs_path, rel_path))
if not os.path.exists(path):
readme_file = os.path.normpath(os.path.join(os.path.dirname(path), "..", "README.md"))
raise RuntimeError("File '%s' does not exist. Please follow the instructions at %s to download and prepare it."%(path, readme_file))
feature_stream_name = 'features'
labels_stream_name = 'labels'

Expand Down Expand Up @@ -62,6 +68,8 @@ def simple_mnist():
if __name__=='__main__':
# Specify the target device to be used for computing
target_device = DeviceDescriptor.gpu_device(0)
# If it is crashing, probably you don't have a GPU, so try with CPU:
# target_device = DeviceDescriptor.cpu_device()
DeviceDescriptor.set_default_device(target_device)

simple_mnist()
6 changes: 6 additions & 0 deletions bindings/python/examples/NumpyInterop/FeedForwardNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
import os
from cntk import learning_rates_per_sample, DeviceDescriptor, Trainer, sgd_learner, cntk_device, StreamConfiguration, text_format_minibatch_source
from cntk.ops import input_variable, cross_entropy_with_softmax, combine, classification_error, sigmoid

abs_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(abs_path, "..", ".."))
from examples.common.nn import fully_connected_classifier_net, print_training_progress

# make sure we get always the same "randomness"
np.random.seed(0)

def generate_random_data(sample_size, feature_dim, num_classes):
# Create synthetic data using NumPy.
Y = np.random.randint(size=(sample_size, 1), low=0, high=num_classes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import time
from cntk import learning_rates_per_sample, momentums_per_sample, DeviceDescriptor, Trainer, momentum_sgd_learner, Axis, text_format_minibatch_source, StreamConfiguration
from cntk.ops import input_variable, cross_entropy_with_softmax, classification_error, sequence, slice, past_value, future_value, element_select

abs_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(abs_path, "..", ".."))
from examples.common.nn import LSTMP_component_with_self_stabilization, stabilize, linear_layer, print_training_progress

# Creates and trains a sequence to sequence translation model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import time
from cntk import learning_rates_per_sample, DeviceDescriptor, Trainer, sgd_learner, Axis, text_format_minibatch_source, StreamConfiguration
from cntk.ops import input_variable, cross_entropy_with_softmax, combine, classification_error

abs_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(abs_path, "..", ".."))
from examples.common.nn import LSTMP_component_with_self_stabilization, embedding, linear_layer, select_last, print_training_progress

# Defines the LSTM model for classifying sequences
Expand Down

0 comments on commit e33eeff

Please sign in to comment.