예제 #1
0
def main(_):
    run_logger = data_collector.current_run()

    # Import data
    mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)

    # Create the model
    x = tf.placeholder(tf.float32, [None, 784])
    W = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    y = tf.matmul(x, W) + b

    # Define loss and optimizer
    y_ = tf.placeholder(tf.float32, [None, 10])

    # The raw formulation of cross-entropy,
    #
    #   tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),
    #                                 reduction_indices=[1]))
    #
    # can be numerically unstable.
    #
    # So here we use tf.nn.softmax_cross_entropy_with_logits on the raw
    # outputs of 'y', and then average across the batch.
    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

    sess = tf.InteractiveSession()
    tf.global_variables_initializer().run()
    metrics = []
    # Train
    for i in range(1000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

        if i % 20 == 0:
            # Test trained model
            correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
            acc = sess.run(accuracy,
                           feed_dict={
                               x: mnist.test.images,
                               y_: mnist.test.labels
                           })
            print("iteration {}, accuracy {}".format(i, acc))
            metrics.append({'Accuracy': acc})

    run_logger.log(pandas.DataFrame(metrics))
예제 #2
0
import numpy
import pandas as pd
import sklearn

from azureml_sdk import data_collector

# initialize the logger
run_logger = data_collector.current_run()

######## Load Data ##############
# TO DO: load data


######## Train a Model ##########
# TO DO: train model


######## Evaluate the Model #####
# TO DO: evaluate model
run_logger.log('Magic Number', 42)


######## Persist the Model ######
# TO DO: persist model
예제 #3
0
# Please make sure scikit-learn is included the conda_dependencies.yml file.

import pickle
import sys

from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from azureml_sdk import data_collector

run_logger = data_collector.current_run() 

print ('Python version: {}'.format(sys.version))
print ()

# load Iris dataset
iris = load_iris()
print ('Iris dataset shape: {}'.format(iris.data.shape))

# load features and labels
X, Y = iris.data, iris.target

# change regularization rate and you will likely get a different accuracy.
reg = 0.01
# log the regulizarion rate
run_logger.metrics.custom_scalar("Regularization", reg)

# train a logistic regression model
clf1 = LogisticRegression(C=1/reg).fit(X, Y)
print (clf1)

accuracy = clf1.score(X, Y)